Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.

Differences Between: [Versions 39 and 311]

   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   * Outputs the navigation tree.
  19   *
  20   * @since     Moodle 2.0
  21   * @package   block_navigation
  22   * @copyright 2009 Sam Hemelryk
  23   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  
  26  /**
  27   * Renderer for block navigation
  28   *
  29   * @package   block_navigation
  30   * @category  navigation
  31   * @copyright 2009 Sam Hemelryk
  32   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  33   */
  34  class block_navigation_renderer extends plugin_renderer_base {
  35  
  36      /**
  37       * Returns the content of the navigation tree.
  38       *
  39       * @param global_navigation $navigation
  40       * @param int $expansionlimit
  41       * @param array $options
  42       * @return string $content
  43       */
  44      public function navigation_tree(global_navigation $navigation, $expansionlimit, array $options = array()) {
  45          $navigation->add_class('navigation_node');
  46          $navigationattrs = array(
  47              'class' => 'block_tree list',
  48              'role' => 'tree',
  49              'data-ajax-loader' => 'block_navigation/nav_loader');
  50          $content = $this->navigation_node(array($navigation), $navigationattrs, $expansionlimit, $options);
  51          if (isset($navigation->id) && !is_numeric($navigation->id) && !empty($content)) {
  52              $content = $this->output->box($content, 'block_tree_box', $navigation->id);
  53          }
  54          return $content;
  55      }
  56      /**
  57       * Produces a navigation node for the navigation tree
  58       *
  59       * @param navigation_node[] $items
  60       * @param array $attrs
  61       * @param int $expansionlimit
  62       * @param array $options
  63       * @param int $depth
  64       * @return string
  65       */
  66      protected function navigation_node($items, $attrs=array(), $expansionlimit=null, array $options = array(), $depth=1) {
  67          // Exit if empty, we don't want an empty ul element.
  68          if (count($items) === 0) {
  69              return '';
  70          }
  71  
  72          // Turn our navigation items into list items.
  73          $lis = array();
  74          // Set the number to be static for unique id's.
  75          static $number = 0;
  76          $htmlidprefix = html_writer::random_id();
  77          foreach ($items as $item) {
  78              $number++;
  79              if (!$item->display && !$item->contains_active_node()) {
  80                  continue;
  81              }
  82  
  83              $isexpandable = (empty($expansionlimit) || ($item->type > navigation_node::TYPE_ACTIVITY || $item->type < $expansionlimit) || ($item->contains_active_node() && $item->children->count() > 0));
  84  
  85              // Skip elements which have no content and no action - no point in showing them
  86              if (!$isexpandable && empty($item->action)) {
  87                  continue;
  88              }
  89  
  90              $id = $item->id ? $item->id : html_writer::random_id();
  91              $content = $item->get_content();
  92              $title = $item->get_title();
  93              $ulattr = ['id' => $id . '_group', 'role' => 'group'];
  94              $liattr = ['class' => [$item->get_css_type(), 'depth_'.$depth], 'role' => 'treeitem'];
  95              $pattr = ['class' => ['tree_item']];
  96              $pattr += !empty($item->id) ? ['id' => $item->id] : [];
  97              $isbranch = $isexpandable && ($item->children->count() > 0 || ($item->has_children() && (isloggedin() || $item->type <= navigation_node::TYPE_CATEGORY)));
  98              $hasicon = ((!$isbranch || $item->type == navigation_node::TYPE_ACTIVITY || $item->type == navigation_node::TYPE_RESOURCE) && $item->icon instanceof renderable);
  99              $icon = '';
 100  
 101              if ($hasicon) {
 102                  $liattr['class'][] = 'item_with_icon';
 103                  $pattr['class'][] = 'hasicon';
 104                  $icon = $this->output->render($item->icon);
 105                  // Because an icon is being used we're going to wrap the actual content in a span.
 106                  // This will allow designers to create columns for the content, as we've done in styles.css.
 107                  $content = $icon . html_writer::span($content, 'item-content-wrap');
 108              }
 109              if ($item->helpbutton !== null) {
 110                  $content = trim($item->helpbutton).html_writer::tag('span', $content, array('class'=>'clearhelpbutton'));
 111              }
 112              if (empty($content)) {
 113                  continue;
 114              }
 115  
 116              $nodetextid = $htmlidprefix . '_label_' . $depth . '_' . $number;
 117              $attributes = array('tabindex' => '-1', 'id' => $nodetextid);
 118              if ($title !== '') {
 119                  $attributes['title'] = $title;
 120              }
 121              if ($item->hidden) {
 122                  $attributes['class'] = 'dimmed_text';
 123              }
 124              if (is_string($item->action) || empty($item->action) ||
 125                      (($item->type === navigation_node::TYPE_CATEGORY || $item->type === navigation_node::TYPE_MY_CATEGORY) &&
 126                      empty($options['linkcategories']))) {
 127                  $content = html_writer::tag('span', $content, $attributes);
 128              } else if ($item->action instanceof action_link) {
 129                  //TODO: to be replaced with something else
 130                  $link = $item->action;
 131                  $link->text = $icon.html_writer::span($link->text, 'item-content-wrap');
 132                  $link->attributes = array_merge($link->attributes, $attributes);
 133                  $content = $this->output->render($link);
 134              } else if ($item->action instanceof moodle_url) {
 135                  $content = html_writer::link($item->action, $content, $attributes);
 136              }
 137  
 138              if ($isbranch) {
 139                  $ariaexpanded = $item->has_children() && (!$item->forceopen || $item->collapse);
 140                  $pattr['class'][] = 'branch';
 141                  $liattr['class'][] = 'contains_branch';
 142                  $liattr += ['aria-expanded' => $ariaexpanded ? "false" : "true"];
 143                  if ($item->requiresajaxloading) {
 144                      $liattr += [
 145                          'data-requires-ajax' => 'true',
 146                          'data-loaded' => 'false',
 147                          'data-node-id' => $item->id,
 148                          'data-node-key' => $item->key,
 149                          'data-node-type' => $item->type
 150                      ];
 151                  } else {
 152                      $liattr += ['aria-owns' => $id . '_group'];
 153                  }
 154              }
 155  
 156              if ($item->isactive === true) {
 157                  $liattr['class'][] = 'current_branch';
 158              }
 159              if (!empty($item->classes) && count($item->classes)>0) {
 160                  $pattr['class'] = array_merge($pattr['class'], $item->classes);
 161              }
 162  
 163              $liattr['class'] = join(' ', $liattr['class']);
 164              $pattr['class'] = join(' ', $pattr['class']);
 165  
 166              $liattr += $depth == 1 ? ['data-collapsible' => 'false'] : [];
 167              if (isset($liattr['aria-expanded']) && $liattr['aria-expanded'] === 'false') {
 168                  $ulattr += ['aria-hidden' => 'true'];
 169              }
 170  
 171              // Create the structure.
 172              $content = html_writer::tag('p', $content, $pattr);
 173              if ($isexpandable) {
 174                  $content .= $this->navigation_node($item->children, $ulattr, $expansionlimit, $options, $depth + 1);
 175              }
 176              if (!empty($item->preceedwithhr) && $item->preceedwithhr===true) {
 177                  $content = html_writer::empty_tag('hr') . $content;
 178              }
 179  
 180              $liattr['aria-labelledby'] = $nodetextid;
 181              $content = html_writer::tag('li', $content, $liattr);
 182              $lis[] = $content;
 183          }
 184  
 185          if (count($lis) === 0) {
 186              // There is still a chance, despite having items, that nothing had content and no list items were created.
 187              return '';
 188          }
 189  
 190          // We used to separate using new lines, however we don't do that now, instead we'll save a few chars.
 191          // The source is complex already anyway.
 192          return html_writer::tag('ul', implode('', $lis), $attrs);
 193      }
 194  }