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 39 and 401]

   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   * Settings block
  19   *
  20   * @package    block_settings
  21   * @copyright  2010 Sam Hemelryk
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  class block_settings_renderer extends plugin_renderer_base {
  26  
  27      public function settings_tree(settings_navigation $navigation) {
  28          $count = 0;
  29          foreach ($navigation->children as &$child) {
  30              $child->preceedwithhr = ($count!==0);
  31              if ($child->display) {
  32                  $count++;
  33              }
  34          }
  35          $navigationattrs = array(
  36              'class' => 'block_tree list',
  37              'role' => 'tree',
  38              'data-ajax-loader' => 'block_navigation/site_admin_loader');
  39          $content = $this->navigation_node($navigation, $navigationattrs);
  40          if (isset($navigation->id) && !is_numeric($navigation->id) && !empty($content)) {
  41              $content = $this->output->box($content, 'block_tree_box', $navigation->id);
  42          }
  43          return $content;
  44      }
  45  
  46      /**
  47       * Build the navigation node.
  48       *
  49       * @param navigation_node $node the navigation node object.
  50       * @param array $attrs list of attributes.
  51       * @param int $depth the depth, default to 1.
  52       * @return string the navigation node code.
  53       */
  54      protected function navigation_node(navigation_node $node, $attrs=array(), $depth = 1) {
  55          $items = $node->children;
  56  
  57          // exit if empty, we don't want an empty ul element
  58          if ($items->count()==0) {
  59              return '';
  60          }
  61  
  62          // array of nested li elements
  63          $lis = array();
  64          $number = 0;
  65          foreach ($items as $item) {
  66              $number++;
  67              if (!$item->display) {
  68                  continue;
  69              }
  70  
  71              $isbranch = ($item->children->count()>0  || $item->nodetype==navigation_node::NODETYPE_BRANCH);
  72  
  73              if ($isbranch) {
  74                  $item->hideicon = true;
  75              }
  76  
  77              $content = $this->output->render($item);
  78              $id = $item->id ? $item->id : html_writer::random_id();
  79              $ulattr = ['id' => $id . '_group', 'role' => 'group'];
  80              $liattr = ['class' => [$item->get_css_type(), 'depth_'.$depth], 'tabindex' => '-1', 'role' => 'treeitem'];
  81              $pattr = ['class' => ['tree_item']];
  82              $pattr += !empty($item->id) ? ['id' => $item->id] : [];
  83              $hasicon = (!$isbranch && $item->icon instanceof renderable);
  84  
  85              if ($isbranch) {
  86                  $liattr['class'][] = 'contains_branch';
  87                  if (!$item->forceopen || (!$item->forceopen && $item->collapse) || ($item->children->count() == 0
  88                          && $item->nodetype == navigation_node::NODETYPE_BRANCH)) {
  89                      $liattr += ['aria-expanded' => 'false'];
  90                  } else {
  91                      $liattr += ['aria-expanded' => 'true'];
  92                  }
  93                  if ($item->requiresajaxloading) {
  94                      $liattr['data-requires-ajax'] = 'true';
  95                      $liattr['data-loaded'] = 'false';
  96                  } else {
  97                      $liattr += ['aria-owns' => $id . '_group'];
  98                  }
  99              } else if ($hasicon) {
 100                  $liattr['class'][] = 'item_with_icon';
 101                  $pattr['class'][] = 'hasicon';
 102              }
 103              if ($item->isactive === true) {
 104                  $liattr['class'][] = 'current_branch';
 105              }
 106              if (!empty($item->classes) && count($item->classes) > 0) {
 107                  $pattr['class'] = array_merge($pattr['class'], $item->classes);
 108              }
 109  
 110              // class attribute on the div item which only contains the item content
 111              $pattr['class'][] = 'tree_item';
 112              if ($isbranch) {
 113                  $pattr['class'][] = 'branch';
 114              } else {
 115                  $pattr['class'][] = 'leaf';
 116              }
 117  
 118              $liattr['class'] = join(' ', $liattr['class']);
 119              $pattr['class'] = join(' ', $pattr['class']);
 120  
 121              if (isset($liattr['aria-expanded']) && $liattr['aria-expanded'] === 'false') {
 122                  $ulattr += ['aria-hidden' => 'true'];
 123              }
 124  
 125              $content = html_writer::tag('p', $content, $pattr) . $this->navigation_node($item, $ulattr, $depth + 1);
 126              if (!empty($item->preceedwithhr) && $item->preceedwithhr===true) {
 127                  $content = html_writer::empty_tag('hr') . $content;
 128              }
 129              $content = html_writer::tag('li', $content, $liattr);
 130              $lis[] = $content;
 131          }
 132  
 133          if (count($lis)) {
 134              if (empty($attrs['role'])) {
 135                  $attrs['role'] = 'group';
 136              }
 137              return html_writer::tag('ul', implode("\n", $lis), $attrs);
 138          } else {
 139              return '';
 140          }
 141      }
 142  
 143      public function search_form(moodle_url $formtarget, $searchvalue) {
 144          $data = [
 145              'action' => $formtarget,
 146              'inputname' => 'query',
 147              'searchstring' => get_string('searchinsettings', 'admin'),
 148              'query' => $searchvalue
 149          ];
 150          return $this->render_from_template('core/search_input', $data);
 151      }
 152  
 153  }