Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 3.9.x will end* 10 May 2021 (12 months).
  • Bug fixes for security issues in 3.9.x will end* 8 May 2023 (36 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.
   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   * Contains the navigation renderable for user data exports.
  19   *
  20   * @package    core_privacy
  21   * @copyright  2018 Adrian Greeve
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  namespace core_privacy\output;
  25  defined('MOODLE_INTERNAL') || die();
  26  
  27  use renderable;
  28  use renderer_base;
  29  use templatable;
  30  
  31  /**
  32   * Class containing the navigation renderable
  33   *
  34   * @copyright  2018 Adrian Greeve
  35   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  36   */
  37  class exported_navigation_page implements renderable, templatable {
  38  
  39      /** @var array $tree Full tree in multidimensional form. */
  40      protected $tree;
  41  
  42      /** @var boolean $firstelement This is used to create unique classes for the first elements in the navigation tree. */
  43      protected $firstelement = true;
  44  
  45      /**
  46       * Constructor
  47       *
  48       * @param \stdClass $tree Full tree to create navigation out of.
  49       */
  50      public function __construct(\stdClass $tree) {
  51          $this->tree = $tree;
  52      }
  53  
  54      /**
  55       * Creates the navigation list html. Why this and not a template? My attempts at trying to get a recursive template
  56       * working failed.
  57       *
  58       * @param  \stdClass $tree Full tree to create navigation out of.
  59       * @return string navigation html.
  60       */
  61      protected function create_navigation(\stdClass $tree) {
  62          if ($this->firstelement) {
  63              $html = \html_writer::start_tag('ul', ['class' => 'treeview parent block_tree list', 'id' => 'my-tree']);
  64              $this->firstelement = false;
  65          } else {
  66              $html = \html_writer::start_tag('ul', ['class' => 'parent', 'role' => 'group']);
  67          }
  68          foreach ($tree->children as $child) {
  69              if (isset($child->children)) {
  70                  $html .= \html_writer::start_tag('li', ['class' => 'menu-item', 'role' => 'treeitem', 'aria-expanded' => 'false']);
  71                  $html .= $child->name;
  72                  $html .= $this->create_navigation($child);
  73              } else {
  74                  $html .= \html_writer::start_tag('li', ['class' => 'item', 'role' => 'treeitem', 'aria-expanded' => 'false']);
  75                  // Normal display.
  76                  if (isset($child->datavar)) {
  77                      $html .= \html_writer::link('#', $child->name, ['data-var' => $child->datavar]);
  78                  } else {
  79                      $html .= \html_writer::link($child->url, $child->name, ['target' => '_blank']);
  80                  }
  81              }
  82              $html .= \html_writer::end_tag('li');
  83          }
  84          $html .= \html_writer::end_tag('ul');
  85          return $html;
  86      }
  87  
  88      /**
  89       * Export this data so it can be used as the context for a mustache template.
  90       *
  91       * @param renderer_base $output
  92       * @return array navigation data for the template.
  93       */
  94      public function export_for_template(renderer_base $output) : Array {
  95          $data = $this->create_navigation($this->tree);
  96          return ['navigation' => $data];
  97      }
  98  }