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 401 and 402] [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  namespace core\navigation\views;
  18  
  19  use navigation_node;
  20  use navigation_node_collection;
  21  
  22  /**
  23   * Class view.
  24   *
  25   * The base view class which expands on the navigation_node,
  26   *
  27   * @package     core
  28   * @category    navigation
  29   * @copyright   2021 onwards Peter Dias
  30   * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  31   */
  32  abstract class view extends navigation_node {
  33      /** @var stdClass $context the current context */
  34      protected $context;
  35      /** @var moodle_page $page the moodle page that the navigation belongs to */
  36      protected $page;
  37      /** @var bool $initialised A switch to see if the navigation node is initialised */
  38      protected $initialised = false;
  39      /** @var navigation_node $activenode A string identifier for the active node*/
  40      public $activenode;
  41  
  42      /**
  43       * Function to initialise the respective view
  44       * @return void
  45       */
  46      abstract public function initialise(): void;
  47  
  48      /**
  49       * navigation constructor.
  50       * @param \moodle_page $page
  51       */
  52      public function __construct(\moodle_page $page) {
  53          global $FULLME;
  54  
  55          if (during_initial_install()) {
  56              return false;
  57          }
  58  
  59          $this->page = $page;
  60          $this->context = $this->page->context;
  61          $this->children = new navigation_node_collection();
  62      }
  63  
  64      /**
  65       * Get the leaf nodes for the nav view
  66       *
  67       * @param navigation_node $source The settingsnav OR navigation object
  68       * @param array $nodes An array of nodes to fetch from the source which specifies the node type and final order
  69       * @return array $nodesordered The fetched nodes ordered based on final specification.
  70       */
  71      protected function get_leaf_nodes(navigation_node $source, array $nodes): array {
  72          $nodesordered = [];
  73          foreach ($nodes as $type => $leaves) {
  74              foreach ($leaves as $leaf => $location) {
  75                  if ($node = $source->find($leaf, $type)) {
  76                      $nodesordered["$location"] = $nodesordered["$location"] ?? $node;
  77                  }
  78              }
  79          }
  80  
  81          return $nodesordered;
  82      }
  83  
  84      /**
  85       * Scan the given node for the active node. It starts first with a strict search and then switches to a base search if
  86       * required.
  87       *
  88       * @param navigation_node $node The node to scan.
  89       * @return navigation_node|null The active node or null.
  90       */
  91      protected function scan_for_active_node(navigation_node $node): ?navigation_node {
  92          $result = $this->active_node_scan($node);
  93          if (!is_null($result)) {
  94              return $result;
  95          } else {
  96              return $this->active_node_scan($node, URL_MATCH_BASE);
  97          }
  98      }
  99  
 100      /**
 101       * This function recursively scans nodes until it finds the active node or there
 102       * are no more nodes. We are using a custom implementation here to adjust the strictness
 103       * and also because we need the parent node and not the specific child node in the new views.
 104       * e.g. Structure for site admin,
 105       *      SecondaryNav
 106       *          - Site Admin
 107       *          - Users
 108       *              - User policies
 109       *          - Courses
 110       * In the above example, if we are on the 'User Policies' page, the active node should be 'Users'
 111       *
 112       * @param navigation_node $node
 113       * @param int $strictness How stict to be with the scan for the active node.
 114       * @return navigation_node|null
 115       */
 116      protected function active_node_scan(navigation_node $node,
 117          int $strictness = URL_MATCH_EXACT): ?navigation_node {
 118  
 119          $result = null;
 120          $activekey = $this->page->get_secondary_active_tab();
 121          if ($activekey) {
 122              if ($node->key && $activekey === $node->key) {
 123                  return $node;
 124              }
 125          } else if ($node->check_if_active($strictness)) {
 126              return $node; // No need to continue, exit function.
 127          }
 128  
 129          foreach ($node->children as $child) {
 130              if ($this->active_node_scan($child, $strictness)) {
 131                  // If node is one of the new views then set the active node to the child.
 132                  if (!$node instanceof view) {
 133                      $node->make_active();
 134                      $result = $node;
 135                  } else {
 136                      $child->make_active();
 137                      $this->activenode = $child;
 138                      $result = $child;
 139                  }
 140  
 141                  // If the secondary active tab not set then just return the result (fallback).
 142                  if ($activekey === null) {
 143                      return $result;
 144                  }
 145              } else {
 146                  // Make sure to reset the active state.
 147                  $child->make_inactive();
 148              }
 149          }
 150  
 151          return $result;
 152      }
 153  }