Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

Differences Between: [Versions 310 and 401] [Versions 311 and 401] [Versions 39 and 401] [Versions 401 and 403]

   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/>.
  16  
  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   */
  24  
  25  class block_site_main_menu extends block_list {
  26      function init() {
  27          $this->title = get_string('pluginname', 'block_site_main_menu');
  28      }
  29  
  30      function applicable_formats() {
  31          return array('site' => true);
  32      }
  33  
  34      function get_content() {
  35          global $USER, $CFG, $DB, $OUTPUT;
  36  
  37          if ($this->content !== NULL) {
  38              return $this->content;
  39          }
  40  
  41          $this->content = new stdClass();
  42          $this->content->items = array();
  43          $this->content->icons = array();
  44          $this->content->footer = '';
  45  
  46          if (empty($this->instance)) {
  47              return $this->content;
  48          }
  49  
  50          require_once($CFG->dirroot . '/course/lib.php');
  51  
  52          $course = get_site();
  53          $format = course_get_format($course);
  54          $courserenderer = $format->get_renderer($this->page);
  55  
  56          $context = context_course::instance($course->id);
  57          $isediting = $this->page->user_is_editing() && has_capability('moodle/course:manageactivities', $context);
  58  
  59          // Output classes.
  60          $cmnameclass = $format->get_output_classname('content\\cm\\cmname');
  61          $controlmenuclass = $format->get_output_classname('content\\cm\\controlmenu');
  62  
  63          // Extra fast view mode.
  64          if (!$isediting) {
  65              $modinfo = get_fast_modinfo($course);
  66              if (!empty($modinfo->sections[0])) {
  67                  foreach($modinfo->sections[0] as $cmid) {
  68                      $cm = $modinfo->cms[$cmid];
  69                      if (!$cm->uservisible || !$cm->is_visible_on_course_page()) {
  70                          continue;
  71                      }
  72  
  73                      if ($cm->indent > 0) {
  74                          $indent = '<div class="mod-indent mod-indent-'.$cm->indent.'"></div>';
  75                      } else {
  76                          $indent = '';
  77                      }
  78  
  79                      $badges = '';
  80                      if (!$cm->visible) {
  81                          $badges = html_writer::tag(
  82                              'span',
  83                              get_string('hiddenfromstudents'),
  84                              ['class' => 'badge badge-pill badge-warning mt-2']
  85                          );
  86                      }
  87  
  88                      if ($cm->is_stealth()) {
  89                          $badges = html_writer::tag(
  90                              'span',
  91                              get_string('hiddenoncoursepage'),
  92                              ['class' => 'badge badge-pill badge-warning mt-2']
  93                          );
  94                      }
  95  
  96                      if (!$cm->url) {
  97                          $activitybasis = html_writer::div(
  98                              $indent . $cm->get_formatted_content(['overflowdiv' => true, 'noclean' => true]),
  99                              'activity-basis d-flex align-items-center');
 100                          $content = html_writer::div(
 101                              $activitybasis . $badges,
 102                              'contentwithoutlink activity-item activity'
 103                          );
 104                      } else {
 105                          $cmname = new $cmnameclass($format, $cm->get_section_info(), $cm);
 106                          $activitybasis = html_writer::div(
 107                              $indent . $courserenderer->render($cmname),
 108                              'activity-basis d-flex align-items-center');
 109                          $content = html_writer::div(
 110                              $activitybasis . $badges,
 111                              'activity-item activity'
 112                          );
 113                      }
 114  
 115                      $this->content->items[] = html_writer::div($content, 'main-menu-content section');
 116                  }
 117              }
 118              return $this->content;
 119          }
 120  
 121          // Slow & hacky editing mode.
 122          $ismoving = ismoving($course->id);
 123          course_create_sections_if_missing($course, 0);
 124          $modinfo = get_fast_modinfo($course);
 125          $section = $modinfo->get_section_info(0);
 126  
 127          if ($ismoving) {
 128              $strmovefull = strip_tags(get_string('movefull', '', "'$USER->activitycopyname'"));
 129              $strcancel= get_string('cancel');
 130          } else {
 131              $strmove = get_string('move');
 132          }
 133  
 134          if ($ismoving) {
 135              $this->content->icons[] = $OUTPUT->pix_icon('t/move', get_string('move'));
 136              $this->content->items[] = $USER->activitycopyname.'&nbsp;(<a href="'.$CFG->wwwroot.'/course/mod.php?cancelcopy=true&amp;sesskey='.sesskey().'">'.$strcancel.'</a>)';
 137          }
 138  
 139          if (!empty($modinfo->sections[0])) {
 140              foreach ($modinfo->sections[0] as $modnumber) {
 141                  $mod = $modinfo->cms[$modnumber];
 142                  if (!$mod->uservisible || !$mod->is_visible_on_course_page()) {
 143                      continue;
 144                  }
 145                  if (!$ismoving) {
 146  
 147                      $controlmenu = new $controlmenuclass(
 148                          $format,
 149                          $mod->get_section_info(),
 150                          $mod
 151                      );
 152  
 153                      $menu = $controlmenu->get_action_menu($OUTPUT);
 154  
 155                      $moveaction = html_writer::link(
 156                          new moodle_url('/course/mod.php', ['sesskey' => sesskey(), 'copy' => $mod->id]),
 157                          $OUTPUT->pix_icon('i/dragdrop', $strmove),
 158                          ['class' => 'editing_move_activity']
 159                      );
 160  
 161                      $editbuttons = html_writer::tag(
 162                          'div',
 163                          $courserenderer->render($controlmenu),
 164                          ['class' => 'buttons activity-actions ml-auto']
 165                      );
 166                  } else {
 167                      $editbuttons = '';
 168                      $moveaction = '';
 169                  }
 170  
 171                  if ($mod->visible || has_capability('moodle/course:viewhiddenactivities', $mod->context)) {
 172                      if ($ismoving) {
 173                          if ($mod->id == $USER->activitycopy) {
 174                              continue;
 175                          }
 176                          $movingurl = new moodle_url('/course/mod.php', array('moveto' => $mod->id, 'sesskey' => sesskey()));
 177                          $this->content->items[] = html_writer::link($movingurl, '', array('title' => $strmovefull,
 178                              'class' => 'movehere'));
 179                          $this->content->icons[] = '';
 180                      }
 181  
 182                      if ($mod->indent > 0) {
 183                          $indent = '<div class="mod-indent mod-indent-'.$mod->indent.'"></div>';
 184                      } else {
 185                          $indent = '';
 186                      }
 187  
 188                      $badges = '';
 189                      if (!$mod->visible) {
 190                          $badges = html_writer::tag(
 191                              'span',
 192                              get_string('hiddenfromstudents'),
 193                              ['class' => 'badge badge-pill badge-warning mt-2']
 194                          );
 195                      }
 196  
 197                      if ($mod->is_stealth()) {
 198                          $badges = html_writer::tag(
 199                              'span',
 200                              get_string('hiddenoncoursepage'),
 201                              ['class' => 'badge badge-pill badge-warning mt-2']
 202                          );
 203                      }
 204  
 205                      if (!$mod->url) {
 206                          $activitybasis = html_writer::div(
 207                              $moveaction .
 208                              $indent .
 209                              $mod->get_formatted_content(['overflowdiv' => true, 'noclean' => true]) .
 210                              $editbuttons,
 211                              'activity-basis d-flex align-items-center');
 212                          $content = html_writer::div(
 213                              $activitybasis . $badges,
 214                              'contentwithoutlink activity-item activity'
 215                          );
 216                      } else {
 217                          $cmname = new $cmnameclass($format, $mod->get_section_info(), $mod);
 218                          $activitybasis = html_writer::div(
 219                              $moveaction .
 220                              $indent .
 221                              $courserenderer->render($cmname) .
 222                              $editbuttons,
 223                              'activity-basis d-flex align-items-center');
 224                          $content = html_writer::div(
 225                              $activitybasis . $badges,
 226                              'activity-item activity'
 227                          );
 228                      }
 229                      $this->content->items[] = html_writer::div($content, 'main-menu-content');
 230                  }
 231              }
 232          }
 233  
 234          if ($ismoving) {
 235              $movingurl = new moodle_url('/course/mod.php', array('movetosection' => $section->id, 'sesskey' => sesskey()));
 236              $this->content->items[] = html_writer::link($movingurl, '', array('title' => $strmovefull, 'class' => 'movehere'));
 237              $this->content->icons[] = '';
 238          }
 239  
 240          if ($this->page->course->id === SITEID) {
 241              $this->content->footer = $courserenderer->course_section_add_cm_control($course,
 242                  0, null, array('inblock' => true));
 243          }
 244          return $this->content;
 245      }
 246  }