03a5d4c93656055b005c2d5303338916ab545da9
[moodle.git] / blocks / site_main_menu / block_site_main_menu.php
1 <?php
3 class block_site_main_menu extends block_list {
4     function init() {
5         $this->title = get_string('pluginname', 'block_site_main_menu');
6     }
8     function applicable_formats() {
9         return array('site' => true);
10     }
12     function get_content() {
13         global $USER, $CFG, $DB, $OUTPUT;
15         if ($this->content !== NULL) {
16             return $this->content;
17         }
19         $this->content = new stdClass();
20         $this->content->items = array();
21         $this->content->icons = array();
22         $this->content->footer = '';
24         if (empty($this->instance)) {
25             return $this->content;
26         }
28         $course = $this->page->course;
29         require_once($CFG->dirroot.'/course/lib.php');
30         $context = context_course::instance($course->id);
31         $isediting = $this->page->user_is_editing() && has_capability('moodle/course:manageactivities', $context);
33 /// extra fast view mode
34         if (!$isediting) {
35             $modinfo = get_fast_modinfo($course);
36             if (!empty($modinfo->sections[0])) {
37                 $options = array('overflowdiv'=>true);
38                 foreach($modinfo->sections[0] as $cmid) {
39                     $cm = $modinfo->cms[$cmid];
40                     if (!$cm->uservisible) {
41                         continue;
42                     }
44                     $content = $cm->get_formatted_content(array('overflowdiv' => true, 'noclean' => true));
45                     $instancename = $cm->get_formatted_name();
47                     if (!($url = $cm->get_url())) {
48                         $this->content->items[] = $content;
49                         $this->content->icons[] = '';
50                     } else {
51                         $linkcss = $cm->visible ? '' : ' class="dimmed" ';
52                         //Accessibility: incidental image - should be empty Alt text
53                         $icon = '<img src="' . $cm->get_icon_url() . '" class="icon" alt="" />';
54                         $this->content->items[] = '<a title="'.$cm->modplural.'" '.$linkcss.' '.$cm->extra.
55                                 ' href="' . $url . '">' . $icon . $instancename . '</a>';
56                     }
57                 }
58             }
59             return $this->content;
60         }
62 /// slow & hacky editing mode
63         $ismoving = ismoving($course->id);
64         course_create_sections_if_missing($course, 0);
65         $modinfo = get_fast_modinfo($course);
66         $section = $modinfo->get_section_info(0);
68         $groupbuttons = $course->groupmode;
69         $groupbuttonslink = (!$course->groupmodeforce);
71         if ($ismoving) {
72             $strmovehere = get_string('movehere');
73             $strmovefull = strip_tags(get_string('movefull', '', "'$USER->activitycopyname'"));
74             $strcancel= get_string('cancel');
75             $stractivityclipboard = $USER->activitycopyname;
76         }
77     /// Casting $course->modinfo to string prevents one notice when the field is null
78         $editbuttons = '';
80         if ($ismoving) {
81             $this->content->icons[] = '<img src="'.$OUTPUT->pix_url('t/move') . '" class="iconsmall" alt="" />';
82             $this->content->items[] = $USER->activitycopyname.'&nbsp;(<a href="'.$CFG->wwwroot.'/course/mod.php?cancelcopy=true&amp;sesskey='.sesskey().'">'.$strcancel.'</a>)';
83         }
85         if (!empty($modinfo->sections[0])) {
86             $options = array('overflowdiv'=>true);
87             foreach ($modinfo->sections[0] as $modnumber) {
88                 $mod = $modinfo->cms[$modnumber];
89                 if (!$mod->uservisible) {
90                     continue;
91                 }
92                 if (!$ismoving) {
93                     if ($groupbuttons) {
94                         if (! $mod->groupmodelink = $groupbuttonslink) {
95                             $mod->groupmode = $course->groupmode;
96                         }
98                     } else {
99                         $mod->groupmode = false;
100                     }
101                     $editbuttons = '<div class="buttons">'.make_editing_buttons($mod, true, true).'</div>';
102                 } else {
103                     $editbuttons = '';
104                 }
105                 if ($mod->visible || has_capability('moodle/course:viewhiddenactivities', $context)) {
106                     if ($ismoving) {
107                         if ($mod->id == $USER->activitycopy) {
108                             continue;
109                         }
110                         $this->content->items[] = '<a title="'.$strmovefull.'" href="'.$CFG->wwwroot.'/course/mod.php?moveto='.$mod->id.'&amp;sesskey='.sesskey().'">'.
111                             '<img style="height:16px; width:80px; border:0px" src="'.$OUTPUT->pix_url('movehere') . '" alt="'.$strmovehere.'" /></a>';
112                         $this->content->icons[] = '';
113                     }
114                     $content = $mod->get_formatted_content(array('overflowdiv' => true, 'noclean' => true));
115                     $instancename = $mod->get_formatted_name();
116                     $linkcss = $mod->visible ? '' : ' class="dimmed" ';
118                     if (!($url = $mod->get_url())) {
119                         $this->content->items[] = $content . $editbuttons;
120                         $this->content->icons[] = '';
121                     } else {
122                         //Accessibility: incidental image - should be empty Alt text
123                         $icon = '<img src="' . $mod->get_icon_url() . '" class="icon" alt="" />';
124                         $this->content->items[] = '<a title="' . $mod->modfullname . '" ' . $linkcss . ' ' . $mod->extra .
125                             ' href="' . $url . '">' . $icon . $instancename . '</a>' . $editbuttons;
126                     }
127                 }
128             }
129         }
131         if ($ismoving) {
132             $this->content->items[] = '<a title="'.$strmovefull.'" href="'.$CFG->wwwroot.'/course/mod.php?movetosection='.$section->id.'&amp;sesskey='.sesskey().'">'.
133                                       '<img style="height:16px; width:80px; border:0px" src="'.$OUTPUT->pix_url('movehere') . '" alt="'.$strmovehere.'" /></a>';
134             $this->content->icons[] = '';
135         }
137         $this->content->footer = print_section_add_menus($course, 0, null, true, true);
139         return $this->content;
140     }