Merge branch 'MDL-72353' of https://github.com/paulholden/moodle
[moodle.git] / blocks / site_main_menu / block_site_main_menu.php
blob7dab31f39ee71a220d1b528a8a6a84628301d75b
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.
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.
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
25 class block_site_main_menu extends block_list {
26 function init() {
27 $this->title = get_string('pluginname', 'block_site_main_menu');
30 function applicable_formats() {
31 return array('site' => true);
34 function get_content() {
35 global $USER, $CFG, $DB, $OUTPUT;
37 if ($this->content !== NULL) {
38 return $this->content;
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;
50 require_once($CFG->dirroot . '/course/lib.php');
52 $course = get_site();
53 $format = course_get_format($course);
54 $courserenderer = $format->get_renderer($this->page);
56 $context = context_course::instance($course->id);
57 $isediting = $this->page->user_is_editing() && has_capability('moodle/course:manageactivities', $context);
59 // Output classes.
60 $cmnameclass = $format->get_output_classname('content\\cm\\cmname');
61 $controlmenuclass = $format->get_output_classname('content\\cm\\controlmenu');
63 $badgeattributes = [
64 'class' => 'badge rounded-pill bg-warning text-dark mt-2',
65 'data-region' => 'visibility'
68 // Extra fast view mode.
69 if (!$isediting) {
70 $modinfo = get_fast_modinfo($course);
71 if (!empty($modinfo->sections[0])) {
72 foreach($modinfo->sections[0] as $cmid) {
73 $cm = $modinfo->cms[$cmid];
74 if (!$cm->uservisible || !$cm->is_visible_on_course_page()) {
75 continue;
78 if ($cm->indent > 0) {
79 $indent = '<div class="mod-indent mod-indent-'.$cm->indent.'"></div>';
80 } else {
81 $indent = '';
84 $badges = '';
85 if (!$cm->visible) {
86 $badges = html_writer::tag(
87 'span',
88 get_string('hiddenfromstudents'),
89 $badgeattributes
93 if ($cm->is_stealth()) {
94 $badges = html_writer::tag(
95 'span',
96 get_string('hiddenoncoursepage'),
97 $badgeattributes
101 if (!$cm->url) {
102 $activitybasis = html_writer::div(
103 $indent . $cm->get_formatted_content(['overflowdiv' => true, 'noclean' => true]),
104 'activity-basis d-flex align-items-center');
105 $content = html_writer::div(
106 $activitybasis . $badges,
107 'contentwithoutlink activity-item activity',
108 ['data-activityname' => $cm->name]
110 } else {
111 $cmname = new $cmnameclass($format, $cm->get_section_info(), $cm);
112 $activitybasis = html_writer::div(
113 $indent . $courserenderer->render($cmname),
114 'activity-basis d-flex align-items-center');
115 $content = html_writer::div(
116 $activitybasis . $badges,
117 'activity-item activity',
118 ['data-activityname' => $cm->name]
122 $this->content->items[] = html_writer::div($content, 'main-menu-content section');
125 return $this->content;
128 // Slow & hacky editing mode.
129 $ismoving = ismoving($course->id);
130 course_create_sections_if_missing($course, 0);
131 $modinfo = get_fast_modinfo($course);
132 $section = $modinfo->get_section_info(0);
134 if ($ismoving) {
135 $strmovefull = strip_tags(get_string('movefull', '', "'$USER->activitycopyname'"));
136 $strcancel= get_string('cancel');
137 } else {
138 $strmove = get_string('move');
141 if ($ismoving) {
142 $this->content->icons[] = $OUTPUT->pix_icon('t/move', get_string('move'));
143 $this->content->items[] = $USER->activitycopyname.'&nbsp;(<a href="'.$CFG->wwwroot.'/course/mod.php?cancelcopy=true&amp;sesskey='.sesskey().'">'.$strcancel.'</a>)';
146 if (!empty($modinfo->sections[0])) {
147 foreach ($modinfo->sections[0] as $modnumber) {
148 $mod = $modinfo->cms[$modnumber];
149 if (!$mod->uservisible || !$mod->is_visible_on_course_page()) {
150 continue;
152 if (!$ismoving) {
154 $controlmenu = new $controlmenuclass(
155 $format,
156 $mod->get_section_info(),
157 $mod
160 $menu = $controlmenu->get_action_menu($OUTPUT);
162 $moveaction = html_writer::link(
163 new moodle_url('/course/mod.php', ['sesskey' => sesskey(), 'copy' => $mod->id]),
164 $OUTPUT->pix_icon('i/dragdrop', $strmove),
165 ['class' => 'editing_move_activity']
168 $editbuttons = html_writer::tag(
169 'div',
170 $courserenderer->render($controlmenu),
171 ['class' => 'buttons activity-actions ml-auto']
173 } else {
174 $editbuttons = '';
175 $moveaction = '';
178 if ($mod->visible || has_capability('moodle/course:viewhiddenactivities', $mod->context)) {
179 if ($ismoving) {
180 if ($mod->id == $USER->activitycopy) {
181 continue;
183 $movingurl = new moodle_url('/course/mod.php', array('moveto' => $mod->id, 'sesskey' => sesskey()));
184 $this->content->items[] = html_writer::link($movingurl, '', array('title' => $strmovefull,
185 'class' => 'movehere'));
186 $this->content->icons[] = '';
189 if ($mod->indent > 0) {
190 $indent = '<div class="mod-indent mod-indent-'.$mod->indent.'"></div>';
191 } else {
192 $indent = '';
195 $badges = '';
196 if (!$mod->visible) {
197 $badges = html_writer::tag(
198 'span',
199 get_string('hiddenfromstudents'),
200 $badgeattributes
204 if ($mod->is_stealth()) {
205 $badges = html_writer::tag(
206 'span',
207 get_string('hiddenoncoursepage'),
208 $badgeattributes
212 if (!$mod->url) {
213 $activitybasis = html_writer::div(
214 $moveaction .
215 $indent .
216 $mod->get_formatted_content(['overflowdiv' => true, 'noclean' => true]) .
217 $editbuttons,
218 'activity-basis d-flex align-items-center');
219 $content = html_writer::div(
220 $activitybasis . $badges,
221 'contentwithoutlink activity-item activity',
222 ['data-activityname' => $mod->name]
224 } else {
225 $cmname = new $cmnameclass($format, $mod->get_section_info(), $mod);
226 $activitybasis = html_writer::div(
227 $moveaction .
228 $indent .
229 $courserenderer->render($cmname) .
230 $editbuttons,
231 'activity-basis d-flex align-items-center');
232 $content = html_writer::div(
233 $activitybasis . $badges,
234 'activity-item activity',
235 ['data-activityname' => $mod->name]
238 $this->content->items[] = html_writer::div($content, 'main-menu-content');
243 if ($ismoving) {
244 $movingurl = new moodle_url('/course/mod.php', array('movetosection' => $section->id, 'sesskey' => sesskey()));
245 $this->content->items[] = html_writer::link($movingurl, '', array('title' => $strmovefull, 'class' => 'movehere'));
246 $this->content->icons[] = '';
249 if ($this->page->course->id === SITEID) {
250 $this->content->footer = $courserenderer->course_section_add_cm_control($course,
251 0, null, array('inblock' => true));
253 return $this->content;