Search moodle.org's
Developer Documentation

  • Bug fixes for general core bugs in 3.11.x will end 9 May 2022 (12 months).
  • Bug fixes for security issues in 3.11.x will end 14 November 2022 (18 months).
  • 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 310 and 311] [Versions 35 and 311] [Versions 36 and 311] [Versions 37 and 311] [Versions 38 and 311] [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   * This file contains classes used to manage the navigation structures within Moodle.
      19   *
      20   * @since      Moodle 2.0
      21   * @package    core
      22   * @copyright  2009 Sam Hemelryk
      23   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
      24   */
      25  
      26  defined('MOODLE_INTERNAL') || die();
      27  
      28  /**
      29   * The name that will be used to separate the navigation cache within SESSION
      30   */
      31  define('NAVIGATION_CACHE_NAME', 'navigation');
      32  define('NAVIGATION_SITE_ADMIN_CACHE_NAME', 'navigationsiteadmin');
      33  
      34  /**
      35   * This class is used to represent a node in a navigation tree
      36   *
      37   * This class is used to represent a node in a navigation tree within Moodle,
      38   * the tree could be one of global navigation, settings navigation, or the navbar.
      39   * Each node can be one of two types either a Leaf (default) or a branch.
      40   * When a node is first created it is created as a leaf, when/if children are added
      41   * the node then becomes a branch.
      42   *
      43   * @package   core
      44   * @category  navigation
      45   * @copyright 2009 Sam Hemelryk
      46   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
      47   */
      48  class navigation_node implements renderable {
      49      /** @var int Used to identify this node a leaf (default) 0 */
      50      const NODETYPE_LEAF =   0;
      51      /** @var int Used to identify this node a branch, happens with children  1 */
      52      const NODETYPE_BRANCH = 1;
      53      /** @var null Unknown node type null */
      54      const TYPE_UNKNOWN =    null;
      55      /** @var int System node type 0 */
      56      const TYPE_ROOTNODE =   0;
      57      /** @var int System node type 1 */
      58      const TYPE_SYSTEM =     1;
      59      /** @var int Category node type 10 */
      60      const TYPE_CATEGORY =   10;
      61      /** var int Category displayed in MyHome navigation node */
      62      const TYPE_MY_CATEGORY = 11;
      63      /** @var int Course node type 20 */
      64      const TYPE_COURSE =     20;
      65      /** @var int Course Structure node type 30 */
      66      const TYPE_SECTION =    30;
      67      /** @var int Activity node type, e.g. Forum, Quiz 40 */
      68      const TYPE_ACTIVITY =   40;
      69      /** @var int Resource node type, e.g. Link to a file, or label 50 */
      70      const TYPE_RESOURCE =   50;
      71      /** @var int A custom node type, default when adding without specifing type 60 */
      72      const TYPE_CUSTOM =     60;
      73      /** @var int Setting node type, used only within settings nav 70 */
      74      const TYPE_SETTING =    70;
      75      /** @var int site admin branch node type, used only within settings nav 71 */
      76      const TYPE_SITE_ADMIN = 71;
      77      /** @var int Setting node type, used only within settings nav 80 */
      78      const TYPE_USER =       80;
      79      /** @var int Setting node type, used for containers of no importance 90 */
      80      const TYPE_CONTAINER =  90;
      81      /** var int Course the current user is not enrolled in */
      82      const COURSE_OTHER = 0;
      83      /** var int Course the current user is enrolled in but not viewing */
      84      const COURSE_MY = 1;
      85      /** var int Course the current user is currently viewing */
      86      const COURSE_CURRENT = 2;
      87      /** var string The course index page navigation node */
      88      const COURSE_INDEX_PAGE = 'courseindexpage';
      89  
      90      /** @var int Parameter to aid the coder in tracking [optional] */
      91      public $id = null;
      92      /** @var string|int The identifier for the node, used to retrieve the node */
      93      public $key = null;
      94      /** @var string The text to use for the node */
      95      public $text = null;
      96      /** @var string Short text to use if requested [optional] */
      97      public $shorttext = null;
      98      /** @var string The title attribute for an action if one is defined */
      99      public $title = null;
     100      /** @var string A string that can be used to build a help button */
     101      public $helpbutton = null;
     102      /** @var moodle_url|action_link|null An action for the node (link) */
     103      public $action = null;
     104      /** @var pix_icon The path to an icon to use for this node */
     105      public $icon = null;
     106      /** @var int See TYPE_* constants defined for this class */
     107      public $type = self::TYPE_UNKNOWN;
     108      /** @var int See NODETYPE_* constants defined for this class */
     109      public $nodetype = self::NODETYPE_LEAF;
     110      /** @var bool If set to true the node will be collapsed by default */
     111      public $collapse = false;
     112      /** @var bool If set to true the node will be expanded by default */
     113      public $forceopen = false;
     114      /** @var array An array of CSS classes for the node */
     115      public $classes = array();
     116      /** @var navigation_node_collection An array of child nodes */
     117      public $children = array();
     118      /** @var bool If set to true the node will be recognised as active */
     119      public $isactive = false;
     120      /** @var bool If set to true the node will be dimmed */
     121      public $hidden = false;
     122      /** @var bool If set to false the node will not be displayed */
     123      public $display = true;
     124      /** @var bool If set to true then an HR will be printed before the node */
     125      public $preceedwithhr = false;
     126      /** @var bool If set to true the the navigation bar should ignore this node */
     127      public $mainnavonly = false;
     128      /** @var bool If set to true a title will be added to the action no matter what */
     129      public $forcetitle = false;
     130      /** @var navigation_node A reference to the node parent, you should never set this directly you should always call set_parent */
     131      public $parent = null;
     132      /** @var bool Override to not display the icon even if one is provided **/
     133      public $hideicon = false;
     134      /** @var bool Set to true if we KNOW that this node can be expanded.  */
     135      public $isexpandable = false;
     136      /** @var array */
     137      protected $namedtypes = array(0 => 'system', 10 => 'category', 20 => 'course', 30 => 'structure', 40 => 'activity',
     138                                    50 => 'resource', 60 => 'custom', 70 => 'setting', 71 => 'siteadmin', 80 => 'user',
     139                                    90 => 'container');
     140      /** @var moodle_url */
     141      protected static $fullmeurl = null;
     142      /** @var bool toogles auto matching of active node */
     143      public static $autofindactive = true;
     144      /** @var bool should we load full admin tree or rely on AJAX for performance reasons */
     145      protected static $loadadmintree = false;
     146      /** @var mixed If set to an int, that section will be included even if it has no activities */
     147      public $includesectionnum = false;
     148      /** @var bool does the node need to be loaded via ajax */
     149      public $requiresajaxloading = false;
     150      /** @var bool If set to true this node will be added to the "flat" navigation */
     151      public $showinflatnavigation = false;
     152  
     153      /**
     154       * Constructs a new navigation_node
     155       *
     156       * @param array|string $properties Either an array of properties or a string to use
     157       *                     as the text for the node
     158       */
     159      public function __construct($properties) {
     160          if (is_array($properties)) {
     161              // Check the array for each property that we allow to set at construction.
     162              // text         - The main content for the node
     163              // shorttext    - A short text if required for the node
     164              // icon         - The icon to display for the node
     165              // type         - The type of the node
     166              // key          - The key to use to identify the node
     167              // parent       - A reference to the nodes parent
     168              // action       - The action to attribute to this node, usually a URL to link to
     169              if (array_key_exists('text', $properties)) {
     170                  $this->text = $properties['text'];
     171              }
     172              if (array_key_exists('shorttext', $properties)) {
     173                  $this->shorttext = $properties['shorttext'];
     174              }
     175              if (!array_key_exists('icon', $properties)) {
     176                  $properties['icon'] = new pix_icon('i/navigationitem', '');
     177              }
     178              $this->icon = $properties['icon'];
     179              if ($this->icon instanceof pix_icon) {
     180                  if (empty($this->icon->attributes['class'])) {
     181                      $this->icon->attributes['class'] = 'navicon';
     182                  } else {
     183                      $this->icon->attributes['class'] .= ' navicon';
     184                  }
     185              }
     186              if (array_key_exists('type', $properties)) {
     187                  $this->type = $properties['type'];
     188              } else {
     189                  $this->type = self::TYPE_CUSTOM;
     190              }
     191              if (array_key_exists('key', $properties)) {
     192                  $this->key = $properties['key'];
     193              }
     194              // This needs to happen last because of the check_if_active call that occurs
     195              if (array_key_exists('action', $properties)) {
     196                  $this->action = $properties['action'];
     197                  if (is_string($this->action)) {
     198                      $this->action = new moodle_url($this->action);
     199                  }
     200                  if (self::$autofindactive) {
     201                      $this->check_if_active();
     202                  }
     203              }
     204              if (array_key_exists('parent', $properties)) {
     205                  $this->set_parent($properties['parent']);
     206              }
     207          } else if (is_string($properties)) {
     208              $this->text = $properties;
     209          }
     210          if ($this->text === null) {
     211              throw new coding_exception('You must set the text for the node when you create it.');
     212          }
     213          // Instantiate a new navigation node collection for this nodes children
     214          $this->children = new navigation_node_collection();
     215      }
     216  
     217      /**
     218       * Checks if this node is the active node.
     219       *
     220       * This is determined by comparing the action for the node against the
     221       * defined URL for the page. A match will see this node marked as active.
     222       *
     223       * @param int $strength One of URL_MATCH_EXACT, URL_MATCH_PARAMS, or URL_MATCH_BASE
     224       * @return bool
     225       */
     226      public function check_if_active($strength=URL_MATCH_EXACT) {
     227          global $FULLME, $PAGE;
     228          // Set fullmeurl if it hasn't already been set
     229          if (self::$fullmeurl == null) {
     230              if ($PAGE->has_set_url()) {
     231                  self::override_active_url(new moodle_url($PAGE->url));
     232              } else {
     233                  self::override_active_url(new moodle_url($FULLME));
     234              }
     235          }
     236  
     237          // Compare the action of this node against the fullmeurl
     238          if ($this->action instanceof moodle_url && $this->action->compare(self::$fullmeurl, $strength)) {
     239              $this->make_active();
     240              return true;
     241          }
     242          return false;
     243      }
     244  
     245      /**
     246       * True if this nav node has siblings in the tree.
     247       *
     248       * @return bool
     249       */
     250      public function has_siblings() {
     251          if (empty($this->parent) || empty($this->parent->children)) {
     252              return false;
     253          }
     254          if ($this->parent->children instanceof navigation_node_collection) {
     255              $count = $this->parent->children->count();
     256          } else {
     257              $count = count($this->parent->children);
     258          }
     259          return ($count > 1);
     260      }
     261  
     262      /**
     263       * Get a list of sibling navigation nodes at the same level as this one.
     264       *
     265       * @return bool|array of navigation_node
     266       */
     267      public function get_siblings() {
     268          // Returns a list of the siblings of the current node for display in a flat navigation element. Either
     269          // the in-page links or the breadcrumb links.
     270          $siblings = false;
     271  
     272          if ($this->has_siblings()) {
     273              $siblings = [];
     274              foreach ($this->parent->children as $child) {
     275                  if ($child->display) {
     276                      $siblings[] = $child;
     277                  }
     278              }
     279          }
     280          return $siblings;
     281      }
     282  
     283      /**
     284       * This sets the URL that the URL of new nodes get compared to when locating
     285       * the active node.
     286       *
     287       * The active node is the node that matches the URL set here. By default this
     288       * is either $PAGE->url or if that hasn't been set $FULLME.
     289       *
     290       * @param moodle_url $url The url to use for the fullmeurl.
     291       * @param bool $loadadmintree use true if the URL point to administration tree
     292       */
     293      public static function override_active_url(moodle_url $url, $loadadmintree = false) {
     294          // Clone the URL, in case the calling script changes their URL later.
     295          self::$fullmeurl = new moodle_url($url);
     296          // True means we do not want AJAX loaded admin tree, required for all admin pages.
     297          if ($loadadmintree) {
     298              // Do not change back to false if already set.
     299              self::$loadadmintree = true;
     300          }
     301      }
     302  
     303      /**
     304       * Use when page is linked from the admin tree,
     305       * if not used navigation could not find the page using current URL
     306       * because the tree is not fully loaded.
     307       */
     308      public static function require_admin_tree() {
     309          self::$loadadmintree = true;
     310      }
     311  
     312      /**
     313       * Creates a navigation node, ready to add it as a child using add_node
     314       * function. (The created node needs to be added before you can use it.)
     315       * @param string $text
     316       * @param moodle_url|action_link $action
     317       * @param int $type
     318       * @param string $shorttext
     319       * @param string|int $key
     320       * @param pix_icon $icon
     321       * @return navigation_node
     322       */
     323      public static function create($text, $action=null, $type=self::TYPE_CUSTOM,
     324              $shorttext=null, $key=null, pix_icon $icon=null) {
     325          // Properties array used when creating the new navigation node
     326          $itemarray = array(
     327              'text' => $text,
     328              'type' => $type
     329          );
     330          // Set the action if one was provided
     331          if ($action!==null) {
     332              $itemarray['action'] = $action;
     333          }
     334          // Set the shorttext if one was provided
     335          if ($shorttext!==null) {
     336              $itemarray['shorttext'] = $shorttext;
     337          }
     338          // Set the icon if one was provided
     339          if ($icon!==null) {
     340              $itemarray['icon'] = $icon;
     341          }
     342          // Set the key
     343          $itemarray['key'] = $key;
     344          // Construct and return
     345          return new navigation_node($itemarray);
     346      }
     347  
     348      /**
     349       * Adds a navigation node as a child of this node.
     350       *
     351       * @param string $text
     352       * @param moodle_url|action_link $action
     353       * @param int $type
     354       * @param string $shorttext
     355       * @param string|int $key
     356       * @param pix_icon $icon
     357       * @return navigation_node
     358       */
     359      public function add($text, $action=null, $type=self::TYPE_CUSTOM, $shorttext=null, $key=null, pix_icon $icon=null) {
     360          // Create child node
     361          $childnode = self::create($text, $action, $type, $shorttext, $key, $icon);
     362  
     363          // Add the child to end and return
     364          return $this->add_node($childnode);
     365      }
     366  
     367      /**
     368       * Adds a navigation node as a child of this one, given a $node object
     369       * created using the create function.
     370       * @param navigation_node $childnode Node to add
     371       * @param string $beforekey
     372       * @return navigation_node The added node
     373       */
     374      public function add_node(navigation_node $childnode, $beforekey=null) {
     375          // First convert the nodetype for this node to a branch as it will now have children
     376          if ($this->nodetype !== self::NODETYPE_BRANCH) {
     377              $this->nodetype = self::NODETYPE_BRANCH;
     378          }
     379          // Set the parent to this node
     380          $childnode->set_parent($this);
     381  
     382          // Default the key to the number of children if not provided
     383          if ($childnode->key === null) {
     384              $childnode->key = $this->children->count();
     385          }
     386  
     387          // Add the child using the navigation_node_collections add method
     388          $node = $this->children->add($childnode, $beforekey);
     389  
     390          // If added node is a category node or the user is logged in and it's a course
     391          // then mark added node as a branch (makes it expandable by AJAX)
     392          $type = $childnode->type;
     393          if (($type == self::TYPE_CATEGORY) || (isloggedin() && ($type == self::TYPE_COURSE)) || ($type == self::TYPE_MY_CATEGORY) ||
     394                  ($type === self::TYPE_SITE_ADMIN)) {
     395              $node->nodetype = self::NODETYPE_BRANCH;
     396          }
     397          // If this node is hidden mark it's children as hidden also
     398          if ($this->hidden) {
     399              $node->hidden = true;
     400          }
     401          // Return added node (reference returned by $this->children->add()
     402          return $node;
     403      }
     404  
     405      /**
     406       * Return a list of all the keys of all the child nodes.
     407       * @return array the keys.
     408       */
     409      public function get_children_key_list() {
     410          return $this->children->get_key_list();
     411      }
     412  
     413      /**
     414       * Searches for a node of the given type with the given key.
     415       *
     416       * This searches this node plus all of its children, and their children....
     417       * If you know the node you are looking for is a child of this node then please
     418       * use the get method instead.
     419       *
     420       * @param int|string $key The key of the node we are looking for
     421       * @param int $type One of navigation_node::TYPE_*
     422       * @return navigation_node|false
     423       */
     424      public function find($key, $type) {
     425          return $this->children->find($key, $type);
     426      }
     427  
     428      /**
     429       * Walk the tree building up a list of all the flat navigation nodes.
     430       *
     431       * @param flat_navigation $nodes List of the found flat navigation nodes.
     432       * @param boolean $showdivider Show a divider before the first node.
     433       * @param string $label A label for the collection of navigation links.
     434       */
     435      public function build_flat_navigation_list(flat_navigation $nodes, $showdivider = false, $label = '') {
     436          if ($this->showinflatnavigation) {
     437              $indent = 0;
     438              if ($this->type == self::TYPE_COURSE || $this->key === self::COURSE_INDEX_PAGE) {
     439                  $indent = 1;
     440              }
     441              $flat = new flat_navigation_node($this, $indent);
     442              $flat->set_showdivider($showdivider, $label);
     443              $nodes->add($flat);
     444          }
     445          foreach ($this->children as $child) {
     446              $child->build_flat_navigation_list($nodes, false);
     447          }
     448      }
     449  
     450      /**
     451       * Get the child of this node that has the given key + (optional) type.
     452       *
     453       * If you are looking for a node and want to search all children + their children
     454       * then please use the find method instead.
     455       *
     456       * @param int|string $key The key of the node we are looking for
     457       * @param int $type One of navigation_node::TYPE_*
     458       * @return navigation_node|false
     459       */
     460      public function get($key, $type=null) {
     461          return $this->children->get($key, $type);
     462      }
     463  
     464      /**
     465       * Removes this node.
     466       *
     467       * @return bool
     468       */
     469      public function remove() {
     470          return $this->parent->children->remove($this->key, $this->type);
     471      }
     472  
     473      /**
     474       * Checks if this node has or could have any children
     475       *
     476       * @return bool Returns true if it has children or could have (by AJAX expansion)
     477       */
     478      public function has_children() {
     479          return ($this->nodetype === navigation_node::NODETYPE_BRANCH || $this->children->count()>0 || $this->isexpandable);
     480      }
     481  
     482      /**
     483       * Marks this node as active and forces it open.
     484       *
     485       * Important: If you are here because you need to mark a node active to get
     486       * the navigation to do what you want have you looked at {@link navigation_node::override_active_url()}?
     487       * You can use it to specify a different URL to match the active navigation node on
     488       * rather than having to locate and manually mark a node active.
     489       */
     490      public function make_active() {
     491          $this->isactive = true;
     492          $this->add_class('active_tree_node');
     493          $this->force_open();
     494          if ($this->parent !== null) {
     495              $this->parent->make_inactive();
     496          }
     497      }
     498  
     499      /**
     500       * Marks a node as inactive and recusised back to the base of the tree
     501       * doing the same to all parents.
     502       */
     503      public function make_inactive() {
     504          $this->isactive = false;
     505          $this->remove_class('active_tree_node');
     506          if ($this->parent !== null) {
     507              $this->parent->make_inactive();
     508          }
     509      }
     510  
     511      /**
     512       * Forces this node to be open and at the same time forces open all
     513       * parents until the root node.
     514       *
     515       * Recursive.
     516       */
     517      public function force_open() {
     518          $this->forceopen = true;
     519          if ($this->parent !== null) {
     520              $this->parent->force_open();
     521          }
     522      }
     523  
     524      /**
     525       * Adds a CSS class to this node.
     526       *
     527       * @param string $class
     528       * @return bool
     529       */
     530      public function add_class($class) {
     531          if (!in_array($class, $this->classes)) {
     532              $this->classes[] = $class;
     533          }
     534          return true;
     535      }
     536  
     537      /**
     538       * Removes a CSS class from this node.
     539       *
     540       * @param string $class
     541       * @return bool True if the class was successfully removed.
     542       */
     543      public function remove_class($class) {
     544          if (in_array($class, $this->classes)) {
     545              $key = array_search($class,$this->classes);
     546              if ($key!==false) {
     547                  // Remove the class' array element.
     548                  unset($this->classes[$key]);
     549                  // Reindex the array to avoid failures when the classes array is iterated later in mustache templates.
     550                  $this->classes = array_values($this->classes);
     551  
     552                  return true;
     553              }
     554          }
     555          return false;
     556      }
     557  
     558      /**
     559       * Sets the title for this node and forces Moodle to utilise it.
     560       * @param string $title
     561       */
     562      public function title($title) {
     563          $this->title = $title;
     564          $this->forcetitle = true;
     565      }
     566  
     567      /**
     568       * Resets the page specific information on this node if it is being unserialised.
     569       */
     570      public function __wakeup(){
     571          $this->forceopen = false;
     572          $this->isactive = false;
     573          $this->remove_class('active_tree_node');
     574      }
     575  
     576      /**
     577       * Checks if this node or any of its children contain the active node.
     578       *
     579       * Recursive.
     580       *
     581       * @return bool
     582       */
     583      public function contains_active_node() {
     584          if ($this->isactive) {
     585              return true;
     586          } else {
     587              foreach ($this->children as $child) {
     588                  if ($child->isactive || $child->contains_active_node()) {
     589                      return true;
     590                  }
     591              }
     592          }
     593          return false;
     594      }
     595  
     596      /**
     597       * To better balance the admin tree, we want to group all the short top branches together.
     598       *
     599       * This means < 8 nodes and no subtrees.
     600       *
     601       * @return bool
     602       */
     603      public function is_short_branch() {
     604          $limit = 8;
     605          if ($this->children->count() >= $limit) {
     606              return false;
     607          }
     608          foreach ($this->children as $child) {
     609              if ($child->has_children()) {
     610                  return false;
     611              }
     612          }
     613          return true;
     614      }
     615  
     616      /**
     617       * Finds the active node.
     618       *
     619       * Searches this nodes children plus all of the children for the active node
     620       * and returns it if found.
     621       *
     622       * Recursive.
     623       *
     624       * @return navigation_node|false
     625       */
     626      public function find_active_node() {
     627          if ($this->isactive) {
     628              return $this;
     629          } else {
     630              foreach ($this->children as &$child) {
     631                  $outcome = $child->find_active_node();
     632                  if ($outcome !== false) {
     633                      return $outcome;
     634                  }
     635              }
     636          }
     637          return false;
     638      }
     639  
     640      /**
     641       * Searches all children for the best matching active node
     642       * @return navigation_node|false
     643       */
     644      public function search_for_active_node() {
     645          if ($this->check_if_active(URL_MATCH_BASE)) {
     646              return $this;
     647          } else {
     648              foreach ($this->children as &$child) {
     649                  $outcome = $child->search_for_active_node();
     650                  if ($outcome !== false) {
     651                      return $outcome;
     652                  }
     653              }
     654          }
     655          return false;
     656      }
     657  
     658      /**
     659       * Gets the content for this node.
     660       *
     661       * @param bool $shorttext If true shorttext is used rather than the normal text
     662       * @return string
     663       */
     664      public function get_content($shorttext=false) {
     665          if ($shorttext && $this->shorttext!==null) {
     666              return format_string($this->shorttext);
     667          } else {
     668              return format_string($this->text);
     669          }
     670      }
     671  
     672      /**
     673       * Gets the title to use for this node.
     674       *
     675       * @return string
     676       */
     677      public function get_title() {
     678          if ($this->forcetitle || $this->action != null){
     679              return $this->title;
     680          } else {
     681              return '';
     682          }
     683      }
     684  
     685      /**
     686       * Used to easily determine if this link in the breadcrumbs has a valid action/url.
     687       *
     688       * @return boolean
     689       */
     690      public function has_action() {
     691          return !empty($this->action);
     692      }
     693  
     694      /**
     695       * Used to easily determine if this link in the breadcrumbs is hidden.
     696       *
     697       * @return boolean
     698       */
     699      public function is_hidden() {
     700          return $this->hidden;
     701      }
     702  
     703      /**
     704       * Gets the CSS class to add to this node to describe its type
     705       *
     706       * @return string
     707       */
     708      public function get_css_type() {
     709          if (array_key_exists($this->type, $this->namedtypes)) {
     710              return 'type_'.$this->namedtypes[$this->type];
     711          }
     712          return 'type_unknown';
     713      }
     714  
     715      /**
     716       * Finds all nodes that are expandable by AJAX
     717       *
     718       * @param array $expandable An array by reference to populate with expandable nodes.
     719       */
     720      public function find_expandable(array &$expandable) {
     721          foreach ($this->children as &$child) {
     722              if ($child->display && $child->has_children() && $child->children->count() == 0) {
     723                  $child->id = 'expandable_branch_'.$child->type.'_'.clean_param($child->key, PARAM_ALPHANUMEXT);
     724                  $this->add_class('canexpand');
     725                  $child->requiresajaxloading = true;
     726                  $expandable[] = array('id' => $child->id, 'key' => $child->key, 'type' => $child->type);
     727              }
     728              $child->find_expandable($expandable);
     729          }
     730      }
     731  
     732      /**
     733       * Finds all nodes of a given type (recursive)
     734       *
     735       * @param int $type One of navigation_node::TYPE_*
     736       * @return array
     737       */
     738      public function find_all_of_type($type) {
     739          $nodes = $this->children->type($type);
     740          foreach ($this->children as &$node) {
     741              $childnodes = $node->find_all_of_type($type);
     742              $nodes = array_merge($nodes, $childnodes);
     743          }
     744          return $nodes;
     745      }
     746  
     747      /**
     748       * Removes this node if it is empty
     749       */
     750      public function trim_if_empty() {
     751          if ($this->children->count() == 0) {
     752              $this->remove();
     753          }
     754      }
     755  
     756      /**
     757       * Creates a tab representation of this nodes children that can be used
     758       * with print_tabs to produce the tabs on a page.
     759       *
     760       * call_user_func_array('print_tabs', $node->get_tabs_array());
     761       *
     762       * @param array $inactive
     763       * @param bool $return
     764       * @return array Array (tabs, selected, inactive, activated, return)
     765       */
     766      public function get_tabs_array(array $inactive=array(), $return=false) {
     767          $tabs = array();
     768          $rows = array();
     769          $selected = null;
     770          $activated = array();
     771          foreach ($this->children as $node) {
     772              $tabs[] = new tabobject($node->key, $node->action, $node->get_content(), $node->get_title());
     773              if ($node->contains_active_node()) {
     774                  if ($node->children->count() > 0) {
     775                      $activated[] = $node->key;
     776                      foreach ($node->children as $child) {
     777                          if ($child->contains_active_node()) {
     778                              $selected = $child->key;
     779                          }
     780                          $rows[] = new tabobject($child->key, $child->action, $child->get_content(), $child->get_title());
     781                      }
     782                  } else {
     783                      $selected = $node->key;
     784                  }
     785              }
     786          }
     787          return array(array($tabs, $rows), $selected, $inactive, $activated, $return);
     788      }
     789  
     790      /**
     791       * Sets the parent for this node and if this node is active ensures that the tree is properly
     792       * adjusted as well.
     793       *
     794       * @param navigation_node $parent
     795       */
     796      public function set_parent(navigation_node $parent) {
     797          // Set the parent (thats the easy part)
     798          $this->parent = $parent;
     799          // Check if this node is active (this is checked during construction)
     800          if ($this->isactive) {
     801              // Force all of the parent nodes open so you can see this node
     802              $this->parent->force_open();
     803              // Make all parents inactive so that its clear where we are.
     804              $this->parent->make_inactive();
     805          }
     806      }
     807  
     808      /**
     809       * Hides the node and any children it has.
     810       *
     811       * @since Moodle 2.5
     812       * @param array $typestohide Optional. An array of node types that should be hidden.
     813       *      If null all nodes will be hidden.
     814       *      If an array is given then nodes will only be hidden if their type mtatches an element in the array.
     815       *          e.g. array(navigation_node::TYPE_COURSE) would hide only course nodes.
     816       */
     817      public function hide(array $typestohide = null) {
     818          if ($typestohide === null || in_array($this->type, $typestohide)) {
     819              $this->display = false;
     820              if ($this->has_children()) {
     821                  foreach ($this->children as $child) {
     822                      $child->hide($typestohide);
     823                  }
     824              }
     825          }
     826      }
     827  
     828      /**
     829       * Get the action url for this navigation node.
     830       * Called from templates.
     831       *
     832       * @since Moodle 3.2
     833       */
     834      public function action() {
     835          if ($this->action instanceof moodle_url) {
     836              return $this->action;
     837          } else if ($this->action instanceof action_link) {
     838              return $this->action->url;
     839          }
     840          return $this->action;
     841      }
     842  
     843      /**
     844       * Add the menu item to handle locking and unlocking of a conext.
     845       *
     846       * @param \navigation_node $node Node to add
     847       * @param \context $context The context to be locked
     848       */
     849      protected function add_context_locking_node(\navigation_node $node, \context $context) {
     850          global $CFG;
     851          // Manage context locking.
     852          if (!empty($CFG->contextlocking) && has_capability('moodle/site:managecontextlocks', $context)) {
     853              $parentcontext = $context->get_parent_context();
     854              if (empty($parentcontext) || !$parentcontext->locked) {
     855                  if ($context->locked) {
     856                      $lockicon = 'i/unlock';
     857                      $lockstring = get_string('managecontextunlock', 'admin');
     858                  } else {
     859                      $lockicon = 'i/lock';
     860                      $lockstring = get_string('managecontextlock', 'admin');
     861                  }
     862                  $node->add(
     863                      $lockstring,
     864                      new moodle_url(
     865                          '/admin/lock.php',
     866                          [
     867                              'id' => $context->id,
     868                          ]
     869                      ),
     870                      self::TYPE_SETTING,
     871                      null,
     872                      'contextlocking',
     873                       new pix_icon($lockicon, '')
     874                  );
     875              }
     876          }
     877  
     878      }
     879  }
     880  
     881  /**
     882   * Navigation node collection
     883   *
     884   * This class is responsible for managing a collection of navigation nodes.
     885   * It is required because a node's unique identifier is a combination of both its
     886   * key and its type.
     887   *
     888   * Originally an array was used with a string key that was a combination of the two
     889   * however it was decided that a better solution would be to use a class that
     890   * implements the standard IteratorAggregate interface.
     891   *
     892   * @package   core
     893   * @category  navigation
     894   * @copyright 2010 Sam Hemelryk
     895   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
     896   */
     897  class navigation_node_collection implements IteratorAggregate, Countable {
     898      /**
     899       * A multidimensional array to where the first key is the type and the second
     900       * key is the nodes key.
     901       * @var array
     902       */
     903      protected $collection = array();
     904      /**
     905       * An array that contains references to nodes in the same order they were added.
     906       * This is maintained as a progressive array.
     907       * @var array
     908       */
     909      protected $orderedcollection = array();
     910      /**
     911       * A reference to the last node that was added to the collection
     912       * @var navigation_node
     913       */
     914      protected $last = null;
     915      /**
     916       * The total number of items added to this array.
     917       * @var int
     918       */
     919      protected $count = 0;
     920  
     921      /**
     922       * Label for collection of nodes.
     923       * @var string
     924       */
     925      protected $collectionlabel = '';
     926  
     927      /**
     928       * Adds a navigation node to the collection
     929       *
     930       * @param navigation_node $node Node to add
     931       * @param string $beforekey If specified, adds before a node with this key,
     932       *   otherwise adds at end
     933       * @return navigation_node Added node
     934       */
     935      public function add(navigation_node $node, $beforekey=null) {
     936          global $CFG;
     937          $key = $node->key;
     938          $type = $node->type;
     939  
     940          // First check we have a 2nd dimension for this type
     941          if (!array_key_exists($type, $this->orderedcollection)) {
     942              $this->orderedcollection[$type] = array();
     943          }
     944          // Check for a collision and report if debugging is turned on
     945          if ($CFG->debug && array_key_exists($key, $this->orderedcollection[$type])) {
     946              debugging('Navigation node intersect: Adding a node that already exists '.$key, DEBUG_DEVELOPER);
     947          }
     948  
     949          // Find the key to add before
     950          $newindex = $this->count;
     951          $last = true;
     952          if ($beforekey !== null) {
     953              foreach ($this->collection as $index => $othernode) {
     954                  if ($othernode->key === $beforekey) {
     955                      $newindex = $index;
     956                      $last = false;
     957                      break;
     958                  }
     959              }
     960              if ($newindex === $this->count) {
     961                  debugging('Navigation node add_before: Reference node not found ' . $beforekey .
     962                          ', options: ' . implode(' ', $this->get_key_list()), DEBUG_DEVELOPER);
     963              }
     964          }
     965  
     966          // Add the node to the appropriate place in the by-type structure (which
     967          // is not ordered, despite the variable name)
     968          $this->orderedcollection[$type][$key] = $node;
     969          if (!$last) {
     970              // Update existing references in the ordered collection (which is the
     971              // one that isn't called 'ordered') to shuffle them along if required
     972              for ($oldindex = $this->count; $oldindex > $newindex; $oldindex--) {
     973                  $this->collection[$oldindex] = $this->collection[$oldindex - 1];
     974              }
     975          }
     976          // Add a reference to the node to the progressive collection.
     977          $this->collection[$newindex] = $this->orderedcollection[$type][$key];
     978          // Update the last property to a reference to this new node.
     979          $this->last = $this->orderedcollection[$type][$key];
     980  
     981          // Reorder the array by index if needed
     982          if (!$last) {
     983              ksort($this->collection);
     984          }
     985          $this->count++;
     986          // Return the reference to the now added node
     987          return $node;
     988      }
     989  
     990      /**
     991       * Return a list of all the keys of all the nodes.
     992       * @return array the keys.
     993       */
     994      public function get_key_list() {
     995          $keys = array();
     996          foreach ($this->collection as $node) {
     997              $keys[] = $node->key;
     998          }
     999          return $keys;
    1000      }
    1001  
    1002      /**
    1003       * Set a label for this collection.
    1004       *
    1005       * @param string $label
    1006       */
    1007      public function set_collectionlabel($label) {
    1008          $this->collectionlabel = $label;
    1009      }
    1010  
    1011      /**
    1012       * Return a label for this collection.
    1013       *
    1014       * @return string
    1015       */
    1016      public function get_collectionlabel() {
    1017          return $this->collectionlabel;
    1018      }
    1019  
    1020      /**
    1021       * Fetches a node from this collection.
    1022       *
    1023       * @param string|int $key The key of the node we want to find.
    1024       * @param int $type One of navigation_node::TYPE_*.
    1025       * @return navigation_node|null
    1026       */
    1027      public function get($key, $type=null) {
    1028          if ($type !== null) {
    1029              // If the type is known then we can simply check and fetch
    1030              if (!empty($this->orderedcollection[$type][$key])) {
    1031                  return $this->orderedcollection[$type][$key];
    1032              }
    1033          } else {
    1034              // Because we don't know the type we look in the progressive array
    1035              foreach ($this->collection as $node) {
    1036                  if ($node->key === $key) {
    1037                      return $node;
    1038                  }
    1039              }
    1040          }
    1041          return false;
    1042      }
    1043  
    1044      /**
    1045       * Searches for a node with matching key and type.
    1046       *
    1047       * This function searches both the nodes in this collection and all of
    1048       * the nodes in each collection belonging to the nodes in this collection.
    1049       *
    1050       * Recursive.
    1051       *
    1052       * @param string|int $key  The key of the node we want to find.
    1053       * @param int $type  One of navigation_node::TYPE_*.
    1054       * @return navigation_node|null
    1055       */
    1056      public function find($key, $type=null) {
    1057          if ($type !== null && array_key_exists($type, $this->orderedcollection) && array_key_exists($key, $this->orderedcollection[$type])) {
    1058              return $this->orderedcollection[$type][$key];
    1059          } else {
    1060              $nodes = $this->getIterator();
    1061              // Search immediate children first
    1062              foreach ($nodes as &$node) {
    1063                  if ($node->key === $key && ($type === null || $type === $node->type)) {
    1064                      return $node;
    1065                  }
    1066              }
    1067              // Now search each childs children
    1068              foreach ($nodes as &$node) {
    1069                  $result = $node->children->find($key, $type);
    1070                  if ($result !== false) {
    1071                      return $result;
    1072                  }
    1073              }
    1074          }
    1075          return false;
    1076      }
    1077  
    1078      /**
    1079       * Fetches the last node that was added to this collection
    1080       *
    1081       * @return navigation_node
    1082       */
    1083      public function last() {
    1084          return $this->last;
    1085      }
    1086  
    1087      /**
    1088       * Fetches all nodes of a given type from this collection
    1089       *
    1090       * @param string|int $type  node type being searched for.
    1091       * @return array ordered collection
    1092       */
    1093      public function type($type) {
    1094          if (!array_key_exists($type, $this->orderedcollection)) {
    1095              $this->orderedcollection[$type] = array();
    1096          }
    1097          return $this->orderedcollection[$type];
    1098      }
    1099      /**
    1100       * Removes the node with the given key and type from the collection
    1101       *
    1102       * @param string|int $key The key of the node we want to find.
    1103       * @param int $type
    1104       * @return bool
    1105       */
    1106      public function remove($key, $type=null) {
    1107          $child = $this->get($key, $type);
    1108          if ($child !== false) {
    1109              foreach ($this->collection as $colkey => $node) {
    1110                  if ($node->key === $key && (is_null($type) || $node->type == $type)) {
    1111                      unset($this->collection[$colkey]);
    1112                      $this->collection = array_values($this->collection);
    1113                      break;
    1114                  }
    1115              }
    1116              unset($this->orderedcollection[$child->type][$child->key]);
    1117              $this->count--;
    1118              return true;
    1119          }
    1120          return false;
    1121      }
    1122  
    1123      /**
    1124       * Gets the number of nodes in this collection
    1125       *
    1126       * This option uses an internal count rather than counting the actual options to avoid
    1127       * a performance hit through the count function.
    1128       *
    1129       * @return int
    1130       */
    1131      public function count() {
    1132          return $this->count;
    1133      }
    1134      /**
    1135       * Gets an array iterator for the collection.
    1136       *
    1137       * This is required by the IteratorAggregator interface and is used by routines
    1138       * such as the foreach loop.
    1139       *
    1140       * @return ArrayIterator
    1141       */
    1142      public function getIterator() {
    1143          return new ArrayIterator($this->collection);
    1144      }
    1145  }
    1146  
    1147  /**
    1148   * The global navigation class used for... the global navigation
    1149   *
    1150   * This class is used by PAGE to store the global navigation for the site
    1151   * and is then used by the settings nav and navbar to save on processing and DB calls
    1152   *
    1153   * See
    1154   * {@link lib/pagelib.php} {@link moodle_page::initialise_theme_and_output()}
    1155   * {@link lib/ajax/getnavbranch.php} Called by ajax
    1156   *
    1157   * @package   core
    1158   * @category  navigation
    1159   * @copyright 2009 Sam Hemelryk
    1160   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
    1161   */
    1162  class global_navigation extends navigation_node {
    1163      /** @var moodle_page The Moodle page this navigation object belongs to. */
    1164      protected $page;
    1165      /** @var bool switch to let us know if the navigation object is initialised*/
    1166      protected $initialised = false;
    1167      /** @var array An array of course information */
    1168      protected $mycourses = array();
    1169      /** @var navigation_node[] An array for containing  root navigation nodes */
    1170      protected $rootnodes = array();
    1171      /** @var bool A switch for whether to show empty sections in the navigation */
    1172      protected $showemptysections = true;
    1173      /** @var bool A switch for whether courses should be shown within categories on the navigation. */
    1174      protected $showcategories = null;
    1175      /** @var null@var bool A switch for whether or not to show categories in the my courses branch. */
    1176      protected $showmycategories = null;
    1177      /** @var array An array of stdClasses for users that the navigation is extended for */
    1178      protected $extendforuser = array();
    1179      /** @var navigation_cache */
    1180      protected $cache;
    1181      /** @var array An array of course ids that are present in the navigation */
    1182      protected $addedcourses = array();
    1183      /** @var bool */
    1184      protected $allcategoriesloaded = false;
    1185      /** @var array An array of category ids that are included in the navigation */
    1186      protected $addedcategories = array();
    1187      /** @var int expansion limit */
    1188      protected $expansionlimit = 0;
    1189      /** @var int userid to allow parent to see child's profile page navigation */
    1190      protected $useridtouseforparentchecks = 0;
    1191      /** @var cache_session A cache that stores information on expanded courses */
    1192      protected $cacheexpandcourse = null;
    1193  
    1194      /** Used when loading categories to load all top level categories [parent = 0] **/
    1195      const LOAD_ROOT_CATEGORIES = 0;
    1196      /** Used when loading categories to load all categories **/
    1197      const LOAD_ALL_CATEGORIES = -1;
    1198  
    1199      /**
    1200       * Constructs a new global navigation
    1201       *
    1202       * @param moodle_page $page The page this navigation object belongs to
    1203       */
    1204      public function __construct(moodle_page $page) {
    1205          global $CFG, $SITE, $USER;
    1206  
    1207          if (during_initial_install()) {
    1208              return;
    1209          }
    1210  
    1211          if (get_home_page() == HOMEPAGE_SITE) {
    1212              // We are using the site home for the root element
    1213              $properties = array(
    1214                  'key' => 'home',
    1215                  'type' => navigation_node::TYPE_SYSTEM,
    1216                  'text' => get_string('home'),
    1217                  'action' => new moodle_url('/'),
    1218                  'icon' => new pix_icon('i/home', '')
    1219              );
    1220          } else {
    1221              // We are using the users my moodle for the root element
    1222              $properties = array(
    1223                  'key' => 'myhome',
    1224                  'type' => navigation_node::TYPE_SYSTEM,
    1225                  'text' => get_string('myhome'),
    1226                  'action' => new moodle_url('/my/'),
    1227                  'icon' => new pix_icon('i/dashboard', '')
    1228              );
    1229          }
    1230  
    1231          // Use the parents constructor.... good good reuse
    1232          parent::__construct($properties);
    1233          $this->showinflatnavigation = true;
    1234  
    1235          // Initalise and set defaults
    1236          $this->page = $page;
    1237          $this->forceopen = true;
    1238          $this->cache = new navigation_cache(NAVIGATION_CACHE_NAME);
    1239      }
    1240  
    1241      /**
    1242       * Mutator to set userid to allow parent to see child's profile
    1243       * page navigation. See MDL-25805 for initial issue. Linked to it
    1244       * is an issue explaining why this is a REALLY UGLY HACK thats not
    1245       * for you to use!
    1246       *
    1247       * @param int $userid userid of profile page that parent wants to navigate around.
    1248       */
    1249      public function set_userid_for_parent_checks($userid) {
    1250          $this->useridtouseforparentchecks = $userid;
    1251      }
    1252  
    1253  
    1254      /**
    1255       * Initialises the navigation object.
    1256       *
    1257       * This causes the navigation object to look at the current state of the page
    1258       * that it is associated with and then load the appropriate content.
    1259       *
    1260       * This should only occur the first time that the navigation structure is utilised
    1261       * which will normally be either when the navbar is called to be displayed or
    1262       * when a block makes use of it.
    1263       *
    1264       * @return bool
    1265       */
    1266      public function initialise() {
    1267          global $CFG, $SITE, $USER;
    1268          // Check if it has already been initialised
    1269          if ($this->initialised || during_initial_install()) {
    1270              return true;
    1271          }
    1272          $this->initialised = true;
    1273  
    1274          // Set up the five base root nodes. These are nodes where we will put our
    1275          // content and are as follows:
    1276          // site: Navigation for the front page.
    1277          // myprofile: User profile information goes here.
    1278          // currentcourse: The course being currently viewed.
    1279          // mycourses: The users courses get added here.
    1280          // courses: Additional courses are added here.
    1281          // users: Other users information loaded here.
    1282          $this->rootnodes = array();
    1283          if (get_home_page() == HOMEPAGE_SITE) {
    1284              // The home element should be my moodle because the root element is the site
    1285              if (isloggedin() && !isguestuser()) {  // Makes no sense if you aren't logged in
    1286                  $this->rootnodes['home'] = $this->add(get_string('myhome'), new moodle_url('/my/'),
    1287                      self::TYPE_SETTING, null, 'myhome', new pix_icon('i/dashboard', ''));
    1288                  $this->rootnodes['home']->showinflatnavigation = true;
    1289              }
    1290          } else {
    1291              // The home element should be the site because the root node is my moodle
    1292              $this->rootnodes['home'] = $this->add(get_string('sitehome'), new moodle_url('/'),
    1293                  self::TYPE_SETTING, null, 'home', new pix_icon('i/home', ''));
    1294              $this->rootnodes['home']->showinflatnavigation = true;
    1295              if (!empty($CFG->defaulthomepage) && ($CFG->defaulthomepage == HOMEPAGE_MY)) {
    1296                  // We need to stop automatic redirection
    1297                  $this->rootnodes['home']->action->param('redirect', '0');
    1298              }
    1299          }
    1300          $this->rootnodes['site'] = $this->add_course($SITE);
    1301          $this->rootnodes['myprofile'] = $this->add(get_string('profile'), null, self::TYPE_USER, null, 'myprofile');
    1302          $this->rootnodes['currentcourse'] = $this->add(get_string('currentcourse'), null, self::TYPE_ROOTNODE, null, 'currentcourse');
    1303          $this->rootnodes['mycourses'] = $this->add(get_string('mycourses'), null, self::TYPE_ROOTNODE, null, 'mycourses', new pix_icon('i/course', ''));
    1304          $this->rootnodes['courses'] = $this->add(get_string('courses'), new moodle_url('/course/index.php'), self::TYPE_ROOTNODE, null, 'courses');
    1305          if (!core_course_category::user_top()) {
    1306              $this->rootnodes['courses']->hide();
    1307          }
    1308          $this->rootnodes['users'] = $this->add(get_string('users'), null, self::TYPE_ROOTNODE, null, 'users');
    1309  
    1310          // We always load the frontpage course to ensure it is available without
    1311          // JavaScript enabled.
    1312          $this->add_front_page_course_essentials($this->rootnodes['site'], $SITE);
    1313          $this->load_course_sections($SITE, $this->rootnodes['site']);
    1314  
    1315          $course = $this->page->course;
    1316          $this->load_courses_enrolled();
    1317  
    1318          // $issite gets set to true if the current pages course is the sites frontpage course
    1319          $issite = ($this->page->course->id == $SITE->id);
    1320  
    1321          // Determine if the user is enrolled in any course.
    1322          $enrolledinanycourse = enrol_user_sees_own_courses();
    1323  
    1324          $this->rootnodes['currentcourse']->mainnavonly = true;
    1325          if ($enrolledinanycourse) {
    1326              $this->rootnodes['mycourses']->isexpandable = true;
    1327              $this->rootnodes['mycourses']->showinflatnavigation = true;
    1328              if ($CFG->navshowallcourses) {
    1329                  // When we show all courses we need to show both the my courses and the regular courses branch.
    1330                  $this->rootnodes['courses']->isexpandable = true;
    1331              }
    1332          } else {
    1333              $this->rootnodes['courses']->isexpandable = true;
    1334          }
    1335          $this->rootnodes['mycourses']->forceopen = true;
    1336  
    1337          $canviewcourseprofile = true;
    1338  
    1339          // Next load context specific content into the navigation
    1340          switch ($this->page->context->contextlevel) {
    1341              case CONTEXT_SYSTEM :
    1342                  // Nothing left to do here I feel.
    1343                  break;
    1344              case CONTEXT_COURSECAT :
    1345                  // This is essential, we must load categories.
    1346                  $this->load_all_categories($this->page->context->instanceid, true);
    1347                  break;
    1348              case CONTEXT_BLOCK :
    1349              case CONTEXT_COURSE :
    1350                  if ($issite) {
    1351                      // Nothing left to do here.
    1352                      break;
    1353                  }
    1354  
    1355                  // Load the course associated with the current page into the navigation.
    1356                  $coursenode = $this->add_course($course, false, self::COURSE_CURRENT);
    1357                  // If the course wasn't added then don't try going any further.
    1358                  if (!$coursenode) {
    1359                      $canviewcourseprofile = false;
    1360                      break;
    1361                  }
    1362  
    1363                  // If the user is not enrolled then we only want to show the
    1364                  // course node and not populate it.
    1365  
    1366                  // Not enrolled, can't view, and hasn't switched roles
    1367                  if (!can_access_course($course, null, '', true)) {
    1368                      if ($coursenode->isexpandable === true) {
    1369                          // Obviously the situation has changed, update the cache and adjust the node.
    1370                          // This occurs if the user access to a course has been revoked (one way or another) after
    1371                          // initially logging in for this session.
    1372                          $this->get_expand_course_cache()->set($course->id, 1);
    1373                          $coursenode->isexpandable = true;
    1374                          $coursenode->nodetype = self::NODETYPE_BRANCH;
    1375                      }
    1376                      // Very ugly hack - do not force "parents" to enrol into course their child is enrolled in,
    1377                      // this hack has been propagated from user/view.php to display the navigation node. (MDL-25805)
    1378                      if (!$this->current_user_is_parent_role()) {
    1379                          $coursenode->make_active();
    1380                          $canviewcourseprofile = false;
    1381                          break;
    1382                      }
    1383                  } else if ($coursenode->isexpandable === false) {
    1384                      // Obviously the situation has changed, update the cache and adjust the node.
    1385                      // This occurs if the user has been granted access to a course (one way or another) after initially
    1386                      // logging in for this session.
    1387                      $this->get_expand_course_cache()->set($course->id, 1);
    1388                      $coursenode->isexpandable = true;
    1389                      $coursenode->nodetype = self::NODETYPE_BRANCH;
    1390                  }
    1391  
    1392                  // Add the essentials such as reports etc...
    1393                  $this->add_course_essentials($coursenode, $course);
    1394                  // Extend course navigation with it's sections/activities
    1395                  $this->load_course_sections($course, $coursenode);
    1396                  if (!$coursenode->contains_active_node() && !$coursenode->search_for_active_node()) {
    1397                      $coursenode->make_active();
    1398                  }
    1399  
    1400                  break;
    1401              case CONTEXT_MODULE :
    1402                  if ($issite) {
    1403                      // If this is the site course then most information will have
    1404                      // already been loaded.
    1405                      // However we need to check if there is more content that can
    1406                      // yet be loaded for the specific module instance.
    1407                      $activitynode = $this->rootnodes['site']->find($this->page->cm->id, navigation_node::TYPE_ACTIVITY);
    1408                      if ($activitynode) {
    1409                          $this->load_activity($this->page->cm, $this->page->course, $activitynode);
    1410                      }
    1411                      break;
    1412                  }
    1413  
    1414                  $course = $this->page->course;
    1415                  $cm = $this->page->cm;
    1416  
    1417                  // Load the course associated with the page into the navigation
    1418                  $coursenode = $this->add_course($course, false, self::COURSE_CURRENT);
    1419  
    1420                  // If the course wasn't added then don't try going any further.
    1421                  if (!$coursenode) {
    1422                      $canviewcourseprofile = false;
    1423                      break;
    1424                  }
    1425  
    1426                  // If the user is not enrolled then we only want to show the
    1427                  // course node and not populate it.
    1428                  if (!can_access_course($course, null, '', true)) {
    1429                      $coursenode->make_active();
    1430                      $canviewcourseprofile = false;
    1431                      break;
    1432                  }
    1433  
    1434                  $this->add_course_essentials($coursenode, $course);
    1435  
    1436                  // Load the course sections into the page
    1437                  $this->load_course_sections($course, $coursenode, null, $cm);
    1438                  $activity = $coursenode->find($cm->id, navigation_node::TYPE_ACTIVITY);
    1439                  if (!empty($activity)) {
    1440                      // Finally load the cm specific navigaton information
    1441                      $this->load_activity($cm, $course, $activity);
    1442                      // Check if we have an active ndoe
    1443                      if (!$activity->contains_active_node() && !$activity->search_for_active_node()) {
    1444                          // And make the activity node active.
    1445                          $activity->make_active();
    1446                      }
    1447                  }
    1448                  break;
    1449              case CONTEXT_USER :
    1450                  if ($issite) {
    1451                      // The users profile information etc is already loaded
    1452                      // for the front page.
    1453                      break;
    1454                  }
    1455                  $course = $this->page->course;
    1456                  // Load the course associated with the user into the navigation
    1457                  $coursenode = $this->add_course($course, false, self::COURSE_CURRENT);
    1458  
    1459                  // If the course wasn't added then don't try going any further.
    1460                  if (!$coursenode) {
    1461                      $canviewcourseprofile = false;
    1462                      break;
    1463                  }
    1464  
    1465                  // If the user is not enrolled then we only want to show the
    1466                  // course node and not populate it.
    1467                  if (!can_access_course($course, null, '', true)) {
    1468                      $coursenode->make_active();
    1469                      $canviewcourseprofile = false;
    1470                      break;
    1471                  }
    1472                  $this->add_course_essentials($coursenode, $course);
    1473                  $this->load_course_sections($course, $coursenode);
    1474                  break;
    1475          }
    1476  
    1477          // Load for the current user
    1478          $this->load_for_user();
    1479          if ($this->page->context->contextlevel >= CONTEXT_COURSE && $this->page->context->instanceid != $SITE->id && $canviewcourseprofile) {
    1480              $this->load_for_user(null, true);
    1481          }
    1482          // Load each extending user into the navigation.
    1483          foreach ($this->extendforuser as $user) {
    1484              if ($user->id != $USER->id) {
    1485                  $this->load_for_user($user);
    1486              }
    1487          }
    1488  
    1489          // Give the local plugins a chance to include some navigation if they want.
    1490          $this->load_local_plugin_navigation();
    1491  
    1492          // Remove any empty root nodes
    1493          foreach ($this->rootnodes as $node) {
    1494              // Dont remove the home node
    1495              /** @var navigation_node $node */
    1496              if (!in_array($node->key, ['home', 'myhome']) && !$node->has_children() && !$node->isactive) {
    1497                  $node->remove();
    1498              }
    1499          }
    1500  
    1501          if (!$this->contains_active_node()) {
    1502              $this->search_for_active_node();
    1503          }
    1504  
    1505          // If the user is not logged in modify the navigation structure as detailed
    1506          // in {@link http://docs.moodle.org/dev/Navigation_2.0_structure}
    1507          if (!isloggedin()) {
    1508              $activities = clone($this->rootnodes['site']->children);
    1509              $this->rootnodes['site']->remove();
    1510              $children = clone($this->children);
    1511              $this->children = new navigation_node_collection();
    1512              foreach ($activities as $child) {
    1513                  $this->children->add($child);
    1514              }
    1515              foreach ($children as $child) {
    1516                  $this->children->add($child);
    1517              }
    1518          }
    1519          return true;
    1520      }
    1521  
    1522      /**
    1523       * This function gives local plugins an opportunity to modify navigation.
    1524       */
    1525      protected function load_local_plugin_navigation() {
    1526          foreach (get_plugin_list_with_function('local', 'extend_navigation') as $function) {
    1527              $function($this);
    1528          }
    1529      }
    1530  
    1531      /**
    1532       * Returns true if the current user is a parent of the user being currently viewed.
    1533       *
    1534       * If the current user is not viewing another user, or if the current user does not hold any parent roles over the
    1535       * other user being viewed this function returns false.
    1536       * In order to set the user for whom we are checking against you must call {@link set_userid_for_parent_checks()}
    1537       *
    1538       * @since Moodle 2.4
    1539       * @return bool
    1540       */
    1541      protected function current_user_is_parent_role() {
    1542          global $USER, $DB;
    1543          if ($this->useridtouseforparentchecks && $this->useridtouseforparentchecks != $USER->id) {
    1544              $usercontext = context_user::instance($this->useridtouseforparentchecks, MUST_EXIST);
    1545              if (!has_capability('moodle/user:viewdetails', $usercontext)) {
    1546                  return false;
    1547              }
    1548              if ($DB->record_exists('role_assignments', array('userid' => $USER->id, 'contextid' => $usercontext->id))) {
    1549                  return true;
    1550              }
    1551          }
    1552          return false;
    1553      }
    1554  
    1555      /**
    1556       * Returns true if courses should be shown within categories on the navigation.
    1557       *
    1558       * @param bool $ismycourse Set to true if you are calculating this for a course.
    1559       * @return bool
    1560       */
    1561      protected function show_categories($ismycourse = false) {
    1562          global $CFG, $DB;
    1563          if ($ismycourse) {
    1564              return $this->show_my_categories();
    1565          }
    1566          if ($this->showcategories === null) {
    1567              $show = false;
    1568              if ($this->page->context->contextlevel == CONTEXT_COURSECAT) {
    1569                  $show = true;
    1570              } else if (!empty($CFG->navshowcategories) && $DB->count_records('course_categories') > 1) {
    1571                  $show = true;
    1572              }
    1573              $this->showcategories = $show;
    1574          }
    1575          return $this->showcategories;
    1576      }
    1577  
    1578      /**
    1579       * Returns true if we should show categories in the My Courses branch.
    1580       * @return bool
    1581       */
    1582      protected function show_my_categories() {
    1583          global $CFG;
    1584          if ($this->showmycategories === null) {
    1585              $this->showmycategories = !empty($CFG->navshowmycoursecategories) && !core_course_category::is_simple_site();
    1586          }
    1587          return $this->showmycategories;
    1588      }
    1589  
    1590      /**
    1591       * Loads the courses in Moodle into the navigation.
    1592       *
    1593       * @global moodle_database $DB
    1594       * @param string|array $categoryids An array containing categories to load courses
    1595       *                     for, OR null to load courses for all categories.
    1596       * @return array An array of navigation_nodes one for each course
    1597       */
    1598      protected function load_all_courses($categoryids = null) {
    1599          global $CFG, $DB, $SITE;
    1600  
    1601          // Work out the limit of courses.
    1602          $limit = 20;
    1603          if (!empty($CFG->navcourselimit)) {
    1604              $limit = $CFG->navcourselimit;
    1605          }
    1606  
    1607          $toload = (empty($CFG->navshowallcourses))?self::LOAD_ROOT_CATEGORIES:self::LOAD_ALL_CATEGORIES;
    1608  
    1609          // If we are going to show all courses AND we are showing categories then
    1610          // to save us repeated DB calls load all of the categories now
    1611          if ($this->show_categories()) {
    1612              $this->load_all_categories($toload);
    1613          }
    1614  
    1615          // Will be the return of our efforts
    1616          $coursenodes = array();
    1617  
    1618          // Check if we need to show categories.
    1619          if ($this->show_categories()) {
    1620              // Hmmm we need to show categories... this is going to be painful.
    1621              // We now need to fetch up to $limit courses for each category to
    1622              // be displayed.
    1623              if ($categoryids !== null) {
    1624                  if (!is_array($categoryids)) {
    1625                      $categoryids = array($categoryids);
    1626                  }
    1627                  list($categorywhere, $categoryparams) = $DB->get_in_or_equal($categoryids, SQL_PARAMS_NAMED, 'cc');
    1628                  $categorywhere = 'WHERE cc.id '.$categorywhere;
    1629              } else if ($toload == self::LOAD_ROOT_CATEGORIES) {
    1630                  $categorywhere = 'WHERE cc.depth = 1 OR cc.depth = 2';
    1631                  $categoryparams = array();
    1632              } else {
    1633                  $categorywhere = '';
    1634                  $categoryparams = array();
    1635              }
    1636  
    1637              // First up we are going to get the categories that we are going to
    1638              // need so that we can determine how best to load the courses from them.
    1639              $sql = "SELECT cc.id, COUNT(c.id) AS coursecount
    1640                          FROM {course_categories} cc
    1641                      LEFT JOIN {course} c ON c.category = cc.id
    1642                              {$categorywhere}
    1643                      GROUP BY cc.id";
    1644              $categories = $DB->get_recordset_sql($sql, $categoryparams);
    1645              $fullfetch = array();
    1646              $partfetch = array();
    1647              foreach ($categories as $category) {
    1648                  if (!$this->can_add_more_courses_to_category($category->id)) {
    1649                      continue;
    1650                  }
    1651                  if ($category->coursecount > $limit * 5) {
    1652                      $partfetch[] = $category->id;
    1653                  } else if ($category->coursecount > 0) {
    1654                      $fullfetch[] = $category->id;
    1655                  }
    1656              }
    1657              $categories->close();
    1658  
    1659              if (count($fullfetch)) {
    1660                  // First up fetch all of the courses in categories where we know that we are going to
    1661                  // need the majority of courses.
    1662                  list($categoryids, $categoryparams) = $DB->get_in_or_equal($fullfetch, SQL_PARAMS_NAMED, 'lcategory');
    1663                  $ccselect = ', ' . context_helper::get_preload_record_columns_sql('ctx');
    1664                  $ccjoin = "LEFT JOIN {context} ctx ON (ctx.instanceid = c.id AND ctx.contextlevel = :contextlevel)";
    1665                  $categoryparams['contextlevel'] = CONTEXT_COURSE;
    1666                  $sql = "SELECT c.id, c.sortorder, c.visible, c.fullname, c.shortname, c.category $ccselect
    1667                              FROM {course} c
    1668                                  $ccjoin
    1669                              WHERE c.category {$categoryids}
    1670                          ORDER BY c.sortorder ASC";
    1671                  $coursesrs = $DB->get_recordset_sql($sql, $categoryparams);
    1672                  foreach ($coursesrs as $course) {
    1673                      if ($course->id == $SITE->id) {
    1674                          // This should not be necessary, frontpage is not in any category.
    1675                          continue;
    1676                      }
    1677                      if (array_key_exists($course->id, $this->addedcourses)) {
    1678                          // It is probably better to not include the already loaded courses
    1679                          // directly in SQL because inequalities may confuse query optimisers
    1680                          // and may interfere with query caching.
    1681                          continue;
    1682                      }
    1683                      if (!$this->can_add_more_courses_to_category($course->category)) {
    1684                          continue;
    1685                      }
    1686                      context_helper::preload_from_record($course);
    1687                      if (!$course->visible && !is_role_switched($course->id) && !has_capability('moodle/course:viewhiddencourses', context_course::instance($course->id))) {
    1688                          continue;
    1689                      }
    1690                      $coursenodes[$course->id] = $this->add_course($course);
    1691                  }
    1692                  $coursesrs->close();
    1693              }
    1694  
    1695              if (count($partfetch)) {
    1696                  // Next we will work our way through the categories where we will likely only need a small
    1697                  // proportion of the courses.
    1698                  foreach ($partfetch as $categoryid) {
    1699                      $ccselect = ', ' . context_helper::get_preload_record_columns_sql('ctx');
    1700                      $ccjoin = "LEFT JOIN {context} ctx ON (ctx.instanceid = c.id AND ctx.contextlevel = :contextlevel)";
    1701                      $sql = "SELECT c.id, c.sortorder, c.visible, c.fullname, c.shortname, c.category $ccselect
    1702                                  FROM {course} c
    1703                                      $ccjoin
    1704                                  WHERE c.category = :categoryid
    1705                              ORDER BY c.sortorder ASC";
    1706                      $courseparams = array('categoryid' => $categoryid, 'contextlevel' => CONTEXT_COURSE);
    1707                      $coursesrs = $DB->get_recordset_sql($sql, $courseparams, 0, $limit * 5);
    1708                      foreach ($coursesrs as $course) {
    1709                          if ($course->id == $SITE->id) {
    1710                              // This should not be necessary, frontpage is not in any category.
    1711                              continue;
    1712                          }
    1713                          if (array_key_exists($course->id, $this->addedcourses)) {
    1714                              // It is probably better to not include the already loaded courses
    1715                              // directly in SQL because inequalities may confuse query optimisers
    1716                              // and may interfere with query caching.
    1717                              // This also helps to respect expected $limit on repeated executions.
    1718                              continue;
    1719                          }
    1720                          if (!$this->can_add_more_courses_to_category($course->category)) {
    1721                              break;
    1722                          }
    1723                          context_helper::preload_from_record($course);
    1724                          if (!$course->visible && !is_role_switched($course->id) && !has_capability('moodle/course:viewhiddencourses', context_course::instance($course->id))) {
    1725                              continue;
    1726                          }
    1727                          $coursenodes[$course->id] = $this->add_course($course);
    1728                      }
    1729                      $coursesrs->close();
    1730                  }
    1731              }
    1732          } else {
    1733              // Prepare the SQL to load the courses and their contexts
    1734              list($courseids, $courseparams) = $DB->get_in_or_equal(array_keys($this->addedcourses), SQL_PARAMS_NAMED, 'lc', false);
    1735              $ccselect = ', ' . context_helper::get_preload_record_columns_sql('ctx');
    1736              $ccjoin = "LEFT JOIN {context} ctx ON (ctx.instanceid = c.id AND ctx.contextlevel = :contextlevel)";
    1737              $courseparams['contextlevel'] = CONTEXT_COURSE;
    1738              $sql = "SELECT c.id, c.sortorder, c.visible, c.fullname, c.shortname, c.category $ccselect
    1739                          FROM {course} c
    1740                              $ccjoin
    1741                          WHERE c.id {$courseids}
    1742                      ORDER BY c.sortorder ASC";
    1743              $coursesrs = $DB->get_recordset_sql($sql, $courseparams);
    1744              foreach ($coursesrs as $course) {
    1745                  if ($course->id == $SITE->id) {
    1746                      // frotpage is not wanted here
    1747                      continue;
    1748                  }
    1749                  if ($this->page->course && ($this->page->course->id == $course->id)) {
    1750                      // Don't include the currentcourse in this nodelist - it's displayed in the Current course node
    1751                      continue;
    1752                  }
    1753                  context_helper::preload_from_record($course);
    1754                  if (!$course->visible && !is_role_switched($course->id) && !has_capability('moodle/course:viewhiddencourses', context_course::instance($course->id))) {
    1755                      continue;
    1756                  }
    1757                  $coursenodes[$course->id] = $this->add_course($course);
    1758                  if (count($coursenodes) >= $limit) {
    1759                      break;
    1760                  }
    1761              }
    1762              $coursesrs->close();
    1763          }
    1764  
    1765          return $coursenodes;
    1766      }
    1767  
    1768      /**
    1769       * Returns true if more courses can be added to the provided category.
    1770       *
    1771       * @param int|navigation_node|stdClass $category
    1772       * @return bool
    1773       */
    1774      protected function can_add_more_courses_to_category($category) {
    1775          global $CFG;
    1776          $limit = 20;
    1777          if (!empty($CFG->navcourselimit)) {
    1778              $limit = (int)$CFG->navcourselimit;
    1779          }
    1780          if (is_numeric($category)) {
    1781              if (!array_key_exists($category, $this->addedcategories)) {
    1782                  return true;
    1783              }
    1784              $coursecount = count($this->addedcategories[$category]->children->type(self::TYPE_COURSE));
    1785          } else if ($category instanceof navigation_node) {
    1786              if (($category->type != self::TYPE_CATEGORY) || ($category->type != self::TYPE_MY_CATEGORY)) {
    1787                  return false;
    1788              }
    1789              $coursecount = count($category->children->type(self::TYPE_COURSE));
    1790          } else if (is_object($category) && property_exists($category,'id')) {
    1791              $coursecount = count($this->addedcategories[$category->id]->children->type(self::TYPE_COURSE));
    1792          }
    1793          return ($coursecount <= $limit);
    1794      }
    1795  
    1796      /**
    1797       * Loads all categories (top level or if an id is specified for that category)
    1798       *
    1799       * @param int $categoryid The category id to load or null/0 to load all base level categories
    1800       * @param bool $showbasecategories If set to true all base level categories will be loaded as well
    1801       *        as the requested category and any parent categories.
    1802       * @return navigation_node|void returns a navigation node if a category has been loaded.
    1803       */
    1804      protected function load_all_categories($categoryid = self::LOAD_ROOT_CATEGORIES, $showbasecategories = false) {
    1805          global $CFG, $DB;
    1806  
    1807          // Check if this category has already been loaded
    1808          if ($this->allcategoriesloaded || ($categoryid < 1 && $this->is_category_fully_loaded($categoryid))) {
    1809              return true;
    1810          }
    1811  
    1812          $catcontextsql = context_helper::get_preload_record_columns_sql('ctx');
    1813          $sqlselect = "SELECT cc.*, $catcontextsql
    1814                        FROM {course_categories} cc
    1815                        JOIN {context} ctx ON cc.id = ctx.instanceid";
    1816          $sqlwhere = "WHERE ctx.contextlevel = ".CONTEXT_COURSECAT;
    1817          $sqlorder = "ORDER BY cc.depth ASC, cc.sortorder ASC, cc.id ASC";
    1818          $params = array();
    1819  
    1820          $categoriestoload = array();
    1821          if ($categoryid == self::LOAD_ALL_CATEGORIES) {
    1822              // We are going to load all categories regardless... prepare to fire
    1823              // on the database server!
    1824          } else if ($categoryid == self::LOAD_ROOT_CATEGORIES) { // can be 0
    1825              // We are going to load all of the first level categories (categories without parents)
    1826              $sqlwhere .= " AND cc.parent = 0";
    1827          } else if (array_key_exists($categoryid, $this->addedcategories)) {
    1828              // The category itself has been loaded already so we just need to ensure its subcategories
    1829              // have been loaded
    1830              $addedcategories = $this->addedcategories;
    1831              unset($addedcategories[$categoryid]);
    1832              if (count($addedcategories) > 0) {
    1833                  list($sql, $params) = $DB->get_in_or_equal(array_keys($addedcategories), SQL_PARAMS_NAMED, 'parent', false);
    1834                  if ($showbasecategories) {
    1835                      // We need to include categories with parent = 0 as well
    1836                      $sqlwhere .= " AND (cc.parent = :categoryid OR cc.parent = 0) AND cc.parent {$sql}";
    1837                  } else {
    1838                      // All we need is categories that match the parent
    1839                      $sqlwhere .= " AND cc.parent = :categoryid AND cc.parent {$sql}";
    1840                  }
    1841              }
    1842              $params['categoryid'] = $categoryid;
    1843          } else {
    1844              // This category hasn't been loaded yet so we need to fetch it, work out its category path
    1845              // and load this category plus all its parents and subcategories
    1846              $category = $DB->get_record('course_categories', array('id' => $categoryid), 'path', MUST_EXIST);
    1847              $categoriestoload = explode('/', trim($category->path, '/'));
    1848              list($select, $params) = $DB->get_in_or_equal($categoriestoload);
    1849              // We are going to use select twice so double the params
    1850              $params = array_merge($params, $params);
    1851              $basecategorysql = ($showbasecategories)?' OR cc.depth = 1':'';
    1852              $sqlwhere .= " AND (cc.id {$select} OR cc.parent {$select}{$basecategorysql})";
    1853          }
    1854  
    1855          $categoriesrs = $DB->get_recordset_sql("$sqlselect $sqlwhere $sqlorder", $params);
    1856          $categories = array();
    1857          foreach ($categoriesrs as $category) {
    1858              // Preload the context.. we'll need it when adding the category in order
    1859              // to format the category name.
    1860              context_helper::preload_from_record($category);
    1861              if (array_key_exists($category->id, $this->addedcategories)) {
    1862                  // Do nothing, its already been added.
    1863              } else if ($category->parent == '0') {
    1864                  // This is a root category lets add it immediately
    1865                  $this->add_category($category, $this->rootnodes['courses']);
    1866              } else if (array_key_exists($category->parent, $this->addedcategories)) {
    1867                  // This categories parent has already been added we can add this immediately
    1868                  $this->add_category($category, $this->addedcategories[$category->parent]);
    1869              } else {
    1870                  $categories[] = $category;
    1871              }
    1872          }
    1873          $categoriesrs->close();
    1874  
    1875          // Now we have an array of categories we need to add them to the navigation.
    1876          while (!empty($categories)) {
    1877              $category = reset($categories);
    1878              if (array_key_exists($category->id, $this->addedcategories)) {
    1879                  // Do nothing
    1880              } else if ($category->parent == '0') {
    1881                  $this->add_category($category, $this->rootnodes['courses']);
    1882              } else if (array_key_exists($category->parent, $this->addedcategories)) {
    1883                  $this->add_category($category, $this->addedcategories[$category->parent]);
    1884              } else {
    1885                  // This category isn't in the navigation and niether is it's parent (yet).
    1886                  // We need to go through the category path and add all of its components in order.
    1887                  $path = explode('/', trim($category->path, '/'));
    1888                  foreach ($path as $catid) {
    1889                      if (!array_key_exists($catid, $this->addedcategories)) {
    1890                          // This category isn't in the navigation yet so add it.
    1891                          $subcategory = $categories[$catid];
    1892                          if ($subcategory->parent == '0') {
    1893                              // Yay we have a root category - this likely means we will now be able
    1894                              // to add categories without problems.
    1895                              $this->add_category($subcategory, $this->rootnodes['courses']);
    1896                          } else if (array_key_exists($subcategory->parent, $this->addedcategories)) {
    1897                              // The parent is in the category (as we'd expect) so add it now.
    1898                              $this->add_category($subcategory, $this->addedcategories[$subcategory->parent]);
    1899                              // Remove the category from the categories array.
    1900                              unset($categories[$catid]);
    1901                          } else {
    1902                              // We should never ever arrive here - if we have then there is a bigger
    1903                              // problem at hand.
    1904                              throw new coding_exception('Category path order is incorrect and/or there are missing categories');
    1905                          }
    1906                      }
    1907                  }
    1908              }
    1909              // Remove the category from the categories array now that we know it has been added.
    1910              unset($categories[$category->id]);
    1911          }
    1912          if ($categoryid === self::LOAD_ALL_CATEGORIES) {
    1913              $this->allcategoriesloaded = true;
    1914          }
    1915          // Check if there are any categories to load.
    1916          if (count($categoriestoload) > 0) {
    1917              $readytoloadcourses = array();
    1918              foreach ($categoriestoload as $category) {
    1919                  if ($this->can_add_more_courses_to_category($category)) {
    1920                      $readytoloadcourses[] = $category;
    1921                  }
    1922              }
    1923              if (count($readytoloadcourses)) {
    1924                  $this->load_all_courses($readytoloadcourses);
    1925              }
    1926          }
    1927  
    1928          // Look for all categories which have been loaded
    1929          if (!empty($this->addedcategories)) {
    1930              $categoryids = array();
    1931              foreach ($this->addedcategories as $category) {
    1932                  if ($this->can_add_more_courses_to_category($category)) {
    1933                      $categoryids[] = $category->key;
    1934                  }
    1935              }
    1936              if ($categoryids) {
    1937                  list($categoriessql, $params) = $DB->get_in_or_equal($categoryids, SQL_PARAMS_NAMED);
    1938                  $params['limit'] = (!empty($CFG->navcourselimit))?$CFG->navcourselimit:20;
    1939                  $sql = "SELECT cc.id, COUNT(c.id) AS coursecount
    1940                            FROM {course_categories} cc
    1941                            JOIN {course} c ON c.category = cc.id
    1942                           WHERE cc.id {$categoriessql}
    1943                        GROUP BY cc.id
    1944                          HAVING COUNT(c.id) > :limit";
    1945                  $excessivecategories = $DB->get_records_sql($sql, $params);
    1946                  foreach ($categories as &$category) {
    1947                      if (array_key_exists($category->key, $excessivecategories) && !$this->can_add_more_courses_to_category($category)) {
    1948                          $url = new moodle_url('/course/index.php', array('categoryid' => $category->key));
    1949                          $category->add(get_string('viewallcourses'), $url, self::TYPE_SETTING);
    1950                      }
    1951                  }
    1952              }
    1953          }
    1954      }
    1955  
    1956      /**
    1957       * Adds a structured category to the navigation in the correct order/place
    1958       *
    1959       * @param stdClass $category category to be added in navigation.
    1960       * @param navigation_node $parent parent navigation node
    1961       * @param int $nodetype type of node, if category is under MyHome then it's TYPE_MY_CATEGORY
    1962       * @return void.
    1963       */
    1964      protected function add_category(stdClass $category, navigation_node $parent, $nodetype = self::TYPE_CATEGORY) {
    1965          if (array_key_exists($category->id, $this->addedcategories)) {
    1966              return;
    1967          }
    1968          $canview = core_course_category::can_view_category($category);
    1969          $url = $canview ? new moodle_url('/course/index.php', array('categoryid' => $category->id)) : null;
    1970          $context = context_coursecat::instance($category->id);
    1971          $categoryname = $canview ? format_string($category->name, true, array('context' => $context)) :
    1972              get_string('categoryhidden');
    1973          $categorynode = $parent->add($categoryname, $url, $nodetype, $categoryname, $category->id);
    1974          if (!$canview) {
    1975              // User does not have required capabilities to view category.
    1976              $categorynode->display = false;
    1977          } else if (!$category->visible) {
    1978              // Category is hidden but user has capability to view hidden categories.
    1979              $categorynode->hidden = true;
    1980          }
    1981          $this->addedcategories[$category->id] = $categorynode;
    1982      }
    1983  
    1984      /**
    1985       * Loads the given course into the navigation
    1986       *
    1987       * @param stdClass $course
    1988       * @return navigation_node
    1989       */
    1990      protected function load_course(stdClass $course) {
    1991          global $SITE;
    1992          if ($course->id == $SITE->id) {
    1993              // This is always loaded during initialisation
    1994              return $this->rootnodes['site'];
    1995          } else if (array_key_exists($course->id, $this->addedcourses)) {
    1996              // The course has already been loaded so return a reference
    1997              return $this->addedcourses[$course->id];
    1998          } else {
    1999              // Add the course
    2000              return $this->add_course($course);
    2001          }
    2002      }
    2003  
    2004      /**
    2005       * Loads all of the courses section into the navigation.
    2006       *
    2007       * This function calls method from current course format, see
    2008       * {@link format_base::extend_course_navigation()}
    2009       * If course module ($cm) is specified but course format failed to create the node,
    2010       * the activity node is created anyway.
    2011       *
    2012       * By default course formats call the method {@link global_navigation::load_generic_course_sections()}
    2013       *
    2014       * @param stdClass $course Database record for the course
    2015       * @param navigation_node $coursenode The course node within the navigation
    2016       * @param null|int $sectionnum If specified load the contents of section with this relative number
    2017       * @param null|cm_info $cm If specified make sure that activity node is created (either
    2018       *    in containg section or by calling load_stealth_activity() )
    2019       */
    2020      protected function load_course_sections(stdClass $course, navigation_node $coursenode, $sectionnum = null, $cm = null) {
    2021          global $CFG, $SITE;
    2022          require_once($CFG->dirroot.'/course/lib.php');
    2023          if (isset($cm->sectionnum)) {
    2024              $sectionnum = $cm->sectionnum;
    2025          }
    2026          if ($sectionnum !== null) {
    2027              $this->includesectionnum = $sectionnum;
    2028          }
    2029          course_get_format($course)->extend_course_navigation($this, $coursenode, $sectionnum, $cm);
    2030          if (isset($cm->id)) {
    2031              $activity = $coursenode->find($cm->id, self::TYPE_ACTIVITY);
    2032              if (empty($activity)) {
    2033                  $activity = $this->load_stealth_activity($coursenode, get_fast_modinfo($course));
    2034              }
    2035          }
    2036     }
    2037  
    2038      /**
    2039       * Generates an array of sections and an array of activities for the given course.
    2040       *
    2041       * This method uses the cache to improve performance and avoid the get_fast_modinfo call
    2042       *
    2043       * @param stdClass $course
    2044       * @return array Array($sections, $activities)
    2045       */
    2046      protected function generate_sections_and_activities(stdClass $course) {
    2047          global $CFG;
    2048          require_once($CFG->dirroot.'/course/lib.php');
    2049  
    2050          $modinfo = get_fast_modinfo($course);
    2051          $sections = $modinfo->get_section_info_all();
    2052  
    2053          // For course formats using 'numsections' trim the sections list
    2054          $courseformatoptions = course_get_format($course)->get_format_options();
    2055          if (isset($courseformatoptions['numsections'])) {
    2056              $sections = array_slice($sections, 0, $courseformatoptions['numsections']+1, true);
    2057          }
    2058  
    2059          $activities = array();
    2060  
    2061          foreach ($sections as $key => $section) {
    2062              // Clone and unset summary to prevent $SESSION bloat (MDL-31802).
    2063              $sections[$key] = clone($section);
    2064              unset($sections[$key]->summary);
    2065              $sections[$key]->hasactivites = false;
    2066              if (!array_key_exists($section->section, $modinfo->sections)) {
    2067                  continue;
    2068              }
    2069              foreach ($modinfo->sections[$section->section] as $cmid) {
    2070                  $cm = $modinfo->cms[$cmid];
    2071                  $activity = new stdClass;
    2072                  $activity->id = $cm->id;
    2073                  $activity->course = $course->id;
    2074                  $activity->section = $section->section;
    2075                  $activity->name = $cm->name;
    2076                  $activity->icon = $cm->icon;
    2077                  $activity->iconcomponent = $cm->iconcomponent;
    2078                  $activity->hidden = (!$cm->visible);
    2079                  $activity->modname = $cm->modname;
    2080                  $activity->nodetype = navigation_node::NODETYPE_LEAF;
    2081                  $activity->onclick = $cm->onclick;
    2082                  $url = $cm->url;
    2083                  if (!$url) {
    2084                      $activity->url = null;
    2085                      $activity->display = false;
    2086                  } else {
    2087                      $activity->url = $url->out();
    2088                      $activity->display = $cm->is_visible_on_course_page() ? true : false;
    2089                      if (self::module_extends_navigation($cm->modname)) {
    2090                          $activity->nodetype = navigation_node::NODETYPE_BRANCH;
    2091                      }
    2092                  }
    2093                  $activities[$cmid] = $activity;
    2094                  if ($activity->display) {
    2095                      $sections[$key]->hasactivites = true;
    2096                  }
    2097              }
    2098          }
    2099  
    2100          return array($sections, $activities);
    2101      }
    2102  
    2103      /**
    2104       * Generically loads the course sections into the course's navigation.
    2105       *
    2106       * @param stdClass $course
    2107       * @param navigation_node $coursenode
    2108       * @return array An array of course section nodes
    2109       */
    2110      public function load_generic_course_sections(stdClass $course, navigation_node $coursenode) {
    2111          global $CFG, $DB, $USER, $SITE;
    2112          require_once($CFG->dirroot.'/course/lib.php');
    2113  
    2114          list($sections, $activities) = $this->generate_sections_and_activities($course);
    2115  
    2116          $navigationsections = array();
    2117          foreach ($sections as $sectionid => $section) {
    2118              $section = clone($section);
    2119              if ($course->id == $SITE->id) {
    2120                  $this->load_section_activities($coursenode, $section->section, $activities);
    2121              } else {
    2122                  if (!$section->uservisible || (!$this->showemptysections &&
    2123                          !$section->hasactivites && $this->includesectionnum !== $section->section)) {
    2124                      continue;
    2125                  }
    2126  
    2127                  $sectionname = get_section_name($course, $section);
    2128                  $url = course_get_url($course, $section->section, array('navigation' => true));
    2129  
    2130                  $sectionnode = $coursenode->add($sectionname, $url, navigation_node::TYPE_SECTION,
    2131                      null, $section->id, new pix_icon('i/section', ''));
    2132                  $sectionnode->nodetype = navigation_node::NODETYPE_BRANCH;
    2133                  $sectionnode->hidden = (!$section->visible || !$section->available);
    2134                  if ($this->includesectionnum !== false && $this->includesectionnum == $section->section) {
    2135                      $this->load_section_activities($sectionnode, $section->section, $activities);
    2136                  }
    2137                  $section->sectionnode = $sectionnode;
    2138                  $navigationsections[$sectionid] = $section;
    2139              }
    2140          }
    2141          return $navigationsections;
    2142      }
    2143  
    2144      /**
    2145       * Loads all of the activities for a section into the navigation structure.
    2146       *
    2147       * @param navigation_node $sectionnode
    2148       * @param int $sectionnumber
    2149       * @param array $activities An array of activites as returned by {@link global_navigation::generate_sections_and_activities()}
    2150       * @param stdClass $course The course object the section and activities relate to.
    2151       * @return array Array of activity nodes
    2152       */
    2153      protected function load_section_activities(navigation_node $sectionnode, $sectionnumber, array $activities, $course = null) {
    2154          global $CFG, $SITE;
    2155          // A static counter for JS function naming
    2156          static $legacyonclickcounter = 0;
    2157  
    2158          $activitynodes = array();
    2159          if (empty($activities)) {
    2160              return $activitynodes;
    2161          }
    2162  
    2163          if (!is_object($course)) {
    2164              $activity = reset($activities);
    2165              $courseid = $activity->course;
    2166          } else {
    2167              $courseid = $course->id;
    2168          }
    2169          $showactivities = ($courseid != $SITE->id || !empty($CFG->navshowfrontpagemods));
    2170  
    2171          foreach ($activities as $activity) {
    2172              if ($activity->section != $sectionnumber) {
    2173                  continue;
    2174              }
    2175              if ($activity->icon) {
    2176                  $icon = new pix_icon($activity->icon, get_string('modulename', $activity->modname), $activity->iconcomponent);
    2177              } else {
    2178                  $icon = new pix_icon('icon', get_string('modulename', $activity->modname), $activity->modname);
    2179              }
    2180  
    2181              // Prepare the default name and url for the node
    2182              $activityname = format_string($activity->name, true, array('context' => context_module::instance($activity->id)));
    2183              $action = new moodle_url($activity->url);
    2184  
    2185              // Check if the onclick property is set (puke!)
    2186              if (!empty($activity->onclick)) {
    2187                  // Increment the counter so that we have a unique number.
    2188                  $legacyonclickcounter++;
    2189                  // Generate the function name we will use
    2190                  $functionname = 'legacy_activity_onclick_handler_'.$legacyonclickcounter;
    2191                  $propogrationhandler = '';
    2192                  // Check if we need to cancel propogation. Remember inline onclick
    2193                  // events would return false if they wanted to prevent propogation and the
    2194                  // default action.
    2195                  if (strpos($activity->onclick, 'return false')) {
    2196                      $propogrationhandler = 'e.halt();';
    2197                  }
    2198                  // Decode the onclick - it has already been encoded for display (puke)
    2199                  $onclick = htmlspecialchars_decode($activity->onclick, ENT_QUOTES);
    2200                  // Build the JS function the click event will call
    2201                  $jscode = "function {$functionname}(e) { $propogrationhandler $onclick }";
    2202                  $this->page->requires->js_amd_inline($jscode);
    2203                  // Override the default url with the new action link
    2204                  $action = new action_link($action, $activityname, new component_action('click', $functionname));
    2205              }
    2206  
    2207              $activitynode = $sectionnode->add($activityname, $action, navigation_node::TYPE_ACTIVITY, null, $activity->id, $icon);
    2208              $activitynode->title(get_string('modulename', $activity->modname));
    2209              $activitynode->hidden = $activity->hidden;
    2210              $activitynode->display = $showactivities && $activity->display;
    2211              $activitynode->nodetype = $activity->nodetype;
    2212              $activitynodes[$activity->id] = $activitynode;
    2213          }
    2214  
    2215          return $activitynodes;
    2216      }
    2217      /**
    2218       * Loads a stealth module from unavailable section
    2219       * @param navigation_node $coursenode
    2220       * @param stdClass $modinfo
    2221       * @return navigation_node or null if not accessible
    2222       */
    2223      protected function load_stealth_activity(navigation_node $coursenode, $modinfo) {
    2224          if (empty($modinfo->cms[$this->page->cm->id])) {
    2225              return null;
    2226          }
    2227          $cm = $modinfo->cms[$this->page->cm->id];
    2228          if ($cm->icon) {
    2229              $icon = new pix_icon($cm->icon, get_string('modulename', $cm->modname), $cm->iconcomponent);
    2230          } else {
    2231              $icon = new pix_icon('icon', get_string('modulename', $cm->modname), $cm->modname);
    2232          }
    2233          $url = $cm->url;
    2234          $activitynode = $coursenode->add(format_string($cm->name), $url, navigation_node::TYPE_ACTIVITY, null, $cm->id, $icon);
    2235          $activitynode->title(get_string('modulename', $cm->modname));
    2236          $activitynode->hidden = (!$cm->visible);
    2237          if (!$cm->is_visible_on_course_page()) {
    2238              // Do not show any error here, let the page handle exception that activity is not visible for the current user.
    2239              // Also there may be no exception at all in case when teacher is logged in as student.
    2240              $activitynode->display = false;
    2241          } else if (!$url) {
    2242              // Don't show activities that don't have links!
    2243              $activitynode->display = false;
    2244          } else if (self::module_extends_navigation($cm->modname)) {
    2245              $activitynode->nodetype = navigation_node::NODETYPE_BRANCH;
    2246          }
    2247          return $activitynode;
    2248      }
    2249      /**
    2250       * Loads the navigation structure for the given activity into the activities node.
    2251       *
    2252       * This method utilises a callback within the modules lib.php file to load the
    2253       * content specific to activity given.
    2254       *
    2255       * The callback is a method: {modulename}_extend_navigation()
    2256       * Examples:
    2257       *  * {@link forum_extend_navigation()}
    2258       *  * {@link workshop_extend_navigation()}
    2259       *
    2260       * @param cm_info|stdClass $cm
    2261       * @param stdClass $course
    2262       * @param navigation_node $activity
    2263       * @return bool
    2264       */
    2265      protected function load_activity($cm, stdClass $course, navigation_node $activity) {
    2266          global $CFG, $DB;
    2267  
    2268          // make sure we have a $cm from get_fast_modinfo as this contains activity access details
    2269          if (!($cm instanceof cm_info)) {
    2270              $modinfo = get_fast_modinfo($course);
    2271              $cm = $modinfo->get_cm($cm->id);
    2272          }
    2273          $activity->nodetype = navigation_node::NODETYPE_LEAF;
    2274          $activity->make_active();
    2275          $file = $CFG->dirroot.'/mod/'.$cm->modname.'/lib.php';
    2276          $function = $cm->modname.'_extend_navigation';
    2277  
    2278          if (file_exists($file)) {
    2279              require_once($file);
    2280              if (function_exists($function)) {
    2281                  $activtyrecord = $DB->get_record($cm->modname, array('id' => $cm->instance), '*', MUST_EXIST);
    2282                  $function($activity, $course, $activtyrecord, $cm);
    2283              }
    2284          }
    2285  
    2286          // Allow the active advanced grading method plugin to append module navigation
    2287          $featuresfunc = $cm->modname.'_supports';
    2288          if (function_exists($featuresfunc) && $featuresfunc(FEATURE_ADVANCED_GRADING)) {
    2289              require_once($CFG->dirroot.'/grade/grading/lib.php');
    2290              $gradingman = get_grading_manager($cm->context,  'mod_'.$cm->modname);
    2291              $gradingman->extend_navigation($this, $activity);
    2292          }
    2293  
    2294          return $activity->has_children();
    2295      }
    2296      /**
    2297       * Loads user specific information into the navigation in the appropriate place.
    2298       *
    2299       * If no user is provided the current user is assumed.
    2300       *
    2301       * @param stdClass $user
    2302       * @param bool $forceforcontext probably force something to be loaded somewhere (ask SamH if not sure what this means)
    2303       * @return bool
    2304       */
    2305      protected function load_for_user($user=null, $forceforcontext=false) {
    2306          global $DB, $CFG, $USER, $SITE;
    2307  
    2308          require_once($CFG->dirroot . '/course/lib.php');
    2309  
    2310          if ($user === null) {
    2311              // We can't require login here but if the user isn't logged in we don't
    2312              // want to show anything
    2313              if (!isloggedin() || isguestuser()) {
    2314                  return false;
    2315              }
    2316              $user = $USER;
    2317          } else if (!is_object($user)) {
    2318              // If the user is not an object then get them from the database
    2319              $select = context_helper::get_preload_record_columns_sql('ctx');
    2320              $sql = "SELECT u.*, $select
    2321                        FROM {user} u
    2322                        JOIN {context} ctx ON u.id = ctx.instanceid
    2323                       WHERE u.id = :userid AND
    2324                             ctx.contextlevel = :contextlevel";
    2325              $user = $DB->get_record_sql($sql, array('userid' => (int)$user, 'contextlevel' => CONTEXT_USER), MUST_EXIST);
    2326              context_helper::preload_from_record($user);
    2327          }
    2328  
    2329          $iscurrentuser = ($user->id == $USER->id);
    2330  
    2331          $usercontext = context_user::instance($user->id);
    2332  
    2333          // Get the course set against the page, by default this will be the site
    2334          $course = $this->page->course;
    2335          $baseargs = array('id'=>$user->id);
    2336          if ($course->id != $SITE->id && (!$iscurrentuser || $forceforcontext)) {
    2337              $coursenode = $this->add_course($course, false, self::COURSE_CURRENT);
    2338              $baseargs['course'] = $course->id;
    2339              $coursecontext = context_course::instance($course->id);
    2340              $issitecourse = false;
    2341          } else {
    2342              // Load all categories and get the context for the system
    2343              $coursecontext = context_system::instance();
    2344              $issitecourse = true;
    2345          }
    2346  
    2347          // Create a node to add user information under.
    2348          $usersnode = null;
    2349          if (!$issitecourse) {
    2350              // Not the current user so add it to the participants node for the current course.
    2351              $usersnode = $coursenode->get('participants', navigation_node::TYPE_CONTAINER);
    2352              $userviewurl = new moodle_url('/user/view.php', $baseargs);
    2353          } else if ($USER->id != $user->id) {
    2354              // This is the site so add a users node to the root branch.
    2355              $usersnode = $this->rootnodes['users'];
    2356              if (course_can_view_participants($coursecontext)) {
    2357                  $usersnode->action = new moodle_url('/user/index.php', array('id' => $course->id));
    2358              }
    2359              $userviewurl = new moodle_url('/user/profile.php', $baseargs);
    2360          }
    2361          if (!$usersnode) {
    2362              // We should NEVER get here, if the course hasn't been populated
    2363              // with a participants node then the navigaiton either wasn't generated
    2364              // for it (you are missing a require_login or set_context call) or
    2365              // you don't have access.... in the interests of no leaking informatin
    2366              // we simply quit...
    2367              return false;
    2368          }
    2369          // Add a branch for the current user.
    2370          // Only reveal user details if $user is the current user, or a user to which the current user has access.
    2371          $viewprofile = true;
    2372          if (!$iscurrentuser) {
    2373              require_once($CFG->dirroot . '/user/lib.php');
    2374              if ($this->page->context->contextlevel == CONTEXT_USER && !has_capability('moodle/user:viewdetails', $usercontext) ) {
    2375                  $viewprofile = false;
    2376              } else if ($this->page->context->contextlevel != CONTEXT_USER && !user_can_view_profile($user, $course, $usercontext)) {
    2377                  $viewprofile = false;
    2378              }
    2379              if (!$viewprofile) {
    2380                  $viewprofile = user_can_view_profile($user, null, $usercontext);
    2381              }
    2382          }
    2383  
    2384          // Now, conditionally add the user node.
    2385          if ($viewprofile) {
    2386              $canseefullname = has_capability('moodle/site:viewfullnames', $coursecontext);
    2387              $usernode = $usersnode->add(fullname($user, $canseefullname), $userviewurl, self::TYPE_USER, null, 'user' . $user->id);
    2388          } else {
    2389              $usernode = $usersnode->add(get_string('user'));
    2390          }
    2391  
    2392          if ($this->page->context->contextlevel == CONTEXT_USER && $user->id == $this->page->context->instanceid) {
    2393              $usernode->make_active();
    2394          }
    2395  
    2396          // Add user information to the participants or user node.
    2397          if ($issitecourse) {
    2398  
    2399              // If the user is the current user or has permission to view the details of the requested
    2400              // user than add a view profile link.
    2401              if ($iscurrentuser || has_capability('moodle/user:viewdetails', $coursecontext) ||
    2402                      has_capability('moodle/user:viewdetails', $usercontext)) {
    2403                  if ($issitecourse || ($iscurrentuser && !$forceforcontext)) {
    2404                      $usernode->add(get_string('viewprofile'), new moodle_url('/user/profile.php', $baseargs));
    2405                  } else {
    2406                      $usernode->add(get_string('viewprofile'), new moodle_url('/user/view.php', $baseargs));
    2407                  }
    2408              }
    2409  
    2410              if (!empty($CFG->navadduserpostslinks)) {
    2411                  // Add nodes for forum posts and discussions if the user can view either or both
    2412                  // There are no capability checks here as the content of the page is based
    2413                  // purely on the forums the current user has access too.
    2414                  $forumtab = $usernode->add(get_string('forumposts', 'forum'));
    2415                  $forumtab->add(get_string('posts', 'forum'), new moodle_url('/mod/forum/user.php', $baseargs));
    2416                  $forumtab->add(get_string('discussions', 'forum'), new moodle_url('/mod/forum/user.php',
    2417                          array_merge($baseargs, array('mode' => 'discussions'))));
    2418              }
    2419  
    2420              // Add blog nodes.
    2421              if (!empty($CFG->enableblogs)) {
    2422                  if (!$this->cache->cached('userblogoptions'.$user->id)) {
    2423                      require_once($CFG->dirroot.'/blog/lib.php');
    2424                      // Get all options for the user.
    2425                      $options = blog_get_options_for_user($user);
    2426                      $this->cache->set('userblogoptions'.$user->id, $options);
    2427                  } else {
    2428                      $options = $this->cache->{'userblogoptions'.$user->id};
    2429                  }
    2430  
    2431                  if (count($options) > 0) {
    2432                      $blogs = $usernode->add(get_string('blogs', 'blog'), null, navigation_node::TYPE_CONTAINER);
    2433                      foreach ($options as $type => $option) {
    2434                          if ($type == "rss") {
    2435                              $blogs->add($option['string'], $option['link'], settings_navigation::TYPE_SETTING, null, null,
    2436                                      new pix_icon('i/rss', ''));
    2437                          } else {
    2438                              $blogs->add($option['string'], $option['link']);
    2439                          }
    2440                      }
    2441                  }
    2442              }
    2443  
    2444              // Add the messages link.
    2445              // It is context based so can appear in the user's profile and in course participants information.
    2446              if (!empty($CFG->messaging)) {
    2447                  $messageargs = array('user1' => $USER->id);
    2448                  if ($USER->id != $user->id) {
    2449                      $messageargs['user2'] = $user->id;
    2450                  }
    2451                  $url = new moodle_url('/message/index.php', $messageargs);
    2452                  $usernode->add(get_string('messages', 'message'), $url, self::TYPE_SETTING, null, 'messages');
    2453              }
    2454  
    2455              // Add the "My private files" link.
    2456              // This link doesn't have a unique display for course context so only display it under the user's profile.
    2457              if ($issitecourse && $iscurrentuser && has_capability('moodle/user:manageownfiles', $usercontext)) {
    2458                  $url = new moodle_url('/user/files.php');
    2459                  $usernode->add(get_string('privatefiles'), $url, self::TYPE_SETTING, null, 'privatefiles');
    2460              }
    2461  
    2462              // Add a node to view the users notes if permitted.
    2463              if (!empty($CFG->enablenotes) &&
    2464                      has_any_capability(array('moodle/notes:manage', 'moodle/notes:view'), $coursecontext)) {
    2465                  $url = new moodle_url('/notes/index.php', array('user' => $user->id));
    2466                  if ($coursecontext->instanceid != SITEID) {
    2467                      $url->param('course', $coursecontext->instanceid);
    2468                  }
    2469                  $usernode->add(get_string('notes', 'notes'), $url);
    2470              }
    2471  
    2472              // Show the grades node.
    2473              if (($issitecourse && $iscurrentuser) || has_capability('moodle/user:viewdetails', $usercontext)) {
    2474                  require_once($CFG->dirroot . '/user/lib.php');
    2475                  // Set the grades node to link to the "Grades" page.
    2476                  if ($course->id == SITEID) {
    2477                      $url = user_mygrades_url($user->id, $course->id);
    2478                  } else { // Otherwise we are in a course and should redirect to the user grade report (Activity report version).
    2479                      $url = new moodle_url('/course/user.php', array('mode' => 'grade', 'id' => $course->id, 'user' => $user->id));
    2480                  }
    2481                  if ($USER->id != $user->id) {
    2482                      $usernode->add(get_string('grades', 'grades'), $url, self::TYPE_SETTING, null, 'usergrades');
    2483                  } else {
    2484                      $usernode->add(get_string('grades', 'grades'), $url);
    2485                  }
    2486              }
    2487  
    2488              // If the user is the current user add the repositories for the current user.
    2489              $hiddenfields = array_flip(explode(',', $CFG->hiddenuserfields));
    2490              if (!$iscurrentuser &&
    2491                      $course->id == $SITE->id &&
    2492                      has_capability('moodle/user:viewdetails', $usercontext) &&
    2493                      (!in_array('mycourses', $hiddenfields) || has_capability('moodle/user:viewhiddendetails', $coursecontext))) {
    2494  
    2495                  // Add view grade report is permitted.
    2496                  $reports = core_component::get_plugin_list('gradereport');
    2497                  arsort($reports); // User is last, we want to test it first.
    2498  
    2499                  $userscourses = enrol_get_users_courses($user->id, false, '*');
    2500                  $userscoursesnode = $usernode->add(get_string('courses'));
    2501  
    2502                  $count = 0;
    2503                  foreach ($userscourses as $usercourse) {
    2504                      if ($count === (int)$CFG->navcourselimit) {
    2505                          $url = new moodle_url('/user/profile.php', array('id' => $user->id, 'showallcourses' => 1));
    2506                          $userscoursesnode->add(get_string('showallcourses'), $url);
    2507                          break;
    2508                      }
    2509                      $count++;
    2510                      $usercoursecontext = context_course::instance($usercourse->id);
    2511                      $usercourseshortname = format_string($usercourse->shortname, true, array('context' => $usercoursecontext));
    2512                      $usercoursenode = $userscoursesnode->add($usercourseshortname, new moodle_url('/user/view.php',
    2513                              array('id' => $user->id, 'course' => $usercourse->id)), self::TYPE_CONTAINER);
    2514  
    2515                      $gradeavailable = has_capability('moodle/grade:view', $usercoursecontext);
    2516                      if (!$gradeavailable && !empty($usercourse->showgrades) && is_array($reports) && !empty($reports)) {
    2517                          foreach ($reports as $plugin => $plugindir) {
    2518                              if (has_capability('gradereport/'.$plugin.':view', $usercoursecontext)) {
    2519                                  // Stop when the first visible plugin is found.
    2520                                  $gradeavailable = true;
    2521                                  break;
    2522                              }
    2523                          }
    2524                      }
    2525  
    2526                      if ($gradeavailable) {
    2527                          $url = new moodle_url('/grade/report/index.php', array('id' => $usercourse->id));
    2528                          $usercoursenode->add(get_string('grades'), $url, self::TYPE_SETTING, null, null,
    2529                                  new pix_icon('i/grades', ''));
    2530                      }
    2531  
    2532                      // Add a node to view the users notes if permitted.
    2533                      if (!empty($CFG->enablenotes) &&
    2534                              has_any_capability(array('moodle/notes:manage', 'moodle/notes:view'), $usercoursecontext)) {
    2535                          $url = new moodle_url('/notes/index.php', array('user' => $user->id, 'course' => $usercourse->id));
    2536                          $usercoursenode->add(get_string('notes', 'notes'), $url, self::TYPE_SETTING);
    2537                      }
    2538  
    2539                      if (can_access_course($usercourse, $user->id, '', true)) {
    2540                          $usercoursenode->add(get_string('entercourse'), new moodle_url('/course/view.php',
    2541                                  array('id' => $usercourse->id)), self::TYPE_SETTING, null, null, new pix_icon('i/course', ''));
    2542                      }
    2543  
    2544                      $reporttab = $usercoursenode->add(get_string('activityreports'));
    2545  
    2546                      $reportfunctions = get_plugin_list_with_function('report', 'extend_navigation_user', 'lib.php');
    2547                      foreach ($reportfunctions as $reportfunction) {
    2548                          $reportfunction($reporttab, $user, $usercourse);
    2549                      }
    2550  
    2551                      $reporttab->trim_if_empty();
    2552                  }
    2553              }
    2554  
    2555              // Let plugins hook into user navigation.
    2556              $pluginsfunction = get_plugins_with_function('extend_navigation_user', 'lib.php');
    2557              foreach ($pluginsfunction as $plugintype => $plugins) {
    2558                  if ($plugintype != 'report') {
    2559                      foreach ($plugins as $pluginfunction) {
    2560                          $pluginfunction($usernode, $user, $usercontext, $course, $coursecontext);
    2561                      }
    2562                  }
    2563              }
    2564          }
    2565          return true;
    2566      }
    2567  
    2568      /**
    2569       * This method simply checks to see if a given module can extend the navigation.
    2570       *
    2571       * @todo (MDL-25290) A shared caching solution should be used to save details on what extends navigation.
    2572       *
    2573       * @param string $modname
    2574       * @return bool
    2575       */
    2576      public static function module_extends_navigation($modname) {
    2577          global $CFG;
    2578          static $extendingmodules = array();
    2579          if (!array_key_exists($modname, $extendingmodules)) {
    2580              $extendingmodules[$modname] = false;
    2581              $file = $CFG->dirroot.'/mod/'.$modname.'/lib.php';
    2582              if (file_exists($file)) {
    2583                  $function = $modname.'_extend_navigation';
    2584                  require_once($file);
    2585                  $extendingmodules[$modname] = (function_exists($function));
    2586              }
    2587          }
    2588          return $extendingmodules[$modname];
    2589      }
    2590      /**
    2591       * Extends the navigation for the given user.
    2592       *
    2593       * @param stdClass $user A user from the database
    2594       */
    2595      public function extend_for_user($user) {
    2596          $this->extendforuser[] = $user;
    2597      }
    2598  
    2599      /**
    2600       * Returns all of the users the navigation is being extended for
    2601       *
    2602       * @return array An array of extending users.
    2603       */
    2604      public function get_extending_users() {
    2605          return $this->extendforuser;
    2606      }
    2607      /**
    2608       * Adds the given course to the navigation structure.
    2609       *
    2610       * @param stdClass $course
    2611       * @param bool $forcegeneric
    2612       * @param bool $ismycourse
    2613       * @return navigation_node
    2614       */
    2615      public function add_course(stdClass $course, $forcegeneric = false, $coursetype = self::COURSE_OTHER) {
    2616          global $CFG, $SITE;
    2617  
    2618          // We found the course... we can return it now :)
    2619          if (!$forcegeneric && array_key_exists($course->id, $this->addedcourses)) {
    2620              return $this->addedcourses[$course->id];
    2621          }
    2622  
    2623          $coursecontext = context_course::instance($course->id);
    2624  
    2625          if ($coursetype != self::COURSE_MY && $coursetype != self::COURSE_CURRENT && $course->id != $SITE->id) {
    2626              if (is_role_switched($course->id)) {
    2627                  // user has to be able to access course in order to switch, let's skip the visibility test here
    2628              } else if (!core_course_category::can_view_course_info($course)) {
    2629                  return false;
    2630              }
    2631          }
    2632  
    2633          $issite = ($course->id == $SITE->id);
    2634          $shortname = format_string($course->shortname, true, array('context' => $coursecontext));
    2635          $fullname = format_string($course->fullname, true, array('context' => $coursecontext));
    2636          // This is the name that will be shown for the course.
    2637          $coursename = empty($CFG->navshowfullcoursenames) ? $shortname : $fullname;
    2638  
    2639          if ($coursetype == self::COURSE_CURRENT) {
    2640              if ($coursenode = $this->rootnodes['mycourses']->find($course->id, self::TYPE_COURSE)) {
    2641                  return $coursenode;
    2642              } else {
    2643                  $coursetype = self::COURSE_OTHER;
    2644              }
    2645          }
    2646  
    2647          // Can the user expand the course to see its content.
    2648          $canexpandcourse = true;
    2649          if ($issite) {
    2650              $parent = $this;
    2651              $url = null;
    2652              if (empty($CFG->usesitenameforsitepages)) {
    2653                  $coursename = get_string('sitepages');
    2654              }
    2655          } else if ($coursetype == self::COURSE_CURRENT) {
    2656              $parent = $this->rootnodes['currentcourse'];
    2657              $url = new moodle_url('/course/view.php', array('id'=>$course->id));
    2658              $canexpandcourse = $this->can_expand_course($course);
    2659          } else if ($coursetype == self::COURSE_MY && !$forcegeneric) {
    2660              if (!empty($CFG->navshowmycoursecategories) && ($parent = $this->rootnodes['mycourses']->find($course->category, self::TYPE_MY_CATEGORY))) {
    2661                  // Nothing to do here the above statement set $parent to the category within mycourses.
    2662              } else {
    2663                  $parent = $this->rootnodes['mycourses'];
    2664              }
    2665              $url = new moodle_url('/course/view.php', array('id'=>$course->id));
    2666          } else {
    2667              $parent = $this->rootnodes['courses'];
    2668              $url = new moodle_url('/course/view.php', array('id'=>$course->id));
    2669              // They can only expand the course if they can access it.
    2670              $canexpandcourse = $this->can_expand_course($course);
    2671              if (!empty($course->category) && $this->show_categories($coursetype == self::COURSE_MY)) {
    2672                  if (!$this->is_category_fully_loaded($course->category)) {
    2673                      // We need to load the category structure for this course
    2674                      $this->load_all_categories($course->category, false);
    2675                  }
    2676                  if (array_key_exists($course->category, $this->addedcategories)) {
    2677                      $parent = $this->addedcategories[$course->category];
    2678                      // This could lead to the course being created so we should check whether it is the case again
    2679                      if (!$forcegeneric && array_key_exists($course->id, $this->addedcourses)) {
    2680                          return $this->addedcourses[$course->id];
    2681                      }
    2682                  }
    2683              }
    2684          }
    2685  
    2686          $coursenode = $parent->add($coursename, $url, self::TYPE_COURSE, $shortname, $course->id, new pix_icon('i/course', ''));
    2687          $coursenode->showinflatnavigation = $coursetype == self::COURSE_MY;
    2688  
    2689          $coursenode->hidden = (!$course->visible);
    2690          $coursenode->title(format_string($course->fullname, true, array('context' => $coursecontext, 'escape' => false)));
    2691          if ($canexpandcourse) {
    2692              // This course can be expanded by the user, make it a branch to make the system aware that its expandable by ajax.
    2693              $coursenode->nodetype = self::NODETYPE_BRANCH;
    2694              $coursenode->isexpandable = true;
    2695          } else {
    2696              $coursenode->nodetype = self::NODETYPE_LEAF;
    2697              $coursenode->isexpandable = false;
    2698          }
    2699          if (!$forcegeneric) {
    2700              $this->addedcourses[$course->id] = $coursenode;
    2701          }
    2702  
    2703          return $coursenode;
    2704      }
    2705  
    2706      /**
    2707       * Returns a cache instance to use for the expand course cache.
    2708       * @return cache_session
    2709       */
    2710      protected function get_expand_course_cache() {
    2711          if ($this->cacheexpandcourse === null) {
    2712              $this->cacheexpandcourse = cache::make('core', 'navigation_expandcourse');
    2713          }
    2714          return $this->cacheexpandcourse;
    2715      }
    2716  
    2717      /**
    2718       * Checks if a user can expand a course in the navigation.
    2719       *
    2720       * We use a cache here because in order to be accurate we need to call can_access_course which is a costly function.
    2721       * Because this functionality is basic + non-essential and because we lack good event triggering this cache
    2722       * permits stale data.
    2723       * In the situation the user is granted access to a course after we've initialised this session cache the cache
    2724       * will be stale.
    2725       * It is brought up to date in only one of two ways.
    2726       *   1. The user logs out and in again.
    2727       *   2. The user browses to the course they've just being given access to.
    2728       *
    2729       * Really all this controls is whether the node is shown as expandable or not. It is uber un-important.
    2730       *
    2731       * @param stdClass $course
    2732       * @return bool
    2733       */
    2734      protected function can_expand_course($course) {
    2735          $cache = $this->get_expand_course_cache();
    2736          $canexpand = $cache->get($course->id);
    2737          if ($canexpand === false) {
    2738              $canexpand = isloggedin() && can_access_course($course, null, '', true);
    2739              $canexpand = (int)$canexpand;
    2740              $cache->set($course->id, $canexpand);
    2741          }
    2742          return ($canexpand === 1);
    2743      }
    2744  
    2745      /**
    2746       * Returns true if the category has already been loaded as have any child categories
    2747       *
    2748       * @param int $categoryid
    2749       * @return bool
    2750       */
    2751      protected function is_category_fully_loaded($categoryid) {
    2752          return (array_key_exists($categoryid, $this->addedcategories) && ($this->allcategoriesloaded || $this->addedcategories[$categoryid]->children->count() > 0));
    2753      }
    2754  
    2755      /**
    2756       * Adds essential course nodes to the navigation for the given course.
    2757       *
    2758       * This method adds nodes such as reports, blogs and participants
    2759       *
    2760       * @param navigation_node $coursenode
    2761       * @param stdClass $course
    2762       * @return bool returns true on successful addition of a node.
    2763       */
    2764      public function add_course_essentials($coursenode, stdClass $course) {
    2765          global $CFG, $SITE;
    2766          require_once($CFG->dirroot . '/course/lib.php');
    2767  
    2768          if ($course->id == $SITE->id) {
    2769              return $this->add_front_page_course_essentials($coursenode, $course);
    2770          }
    2771  
    2772          if ($coursenode == false || !($coursenode instanceof navigation_node) || $coursenode->get('participants', navigation_node::TYPE_CONTAINER)) {
    2773              return true;
    2774          }
    2775  
    2776          $navoptions = course_get_user_navigation_options($this->page->context, $course);
    2777  
    2778          //Participants
    2779          if ($navoptions->participants) {
    2780              $participants = $coursenode->add(get_string('participants'), new moodle_url('/user/index.php?id='.$course->id),
    2781                  self::TYPE_CONTAINER, get_string('participants'), 'participants', new pix_icon('i/users', ''));
    2782  
    2783              if ($navoptions->blogs) {
    2784                  $blogsurls = new moodle_url('/blog/index.php');
    2785                  if ($currentgroup = groups_get_course_group($course, true)) {
    2786                      $blogsurls->param('groupid', $currentgroup);
    2787                  } else {
    2788                      $blogsurls->param('courseid', $course->id);
    2789                  }
    2790                  $participants->add(get_string('blogscourse', 'blog'), $blogsurls->out(), self::TYPE_SETTING, null, 'courseblogs');
    2791              }
    2792  
    2793              if ($navoptions->notes) {
    2794                  $participants->add(get_string('notes', 'notes'), new moodle_url('/notes/index.php', array('filtertype' => 'course', 'filterselect' => $course->id)), self::TYPE_SETTING, null, 'currentcoursenotes');
    2795              }
    2796          } else if (count($this->extendforuser) > 0 || $this->page->course->id == $course->id) {
    2797              $participants = $coursenode->add(get_string('participants'), null, self::TYPE_CONTAINER, get_string('participants'), 'participants');
    2798          }
    2799  
    2800          // Badges.
    2801          if ($navoptions->badges) {
    2802              $url = new moodle_url('/badges/view.php', array('type' => 2, 'id' => $course->id));
    2803  
    2804              $coursenode->add(get_string('coursebadges', 'badges'), $url,
    2805                      navigation_node::TYPE_SETTING, null, 'badgesview',
    2806                      new pix_icon('i/badge', get_string('coursebadges', 'badges')));
    2807          }
    2808  
    2809          // Check access to the course and competencies page.
    2810          if ($navoptions->competencies) {
    2811              // Just a link to course competency.
    2812              $title = get_string('competencies', 'core_competency');
    2813              $path = new moodle_url("/admin/tool/lp/coursecompetencies.php", array('courseid' => $course->id));
    2814              $coursenode->add($title, $path, navigation_node::TYPE_SETTING, null, 'competencies',
    2815                      new pix_icon('i/competencies', ''));
    2816          }
    2817          if ($navoptions->grades) {
    2818              $url = new moodle_url('/grade/report/index.php', array('id'=>$course->id));
    2819              $gradenode = $coursenode->add(get_string('grades'), $url, self::TYPE_SETTING, null,
    2820                  'grades', new pix_icon('i/grades', ''));
    2821              // If the page type matches the grade part, then make the nav drawer grade node (incl. all sub pages) active.
    2822              if ($this->page->context->contextlevel < CONTEXT_MODULE && strpos($this->page->pagetype, 'grade-') === 0) {
    2823                  $gradenode->make_active();
    2824              }
    2825          }
    2826  
    2827          return true;
    2828      }
    2829      /**
    2830       * This generates the structure of the course that won't be generated when
    2831       * the modules and sections are added.
    2832       *
    2833       * Things such as the reports branch, the participants branch, blogs... get
    2834       * added to the course node by this method.
    2835       *
    2836       * @param navigation_node $coursenode
    2837       * @param stdClass $course
    2838       * @return bool True for successfull generation
    2839       */
    2840      public function add_front_page_course_essentials(navigation_node $coursenode, stdClass $course) {
    2841          global $CFG, $USER, $COURSE, $SITE;
    2842          require_once($CFG->dirroot . '/course/lib.php');
    2843  
    2844          if ($coursenode == false || $coursenode->get('frontpageloaded', navigation_node::TYPE_CUSTOM)) {
    2845              return true;
    2846          }
    2847  
    2848          $systemcontext = context_system::instance();
    2849          $navoptions = course_get_user_navigation_options($systemcontext, $course);
    2850  
    2851          // Hidden node that we use to determine if the front page navigation is loaded.
    2852          // This required as there are not other guaranteed nodes that may be loaded.
    2853          $coursenode->add('frontpageloaded', null, self::TYPE_CUSTOM, null, 'frontpageloaded')->display = false;
    2854  
    2855          // Participants.
    2856          if ($navoptions->participants) {
    2857              $coursenode->add(get_string('participants'), new moodle_url('/user/index.php?id='.$course->id), self::TYPE_CUSTOM, get_string('participants'), 'participants');
    2858          }
    2859  
    2860          // Blogs.
    2861          if ($navoptions->blogs) {
    2862              $blogsurls = new moodle_url('/blog/index.php');
    2863              $coursenode->add(get_string('blogssite', 'blog'), $blogsurls->out(), self::TYPE_SYSTEM, null, 'siteblog');
    2864          }
    2865  
    2866          $filterselect = 0;
    2867  
    2868          // Badges.
    2869          if ($navoptions->badges) {
    2870              $url = new moodle_url($CFG->wwwroot . '/badges/view.php', array('type' => 1));
    2871              $coursenode->add(get_string('sitebadges', 'badges'), $url, navigation_node::TYPE_CUSTOM);
    2872          }
    2873  
    2874          // Notes.
    2875          if ($navoptions->notes) {
    2876              $coursenode->add(get_string('notes', 'notes'), new moodle_url('/notes/index.php',
    2877                  array('filtertype' => 'course', 'filterselect' => $filterselect)), self::TYPE_SETTING, null, 'notes');
    2878          }
    2879  
    2880          // Tags
    2881          if ($navoptions->tags) {
    2882              $node = $coursenode->add(get_string('tags', 'tag'), new moodle_url('/tag/search.php'),
    2883                      self::TYPE_SETTING, null, 'tags');
    2884          }
    2885  
    2886          // Search.
    2887          if ($navoptions->search) {
    2888              $node = $coursenode->add(get_string('search', 'search'), new moodle_url('/search/index.php'),
    2889                      self::TYPE_SETTING, null, 'search');
    2890          }
    2891  
    2892          if ($navoptions->calendar) {
    2893              $courseid = $COURSE->id;
    2894              $params = array('view' => 'month');
    2895              if ($courseid != $SITE->id) {
    2896                  $params['course'] = $courseid;
    2897              }
    2898  
    2899              // Calendar
    2900              $calendarurl = new moodle_url('/calendar/view.php', $params);
    2901              $node = $coursenode->add(get_string('calendar', 'calendar'), $calendarurl,
    2902                  self::TYPE_CUSTOM, null, 'calendar', new pix_icon('i/calendar', ''));
    2903              $node->showinflatnavigation = true;
    2904          }
    2905  
    2906          if (isloggedin()) {
    2907              $usercontext = context_user::instance($USER->id);
    2908              if (has_capability('moodle/user:manageownfiles', $usercontext)) {
    2909                  $url = new moodle_url('/user/files.php');
    2910                  $node = $coursenode->add(get_string('privatefiles'), $url,
    2911                      self::TYPE_SETTING, null, 'privatefiles', new pix_icon('i/privatefiles', ''));
    2912                  $node->display = false;
    2913                  $node->showinflatnavigation = true;
    2914              }
    2915          }
    2916  
    2917          if (isloggedin()) {
    2918              $context = $this->page->context;
    2919              switch ($context->contextlevel) {
    2920                  case CONTEXT_COURSECAT:
    2921                      // OK, expected context level.
    2922                      break;
    2923                  case CONTEXT_COURSE:
    2924                      // OK, expected context level if not on frontpage.
    2925                      if ($COURSE->id != $SITE->id) {
    2926                          break;
    2927                      }
    2928                  default:
    2929                      // If this context is part of a course (excluding frontpage), use the course context.
    2930                      // Otherwise, use the system context.
    2931                      $coursecontext = $context->get_course_context(false);
    2932                      if ($coursecontext && $coursecontext->instanceid !== $SITE->id) {
    2933                          $context = $coursecontext;
    2934                      } else {
    2935                          $context = $systemcontext;
    2936                      }
    2937              }
    2938  
    2939              $params = ['contextid' => $context->id];
    2940              if (has_capability('moodle/contentbank:access', $context)) {
    2941                  $url = new moodle_url('/contentbank/index.php', $params);
    2942                  $node = $coursenode->add(get_string('contentbank'), $url,
    2943                      self::TYPE_CUSTOM, null, 'contentbank', new pix_icon('i/contentbank', ''));
    2944                  $node->showinflatnavigation = true;
    2945              }
    2946          }
    2947  
    2948          return true;
    2949      }
    2950  
    2951      /**
    2952       * Clears the navigation cache
    2953       */
    2954      public function clear_cache() {
    2955          $this->cache->clear();
    2956      }
    2957  
    2958      /**
    2959       * Sets an expansion limit for the navigation
    2960       *
    2961       * The expansion limit is used to prevent the display of content that has a type
    2962       * greater than the provided $type.
    2963       *
    2964       * Can be used to ensure things such as activities or activity content don't get
    2965       * shown on the navigation.
    2966       * They are still generated in order to ensure the navbar still makes sense.
    2967       *
    2968       * @param int $type One of navigation_node::TYPE_*
    2969       * @return bool true when complete.
    2970       */
    2971      public function set_expansion_limit($type) {
    2972          global $SITE;
    2973          $nodes = $this->find_all_of_type($type);
    2974  
    2975          // We only want to hide specific types of nodes.
    2976          // Only nodes that represent "structure" in the navigation tree should be hidden.
    2977          // If we hide all nodes then we risk hiding vital information.
    2978          $typestohide = array(
    2979              self::TYPE_CATEGORY,
    2980              self::TYPE_COURSE,
    2981              self::TYPE_SECTION,
    2982              self::TYPE_ACTIVITY
    2983          );
    2984  
    2985          foreach ($nodes as $node) {
    2986              // We need to generate the full site node
    2987              if ($type == self::TYPE_COURSE && $node->key == $SITE->id) {
    2988                  continue;
    2989              }
    2990              foreach ($node->children as $child) {
    2991                  $child->hide($typestohide);
    2992              }
    2993          }
    2994          return true;
    2995      }
    2996      /**
    2997       * Attempts to get the navigation with the given key from this nodes children.
    2998       *
    2999       * This function only looks at this nodes children, it does NOT look recursivily.
    3000       * If the node can't be found then false is returned.
    3001       *
    3002       * If you need to search recursivily then use the {@link global_navigation::find()} method.
    3003       *
    3004       * Note: If you are trying to set the active node {@link navigation_node::override_active_url()}
    3005       * may be of more use to you.
    3006       *
    3007       * @param string|int $key The key of the node you wish to receive.
    3008       * @param int $type One of navigation_node::TYPE_*
    3009       * @return navigation_node|false
    3010       */
    3011      public function get($key, $type = null) {
    3012          if (!$this->initialised) {
    3013              $this->initialise();
    3014          }
    3015          return parent::get($key, $type);
    3016      }
    3017  
    3018      /**
    3019       * Searches this nodes children and their children to find a navigation node
    3020       * with the matching key and type.
    3021       *
    3022       * This method is recursive and searches children so until either a node is
    3023       * found or there are no more nodes to search.
    3024       *
    3025       * If you know that the node being searched for is a child of this node
    3026       * then use the {@link global_navigation::get()} method instead.
    3027       *
    3028       * Note: If you are trying to set the active node {@link navigation_node::override_active_url()}
    3029       * may be of more use to you.
    3030       *
    3031       * @param string|int $key The key of the node you wish to receive.
    3032       * @param int $type One of navigation_node::TYPE_*
    3033       * @return navigation_node|false
    3034       */
    3035      public function find($key, $type) {
    3036          if (!$this->initialised) {
    3037              $this->initialise();
    3038          }
    3039          if ($type == self::TYPE_ROOTNODE && array_key_exists($key, $this->rootnodes)) {
    3040              return $this->rootnodes[$key];
    3041          }
    3042          return parent::find($key, $type);
    3043      }
    3044  
    3045      /**
    3046       * They've expanded the 'my courses' branch.
    3047       */
    3048      protected function load_courses_enrolled() {
    3049          global $CFG;
    3050  
    3051          $limit = (int) $CFG->navcourselimit;
    3052  
    3053          $courses = enrol_get_my_courses('*');
    3054          $flatnavcourses = [];
    3055  
    3056          // Go through the courses and see which ones we want to display in the flatnav.
    3057          foreach ($courses as $course) {
    3058              $classify = course_classify_for_timeline($course);
    3059  
    3060              if ($classify == COURSE_TIMELINE_INPROGRESS) {
    3061                  $flatnavcourses[$course->id] = $course;
    3062              }
    3063          }
    3064  
    3065          // Get the number of courses that can be displayed in the nav block and in the flatnav.
    3066          $numtotalcourses = count($courses);
    3067          $numtotalflatnavcourses = count($flatnavcourses);
    3068  
    3069          // Reduce the size of the arrays to abide by the 'navcourselimit' setting.
    3070          $courses = array_slice($courses, 0, $limit, true);
    3071          $flatnavcourses = array_slice($flatnavcourses, 0, $limit, true);
    3072  
    3073          // Get the number of courses we are going to show for each.
    3074          $numshowncourses = count($courses);
    3075          $numshownflatnavcourses = count($flatnavcourses);
    3076          if ($numshowncourses && $this->show_my_categories()) {
    3077              // Generate an array containing unique values of all the courses' categories.
    3078              $categoryids = array();
    3079              foreach ($courses as $course) {
    3080                  if (in_array($course->category, $categoryids)) {
    3081                      continue;
    3082                  }
    3083                  $categoryids[] = $course->category;
    3084              }
    3085  
    3086              // Array of category IDs that include the categories of the user's courses and the related course categories.
    3087              $fullpathcategoryids = [];
    3088              // Get the course categories for the enrolled courses' category IDs.
    3089              $mycoursecategories = core_course_category::get_many($categoryids);
    3090              // Loop over each of these categories and build the category tree using each category's path.
    3091              foreach ($mycoursecategories as $mycoursecat) {
    3092                  $pathcategoryids = explode('/', $mycoursecat->path);
    3093                  // First element of the exploded path is empty since paths begin with '/'.
    3094                  array_shift($pathcategoryids);
    3095                  // Merge the exploded category IDs into the full list of category IDs that we will fetch.
    3096                  $fullpathcategoryids = array_merge($fullpathcategoryids, $pathcategoryids);
    3097              }
    3098  
    3099              // Fetch all of the categories related to the user's courses.
    3100              $pathcategories = core_course_category::get_many($fullpathcategoryids);
    3101              // Loop over each of these categories and build the category tree.
    3102              foreach ($pathcategories as $coursecat) {
    3103                  // No need to process categories that have already been added.
    3104                  if (isset($this->addedcategories[$coursecat->id])) {
    3105                      continue;
    3106                  }
    3107                  // Skip categories that are not visible.
    3108                  if (!$coursecat->is_uservisible()) {
    3109                      continue;
    3110                  }
    3111  
    3112                  // Get this course category's parent node.
    3113                  $parent = null;
    3114                  if ($coursecat->parent && isset($this->addedcategories[$coursecat->parent])) {
    3115                      $parent = $this->addedcategories[$coursecat->parent];
    3116                  }
    3117                  if (!$parent) {
    3118                      // If it has no parent, then it should be right under the My courses node.
    3119                      $parent = $this->rootnodes['mycourses'];
    3120                  }
    3121  
    3122                  // Build the category object based from the coursecat object.
    3123                  $mycategory = new stdClass();
    3124                  $mycategory->id = $coursecat->id;
    3125                  $mycategory->name = $coursecat->name;
    3126                  $mycategory->visible = $coursecat->visible;
    3127  
    3128                  // Add this category to the nav tree.
    3129                  $this->add_category($mycategory, $parent, self::TYPE_MY_CATEGORY);
    3130              }
    3131          }
    3132  
    3133          // Go through each course now and add it to the nav block, and the flatnav if applicable.
    3134          foreach ($courses as $course) {
    3135              $node = $this->add_course($course, false, self::COURSE_MY);
    3136              if ($node) {
    3137                  $node->showinflatnavigation = false;
    3138                  // Check if we should also add this to the flat nav as well.
    3139                  if (isset($flatnavcourses[$course->id])) {
    3140                      $node->showinflatnavigation = true;
    3141                  }
    3142              }
    3143          }
    3144  
    3145          // Go through each course in the flatnav now.
    3146          foreach ($flatnavcourses as $course) {
    3147              // Check if we haven't already added it.
    3148              if (!isset($courses[$course->id])) {
    3149                  // Ok, add it to the flatnav only.
    3150                  $node = $this->add_course($course, false, self::COURSE_MY);
    3151                  $node->display = false;
    3152                  $node->showinflatnavigation = true;
    3153              }
    3154          }
    3155  
    3156          $showmorelinkinnav = $numtotalcourses > $numshowncourses;
    3157          $showmorelinkinflatnav = $numtotalflatnavcourses > $numshownflatnavcourses;
    3158          // Show a link to the course page if there are more courses the user is enrolled in.
    3159          if ($showmorelinkinnav || $showmorelinkinflatnav) {
    3160              // Adding hash to URL so the link is not highlighted in the navigation when clicked.
    3161              $url = new moodle_url('/my/');
    3162              $parent = $this->rootnodes['mycourses'];
    3163              $coursenode = $parent->add(get_string('morenavigationlinks'), $url, self::TYPE_CUSTOM, null, self::COURSE_INDEX_PAGE);
    3164  
    3165              if ($showmorelinkinnav) {
    3166                  $coursenode->display = true;
    3167              }
    3168  
    3169              if ($showmorelinkinflatnav) {
    3170                  $coursenode->showinflatnavigation = true;
    3171              }
    3172          }
    3173      }
    3174  }
    3175  
    3176  /**
    3177   * The global navigation class used especially for AJAX requests.
    3178   *
    3179   * The primary methods that are used in the global navigation class have been overriden
    3180   * to ensure that only the relevant branch is generated at the root of the tree.
    3181   * This can be done because AJAX is only used when the backwards structure for the
    3182   * requested branch exists.
    3183   * This has been done only because it shortens the amounts of information that is generated
    3184   * which of course will speed up the response time.. because no one likes laggy AJAX.
    3185   *
    3186   * @package   core
    3187   * @category  navigation
    3188   * @copyright 2009 Sam Hemelryk
    3189   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
    3190   */
    3191  class global_navigation_for_ajax extends global_navigation {
    3192  
    3193      /** @var int used for determining what type of navigation_node::TYPE_* is being used */
    3194      protected $branchtype;
    3195  
    3196      /** @var int the instance id */
    3197      protected $instanceid;
    3198  
    3199      /** @var array Holds an array of expandable nodes */
    3200      protected $expandable = array();
    3201  
    3202      /**
    3203       * Constructs the navigation for use in an AJAX request
    3204       *
    3205       * @param moodle_page $page moodle_page object
    3206       * @param int $branchtype
    3207       * @param int $id
    3208       */
    3209      public function __construct($page, $branchtype, $id) {
    3210          $this->page = $page;
    3211          $this->cache = new navigation_cache(NAVIGATION_CACHE_NAME);
    3212          $this->children = new navigation_node_collection();
    3213          $this->branchtype = $branchtype;
    3214          $this->instanceid = $id;
    3215          $this->initialise();
    3216      }
    3217      /**
    3218       * Initialise the navigation given the type and id for the branch to expand.
    3219       *
    3220       * @return array An array of the expandable nodes
    3221       */
    3222      public function initialise() {
    3223          global $DB, $SITE;
    3224  
    3225          if ($this->initialised || during_initial_install()) {
    3226              return $this->expandable;
    3227          }
    3228          $this->initialised = true;
    3229  
    3230          $this->rootnodes = array();
    3231          $this->rootnodes['site']    = $this->add_course($SITE);
    3232          $this->rootnodes['mycourses'] = $this->add(get_string('mycourses'), new moodle_url('/my'), self::TYPE_ROOTNODE, null, 'mycourses');
    3233          $this->rootnodes['courses'] = $this->add(get_string('courses'), null, self::TYPE_ROOTNODE, null, 'courses');
    3234          // The courses branch is always displayed, and is always expandable (although may be empty).
    3235          // This mimicks what is done during {@link global_navigation::initialise()}.
    3236          $this->rootnodes['courses']->isexpandable = true;
    3237  
    3238          // Branchtype will be one of navigation_node::TYPE_*
    3239          switch ($this->branchtype) {
    3240              case 0:
    3241                  if ($this->instanceid === 'mycourses') {
    3242                      $this->load_courses_enrolled();
    3243                  } else if ($this->instanceid === 'courses') {
    3244                      $this->load_courses_other();
    3245                  }
    3246                  break;
    3247              case self::TYPE_CATEGORY :
    3248                  $this->load_category($this->instanceid);
    3249                  break;
    3250              case self::TYPE_MY_CATEGORY :
    3251                  $this->load_category($this->instanceid, self::TYPE_MY_CATEGORY);
    3252                  break;
    3253              case self::TYPE_COURSE :
    3254                  $course = $DB->get_record('course', array('id' => $this->instanceid), '*', MUST_EXIST);
    3255                  if (!can_access_course($course, null, '', true)) {
    3256                      // Thats OK all courses are expandable by default. We don't need to actually expand it we can just
    3257                      // add the course node and break. This leads to an empty node.
    3258                      $this->add_course($course);
    3259                      break;
    3260                  }
    3261                  require_course_login($course, true, null, false, true);
    3262                  $this->page->set_context(context_course::instance($course->id));
    3263                  $coursenode = $this->add_course($course, false, self::COURSE_CURRENT);
    3264                  $this->add_course_essentials($coursenode, $course);
    3265                  $this->load_course_sections($course, $coursenode);
    3266                  break;
    3267              case self::TYPE_SECTION :
    3268                  $sql = 'SELECT c.*, cs.section AS sectionnumber
    3269                          FROM {course} c
    3270                          LEFT JOIN {course_sections} cs ON cs.course = c.id
    3271                          WHERE cs.id = ?';
    3272                  $course = $DB->get_record_sql($sql, array($this->instanceid), MUST_EXIST);
    3273                  require_course_login($course, true, null, false, true);
    3274                  $this->page->set_context(context_course::instance($course->id));
    3275                  $coursenode = $this->add_course($course, false, self::COURSE_CURRENT);
    3276                  $this->add_course_essentials($coursenode, $course);
    3277                  $this->load_course_sections($course, $coursenode, $course->sectionnumber);
    3278                  break;
    3279              case self::TYPE_ACTIVITY :
    3280                  $sql = "SELECT c.*
    3281                            FROM {course} c
    3282                            JOIN {course_modules} cm ON cm.course = c.id
    3283                           WHERE cm.id = :cmid";
    3284                  $params = array('cmid' => $this->instanceid);
    3285                  $course = $DB->get_record_sql($sql, $params, MUST_EXIST);
    3286                  $modinfo = get_fast_modinfo($course);
    3287                  $cm = $modinfo->get_cm($this->instanceid);
    3288                  require_course_login($course, true, $cm, false, true);
    3289                  $this->page->set_context(context_module::instance($cm->id));
    3290                  $coursenode = $this->add_course($course, false, self::COURSE_CURRENT);
    3291                  $this->load_course_sections($course, $coursenode, null, $cm);
    3292                  $activitynode = $coursenode->find($cm->id, self::TYPE_ACTIVITY);
    3293                  if ($activitynode) {
    3294                      $modulenode = $this->load_activity($cm, $course, $activitynode);
    3295                  }
    3296                  break;
    3297              default:
    3298                  throw new Exception('Unknown type');
    3299                  return $this->expandable;
    3300          }
    3301  
    3302          if ($this->page->context->contextlevel == CONTEXT_COURSE && $this->page->context->instanceid != $SITE->id) {
    3303              $this->load_for_user(null, true);
    3304          }
    3305  
    3306          // Give the local plugins a chance to include some navigation if they want.
    3307          $this->load_local_plugin_navigation();
    3308  
    3309          $this->find_expandable($this->expandable);
    3310          return $this->expandable;
    3311      }
    3312  
    3313      /**
    3314       * They've expanded the general 'courses' branch.
    3315       */
    3316      protected function load_courses_other() {
    3317          $this->load_all_courses();
    3318      }
    3319  
    3320      /**
    3321       * Loads a single category into the AJAX navigation.
    3322       *
    3323       * This function is special in that it doesn't concern itself with the parent of
    3324       * the requested category or its siblings.
    3325       * This is because with the AJAX navigation we know exactly what is wanted and only need to
    3326       * request that.
    3327       *
    3328       * @global moodle_database $DB
    3329       * @param int $categoryid id of category to load in navigation.
    3330       * @param int $nodetype type of node, if category is under MyHome then it's TYPE_MY_CATEGORY
    3331       * @return void.
    3332       */
    3333      protected function load_category($categoryid, $nodetype = self::TYPE_CATEGORY) {
    3334          global $CFG, $DB;
    3335  
    3336          $limit = 20;
    3337          if (!empty($CFG->navcourselimit)) {
    3338              $limit = (int)$CFG->navcourselimit;
    3339          }
    3340  
    3341          $catcontextsql = context_helper::get_preload_record_columns_sql('ctx');
    3342          $sql = "SELECT cc.*, $catcontextsql
    3343                    FROM {course_categories} cc
    3344                    JOIN {context} ctx ON cc.id = ctx.instanceid
    3345                   WHERE ctx.contextlevel = ".CONTEXT_COURSECAT." AND
    3346                         (cc.id = :categoryid1 OR cc.parent = :categoryid2)
    3347                ORDER BY cc.depth ASC, cc.sortorder ASC, cc.id ASC";
    3348          $params = array('categoryid1' => $categoryid, 'categoryid2' => $categoryid);
    3349          $categories = $DB->get_recordset_sql($sql, $params, 0, $limit);
    3350          $categorylist = array();
    3351          $subcategories = array();
    3352          $basecategory = null;
    3353          foreach ($categories as $category) {
    3354              $categorylist[] = $category->id;
    3355              context_helper::preload_from_record($category);
    3356              if ($category->id == $categoryid) {
    3357                  $this->add_category($category, $this, $nodetype);
    3358                  $basecategory = $this->addedcategories[$category->id];
    3359              } else {
    3360                  $subcategories[$category->id] = $category;
    3361              }
    3362          }
    3363          $categories->close();
    3364  
    3365  
    3366          // If category is shown in MyHome then only show enrolled courses and hide empty subcategories,
    3367          // else show all courses.
    3368          if ($nodetype === self::TYPE_MY_CATEGORY) {
    3369              $courses = enrol_get_my_courses('*');
    3370              $categoryids = array();
    3371  
    3372              // Only search for categories if basecategory was found.
    3373              if (!is_null($basecategory)) {
    3374                  // Get course parent category ids.
    3375                  foreach ($courses as $course) {
    3376                      $categoryids[] = $course->category;
    3377                  }
    3378  
    3379                  // Get a unique list of category ids which a part of the path
    3380                  // to user's courses.
    3381                  $coursesubcategories = array();
    3382                  $addedsubcategories = array();
    3383  
    3384                  list($sql, $params) = $DB->get_in_or_equal($categoryids);
    3385                  $categories = $DB->get_recordset_select('course_categories', 'id '.$sql, $params, 'sortorder, id', 'id, path');
    3386  
    3387                  foreach ($categories as $category){
    3388                      $coursesubcategories = array_merge($coursesubcategories, explode('/', trim($category->path, "/")));
    3389                  }
    3390                  $categories->close();
    3391                  $coursesubcategories = array_unique($coursesubcategories);
    3392  
    3393                  // Only add a subcategory if it is part of the path to user's course and
    3394                  // wasn't already added.
    3395                  foreach ($subcategories as $subid => $subcategory) {
    3396                      if (in_array($subid, $coursesubcategories) &&
    3397                              !in_array($subid, $addedsubcategories)) {
    3398                              $this->add_category($subcategory, $basecategory, $nodetype);
    3399                              $addedsubcategories[] = $subid;
    3400                      }
    3401                  }
    3402              }
    3403  
    3404              foreach ($courses as $course) {
    3405                  // Add course if it's in category.
    3406                  if (in_array($course->category, $categorylist)) {
    3407                      $this->add_course($course, true, self::COURSE_MY);
    3408                  }
    3409              }
    3410          } else {
    3411              if (!is_null($basecategory)) {
    3412                  foreach ($subcategories as $key=>$category) {
    3413                      $this->add_category($category, $basecategory, $nodetype);
    3414                  }
    3415              }
    3416              $courses = $DB->get_recordset('course', array('category' => $categoryid), 'sortorder', '*' , 0, $limit);
    3417              foreach ($courses as $course) {
    3418                  $this->add_course($course);
    3419              }
    3420              $courses->close();
    3421          }
    3422      }
    3423  
    3424      /**
    3425       * Returns an array of expandable nodes
    3426       * @return array
    3427       */
    3428      public function get_expandable() {
    3429          return $this->expandable;
    3430      }
    3431  }
    3432  
    3433  /**
    3434   * Navbar class
    3435   *
    3436   * This class is used to manage the navbar, which is initialised from the navigation
    3437   * object held by PAGE
    3438   *
    3439   * @package   core
    3440   * @category  navigation
    3441   * @copyright 2009 Sam Hemelryk
    3442   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
    3443   */
    3444  class navbar extends navigation_node {
    3445      /** @var bool A switch for whether the navbar is initialised or not */
    3446      protected $initialised = false;
    3447      /** @var mixed keys used to reference the nodes on the navbar */
    3448      protected $keys = array();
    3449      /** @var null|string content of the navbar */
    3450      protected $content = null;
    3451      /** @var moodle_page object the moodle page that this navbar belongs to */
    3452      protected $page;
    3453      /** @var bool A switch for whether to ignore the active navigation information */
    3454      protected $ignoreactive = false;
    3455      /** @var bool A switch to let us know if we are in the middle of an install */
    3456      protected $duringinstall = false;
    3457      /** @var bool A switch for whether the navbar has items */
    3458      protected $hasitems = false;
    3459      /** @var array An array of navigation nodes for the navbar */
    3460      protected $items;
    3461      /** @var array An array of child node objects */
    3462      public $children = array();
    3463      /** @var bool A switch for whether we want to include the root node in the navbar */
    3464      public $includesettingsbase = false;
    3465      /** @var breadcrumb_navigation_node[] $prependchildren */
    3466      protected $prependchildren = array();
    3467  
    3468      /**
    3469       * The almighty constructor
    3470       *
    3471       * @param moodle_page $page
    3472       */
    3473      public function __construct(moodle_page $page) {
    3474          global $CFG;
    3475          if (during_initial_install()) {
    3476              $this->duringinstall = true;
    3477              return false;
    3478          }
    3479          $this->page = $page;
    3480          $this->text = get_string('home');
    3481          $this->shorttext = get_string('home');
    3482          $this->action = new moodle_url($CFG->wwwroot);
    3483          $this->nodetype = self::NODETYPE_BRANCH;
    3484          $this->type = self::TYPE_SYSTEM;
    3485      }
    3486  
    3487      /**
    3488       * Quick check to see if the navbar will have items in.
    3489       *
    3490       * @return bool Returns true if the navbar will have items, false otherwise
    3491       */
    3492      public function has_items() {
    3493          if ($this->duringinstall) {
    3494              return false;
    3495          } else if ($this->hasitems !== false) {
    3496              return true;
    3497          }
    3498          if (count($this->children) > 0 || count($this->prependchildren) > 0) {
    3499              // There have been manually added items - there are definitely items.
    3500              $outcome = true;
    3501          } else if (!$this->ignoreactive) {
    3502              // We will need to initialise the navigation structure to check if there are active items.
    3503              $this->page->navigation->initialise($this->page);
    3504              $outcome = ($this->page->navigation->contains_active_node() || $this->page->settingsnav->contains_active_node());
    3505          }
    3506          $this->hasitems = $outcome;
    3507          return $outcome;
    3508      }
    3509  
    3510      /**
    3511       * Turn on/off ignore active
    3512       *
    3513       * @param bool $setting
    3514       */
    3515      public function ignore_active($setting=true) {
    3516          $this->ignoreactive = ($setting);
    3517      }
    3518  
    3519      /**
    3520       * Gets a navigation node
    3521       *
    3522       * @param string|int $key for referencing the navbar nodes
    3523       * @param int $type breadcrumb_navigation_node::TYPE_*
    3524       * @return breadcrumb_navigation_node|bool
    3525       */
    3526      public function get($key, $type = null) {
    3527          foreach ($this->children as &$child) {
    3528              if ($child->key === $key && ($type == null || $type == $child->type)) {
    3529                  return $child;
    3530              }
    3531          }
    3532          foreach ($this->prependchildren as &$child) {
    3533              if ($child->key === $key && ($type == null || $type == $child->type)) {
    3534                  return $child;
    3535              }
    3536          }
    3537          return false;
    3538      }
    3539      /**
    3540       * Returns an array of breadcrumb_navigation_nodes that make up the navbar.
    3541       *
    3542       * @return array
    3543       */
    3544      public function get_items() {
    3545          global $CFG;
    3546          $items = array();
    3547          // Make sure that navigation is initialised
    3548          if (!$this->has_items()) {
    3549              return $items;
    3550          }
    3551          if ($this->items !== null) {
    3552              return $this->items;
    3553          }
    3554  
    3555          if (count($this->children) > 0) {
    3556              // Add the custom children.
    3557              $items = array_reverse($this->children);
    3558          }
    3559  
    3560          // Check if navigation contains the active node
    3561          if (!$this->ignoreactive) {
    3562              // We will need to ensure the navigation has been initialised.
    3563              $this->page->navigation->initialise($this->page);
    3564              // Now find the active nodes on both the navigation and settings.
    3565              $navigationactivenode = $this->page->navigation->find_active_node();
    3566              $settingsactivenode = $this->page->settingsnav->find_active_node();
    3567  
    3568              if ($navigationactivenode && $settingsactivenode) {
    3569                  // Parse a combined navigation tree
    3570                  while ($settingsactivenode && $settingsactivenode->parent !== null) {
    3571                      if (!$settingsactivenode->mainnavonly) {
    3572                          $items[] = new breadcrumb_navigation_node($settingsactivenode);
    3573                      }
    3574                      $settingsactivenode = $settingsactivenode->parent;
    3575                  }
    3576                  if (!$this->includesettingsbase) {
    3577                      // Removes the first node from the settings (root node) from the list
    3578                      array_pop($items);
    3579                  }
    3580                  while ($navigationactivenode && $navigationactivenode->parent !== null) {
    3581                      if (!$navigationactivenode->mainnavonly) {
    3582                          $items[] = new breadcrumb_navigation_node($navigationactivenode);
    3583                      }
    3584                      if (!empty($CFG->navshowcategories) &&
    3585                              $navigationactivenode->type === self::TYPE_COURSE &&
    3586                              $navigationactivenode->parent->key === 'currentcourse') {
    3587                          foreach ($this->get_course_categories() as $item) {
    3588                              $items[] = new breadcrumb_navigation_node($item);
    3589                          }
    3590                      }
    3591                      $navigationactivenode = $navigationactivenode->parent;
    3592                  }
    3593              } else if ($navigationactivenode) {
    3594                  // Parse the navigation tree to get the active node
    3595                  while ($navigationactivenode && $navigationactivenode->parent !== null) {
    3596                      if (!$navigationactivenode->mainnavonly) {
    3597                          $items[] = new breadcrumb_navigation_node($navigationactivenode);
    3598                      }
    3599                      if (!empty($CFG->navshowcategories) &&
    3600                              $navigationactivenode->type === self::TYPE_COURSE &&
    3601                              $navigationactivenode->parent->key === 'currentcourse') {
    3602                          foreach ($this->get_course_categories() as $item) {
    3603                              $items[] = new breadcrumb_navigation_node($item);
    3604                          }
    3605                      }
    3606                      $navigationactivenode = $navigationactivenode->parent;
    3607                  }
    3608              } else if ($settingsactivenode) {
    3609                  // Parse the settings navigation to get the active node
    3610                  while ($settingsactivenode && $settingsactivenode->parent !== null) {
    3611                      if (!$settingsactivenode->mainnavonly) {
    3612                          $items[] = new breadcrumb_navigation_node($settingsactivenode);
    3613                      }
    3614                      $settingsactivenode = $settingsactivenode->parent;
    3615                  }
    3616              }
    3617          }
    3618  
    3619          $items[] = new breadcrumb_navigation_node(array(
    3620              'text' => $this->page->navigation->text,
    3621              'shorttext' => $this->page->navigation->shorttext,
    3622              'key' => $this->page->navigation->key,
    3623              'action' => $this->page->navigation->action
    3624          ));
    3625  
    3626          if (count($this->prependchildren) > 0) {
    3627              // Add the custom children
    3628              $items = array_merge($items, array_reverse($this->prependchildren));
    3629          }
    3630  
    3631          $last = reset($items);
    3632          if ($last) {
    3633              $last->set_last(true);
    3634          }
    3635          $this->items = array_reverse($items);
    3636          return $this->items;
    3637      }
    3638  
    3639      /**
    3640       * Get the list of categories leading to this course.
    3641       *
    3642       * This function is used by {@link navbar::get_items()} to add back the "courses"
    3643       * node and category chain leading to the current course.  Note that this is only ever
    3644       * called for the current course, so we don't need to bother taking in any parameters.
    3645       *
    3646       * @return array
    3647       */
    3648      private function get_course_categories() {
    3649          global $CFG;
    3650          require_once($CFG->dirroot.'/course/lib.php');
    3651  
    3652          $categories = array();
    3653          $cap = 'moodle/category:viewhiddencategories';
    3654          $showcategories = !core_course_category::is_simple_site();
    3655  
    3656          if ($showcategories) {
    3657              foreach ($this->page->categories as $category) {
    3658                  $context = context_coursecat::instance($category->id);
    3659                  if (!core_course_category::can_view_category($category)) {
    3660                      continue;
    3661                  }
    3662                  $url = new moodle_url('/course/index.php', array('categoryid' => $category->id));
    3663                  $name = format_string($category->name, true, array('context' => $context));
    3664                  $categorynode = breadcrumb_navigation_node::create($name, $url, self::TYPE_CATEGORY, null, $category->id);
    3665                  if (!$category->visible) {
    3666                      $categorynode->hidden = true;
    3667                  }
    3668                  $categories[] = $categorynode;
    3669              }
    3670          }
    3671  
    3672          // Don't show the 'course' node if enrolled in this course.
    3673          $coursecontext = context_course::instance($this->page->course->id);
    3674          if (!is_enrolled($coursecontext, null, '', true)) {
    3675              $courses = $this->page->navigation->get('courses');
    3676              if (!$courses) {
    3677                  // Courses node may not be present.
    3678                  $courses = breadcrumb_navigation_node::create(
    3679                      get_string('courses'),
    3680                      new moodle_url('/course/index.php'),
    3681                      self::TYPE_CONTAINER
    3682                  );
    3683              }
    3684              $categories[] = $courses;
    3685          }
    3686  
    3687          return $categories;
    3688      }
    3689  
    3690      /**
    3691       * Add a new breadcrumb_navigation_node to the navbar, overrides parent::add
    3692       *
    3693       * This function overrides {@link breadcrumb_navigation_node::add()} so that we can change
    3694       * the way nodes get added to allow us to simply call add and have the node added to the
    3695       * end of the navbar
    3696       *
    3697       * @param string $text
    3698       * @param string|moodle_url|action_link $action An action to associate with this node.
    3699       * @param int $type One of navigation_node::TYPE_*
    3700       * @param string $shorttext
    3701       * @param string|int $key A key to identify this node with. Key + type is unique to a parent.
    3702       * @param pix_icon $icon An optional icon to use for this node.
    3703       * @return navigation_node
    3704       */
    3705      public function add($text, $action=null, $type=self::TYPE_CUSTOM, $shorttext=null, $key=null, pix_icon $icon=null) {
    3706          if ($this->content !== null) {
    3707              debugging('Nav bar items must be printed before $OUTPUT->header() has been called', DEBUG_DEVELOPER);
    3708          }
    3709  
    3710          // Properties array used when creating the new navigation node
    3711          $itemarray = array(
    3712              'text' => $text,
    3713              'type' => $type
    3714          );
    3715          // Set the action if one was provided
    3716          if ($action!==null) {
    3717              $itemarray['action'] = $action;
    3718          }
    3719          // Set the shorttext if one was provided
    3720          if ($shorttext!==null) {
    3721              $itemarray['shorttext'] = $shorttext;
    3722          }
    3723          // Set the icon if one was provided
    3724          if ($icon!==null) {
    3725              $itemarray['icon'] = $icon;
    3726          }
    3727          // Default the key to the number of children if not provided
    3728          if ($key === null) {
    3729              $key = count($this->children);
    3730          }
    3731          // Set the key
    3732          $itemarray['key'] = $key;
    3733          // Set the parent to this node
    3734          $itemarray['parent'] = $this;
    3735          // Add the child using the navigation_node_collections add method
    3736          $this->children[] = new breadcrumb_navigation_node($itemarray);
    3737          return $this;
    3738      }
    3739  
    3740      /**
    3741       * Prepends a new navigation_node to the start of the navbar
    3742       *
    3743       * @param string $text
    3744       * @param string|moodle_url|action_link $action An action to associate with this node.
    3745       * @param int $type One of navigation_node::TYPE_*
    3746       * @param string $shorttext
    3747       * @param string|int $key A key to identify this node with. Key + type is unique to a parent.
    3748       * @param pix_icon $icon An optional icon to use for this node.
    3749       * @return navigation_node
    3750       */
    3751      public function prepend($text, $action=null, $type=self::TYPE_CUSTOM, $shorttext=null, $key=null, pix_icon $icon=null) {
    3752          if ($this->content !== null) {
    3753              debugging('Nav bar items must be printed before $OUTPUT->header() has been called', DEBUG_DEVELOPER);
    3754          }
    3755          // Properties array used when creating the new navigation node.
    3756          $itemarray = array(
    3757              'text' => $text,
    3758              'type' => $type
    3759          );
    3760          // Set the action if one was provided.
    3761          if ($action!==null) {
    3762              $itemarray['action'] = $action;
    3763          }
    3764          // Set the shorttext if one was provided.
    3765          if ($shorttext!==null) {
    3766              $itemarray['shorttext'] = $shorttext;
    3767          }
    3768          // Set the icon if one was provided.
    3769          if ($icon!==null) {
    3770              $itemarray['icon']