Merge branch '23178-29' of git://github.com/samhemelryk/moodle
[moodle.git] / blocks / site_main_menu / block_site_main_menu.php
1 <?php
2 // This file is part of Moodle - http://moodle.org/
3 //
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
17 /**
18  * Site main menu block.
19  *
20  * @package    block_site_main_menu
21  * @copyright  1999 onwards Martin Dougiamas (http://dougiamas.com)
22  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23  */
25 class block_site_main_menu extends block_list {
26     function init() {
27         $this->title = get_string('pluginname', 'block_site_main_menu');
28     }
30     function applicable_formats() {
31         return array('site' => true);
32     }
34     function get_content() {
35         global $USER, $CFG, $DB, $OUTPUT;
37         if ($this->content !== NULL) {
38             return $this->content;
39         }
41         $this->content = new stdClass();
42         $this->content->items = array();
43         $this->content->icons = array();
44         $this->content->footer = '';
46         if (empty($this->instance)) {
47             return $this->content;
48         }
50         $course = $this->page->course;
51         require_once($CFG->dirroot.'/course/lib.php');
52         $context = context_course::instance($course->id);
53         $isediting = $this->page->user_is_editing() && has_capability('moodle/course:manageactivities', $context);
55 /// extra fast view mode
56         if (!$isediting) {
57             $modinfo = get_fast_modinfo($course);
58             if (!empty($modinfo->sections[0])) {
59                 foreach($modinfo->sections[0] as $cmid) {
60                     $cm = $modinfo->cms[$cmid];
61                     if (!$cm->uservisible) {
62                         continue;
63                     }
65                     if ($cm->indent > 0) {
66                         $indent = '<div class="mod-indent mod-indent-'.$cm->indent.'"></div>';
67                     } else {
68                         $indent = '';
69                     }
71                     if (!empty($cm->url)) {
72                         $attrs = array();
73                         $attrs['title'] = $cm->modfullname;
74                         $attrs['class'] = $cm->extraclasses . ' activity-action';
75                         if ($cm->onclick) {
76                             $attrs['id'] = html_writer::random_id('onclick');
77                             $OUTPUT->add_action_handler(new component_action('click', $cm->onclick), $attrs['id']);
78                         }
79                         if (!$cm->visible) {
80                             $attrs['class'] .= ' dimmed';
81                         }
82                         $icon = '<img src="' . $cm->get_icon_url() . '" class="icon" alt="" />';
83                         $content = html_writer::link($cm->url, $icon . $cm->get_formatted_name(), $attrs);
84                     } else {
85                         $content = $cm->get_formatted_content(array('overflowdiv' => true, 'noclean' => true));
86                     }
88                     $this->content->items[] = $indent.html_writer::div($content, 'main-menu-content');
89                 }
90             }
91             return $this->content;
92         }
94         // Slow & hacky editing mode.
95         /** @var core_course_renderer $courserenderer */
96         $courserenderer = $this->page->get_renderer('core', 'course');
97         $ismoving = ismoving($course->id);
98         course_create_sections_if_missing($course, 0);
99         $modinfo = get_fast_modinfo($course);
100         $section = $modinfo->get_section_info(0);
102         if ($ismoving) {
103             $strmovehere = get_string('movehere');
104             $strmovefull = strip_tags(get_string('movefull', '', "'$USER->activitycopyname'"));
105             $strcancel= get_string('cancel');
106             $stractivityclipboard = $USER->activitycopyname;
107         } else {
108             $strmove = get_string('move');
109         }
110         $editbuttons = '';
112         if ($ismoving) {
113             $this->content->icons[] = '<img src="'.$OUTPUT->pix_url('t/move') . '" class="iconsmall" alt="" />';
114             $this->content->items[] = $USER->activitycopyname.'&nbsp;(<a href="'.$CFG->wwwroot.'/course/mod.php?cancelcopy=true&amp;sesskey='.sesskey().'">'.$strcancel.'</a>)';
115         }
117         if (!empty($modinfo->sections[0])) {
118             $options = array('overflowdiv'=>true);
119             foreach ($modinfo->sections[0] as $modnumber) {
120                 $mod = $modinfo->cms[$modnumber];
121                 if (!$mod->uservisible) {
122                     continue;
123                 }
124                 if (!$ismoving) {
125                     $actions = course_get_cm_edit_actions($mod, $mod->indent);
127                     // Prepend list of actions with the 'move' action.
128                     $actions = array('move' => new action_menu_link_primary(
129                         new moodle_url('/course/mod.php', array('sesskey' => sesskey(), 'copy' => $mod->id)),
130                         new pix_icon('t/move', $strmove, 'moodle', array('class' => 'iconsmall', 'title' => '')),
131                         $strmove
132                     )) + $actions;
134                     $editbuttons = html_writer::tag('div',
135                         $courserenderer->course_section_cm_edit_actions($actions, $mod, array('donotenhance' => true)),
136                         array('class' => 'buttons')
137                     );
138                 } else {
139                     $editbuttons = '';
140                 }
141                 if ($mod->visible || has_capability('moodle/course:viewhiddenactivities', $mod->context)) {
142                     if ($ismoving) {
143                         if ($mod->id == $USER->activitycopy) {
144                             continue;
145                         }
146                         $this->content->items[] = '<a title="'.$strmovefull.'" href="'.$CFG->wwwroot.'/course/mod.php?moveto='.$mod->id.'&amp;sesskey='.sesskey().'">'.
147                             '<img style="height:16px; width:80px; border:0px" src="'.$OUTPUT->pix_url('movehere') . '" alt="'.$strmovehere.'" /></a>';
148                         $this->content->icons[] = '';
149                     }
150                     if ($mod->indent > 0) {
151                         $indent = '<div class="mod-indent mod-indent-'.$mod->indent.'"></div>';
152                     } else {
153                         $indent = '';
154                     }
155                     $url = $mod->url;
156                     if (!$url) {
157                         $content = $mod->get_formatted_content(array('overflowdiv' => true, 'noclean' => true));
158                     } else {
159                         //Accessibility: incidental image - should be empty Alt text
160                         $attrs = array();
161                         $attrs['title'] = $mod->modfullname;
162                         $attrs['class'] = $mod->extraclasses . ' activity-action';
163                         if ($mod->onclick) {
164                             $attrs['id'] = html_writer::random_id('onclick');
165                             $OUTPUT->add_action_handler(new component_action('click', $mod->onclick), $attrs['id']);
166                         }
167                         if (!$mod->visible) {
168                             $attrs['class'] .= ' dimmed';
169                         }
171                         $icon = '<img src="' . $mod->get_icon_url() . '" class="icon" alt="" />';
172                         $content = html_writer::link($url, $icon . $mod->get_formatted_name(), $attrs);
173                     }
174                     $this->content->items[] = $indent.html_writer::div($content . $editbuttons, 'main-menu-content');
175                 }
176             }
177         }
179         if ($ismoving) {
180             $this->content->items[] = '<a title="'.$strmovefull.'" href="'.$CFG->wwwroot.'/course/mod.php?movetosection='.$section->id.'&amp;sesskey='.sesskey().'">'.
181                                       '<img style="height:16px; width:80px; border:0px" src="'.$OUTPUT->pix_url('movehere') . '" alt="'.$strmovehere.'" /></a>';
182             $this->content->icons[] = '';
183         }
185         $this->content->footer = $courserenderer->course_section_add_cm_control($course,
186                 0, null, array('inblock' => true));
188         return $this->content;
189     }