Commit | Line | Data |
---|---|---|
7d2a0492 | 1 | <?php |
7d2a0492 | 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 | /** | |
98556b23 | 18 | * This file contains classes used to manage the navigation structures within Moodle. |
7d2a0492 | 19 | * |
5bcfd504 | 20 | * @since Moodle 2.0 |
78bfb562 | 21 | * @package core |
78bfb562 PS |
22 | * @copyright 2009 Sam Hemelryk |
23 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | |
7d2a0492 | 24 | */ |
25 | ||
78bfb562 PS |
26 | defined('MOODLE_INTERNAL') || die(); |
27 | ||
f5b5a822 | 28 | /** |
2561c4e9 | 29 | * The name that will be used to separate the navigation cache within SESSION |
f5b5a822 | 30 | */ |
31 | define('NAVIGATION_CACHE_NAME', 'navigation'); | |
5ab32c11 | 32 | define('NAVIGATION_SITE_ADMIN_CACHE_NAME', 'navigationsiteadmin'); |
f5b5a822 | 33 | |
7d2a0492 | 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 | * | |
31fde54f AG |
43 | * @package core |
44 | * @category navigation | |
7d2a0492 | 45 | * @copyright 2009 Sam Hemelryk |
46 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | |
47 | */ | |
3406acde SH |
48 | class navigation_node implements renderable { |
49 | /** @var int Used to identify this node a leaf (default) 0 */ | |
507a7a9a | 50 | const NODETYPE_LEAF = 0; |
3406acde | 51 | /** @var int Used to identify this node a branch, happens with children 1 */ |
7d2a0492 | 52 | const NODETYPE_BRANCH = 1; |
3406acde | 53 | /** @var null Unknown node type null */ |
7d2a0492 | 54 | const TYPE_UNKNOWN = null; |
3406acde SH |
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 */ | |
7d2a0492 | 60 | const TYPE_CATEGORY = 10; |
db47f68f RT |
61 | /** var int Category displayed in MyHome navigation node */ |
62 | const TYPE_MY_CATEGORY = 11; | |
3406acde | 63 | /** @var int Course node type 20 */ |
7d2a0492 | 64 | const TYPE_COURSE = 20; |
3406acde | 65 | /** @var int Course Structure node type 30 */ |
507a7a9a | 66 | const TYPE_SECTION = 30; |
3406acde | 67 | /** @var int Activity node type, e.g. Forum, Quiz 40 */ |
7d2a0492 | 68 | const TYPE_ACTIVITY = 40; |
3406acde | 69 | /** @var int Resource node type, e.g. Link to a file, or label 50 */ |
7d2a0492 | 70 | const TYPE_RESOURCE = 50; |
3406acde | 71 | /** @var int A custom node type, default when adding without specifing type 60 */ |
7d2a0492 | 72 | const TYPE_CUSTOM = 60; |
3406acde | 73 | /** @var int Setting node type, used only within settings nav 70 */ |
7d2a0492 | 74 | const TYPE_SETTING = 70; |
5ab32c11 RT |
75 | /** @var int site admin branch node type, used only within settings nav 71 */ |
76 | const TYPE_SITE_ADMIN = 71; | |
3406acde | 77 | /** @var int Setting node type, used only within settings nav 80 */ |
507a7a9a | 78 | const TYPE_USER = 80; |
3406acde SH |
79 | /** @var int Setting node type, used for containers of no importance 90 */ |
80 | const TYPE_CONTAINER = 90; | |
6caf3f5c ARN |
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; | |
fa238237 MN |
87 | /** var string The course index page navigation node */ |
88 | const COURSE_INDEX_PAGE = 'courseindexpage'; | |
7d2a0492 | 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; | |
3406acde | 102 | /** @var moodle_url|action_link|null An action for the node (link) */ |
7d2a0492 | 103 | public $action = null; |
f9fc1461 | 104 | /** @var pix_icon The path to an icon to use for this node */ |
7d2a0492 | 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; | |
3406acde | 114 | /** @var array An array of CSS classes for the node */ |
7d2a0492 | 115 | public $classes = array(); |
3406acde | 116 | /** @var navigation_node_collection An array of child nodes */ |
7d2a0492 | 117 | public $children = array(); |
118 | /** @var bool If set to true the node will be recognised as active */ | |
119 | public $isactive = false; | |
3406acde | 120 | /** @var bool If set to true the node will be dimmed */ |
7d2a0492 | 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; | |
d38a4849 | 130 | /** @var navigation_node A reference to the node parent, you should never set this directly you should always call set_parent */ |
3406acde | 131 | public $parent = null; |
493a48f3 SH |
132 | /** @var bool Override to not display the icon even if one is provided **/ |
133 | public $hideicon = false; | |
58b602da SH |
134 | /** @var bool Set to true if we KNOW that this node can be expanded. */ |
135 | public $isexpandable = false; | |
7d2a0492 | 136 | /** @var array */ |
5d0cc05b JD |
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'); | |
7d2a0492 | 140 | /** @var moodle_url */ |
141 | protected static $fullmeurl = null; | |
d9d2817a SH |
142 | /** @var bool toogles auto matching of active node */ |
143 | public static $autofindactive = true; | |
61bb8c93 PS |
144 | /** @var bool should we load full admin tree or rely on AJAX for performance reasons */ |
145 | protected static $loadadmintree = false; | |
d67e4821 | 146 | /** @var mixed If set to an int, that section will be included even if it has no activities */ |
147 | public $includesectionnum = false; | |
f8895446 JO |
148 | /** @var bool does the node need to be loaded via ajax */ |
149 | public $requiresajaxloading = false; | |
99061152 DW |
150 | /** @var bool If set to true this node will be added to the "flat" navigation */ |
151 | public $showinflatnavigation = false; | |
7d2a0492 | 152 | |
153 | /** | |
3406acde | 154 | * Constructs a new navigation_node |
7d2a0492 | 155 | * |
3406acde SH |
156 | * @param array|string $properties Either an array of properties or a string to use |
157 | * as the text for the node | |
7d2a0492 | 158 | */ |
159 | public function __construct($properties) { | |
7d2a0492 | 160 | if (is_array($properties)) { |
3406acde SH |
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 | |
7d2a0492 | 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 | } | |
7081714d | 175 | if (!array_key_exists('icon', $properties)) { |
775fe700 | 176 | $properties['icon'] = new pix_icon('i/navigationitem', ''); |
7081714d SH |
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'; | |
7da3a799 | 184 | } |
7d2a0492 | 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 | } | |
3406acde SH |
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 | } | |
d38a4849 SH |
204 | if (array_key_exists('parent', $properties)) { |
205 | $this->set_parent($properties['parent']); | |
206 | } | |
7d2a0492 | 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 | } | |
3406acde SH |
213 | // Instantiate a new navigation node collection for this nodes children |
214 | $this->children = new navigation_node_collection(); | |
7d2a0492 | 215 | } |
216 | ||
95b97515 | 217 | /** |
3406acde | 218 | * Checks if this node is the active node. |
0baa5d46 | 219 | * |
3406acde SH |
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. | |
0baa5d46 | 222 | * |
3406acde SH |
223 | * @param int $strength One of URL_MATCH_EXACT, URL_MATCH_PARAMS, or URL_MATCH_BASE |
224 | * @return bool | |
7d2a0492 | 225 | */ |
bf6c37c7 | 226 | public function check_if_active($strength=URL_MATCH_EXACT) { |
227 | global $FULLME, $PAGE; | |
3406acde | 228 | // Set fullmeurl if it hasn't already been set |
7d2a0492 | 229 | if (self::$fullmeurl == null) { |
bf6c37c7 | 230 | if ($PAGE->has_set_url()) { |
3406acde | 231 | self::override_active_url(new moodle_url($PAGE->url)); |
bf6c37c7 | 232 | } else { |
3406acde | 233 | self::override_active_url(new moodle_url($FULLME)); |
7d2a0492 | 234 | } |
235 | } | |
bf6c37c7 | 236 | |
3406acde | 237 | // Compare the action of this node against the fullmeurl |
bf6c37c7 | 238 | if ($this->action instanceof moodle_url && $this->action->compare(self::$fullmeurl, $strength)) { |
7d2a0492 | 239 | $this->make_active(); |
240 | return true; | |
7d2a0492 | 241 | } |
242 | return false; | |
243 | } | |
3406acde | 244 | |
99061152 DW |
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 | ||
99061152 DW |
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 | ||
7d2a0492 | 283 | /** |
d0cfbab3 SH |
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. | |
7d2a0492 | 289 | * |
3406acde | 290 | * @param moodle_url $url The url to use for the fullmeurl. |
61bb8c93 | 291 | * @param bool $loadadmintree use true if the URL point to administration tree |
3406acde | 292 | */ |
61bb8c93 | 293 | public static function override_active_url(moodle_url $url, $loadadmintree = false) { |
633c0843 TH |
294 | // Clone the URL, in case the calling script changes their URL later. |
295 | self::$fullmeurl = new moodle_url($url); | |
61bb8c93 PS |
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; | |
3406acde SH |
310 | } |
311 | ||
312 | /** | |
188a8127 | 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.) | |
3406acde SH |
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 | |
7d2a0492 | 322 | */ |
188a8127 | 323 | public static function create($text, $action=null, $type=self::TYPE_CUSTOM, |
324 | $shorttext=null, $key=null, pix_icon $icon=null) { | |
3406acde SH |
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 | |
7d2a0492 | 331 | if ($action!==null) { |
332 | $itemarray['action'] = $action; | |
333 | } | |
3406acde | 334 | // Set the shorttext if one was provided |
7d2a0492 | 335 | if ($shorttext!==null) { |
336 | $itemarray['shorttext'] = $shorttext; | |
337 | } | |
3406acde | 338 | // Set the icon if one was provided |
7d2a0492 | 339 | if ($icon!==null) { |
340 | $itemarray['icon'] = $icon; | |
341 | } | |
3406acde | 342 | // Set the key |
7d2a0492 | 343 | $itemarray['key'] = $key; |
188a8127 | 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 | |
31fde54f | 371 | * @param string $beforekey |
188a8127 | 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 | } | |
3406acde | 379 | // Set the parent to this node |
d38a4849 | 380 | $childnode->set_parent($this); |
188a8127 | 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 | ||
3406acde | 387 | // Add the child using the navigation_node_collections add method |
188a8127 | 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; | |
5ab32c11 RT |
393 | if (($type == self::TYPE_CATEGORY) || (isloggedin() && ($type == self::TYPE_COURSE)) || ($type == self::TYPE_MY_CATEGORY) || |
394 | ($type === self::TYPE_SITE_ADMIN)) { | |
3406acde | 395 | $node->nodetype = self::NODETYPE_BRANCH; |
7d2a0492 | 396 | } |
3406acde | 397 | // If this node is hidden mark it's children as hidden also |
7d2a0492 | 398 | if ($this->hidden) { |
3406acde | 399 | $node->hidden = true; |
7d2a0492 | 400 | } |
188a8127 | 401 | // Return added node (reference returned by $this->children->add() |
3406acde | 402 | return $node; |
7d2a0492 | 403 | } |
404 | ||
0c2f94e0 TH |
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 | ||
7d2a0492 | 413 | /** |
3406acde | 414 | * Searches for a node of the given type with the given key. |
7d2a0492 | 415 | * |
3406acde SH |
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 | |
7d2a0492 | 423 | */ |
3406acde SH |
424 | public function find($key, $type) { |
425 | return $this->children->find($key, $type); | |
426 | } | |
427 | ||
99061152 DW |
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. | |
c9d6a047 | 432 | * @param boolean $showdivider Show a divider before the first node. |
9df03b19 | 433 | * @param string $label A label for the collection of navigation links. |
99061152 | 434 | */ |
9df03b19 | 435 | public function build_flat_navigation_list(flat_navigation $nodes, $showdivider = false, $label = '') { |
99061152 DW |
436 | if ($this->showinflatnavigation) { |
437 | $indent = 0; | |
e53a38c6 | 438 | if ($this->type == self::TYPE_COURSE || $this->key === self::COURSE_INDEX_PAGE) { |
99061152 DW |
439 | $indent = 1; |
440 | } | |
c9d6a047 | 441 | $flat = new flat_navigation_node($this, $indent); |
9df03b19 | 442 | $flat->set_showdivider($showdivider, $label); |
c9d6a047 | 443 | $nodes->add($flat); |
99061152 DW |
444 | } |
445 | foreach ($this->children as $child) { | |
c9d6a047 | 446 | $child->build_flat_navigation_list($nodes, false); |
99061152 DW |
447 | } |
448 | } | |
449 | ||
3406acde | 450 | /** |
31fde54f | 451 | * Get the child of this node that has the given key + (optional) type. |
3406acde | 452 | * |
f495187d | 453 | * If you are looking for a node and want to search all children + their children |
3406acde SH |
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() { | |
58b602da | 479 | return ($this->nodetype === navigation_node::NODETYPE_BRANCH || $this->children->count()>0 || $this->isexpandable); |
3406acde SH |
480 | } |
481 | ||
482 | /** | |
483 | * Marks this node as active and forces it open. | |
d0cfbab3 SH |
484 | * |
485 | * Important: If you are here because you need to mark a node active to get | |
93123b17 | 486 | * the navigation to do what you want have you looked at {@link navigation_node::override_active_url()}? |
d0cfbab3 SH |
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. | |
3406acde SH |
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(); | |
7d2a0492 | 508 | } |
509 | } | |
510 | ||
511 | /** | |
3406acde SH |
512 | * Forces this node to be open and at the same time forces open all |
513 | * parents until the root node. | |
117bd748 | 514 | * |
3406acde SH |
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 | |
7d2a0492 | 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 | /** | |
3406acde | 538 | * Removes a CSS class from this node. |
7d2a0492 | 539 | * |
540 | * @param string $class | |
3406acde | 541 | * @return bool True if the class was successfully removed. |
7d2a0492 | 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) { | |
d4737c21 | 547 | // Remove the class' array element. |
7d2a0492 | 548 | unset($this->classes[$key]); |
d4737c21 AB |
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 | ||
7d2a0492 | 552 | return true; |
553 | } | |
554 | } | |
555 | return false; | |
556 | } | |
557 | ||
558 | /** | |
3406acde SH |
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. | |
435a512e | 578 | * |
3406acde | 579 | * Recursive. |
7d2a0492 | 580 | * |
3406acde | 581 | * @return bool |
7d2a0492 | 582 | */ |
3406acde SH |
583 | public function contains_active_node() { |
584 | if ($this->isactive) { | |
7d2a0492 | 585 | return true; |
586 | } else { | |
3406acde SH |
587 | foreach ($this->children as $child) { |
588 | if ($child->isactive || $child->contains_active_node()) { | |
589 | return true; | |
590 | } | |
591 | } | |
7d2a0492 | 592 | } |
3406acde | 593 | return false; |
7d2a0492 | 594 | } |
595 | ||
aad8c716 DW |
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 | ||
7d2a0492 | 616 | /** |
3406acde SH |
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. | |
7d2a0492 | 621 | * |
3406acde SH |
622 | * Recursive. |
623 | * | |
624 | * @return navigation_node|false | |
7d2a0492 | 625 | */ |
3406acde SH |
626 | public function find_active_node() { |
627 | if ($this->isactive) { | |
628 | return $this; | |
629 | } else { | |
7d2a0492 | 630 | foreach ($this->children as &$child) { |
3406acde SH |
631 | $outcome = $child->find_active_node(); |
632 | if ($outcome !== false) { | |
633 | return $outcome; | |
7d2a0492 | 634 | } |
635 | } | |
7d2a0492 | 636 | } |
3406acde | 637 | return false; |
7d2a0492 | 638 | } |
639 | ||
7c4efe3b SH |
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 | ||
7d2a0492 | 658 | /** |
3406acde | 659 | * Gets the content for this node. |
7d2a0492 | 660 | * |
3406acde SH |
661 | * @param bool $shorttext If true shorttext is used rather than the normal text |
662 | * @return string | |
7d2a0492 | 663 | */ |
3406acde | 664 | public function get_content($shorttext=false) { |
7d2a0492 | 665 | if ($shorttext && $this->shorttext!==null) { |
3406acde | 666 | return format_string($this->shorttext); |
7d2a0492 | 667 | } else { |
3406acde | 668 | return format_string($this->text); |
1c4eef57 | 669 | } |
3406acde | 670 | } |
1c4eef57 | 671 | |
3406acde SH |
672 | /** |
673 | * Gets the title to use for this node. | |
435a512e | 674 | * |
3406acde SH |
675 | * @return string |
676 | */ | |
677 | public function get_title() { | |
bbfa9be0 | 678 | if ($this->forcetitle || $this->action != null){ |
3406acde SH |
679 | return $this->title; |
680 | } else { | |
9bf16314 PS |
681 | return ''; |
682 | } | |
7d2a0492 | 683 | } |
117bd748 | 684 | |
d893a411 DW |
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 | ||
8665cc33 FS |
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 | ||
7d2a0492 | 703 | /** |
3406acde | 704 | * Gets the CSS class to add to this node to describe its type |
435a512e | 705 | * |
7d2a0492 | 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 | /** | |
3406acde | 716 | * Finds all nodes that are expandable by AJAX |
7d2a0492 | 717 | * |
3406acde | 718 | * @param array $expandable An array by reference to populate with expandable nodes. |
7d2a0492 | 719 | */ |
3406acde | 720 | public function find_expandable(array &$expandable) { |
3406acde | 721 | foreach ($this->children as &$child) { |
58b602da | 722 | if ($child->display && $child->has_children() && $child->children->count() == 0) { |
09da042b | 723 | $child->id = 'expandable_branch_'.$child->type.'_'.clean_param($child->key, PARAM_ALPHANUMEXT); |
3406acde | 724 | $this->add_class('canexpand'); |
f8895446 | 725 | $child->requiresajaxloading = true; |
8ad24c1a | 726 | $expandable[] = array('id' => $child->id, 'key' => $child->key, 'type' => $child->type); |
7d2a0492 | 727 | } |
3406acde | 728 | $child->find_expandable($expandable); |
7d2a0492 | 729 | } |
7d2a0492 | 730 | } |
88f77c3c | 731 | |
4766a50c SH |
732 | /** |
733 | * Finds all nodes of a given type (recursive) | |
734 | * | |
31fde54f | 735 | * @param int $type One of navigation_node::TYPE_* |
4766a50c SH |
736 | * @return array |
737 | */ | |
88f77c3c SH |
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 | } | |
56ed242b SH |
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 | } | |
d38a4849 SH |
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 | } | |
ba7b1ca5 SH |
807 | |
808 | /** | |
809 | * Hides the node and any children it has. | |
810 | * | |
5bcfd504 | 811 | * @since Moodle 2.5 |
8718ffba SH |
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 | } | |
ba7b1ca5 SH |
824 | } |
825 | } | |
826 | } | |
88e709ae DW |
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 | } | |
3a8cf5e6 AN |
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 | } | |
3406acde | 879 | } |
7d2a0492 | 880 | |
3406acde SH |
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 | * | |
31fde54f AG |
892 | * @package core |
893 | * @category navigation | |
3406acde SH |
894 | * @copyright 2010 Sam Hemelryk |
895 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | |
896 | */ | |
78da366b | 897 | class navigation_node_collection implements IteratorAggregate, Countable { |
7d2a0492 | 898 | /** |
3406acde SH |
899 | * A multidimensional array to where the first key is the type and the second |
900 | * key is the nodes key. | |
901 | * @var array | |
7d2a0492 | 902 | */ |
3406acde | 903 | protected $collection = array(); |
7d2a0492 | 904 | /** |
3406acde SH |
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 | |
7d2a0492 | 908 | */ |
3406acde | 909 | protected $orderedcollection = array(); |
da3ab9c4 | 910 | /** |
3406acde SH |
911 | * A reference to the last node that was added to the collection |
912 | * @var navigation_node | |
da3ab9c4 | 913 | */ |
3406acde | 914 | protected $last = null; |
6644741d | 915 | /** |
3406acde SH |
916 | * The total number of items added to this array. |
917 | * @var int | |
6644741d | 918 | */ |
3406acde | 919 | protected $count = 0; |
0c2f94e0 | 920 | |
9df03b19 DW |
921 | /** |
922 | * Label for collection of nodes. | |
923 | * @var string | |
924 | */ | |
925 | protected $collectionlabel = ''; | |
926 | ||
7d2a0492 | 927 | /** |
3406acde | 928 | * Adds a navigation node to the collection |
7d2a0492 | 929 | * |
188a8127 | 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 | |
7d2a0492 | 934 | */ |
188a8127 | 935 | public function add(navigation_node $node, $beforekey=null) { |
3406acde SH |
936 | global $CFG; |
937 | $key = $node->key; | |
938 | $type = $node->type; | |
188a8127 | 939 | |
3406acde SH |
940 | // First check we have a 2nd dimension for this type |
941 | if (!array_key_exists($type, $this->orderedcollection)) { | |
942 | $this->orderedcollection[$type] = array(); | |
7d2a0492 | 943 | } |
3406acde SH |
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); | |
7d2a0492 | 947 | } |
188a8127 | 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) { | |
188a8127 | 961 | debugging('Navigation node add_before: Reference node not found ' . $beforekey . |
0c2f94e0 | 962 | ', options: ' . implode(' ', $this->get_key_list()), DEBUG_DEVELOPER); |
188a8127 | 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) | |
3406acde | 968 | $this->orderedcollection[$type][$key] = $node; |
188a8127 | 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 | } | |
3406acde | 976 | // Add a reference to the node to the progressive collection. |
082513ba | 977 | $this->collection[$newindex] = $this->orderedcollection[$type][$key]; |
3406acde | 978 | // Update the last property to a reference to this new node. |
082513ba | 979 | $this->last = $this->orderedcollection[$type][$key]; |
5436561c | 980 | |
188a8127 | 981 | // Reorder the array by index if needed |
982 | if (!$last) { | |
983 | ksort($this->collection); | |
188a8127 | 984 | } |
3406acde SH |
985 | $this->count++; |
986 | // Return the reference to the now added node | |
188a8127 | 987 | return $node; |
7d2a0492 | 988 | } |
989 | ||
0c2f94e0 TH |
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 | ||
9df03b19 DW |
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 | ||
7d2a0492 | 1020 | /** |
3406acde | 1021 | * Fetches a node from this collection. |
7d2a0492 | 1022 | * |
3406acde SH |
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 | |
7d2a0492 | 1026 | */ |
3406acde SH |
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; | |
7d2a0492 | 1038 | } |
1039 | } | |
1040 | } | |
1041 | return false; | |
1042 | } | |
0c2f94e0 | 1043 | |
7d2a0492 | 1044 | /** |
3406acde | 1045 | * Searches for a node with matching key and type. |
7d2a0492 | 1046 | * |
3406acde SH |
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. | |
7d2a0492 | 1049 | * |
3406acde | 1050 | * Recursive. |
7d2a0492 | 1051 | * |
3406acde SH |
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 | |
7d2a0492 | 1055 | */ |
3406acde SH |
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) { | |
d9219fc9 | 1063 | if ($node->key === $key && ($type === null || $type === $node->type)) { |
3406acde | 1064 | return $node; |
d926f4b1 | 1065 | } |
3406acde SH |
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; | |
d926f4b1 | 1072 | } |
7d2a0492 | 1073 | } |
1074 | } | |
1075 | return false; | |
1076 | } | |
1077 | ||
d926f4b1 | 1078 | /** |
3406acde | 1079 | * Fetches the last node that was added to this collection |
435a512e | 1080 | * |
3406acde | 1081 | * @return navigation_node |
d926f4b1 | 1082 | */ |
3406acde SH |
1083 | public function last() { |
1084 | return $this->last; | |
1085 | } | |
0c2f94e0 | 1086 | |
3406acde SH |
1087 | /** |
1088 | * Fetches all nodes of a given type from this collection | |
31fde54f AG |
1089 | * |
1090 | * @param string|int $type node type being searched for. | |
1091 | * @return array ordered collection | |
3406acde SH |
1092 | */ |
1093 | public function type($type) { | |
1094 | if (!array_key_exists($type, $this->orderedcollection)) { | |
1095 | $this->orderedcollection[$type] = array(); | |
d926f4b1 | 1096 | } |
3406acde | 1097 | return $this->orderedcollection[$type]; |
d926f4b1 | 1098 | } |
7d2a0492 | 1099 | /** |
3406acde | 1100 | * Removes the node with the given key and type from the collection |
7d2a0492 | 1101 | * |
31fde54f | 1102 | * @param string|int $key The key of the node we want to find. |
3406acde SH |
1103 | * @param int $type |
1104 | * @return bool | |
7d2a0492 | 1105 | */ |
3406acde SH |
1106 | public function remove($key, $type=null) { |
1107 | $child = $this->get($key, $type); | |
1108 | if ($child !== false) { | |
1109 | foreach ($this->collection as $colkey => $node) { | |
eb9a3e93 | 1110 | if ($node->key === $key && (is_null($type) || $node->type == $type)) { |
3406acde | 1111 | unset($this->collection[$colkey]); |
5150c302 | 1112 | $this->collection = array_values($this->collection); |
3406acde SH |
1113 | break; |
1114 | } | |
7d2a0492 | 1115 | } |
3406acde SH |
1116 | unset($this->orderedcollection[$child->type][$child->key]); |
1117 | $this->count--; | |
1118 | return true; | |
7d2a0492 | 1119 | } |
3406acde | 1120 | return false; |
7d2a0492 | 1121 | } |
1122 | ||
9da1ec27 | 1123 | /** |
3406acde | 1124 | * Gets the number of nodes in this collection |
e26507b3 SH |
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 | * | |
3406acde | 1129 | * @return int |
7d2a0492 | 1130 | */ |
3406acde | 1131 | public function count() { |
e26507b3 | 1132 | return $this->count; |
7d2a0492 | 1133 | } |
7d2a0492 | 1134 | /** |
3406acde | 1135 | * Gets an array iterator for the collection. |
7d2a0492 | 1136 | * |
3406acde SH |
1137 | * This is required by the IteratorAggregator interface and is used by routines |
1138 | * such as the foreach loop. | |
7d2a0492 | 1139 | * |
3406acde | 1140 | * @return ArrayIterator |
7d2a0492 | 1141 | */ |
3406acde SH |
1142 | public function getIterator() { |
1143 | return new ArrayIterator($this->collection); | |
7d2a0492 | 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 | |
93123b17 | 1154 | * {@link lib/pagelib.php} {@link moodle_page::initialise_theme_and_output()} |
31fde54f | 1155 | * {@link lib/ajax/getnavbranch.php} Called by ajax |
7d2a0492 | 1156 | * |
31fde54f AG |
1157 | * @package core |
1158 | * @category navigation | |
7d2a0492 | 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 { | |
31fde54f | 1163 | /** @var moodle_page The Moodle page this navigation object belongs to. */ |
3406acde | 1164 | protected $page; |
31fde54f | 1165 | /** @var bool switch to let us know if the navigation object is initialised*/ |
7d2a0492 | 1166 | protected $initialised = false; |
31fde54f | 1167 | /** @var array An array of course information */ |
3406acde | 1168 | protected $mycourses = array(); |
aab6438c | 1169 | /** @var navigation_node[] An array for containing root navigation nodes */ |
3406acde | 1170 | protected $rootnodes = array(); |
31fde54f | 1171 | /** @var bool A switch for whether to show empty sections in the navigation */ |
fe221dd5 | 1172 | protected $showemptysections = true; |
31fde54f | 1173 | /** @var bool A switch for whether courses should be shown within categories on the navigation. */ |
9bf5af21 | 1174 | protected $showcategories = null; |
58b602da SH |
1175 | /** @var null@var bool A switch for whether or not to show categories in the my courses branch. */ |
1176 | protected $showmycategories = null; | |
31fde54f | 1177 | /** @var array An array of stdClasses for users that the navigation is extended for */ |
7a7e209d | 1178 | protected $extendforuser = array(); |
3406acde SH |
1179 | /** @var navigation_cache */ |
1180 | protected $cache; | |
31fde54f | 1181 | /** @var array An array of course ids that are present in the navigation */ |
3406acde | 1182 | protected $addedcourses = array(); |
8e8de15f SH |
1183 | /** @var bool */ |
1184 | protected $allcategoriesloaded = false; | |
31fde54f | 1185 | /** @var array An array of category ids that are included in the navigation */ |
e26507b3 | 1186 | protected $addedcategories = array(); |
31fde54f | 1187 | /** @var int expansion limit */ |
88f77c3c | 1188 | protected $expansionlimit = 0; |
31fde54f | 1189 | /** @var int userid to allow parent to see child's profile page navigation */ |
870815fa | 1190 | protected $useridtouseforparentchecks = 0; |
cbd063f4 SH |
1191 | /** @var cache_session A cache that stores information on expanded courses */ |
1192 | protected $cacheexpandcourse = null; | |
88f77c3c | 1193 | |
8e8de15f SH |
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 | ||
7d2a0492 | 1199 | /** |
3406acde SH |
1200 | * Constructs a new global navigation |
1201 | * | |
3406acde | 1202 | * @param moodle_page $page The page this navigation object belongs to |
7d2a0492 | 1203 | */ |
3406acde | 1204 | public function __construct(moodle_page $page) { |
4766a50c | 1205 | global $CFG, $SITE, $USER; |
3406acde | 1206 | |
7d2a0492 | 1207 | if (during_initial_install()) { |
3406acde | 1208 | return; |
7d2a0492 | 1209 | } |
3406acde | 1210 | |
4766a50c SH |
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'), | |
45ae297f KO |
1217 | 'action' => new moodle_url('/'), |
1218 | 'icon' => new pix_icon('i/home', '') | |
4766a50c SH |
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'), | |
64448e1e KO |
1226 | 'action' => new moodle_url('/my/'), |
1227 | 'icon' => new pix_icon('i/dashboard', '') | |
4766a50c | 1228 | ); |
dd8e5011 | 1229 | } |
4766a50c | 1230 | |
31fde54f | 1231 | // Use the parents constructor.... good good reuse |
3406acde | 1232 | parent::__construct($properties); |
99061152 | 1233 | $this->showinflatnavigation = true; |
3406acde SH |
1234 | |
1235 | // Initalise and set defaults | |
1236 | $this->page = $page; | |
7d2a0492 | 1237 | $this->forceopen = true; |
f5b5a822 | 1238 | $this->cache = new navigation_cache(NAVIGATION_CACHE_NAME); |
7d2a0492 | 1239 | } |
1240 | ||
b9bcad24 AB |
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 | * | |
5607036a | 1247 | * @param int $userid userid of profile page that parent wants to navigate around. |
b9bcad24 | 1248 | */ |
870815fa SH |
1249 | public function set_userid_for_parent_checks($userid) { |
1250 | $this->useridtouseforparentchecks = $userid; | |
b9bcad24 AB |
1251 | } |
1252 | ||
1253 | ||
7d2a0492 | 1254 | /** |
3406acde | 1255 | * Initialises the navigation object. |
7d2a0492 | 1256 | * |
3406acde SH |
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. | |
7d2a0492 | 1259 | * |
3406acde SH |
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. | |
7d2a0492 | 1263 | * |
3406acde | 1264 | * @return bool |
7d2a0492 | 1265 | */ |
3406acde | 1266 | public function initialise() { |
58b602da SH |
1267 | global $CFG, $SITE, $USER; |
1268 | // Check if it has already been initialised | |
7d2a0492 | 1269 | if ($this->initialised || during_initial_install()) { |
1270 | return true; | |
1271 | } | |
e2b436d0 | 1272 | $this->initialised = true; |
3406acde SH |
1273 | |
1274 | // Set up the five base root nodes. These are nodes where we will put our | |
1275 | // content and are as follows: | |
58b602da SH |
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. | |
3406acde | 1282 | $this->rootnodes = array(); |
4766a50c | 1283 | if (get_home_page() == HOMEPAGE_SITE) { |
3f9ccf85 | 1284 | // The home element should be my moodle because the root element is the site |
b9d4c7d3 | 1285 | if (isloggedin() && !isguestuser()) { // Makes no sense if you aren't logged in |
45ae297f | 1286 | $this->rootnodes['home'] = $this->add(get_string('myhome'), new moodle_url('/my/'), |
f2863769 | 1287 | self::TYPE_SETTING, null, 'myhome', new pix_icon('i/dashboard', '')); |
8a2ab5f7 | 1288 | $this->rootnodes['home']->showinflatnavigation = true; |
3f9ccf85 | 1289 | } |
4766a50c SH |
1290 | } else { |
1291 | // The home element should be the site because the root node is my moodle | |
64448e1e KO |
1292 | $this->rootnodes['home'] = $this->add(get_string('sitehome'), new moodle_url('/'), |
1293 | self::TYPE_SETTING, null, 'home', new pix_icon('i/home', '')); | |
8a2ab5f7 | 1294 | $this->rootnodes['home']->showinflatnavigation = true; |
ab6ec58a | 1295 | if (!empty($CFG->defaulthomepage) && ($CFG->defaulthomepage == HOMEPAGE_MY)) { |
4766a50c SH |
1296 | // We need to stop automatic redirection |
1297 | $this->rootnodes['home']->action->param('redirect', '0'); | |
1298 | } | |
1299 | } | |
58b602da | 1300 | $this->rootnodes['site'] = $this->add_course($SITE); |
4887d152 | 1301 | $this->rootnodes['myprofile'] = $this->add(get_string('profile'), null, self::TYPE_USER, null, 'myprofile'); |
6caf3f5c | 1302 | $this->rootnodes['currentcourse'] = $this->add(get_string('currentcourse'), null, self::TYPE_ROOTNODE, null, 'currentcourse'); |
64448e1e | 1303 | $this->rootnodes['mycourses'] = $this->add(get_string('mycourses'), null, self::TYPE_ROOTNODE, null, 'mycourses', new pix_icon('i/course', '')); |
58b602da | 1304 | $this->rootnodes['courses'] = $this->add(get_string('courses'), new moodle_url('/course/index.php'), self::TYPE_ROOTNODE, null, 'courses'); |
beff3806 MG |
1305 | if (!core_course_category::user_top()) { |
1306 | $this->rootnodes['courses']->hide(); | |
1307 | } | |
58b602da | 1308 | $this->rootnodes['users'] = $this->add(get_string('users'), null, self::TYPE_ROOTNODE, null, 'users'); |
3406acde | 1309 | |
80c69522 SH |
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 | ||
58b602da | 1315 | $course = $this->page->course; |
99061152 | 1316 | $this->load_courses_enrolled(); |
58b602da | 1317 | |
8e8de15f | 1318 | // $issite gets set to true if the current pages course is the sites frontpage course |
98556b23 | 1319 | $issite = ($this->page->course->id == $SITE->id); |
99061152 | 1320 | |
58b602da SH |
1321 | // Determine if the user is enrolled in any course. |
1322 | $enrolledinanycourse = enrol_user_sees_own_courses(); | |
4766a50c | 1323 | |
58b602da SH |
1324 | $this->rootnodes['currentcourse']->mainnavonly = true; |
1325 | if ($enrolledinanycourse) { | |
1326 | $this->rootnodes['mycourses']->isexpandable = true; | |
99061152 | 1327 | $this->rootnodes['mycourses']->showinflatnavigation = true; |
58b602da SH |
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; | |
8e8de15f | 1331 | } |
58b602da SH |
1332 | } else { |
1333 | $this->rootnodes['courses']->isexpandable = true; | |
3406acde | 1334 | } |
8a2ab5f7 | 1335 | $this->rootnodes['mycourses']->forceopen = true; |
f87ce4e9 | 1336 | |
cede87e0 SH |
1337 | $canviewcourseprofile = true; |
1338 | ||
6a16e136 SH |
1339 | // Next load context specific content into the navigation |
1340 | switch ($this->page->context->contextlevel) { | |
1341 | case CONTEXT_SYSTEM : | |
58b602da | 1342 | // Nothing left to do here I feel. |
6a16e136 SH |
1343 | break; |
1344 | case CONTEXT_COURSECAT : | |
58b602da SH |
1345 | // This is essential, we must load categories. |
1346 | $this->load_all_categories($this->page->context->instanceid, true); | |
6a16e136 SH |
1347 | break; |
1348 | case CONTEXT_BLOCK : | |
1349 | case CONTEXT_COURSE : | |
1350 | if ($issite) { | |
58b602da | 1351 | // Nothing left to do here. |
56c062a4 | 1352 | break; |
6a16e136 | 1353 | } |
b9bcad24 | 1354 | |
6caf3f5c | 1355 | // Load the course associated with the current page into the navigation. |
6caf3f5c | 1356 | $coursenode = $this->add_course($course, false, self::COURSE_CURRENT); |
6a16e136 SH |
1357 | // If the course wasn't added then don't try going any further. |
1358 | if (!$coursenode) { | |
1359 | $canviewcourseprofile = false; | |
1360 | break; | |
1361 | } | |
e26507b3 | 1362 | |
6a16e136 SH |
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 | |
8b456850 | 1367 | if (!can_access_course($course, null, '', true)) { |
cbd063f4 SH |
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 | } | |
58b602da | 1376 | // Very ugly hack - do not force "parents" to enrol into course their child is enrolled in, |
6a16e136 | 1377 | // this hack has been propagated from user/view.php to display the navigation node. (MDL-25805) |
58b602da | 1378 | if (!$this->current_user_is_parent_role()) { |
56c062a4 | 1379 | $coursenode->make_active(); |
6a16e136 SH |
1380 | $canviewcourseprofile = false; |
1381 | break; | |
1382 | } | |
8b456850 | 1383 | } else if ($coursenode->isexpandable === false) { |
cbd063f4 SH |
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 | ||
6a16e136 SH |
1392 | // Add the essentials such as reports etc... |
1393 | $this->add_course_essentials($coursenode, $course); | |
ee7084e9 MG |
1394 | // Extend course navigation with it's sections/activities |
1395 | $this->load_course_sections($course, $coursenode); | |
6a16e136 SH |
1396 | if (!$coursenode->contains_active_node() && !$coursenode->search_for_active_node()) { |
1397 | $coursenode->make_active(); | |
1398 | } | |
6caf3f5c | 1399 | |
6a16e136 SH |
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. | |
e010b850 | 1407 | $activitynode = $this->rootnodes['site']->find($this->page->cm->id, navigation_node::TYPE_ACTIVITY); |
6a16e136 SH |
1408 | if ($activitynode) { |
1409 | $this->load_activity($this->page->cm, $this->page->course, $activitynode); | |
56c062a4 | 1410 | } |
2027c10e | 1411 | break; |
6a16e136 | 1412 | } |
2027c10e | 1413 | |
6a16e136 SH |
1414 | $course = $this->page->course; |
1415 | $cm = $this->page->cm; | |
cede87e0 | 1416 | |
6a16e136 | 1417 | // Load the course associated with the page into the navigation |
58b602da | 1418 | $coursenode = $this->add_course($course, false, self::COURSE_CURRENT); |
56c062a4 | 1419 | |
6a16e136 SH |
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. | |
8b456850 | 1428 | if (!can_access_course($course, null, '', true)) { |
6a16e136 SH |
1429 | $coursenode->make_active(); |
1430 | $canviewcourseprofile = false; | |
1431 | break; | |
1432 | } | |
56c062a4 | 1433 | |
6a16e136 | 1434 | $this->add_course_essentials($coursenode, $course); |
56c062a4 | 1435 | |
6a16e136 | 1436 | // Load the course sections into the page |
e010b850 MG |
1437 | $this->load_course_sections($course, $coursenode, null, $cm); |
1438 | $activity = $coursenode->find($cm->id, navigation_node::TYPE_ACTIVITY); | |
069c29b9 DM |
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 | } | |
6a16e136 SH |
1447 | } |
1448 | break; | |
1449 | case CONTEXT_USER : | |
1450 | if ($issite) { | |
1451 | // The users profile information etc is already loaded | |
1452 | // for the front page. | |
96e78552 | 1453 | break; |
6a16e136 SH |
1454 | } |
1455 | $course = $this->page->course; | |
6a16e136 | 1456 | // Load the course associated with the user into the navigation |
58b602da | 1457 | $coursenode = $this->add_course($course, false, self::COURSE_CURRENT); |
2027c10e | 1458 | |
6a16e136 SH |
1459 | // If the course wasn't added then don't try going any further. |
1460 | if (!$coursenode) { | |
1461 | $canviewcourseprofile = false; | |
1462 | break; | |
1463 | } | |
2027c10e | 1464 | |
6a16e136 SH |
1465 | // If the user is not enrolled then we only want to show the |
1466 | // course node and not populate it. | |
8b456850 | 1467 | if (!can_access_course($course, null, '', true)) { |
6a16e136 SH |
1468 | $coursenode->make_active(); |
1469 | $canviewcourseprofile = false; | |
56c062a4 | 1470 | break; |
e52a5d3a | 1471 | } |
6a16e136 | 1472 | $this->add_course_essentials($coursenode, $course); |
ee7084e9 | 1473 | $this->load_course_sections($course, $coursenode); |
6a16e136 | 1474 | break; |
7d2a0492 | 1475 | } |
7a7e209d | 1476 | |
3406acde SH |
1477 | // Load for the current user |
1478 | $this->load_for_user(); | |
98556b23 | 1479 | if ($this->page->context->contextlevel >= CONTEXT_COURSE && $this->page->context->instanceid != $SITE->id && $canviewcourseprofile) { |
87c215de SH |
1480 | $this->load_for_user(null, true); |
1481 | } | |
7a7e209d SH |
1482 | // Load each extending user into the navigation. |
1483 | foreach ($this->extendforuser as $user) { | |
44303ca6 | 1484 | if ($user->id != $USER->id) { |
7a7e209d SH |
1485 | $this->load_for_user($user); |
1486 | } | |
1487 | } | |
7a7e209d | 1488 | |
a683da3c | 1489 | // Give the local plugins a chance to include some navigation if they want. |
ac8e6f8e | 1490 | $this->load_local_plugin_navigation(); |
a683da3c | 1491 | |
3406acde SH |
1492 | // Remove any empty root nodes |
1493 | foreach ($this->rootnodes as $node) { | |
4766a50c | 1494 | // Dont remove the home node |
75eb202e | 1495 | /** @var navigation_node $node */ |
f2863769 | 1496 | if (!in_array($node->key, ['home', 'myhome']) && !$node->has_children() && !$node->isactive) { |
3406acde SH |
1497 | $node->remove(); |
1498 | } | |
1499 | } | |
1500 | ||
7c4efe3b SH |
1501 | if (!$this->contains_active_node()) { |
1502 | $this->search_for_active_node(); | |
1503 | } | |
1504 | ||
3406acde | 1505 | // If the user is not logged in modify the navigation structure as detailed |
728ebac7 | 1506 | // in {@link http://docs.moodle.org/dev/Navigation_2.0_structure} |
3406acde SH |
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 | } | |
3406acde | 1518 | } |
7d2a0492 | 1519 | return true; |
1520 | } | |
9bf5af21 | 1521 | |
ac8e6f8e MG |
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 | ||
58b602da SH |
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 | * | |
5bcfd504 | 1538 | * @since Moodle 2.4 |
58b602da SH |
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 | ||
9bf5af21 | 1555 | /** |
31fde54f | 1556 | * Returns true if courses should be shown within categories on the navigation. |
9bf5af21 | 1557 | * |
58b602da | 1558 | * @param bool $ismycourse Set to true if you are calculating this for a course. |
9bf5af21 SH |
1559 | * @return bool |
1560 | */ | |
58b602da | 1561 | protected function show_categories($ismycourse = false) { |
9bf5af21 | 1562 | global $CFG, $DB; |
58b602da SH |
1563 | if ($ismycourse) { |
1564 | return $this->show_my_categories(); | |
1565 | } | |
9bf5af21 | 1566 | if ($this->showcategories === null) { |
58b602da SH |
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 | } | |
6a16e136 | 1573 | $this->showcategories = $show; |
9bf5af21 SH |
1574 | } |
1575 | return $this->showcategories; | |
1576 | } | |
1577 | ||
58b602da SH |
1578 | /** |
1579 | * Returns true if we should show categories in the My Courses branch. | |
1580 | * @return bool | |
1581 | */ | |
1582 | protected function show_my_categories() { | |
8aedc037 | 1583 | global $CFG; |
58b602da | 1584 | if ($this->showmycategories === null) { |
beff3806 | 1585 | $this->showmycategories = !empty($CFG->navshowmycoursecategories) && !core_course_category::is_simple_site(); |
58b602da SH |
1586 | } |
1587 | return $this->showmycategories; | |
1588 | } | |
1589 | ||
3406acde | 1590 | /** |
31fde54f | 1591 | * Loads the courses in Moodle into the navigation. |
3406acde | 1592 | * |
8e8de15f SH |
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 | |
3406acde | 1597 | */ |
8e8de15f | 1598 | protected function load_all_courses($categoryids = null) { |
98556b23 | 1599 | global $CFG, $DB, $SITE; |
4766a50c | 1600 | |
8e8de15f | 1601 | // Work out the limit of courses. |
4766a50c SH |
1602 | $limit = 20; |
1603 | if (!empty($CFG->navcourselimit)) { | |
1604 | $limit = $CFG->navcourselimit; | |
1605 | } | |
4766a50c | 1606 | |
8e8de15f SH |
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 | |
3406acde | 1616 | $coursenodes = array(); |
8e8de15f | 1617 | |
f7ee4baa SH |
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); | |
8e8de15f | 1626 | } |
f7ee4baa SH |
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 | } | |
8e8de15f | 1636 | |
f7ee4baa SH |
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. | |
f7ee4baa | 1662 | list($categoryids, $categoryparams) = $DB->get_in_or_equal($fullfetch, SQL_PARAMS_NAMED, 'lcategory'); |
2e4c0c91 FM |
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; | |
f7ee4baa SH |
1666 | $sql = "SELECT c.id, c.sortorder, c.visible, c.fullname, c.shortname, c.category $ccselect |
1667 | FROM {course} c | |
1668 | $ccjoin | |
fca2d3d7 | 1669 | WHERE c.category {$categoryids} |
f7ee4baa | 1670 | ORDER BY c.sortorder ASC"; |
fca2d3d7 | 1671 | $coursesrs = $DB->get_recordset_sql($sql, $categoryparams); |
f7ee4baa | 1672 | foreach ($coursesrs as $course) { |
fca2d3d7 PS |
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 | } | |
f7ee4baa | 1683 | if (!$this->can_add_more_courses_to_category($course->category)) { |
8e8de15f SH |
1684 | continue; |
1685 | } | |
db314f34 | 1686 | context_helper::preload_from_record($course); |
b0c6dc1c | 1687 | if (!$course->visible && !is_role_switched($course->id) && !has_capability('moodle/course:viewhiddencourses', context_course::instance($course->id))) { |
f7ee4baa | 1688 | continue; |
8e8de15f | 1689 | } |
f7ee4baa | 1690 | $coursenodes[$course->id] = $this->add_course($course); |
8e8de15f | 1691 | } |
f7ee4baa SH |
1692 | $coursesrs->close(); |
1693 | } | |
8e8de15f | 1694 | |
f7ee4baa SH |
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) { | |
2e4c0c91 FM |
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)"; | |
8e8de15f | 1701 | $sql = "SELECT c.id, c.sortorder, c.visible, c.fullname, c.shortname, c.category $ccselect |
f7ee4baa SH |
1702 | FROM {course} c |
1703 | $ccjoin | |
fca2d3d7 | 1704 | WHERE c.category = :categoryid |
f7ee4baa | 1705 | ORDER BY c.sortorder ASC"; |
2e4c0c91 | 1706 | $courseparams = array('categoryid' => $categoryid, 'contextlevel' => CONTEXT_COURSE); |
f7ee4baa | 1707 | $coursesrs = $DB->get_recordset_sql($sql, $courseparams, 0, $limit * 5); |
8e8de15f | 1708 | foreach ($coursesrs as $course) { |
fca2d3d7 PS |
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 | } | |
8e8de15f | 1720 | if (!$this->can_add_more_courses_to_category($course->category)) { |
f7ee4baa | 1721 | break; |
8e8de15f | 1722 | } |
db314f34 | 1723 | context_helper::preload_from_record($course); |
b0c6dc1c | 1724 | if (!$course->visible && !is_role_switched($course->id) && !has_capability('moodle/course:viewhiddencourses', context_course::instance($course->id))) { |
8e8de15f SH |
1725 | continue; |
1726 | } | |
1727 | $coursenodes[$course->id] = $this->add_course($course); | |
1728 | } | |
1729 | $coursesrs->close(); | |
1730 | } | |
8e8de15f | 1731 | } |
8e8de15f | 1732 | } else { |
8e8de15f | 1733 | // Prepare the SQL to load the courses and their contexts |
f7ee4baa | 1734 | list($courseids, $courseparams) = $DB->get_in_or_equal(array_keys($this->addedcourses), SQL_PARAMS_NAMED, 'lc', false); |
2e4c0c91 FM |
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; | |
8e8de15f | 1738 | $sql = "SELECT c.id, c.sortorder, c.visible, c.fullname, c.shortname, c.category $ccselect |
f7ee4baa SH |
1739 | FROM {course} c |
1740 | $ccjoin | |
1741 | WHERE c.id {$courseids} | |
1742 | ORDER BY c.sortorder ASC"; | |
8e8de15f SH |
1743 | $coursesrs = $DB->get_recordset_sql($sql, $courseparams); |
1744 | foreach ($coursesrs as $course) { | |
fca2d3d7 PS |
1745 | if ($course->id == $SITE->id) { |
1746 | // frotpage is not wanted here | |
1747 | continue; | |
1748 | } | |
6caf3f5c ARN |
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 | } | |
db314f34 | 1753 | context_helper::preload_from_record($course); |
b0c6dc1c | 1754 | if (!$course->visible && !is_role_switched($course->id) && !has_capability('moodle/course:viewhiddencourses', context_course::instance($course->id))) { |
f7ee4baa SH |
1755 | continue; |
1756 | } | |
8e8de15f | 1757 | $coursenodes[$course->id] = $this->add_course($course); |
f7ee4baa SH |
1758 | if (count($coursenodes) >= $limit) { |
1759 | break; | |
1760 | } | |
8e8de15f SH |
1761 | } |
1762 | $coursesrs->close(); | |
3406acde | 1763 | } |
f7ee4baa | 1764 | |
3406acde SH |
1765 | return $coursenodes; |
1766 | } | |
1767 | ||
8e8de15f SH |
1768 | /** |
1769 | * Returns true if more courses can be added to the provided category. | |
1770 | * | |
98556b23 SH |
1771 | * @param int|navigation_node|stdClass $category |
1772 | * @return bool | |
8e8de15f SH |
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) { | |
db47f68f | 1786 | if (($category->type != self::TYPE_CATEGORY) || ($category->type != self::TYPE_MY_CATEGORY)) { |
8e8de15f SH |
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 | } | |
d4bb6462 | 1793 | return ($coursecount <= $limit); |
8e8de15f SH |
1794 | } |
1795 | ||
4766a50c SH |
1796 | /** |
1797 | * Loads all categories (top level or if an id is specified for that category) | |
1798 | * | |
e26507b3 SH |
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. | |
31fde54f | 1802 | * @return navigation_node|void returns a navigation node if a category has been loaded. |
4766a50c | 1803 | */ |
8e8de15f | 1804 | protected function load_all_categories($categoryid = self::LOAD_ROOT_CATEGORIES, $showbasecategories = false) { |
58b602da | 1805 | global $CFG, $DB; |
e26507b3 SH |
1806 | |
1807 | // Check if this category has already been loaded | |
8e8de15f SH |
1808 | if ($this->allcategoriesloaded || ($categoryid < 1 && $this->is_category_fully_loaded($categoryid))) { |
1809 | return true; | |
e26507b3 SH |
1810 | } |
1811 | ||
176b75b5 SH |
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; | |
957983b7 | 1817 | $sqlorder = "ORDER BY cc.depth ASC, cc.sortorder ASC, cc.id ASC"; |
176b75b5 SH |
1818 | $params = array(); |
1819 | ||
8e8de15f SH |
1820 | $categoriestoload = array(); |
1821 | if ($categoryid == self::LOAD_ALL_CATEGORIES) { | |
176b75b5 SH |
1822 | // We are going to load all categories regardless... prepare to fire |
1823 | // on the database server! | |
8e8de15f | 1824 | } else if ($categoryid == self::LOAD_ROOT_CATEGORIES) { // can be 0 |
e26507b3 | 1825 | // We are going to load all of the first level categories (categories without parents) |
176b75b5 | 1826 | $sqlwhere .= " AND cc.parent = 0"; |
e26507b3 SH |
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 | |
85e59ac2 TP |
1830 | $addedcategories = $this->addedcategories; |
1831 | unset($addedcategories[$categoryid]); | |
6eb3da84 TP |
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 | } | |
e26507b3 SH |
1841 | } |
1842 | $params['categoryid'] = $categoryid; | |
4766a50c | 1843 | } else { |
e26507b3 SH |
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 | |
3992a46e | 1846 | $category = $DB->get_record('course_categories', array('id' => $categoryid), 'path', MUST_EXIST); |
8e8de15f SH |
1847 | $categoriestoload = explode('/', trim($category->path, '/')); |
1848 | list($select, $params) = $DB->get_in_or_equal($categoriestoload); | |
176b75b5 | 1849 | // We are going to use select twice so double the params |
4766a50c | 1850 | $params = array_merge($params, $params); |
d4bb6462 SH |
1851 | $basecategorysql = ($showbasecategories)?' OR cc.depth = 1':''; |
1852 | $sqlwhere .= " AND (cc.id {$select} OR cc.parent {$select}{$basecategorysql})"; | |
176b75b5 SH |
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 | } | |
4766a50c | 1872 | } |
176b75b5 | 1873 | $categoriesrs->close(); |
e26507b3 SH |
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]); | |
4766a50c | 1884 | } else { |
e26507b3 SH |
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]; | |
c4afcf84 SH |
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)) { | |
e26507b3 SH |
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. | |
5607036a | 1904 | throw new coding_exception('Category path order is incorrect and/or there are missing categories'); |
e26507b3 | 1905 | } |
4766a50c | 1906 | } |
4766a50c SH |
1907 | } |
1908 | } | |
e26507b3 SH |
1909 | // Remove the category from the categories array now that we know it has been added. |
1910 | unset($categories[$category->id]); | |
4766a50c | 1911 | } |
8e8de15f SH |
1912 | if ($categoryid === self::LOAD_ALL_CATEGORIES) { |
1913 | $this->allcategoriesloaded = true; | |
1914 | } | |
e26507b3 | 1915 | // Check if there are any categories to load. |
8e8de15f SH |
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 | } | |
4766a50c | 1926 | } |
58b602da SH |
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 | } | |
bb900d13 PS |
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)) { | |
30142c19 | 1948 | $url = new moodle_url('/course/index.php', array('categoryid' => $category->key)); |
bb900d13 PS |
1949 | $category->add(get_string('viewallcourses'), $url, self::TYPE_SETTING); |
1950 | } | |
58b602da SH |
1951 | } |
1952 | } | |
1953 | } | |
4766a50c SH |
1954 | } |
1955 | ||
1956 | /** | |
1957 | * Adds a structured category to the navigation in the correct order/place | |
1958 | * | |
db47f68f RT |
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. | |
4766a50c | 1963 | */ |
db47f68f | 1964 | protected function add_category(stdClass $category, navigation_node $parent, $nodetype = self::TYPE_CATEGORY) { |
e26507b3 | 1965 | if (array_key_exists($category->id, $this->addedcategories)) { |
563329e8 | 1966 | return; |
e26507b3 | 1967 | } |
beff3806 MG |
1968 | $canview = core_course_category::can_view_category($category); |
1969 | $url = $canview ? new moodle_url('/course/index.php', array('categoryid' => $category->id)) : null; | |
b0c6dc1c | 1970 | $context = context_coursecat::instance($category->id); |
beff3806 MG |
1971 | $categoryname = $canview ? format_string($category->name, true, array('context' => $context)) : |
1972 | get_string('categoryhidden'); | |
db47f68f | 1973 | $categorynode = $parent->add($categoryname, $url, $nodetype, $categoryname, $category->id); |
beff3806 MG |
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; | |
4766a50c | 1980 | } |
082513ba | 1981 | $this->addedcategories[$category->id] = $categorynode; |
4766a50c SH |
1982 | } |
1983 | ||
3406acde SH |
1984 | /** |
1985 | * Loads the given course into the navigation | |
7d2a0492 | 1986 | * |
3406acde SH |
1987 | * @param stdClass $course |
1988 | * @return navigation_node | |
1989 | */ | |
1990 | protected function load_course(stdClass $course) { | |
98556b23 SH |
1991 | global $SITE; |
1992 | if ($course->id == $SITE->id) { | |
80c69522 | 1993 | // This is always loaded during initialisation |
e26507b3 SH |
1994 | return $this->rootnodes['site']; |
1995 | } else if (array_key_exists($course->id, $this->addedcourses)) { | |
80c69522 | 1996 | // The course has already been loaded so return a reference |
e26507b3 | 1997 | return $this->addedcourses[$course->id]; |
3406acde | 1998 | } else { |
80c69522 | 1999 | // Add the course |
e26507b3 | 2000 | return $this->add_course($course); |
3406acde | 2001 | } |
3406acde SH |
2002 | } |
2003 | ||
2004 | /** | |
2005 | * Loads all of the courses section into the navigation. | |
2006 | * | |
e010b850 MG |
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. | |
3406acde | 2011 | * |
e010b850 | 2012 | * By default course formats call the method {@link global_navigation::load_generic_course_sections()} |
3406acde | 2013 | * |
3406acde SH |
2014 | * @param stdClass $course Database record for the course |
2015 | * @param navigation_node $coursenode The course node within the navigation | |
e010b850 MG |
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() ) | |
3406acde | 2019 | */ |
e010b850 MG |
2020 | protected function load_course_sections(stdClass $course, navigation_node $coursenode, $sectionnum = null, $cm = null) { |
2021 | global $CFG, $SITE; | |
ee7084e9 | 2022 | require_once($CFG->dirroot.'/course/lib.php'); |
e010b850 MG |
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 | } | |
3406acde | 2037 | |
e26507b3 SH |
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 | ||
c18facf2 | 2050 | $modinfo = get_fast_modinfo($course); |
b5cf83f0 | 2051 | $sections = $modinfo->get_section_info_all(); |
850acb35 MG |
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); | |
b5cf83f0 | 2057 | } |
850acb35 | 2058 | |
c18facf2 | 2059 | $activities = array(); |
e26507b3 | 2060 | |
c18facf2 | 2061 | foreach ($sections as $key => $section) { |
2d9c0d11 | 2062 | // Clone and unset summary to prevent $SESSION bloat (MDL-31802). |
51591b2c AO |
2063 | $sections[$key] = clone($section); |
2064 | unset($sections[$key]->summary); | |
c18facf2 SH |
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]; | |
c18facf2 SH |
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; | |
73ee2fda DP |
2081 | $activity->onclick = $cm->onclick; |
2082 | $url = $cm->url; | |
c18facf2 SH |
2083 | if (!$url) { |
2084 | $activity->url = null; | |
2085 | $activity->display = false; | |
2086 | } else { | |
73ee2fda | 2087 | $activity->url = $url->out(); |
8341055e | 2088 | $activity->display = $cm->is_visible_on_course_page() ? true : false; |
c18facf2 SH |
2089 | if (self::module_extends_navigation($cm->modname)) { |
2090 | $activity->nodetype = navigation_node::NODETYPE_BRANCH; | |
d67e4821 | 2091 | } |
e26507b3 | 2092 | } |
c18facf2 SH |
2093 | $activities[$cmid] = $activity; |
2094 | if ($activity->display) { | |
2095 | $sections[$key]->hasactivites = true; | |
2096 | } | |
e26507b3 | 2097 | } |
e26507b3 | 2098 | } |
c18facf2 | 2099 | |
e26507b3 SH |
2100 | return array($sections, $activities); |
2101 | } | |
2102 | ||
3406acde SH |
2103 | /** |
2104 | * Generically loads the course sections into the course's navigation. | |
2105 | * | |
2106 | * @param stdClass $course | |
2107 | * @param navigation_node $coursenode | |
3406acde SH |
2108 | * @return array An array of course section nodes |
2109 | */ | |
ee7084e9 | 2110 | public function load_generic_course_sections(stdClass $course, navigation_node $coursenode) { |
98556b23 | 2111 | global $CFG, $DB, $USER, $SITE; |
df997f84 | 2112 | require_once($CFG->dirroot.'/course/lib.php'); |
435a512e | 2113 | |
e26507b3 | 2114 | list($sections, $activities) = $this->generate_sections_and_activities($course); |
0f4ab67d | 2115 | |
7487c856 | 2116 | $navigationsections = array(); |
e26507b3 | 2117 | foreach ($sections as $sectionid => $section) { |
7487c856 | 2118 | $section = clone($section); |
98556b23 | 2119 | if ($course->id == $SITE->id) { |
e26507b3 | 2120 | $this->load_section_activities($coursenode, $section->section, $activities); |
3406acde | 2121 | } else { |
ce4dfd27 | 2122 | if (!$section->uservisible || (!$this->showemptysections && |
d67e4821 | 2123 | !$section->hasactivites && $this->includesectionnum !== $section->section)) { |
3406acde SH |
2124 | continue; |
2125 | } | |
ce4dfd27 | 2126 | |
ee7084e9 MG |
2127 | $sectionname = get_section_name($course, $section); |
2128 | $url = course_get_url($course, $section->section, array('navigation' => true)); | |
ad470097 | 2129 | |
64448e1e KO |
2130 | $sectionnode = $coursenode->add($sectionname, $url, navigation_node::TYPE_SECTION, |
2131 | null, $section->id, new pix_icon('i/section', '')); | |
3406acde | 2132 | $sectionnode->nodetype = navigation_node::NODETYPE_BRANCH; |
ce4dfd27 | 2133 | $sectionnode->hidden = (!$section->visible || !$section->available); |
e010b850 | 2134 | if ($this->includesectionnum !== false && $this->includesectionnum == $section->section) { |
e26507b3 | 2135 | $this->load_section_activities($sectionnode, $section->section, $activities); |
3406acde SH |
2136 | } |
2137 | $section->sectionnode = $sectionnode; | |
7487c856 | 2138 | $navigationsections[$sectionid] = $section; |
3406acde SH |
2139 | } |
2140 | } | |
7487c856 | 2141 | return $navigationsections; |
3406acde | 2142 | } |
e010b850 | 2143 | |
3406acde SH |
2144 | /** |
2145 | * Loads all of the activities for a section into the navigation structure. | |
2146 | * | |
2147 | * @param navigation_node $sectionnode | |
2148 | * @param int $sectionnumber | |
93123b17 | 2149 | * @param array $activities An array of activites as returned by {@link global_navigation::generate_sections_and_activities()} |
1580cfdb | 2150 | * @param stdClass $course The course object the section and activities relate to. |
3406acde SH |
2151 | * @return array Array of activity nodes |
2152 | */ | |
1580cfdb | 2153 | protected function load_section_activities(navigation_node $sectionnode, $sectionnumber, array $activities, $course = null) { |
98556b23 | 2154 | global $CFG, $SITE; |
dee1a0fd SH |
2155 | // A static counter for JS function naming |
2156 | static $legacyonclickcounter = 0; | |
3406acde | 2157 | |
e26507b3 | 2158 | $activitynodes = array(); |
4037098e SH |
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 | } | |
98556b23 | 2169 | $showactivities = ($courseid != $SITE->id || !empty($CFG->navshowfrontpagemods)); |
4037098e | 2170 | |
e26507b3 SH |
2171 | foreach ($activities as $activity) { |
2172 | if ($activity->section != $sectionnumber) { | |
3406acde SH |
2173 | continue; |
2174 | } | |
e26507b3 SH |
2175 | if ($activity->icon) { |
2176 | $icon = new pix_icon($activity->icon, get_string('modulename', $activity->modname), $activity->iconcomponent); | |
3406acde | 2177 | } else { |
e26507b3 | 2178 | $icon = new pix_icon('icon', get_string('modulename', $activity->modname), $activity->modname); |
3406acde | 2179 | } |
dee1a0fd SH |
2180 | |
2181 | // Prepare the default name and url for the node | |
b0c6dc1c | 2182 | $activityname = format_string($activity->name, true, array('context' => context_module::instance($activity->id))); |
dee1a0fd SH |
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) | |
e5ad278a | 2199 | $onclick = htmlspecialchars_decode($activity->onclick, ENT_QUOTES); |
dee1a0fd SH |
2200 | // Build the JS function the click event will call |
2201 | $jscode = "function {$functionname}(e) { $propogrationhandler $onclick }"; | |
c41883f0 | 2202 | $this->page->requires->js_amd_inline($jscode); |
dee1a0fd SH |
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); | |
e26507b3 SH |
2208 | $activitynode->title(get_string('modulename', $activity->modname)); |
2209 | $activitynode->hidden = $activity->hidden; | |
4037098e | 2210 | $activitynode->display = $showactivities && $activity->display; |
e26507b3 SH |
2211 | $activitynode->nodetype = $activity->nodetype; |
2212 | $activitynodes[$activity->id] = $activitynode; | |
3406acde SH |
2213 | } |
2214 | ||
e26507b3 | 2215 | return $activitynodes; |
3406acde | 2216 | } |
2a62743c PS |
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]; | |
2a62743c PS |
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 | } | |
73ee2fda | 2233 | $url = $cm->url; |
2a62743c PS |
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); | |
8341055e | 2237 | if (!$cm->is_visible_on_course_page()) { |
4fd2a16e MG |
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) { | |
0d8b6a69 | 2242 | // Don't show activities that don't have links! |
2a62743c | 2243 | $activitynode->display = false; |
e26507b3 | 2244 | } else if (self::module_extends_navigation($cm->modname)) { |
2a62743c PS |
2245 | $activitynode->nodetype = navigation_node::NODETYPE_BRANCH; |
2246 | } | |
2247 | return $activitynode; | |
2248 | } | |
3406acde SH |
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: | |
93123b17 EL |
2257 | * * {@link forum_extend_navigation()} |
2258 | * * {@link workshop_extend_navigation()} | |
3406acde | 2259 | * |
f0dcc212 | 2260 | * @param cm_info|stdClass $cm |
3406acde SH |
2261 | * @param stdClass $course |
2262 | * @param navigation_node $activity | |
2263 | * @return bool | |
2264 | */ | |
0d8b6a69 | 2265 | protected function load_activity($cm, stdClass $course, navigation_node $activity) { |
3406acde | 2266 | global $CFG, $DB; |
44303ca6 | 2267 | |
f0dcc212 SH |
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 | } | |
577c8964 | 2273 | $activity->nodetype = navigation_node::NODETYPE_LEAF; |
3406acde SH |
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); | |
3406acde SH |
2283 | } |
2284 | } | |
577c8964 MG |
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'); | |
6cdfe1a8 | 2290 | $gradingman = get_grading_manager($cm->context, 'mod_'.$cm->modname); |
577c8964 MG |
2291 | $gradingman->extend_navigation($this, $activity); |
2292 | } | |
2293 | ||
2294 | return $activity->has_children(); | |
3406acde SH |
2295 | } |
2296 | /** | |
4d00fded | 2297 | * Loads user specific information into the navigation in the appropriate place. |
3406acde SH |
2298 | * |
2299 | * If no user is provided the current user is assumed. | |
2300 | * | |
3406acde | 2301 | * @param stdClass $user |
4d00fded | 2302 | * @param bool $forceforcontext probably force something to be loaded somewhere (ask SamH if not sure what this means) |
3406acde | 2303 | * @return bool |
7a7e209d | 2304 | */ |
87c215de | 2305 | protected function load_for_user($user=null, $forceforcontext=false) { |
98556b23 | 2306 | global $DB, $CFG, $USER, $SITE; |
4f0c2d00 | 2307 | |
93b47710 MN |
2308 | require_once($CFG->dirroot . '/course/lib.php'); |
2309 | ||
7a7e209d SH |
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 | |
b9d4c7d3 | 2313 | if (!isloggedin() || isguestuser()) { |
7a7e209d SH |
2314 | return false; |
2315 | } | |
2316 | $user = $USER; | |
7a7e209d SH |
2317 | } else if (!is_object($user)) { |
2318 | // If the user is not an object then get them from the database | |
e141bc81 SH |
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); | |
7a7e209d | 2327 | } |
136ca7c8 SH |
2328 | |
2329 | $iscurrentuser = ($user->id == $USER->id); | |
2330 | ||
b0c6dc1c | 2331 | $usercontext = context_user::instance($user->id); |
7a7e209d SH |
2332 | |
2333 | // Get the course set against the page, by default this will be the site | |
3406acde SH |
2334 | $course = $this->page->course; |
2335 | $baseargs = array('id'=>$user->id); | |
98556b23 | 2336 | if ($course->id != $SITE->id && (!$iscurrentuser || $forceforcontext)) { |
58b602da | 2337 | $coursenode = $this->add_course($course, false, self::COURSE_CURRENT); |
7a7e209d | 2338 | $baseargs['course'] = $course->id; |
b0c6dc1c | 2339 | $coursecontext = context_course::instance($course->id); |
7a7e209d | 2340 | $issitecourse = false; |
7d2a0492 | 2341 | } else { |
7a7e209d | 2342 | // Load all categories and get the context for the system |
b0c6dc1c | 2343 | $coursecontext = context_system::instance(); |
7a7e209d SH |
2344 | $issitecourse = true; |
2345 | } | |
2346 | ||
2347 | // Create a node to add user information under. | |
f495187d AG |
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']; | |
93b47710 | 2356 | if (course_can_view_participants($coursecontext)) { |
45367bdf | 2357 | $usersnode->action = new moodle_url('/user/index.php', array('id' => $course->id)); |
f495187d AG |
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; | |
7a7e209d | 2368 | } |
f495187d | 2369 | // Add a branch for the current user. |
02e5a9d7 | 2370 | // Only reveal user details if $user is the current user, or a user to which the current user has access. |
01255549 JD |
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) { | |
02e5a9d7 JD |
2386 | $canseefullname = has_capability('moodle/site:viewfullnames', $coursecontext); |
2387 | $usernode = $usersnode->add(fullname($user, $canseefullname), $userviewurl, self::TYPE_USER, null, 'user' . $user->id); | |
01255549 JD |
2388 | } else { |
2389 | $usernode = $usersnode->add(get_string('user')); | |
02e5a9d7 | 2390 | } |
01255549 | 2391 | |
f495187d AG |
2392 | if ($this->page->context->contextlevel == CONTEXT_USER && $user->id == $this->page->context->instanceid) { |
2393 | $usernode->make_active(); | |
7a7e209d SH |
2394 | } |
2395 | ||
f495187d AG |
2396 | // Add user information to the participants or user node. |
2397 | if ($issitecourse) { | |
7a7e209d | 2398 | |
f495187d AG |
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)) { | |
45367bdf | 2404 | $usernode->add(get_string('viewprofile'), new moodle_url('/user/profile.php', $baseargs)); |
f495187d | 2405 | } else { |
45367bdf | 2406 | $usernode->add(get_string('viewprofile'), new moodle_url('/user/view.php', $baseargs)); |
f495187d | 2407 | } |
e26507b3 SH |
2408 | } |
2409 | ||
f495187d AG |
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', | |
45367bdf | 2417 | array_merge($baseargs, array('mode' => 'discussions')))); |
f495187d AG |
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 | } | |
c000545d | 2440 | } |
27bad0a6 SH |
2441 | } |
2442 | } | |
27bad0a6 | 2443 | |
f495187d | 2444 | // Add the messages link. |
4887d152 | 2445 | // It is context based so can appear in the user's profile and in course participants information. |
f495187d AG |
2446 | if (!empty($CFG->messaging)) { |
2447 | $messageargs = array('user1' => $USER->id); | |
2448 | if ($USER->id != $user->id) { | |
2449 | $messageargs['user2'] = $user->id; | |
2450 | } | |
45367bdf | 2451 | $url = new moodle_url('/message/index.php', $messageargs); |
f495187d | 2452 | $usernode->add(get_string('messages', 'message'), $url, self::TYPE_SETTING, null, 'messages'); |
5ac851fb | 2453 | } |
78765507 | 2454 | |
f495187d | 2455 | // Add the "My private files" link. |
4887d152 | 2456 | // This link doesn't have a unique display for course context so only display it under the user's profile. |
f495187d AG |
2457 | if ($issitecourse && $iscurrentuser && has_capability('moodle/user:manageownfiles', $usercontext)) { |
2458 | $url = new moodle_url('/user/files.php'); | |
27456a64 | 2459 | $usernode->add(get_string('privatefiles'), $url, self::TYPE_SETTING, null, 'privatefiles'); |
f495187d | 2460 | } |
27806552 | 2461 | |
f495187d AG |
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)) { | |
45367bdf | 2465 | $url = new moodle_url('/notes/index.php', array('user' => $user->id)); |
f495187d AG |
2466 | if ($coursecontext->instanceid != SITEID) { |
2467 | $url->param('course', $coursecontext->instanceid); | |
2468 | } | |
2469 | $usernode->add(get_string('notes', 'notes'), $url); | |
c9451960 | 2470 | } |
f495187d | 2471 | |
4887d152 | 2472 | // Show the grades node. |
f495187d AG |
2473 | if (($issitecourse && $iscurrentuser) || has_capability('moodle/user:viewdetails', $usercontext)) { |
2474 | require_once($CFG->dirroot . '/user/lib.php'); | |
4887d152 | 2475 | // Set the grades node to link to the "Grades" page. |
f495187d AG |
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) { | |
c78dbe03 | 2482 | $usernode->add(get_string('grades', 'grades'), $url, self::TYPE_SETTING, null, 'usergrades'); |
f495187d | 2483 | } else { |
4887d152 | 2484 | $usernode->add(get_string('grades', 'grades'), $url); |
f495187d | 2485 | } |
c9451960 | 2486 | } |
c9451960 | 2487 | |
f495187d AG |
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))) { | |
9acb8241 | 2494 | |
f495187d AG |
2495 | // Add view grade report is permitted. |
2496 | $reports = core_component::get_plugin_list('gradereport'); | |
45367bdf | 2497 | arsort($reports); // User is last, we want to test it first. |
9acb8241 | 2498 | |
35b9bca0 | 2499 | $userscourses = enrol_get_users_courses($user->id, false, '*'); |
f495187d | 2500 | $userscoursesnode = $usernode->add(get_string('courses')); |
69816a5c | 2501 | |
f495187d AG |
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', | |
45367bdf | 2513 | array('id' => $user->id, 'course' => $usercourse->id)), self::TYPE_CONTAINER); |
f495187d | 2514 | |
d430a7c3 | 2515 | $gradeavailable = has_capability('moodle/grade:view', $usercoursecontext); |
f495187d AG |
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)) { | |
45367bdf | 2519 | // Stop when the first visible plugin is found. |
f495187d AG |
2520 | $gradeavailable = true; |
2521 | break; | |
2522 | } | |
deaf04c7 | 2523 | } |
9acb8241 | 2524 | } |
9acb8241 | 2525 | |
f495187d | 2526 | if ($gradeavailable) { |
45367bdf | 2527 | $url = new moodle_url('/grade/report/index.php', array('id' => $usercourse->id)); |
f495187d AG |
2528 | $usercoursenode->add(get_string('grades'), $url, self::TYPE_SETTING, null, null, |
2529 | new pix_icon('i/grades', '')); | |
2530 | } | |
9acb8241 | 2531 | |
f495187d AG |
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)) { | |
45367bdf | 2535 | $url = new moodle_url('/notes/index.php', array('user' => $user->id, 'course' => $usercourse->id)); |
f495187d AG |
2536 | $usercoursenode->add(get_string('notes', 'notes'), $url, self::TYPE_SETTING); |
2537 | } | |
9acb8241 | 2538 | |
8b456850 | 2539 | if (can_access_course($usercourse, $user->id, '', true)) { |
f495187d | 2540 | $usercoursenode->add(get_string('entercourse'), new moodle_url('/course/view.php', |
45367bdf | 2541 | array('id' => $usercourse->id)), self::TYPE_SETTING, null, null, new pix_icon('i/course', '')); |
f495187d | 2542 | } |
9acb8241 | 2543 | |
f495187d | 2544 | $reporttab = $usercoursenode->add(get_string('activityreports')); |
4d00fded | 2545 | |
0b844a83 MG |
2546 | $reportfunctions = get_plugin_list_with_function('report', 'extend_navigation_user', 'lib.php'); |
2547 | foreach ($reportfunctions as $reportfunction) { | |
f495187d AG |
2548 | $reportfunction($reporttab, $user, $usercourse); |
2549 | } | |
4d00fded | 2550 | |
f495187d AG |
2551 | $reporttab->trim_if_empty(); |
2552 | } | |
9acb8241 | 2553 | } |
f495187d | 2554 | |
20dde5e6 JPG |
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 | } | |
7a7e209d SH |
2564 | } |
2565 | return true; | |
2566 | } | |
2567 | ||
2568 | /** | |
3406acde | 2569 | * This method simply checks to see if a given module can extend the navigation. |
7d2a0492 | 2570 | * |
31fde54f | 2571 | * @todo (MDL-25290) A shared caching solution should be used to save details on what extends navigation. |
e26507b3 | 2572 | * |
7d2a0492 | 2573 | * @param string $modname |
2574 | * @return bool | |
2575 | */ | |
e010b850 | 2576 | public static function module_extends_navigation($modname) { |
7d2a0492 | 2577 | global $CFG; |
e26507b3 SH |
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)); | |
7d2a0492 | 2586 | } |
2587 | } | |
e26507b3 | 2588 | return $extendingmodules[$modname]; |
7d2a0492 | 2589 | } |
2590 | /** | |
3406acde | 2591 | * Extends the navigation for the given user. |
435a512e | 2592 | * |
3406acde | 2593 | * @param stdClass $user A user from the database |
7d2a0492 | 2594 | */ |
3406acde SH |
2595 | public function extend_for_user($user) { |
2596 | $this->extendforuser[] = $user; | |
5d07e957 SH |
2597 | } |
2598 | ||
2599 | /** | |
2600 | * Returns all of the users the navigation is being extended for | |
2601 | * | |
31fde54f | 2602 | * @return array An array of extending users. |
5d07e957 SH |
2603 | */ |
2604 | public function get_extending_users() { | |
2605 | return $this->extendforuser; | |
7d2a0492 | 2606 | } |
7d2a0492 | 2607 | /** |
3406acde | 2608 | * Adds the given course to the navigation structure. |
7d2a0492 | 2609 | * |
3406acde | 2610 | * @param stdClass $course |
8e8de15f SH |
2611 | * @param bool $forcegeneric |
2612 | * @param bool $ismycourse | |
3406acde | 2613 | * @return navigation_node |
7d2a0492 | 2614 | */ |
6caf3f5c | 2615 | public function add_course(stdClass $course, $forcegeneric = false, $coursetype = self::COURSE_OTHER) { |
98556b23 | 2616 | global $CFG, $SITE; |
44303ca6 | 2617 | |
e26507b3 SH |
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 | ||
b0c6dc1c | 2623 | $coursecontext = context_course::instance($course->id); |
8ebbb06a | 2624 | |
beff3806 | 2625 | if ($coursetype != self::COURSE_MY && $coursetype != self::COURSE_CURRENT && $course->id != $SITE->id) { |
e26507b3 SH |
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 | |
beff3806 | 2628 | } else if (!core_course_category::can_view_course_info($course)) { |
e26507b3 | 2629 | return false; |
44303ca6 | 2630 | } |
7d2a0492 | 2631 | } |
7d2a0492 | 2632 | |
98556b23 | 2633 | $issite = ($course->id == $SITE->id); |
8ebbb06a | 2634 | $shortname = format_string($course->shortname, true, array('context' => $coursecontext)); |
d20337f1 SH |
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; | |
4766a50c | 2638 | |
99061152 DW |
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 | ||
cbd063f4 SH |
2647 | // Can the user expand the course to see its content. |
2648 | $canexpandcourse = true; | |
4766a50c | 2649 | if ($issite) { |
3406acde | 2650 | $parent = $this; |
4766a50c | 2651 | $url = null; |
016ba638 | 2652 | if (empty($CFG->usesitenameforsitepages)) { |
d20337f1 | 2653 | $coursename = get_string('sitepages'); |
016ba638 | 2654 | } |
6caf3f5c ARN |
2655 | } else if ($coursetype == self::COURSE_CURRENT) { |
2656 | $parent = $this->rootnodes['currentcourse']; | |
2657 | $url = new moodle_url('/course/view.php', array('id'=>$course->id)); | |
8b456850 | 2658 | $canexpandcourse = $this->can_expand_course($course); |
6caf3f5c | 2659 | } else if ($coursetype == self::COURSE_MY && !$forcegeneric) { |
db47f68f | 2660 | if (!empty($CFG->navshowmycoursecategories) && ($parent = $this->rootnodes['mycourses']->find($course->category, self::TYPE_MY_CATEGORY))) { |
b0712163 SH |
2661 | // Nothing to do here the above statement set $parent to the category within mycourses. |
2662 | } else { | |
2663 | $parent = $this->rootnodes['mycourses']; | |
2664 | } | |
3406acde | 2665 | $url = new moodle_url('/course/view.php', array('id'=>$course->id)); |
7a7e209d | 2666 | } else { |
3406acde | 2667 | $parent = $this->rootnodes['courses']; |
a6855934 | 2668 | $url = new moodle_url('/course/view.php', array('id'=>$course->id)); |
cbd063f4 SH |
2669 | // They can only expand the course if they can access it. |
2670 | $canexpandcourse = $this->can_expand_course($course); | |
58b602da SH |
2671 | if (!empty($course->category) && $this->show_categories($coursetype == self::COURSE_MY)) { |
2672 | if (!$this->is_category_fully_loaded($course->category)) { | |
8e8de15f | 2673 | // We need to load the category structure for this course |
80c69522 | 2674 | $this->load_all_categories($course->category, false); |
8e8de15f SH |
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 | } | |
3992a46e | 2682 | } |
4766a50c SH |
2683 | } |
2684 | } | |
2685 | ||
64448e1e | 2686 | $coursenode = $parent->add($coursename, $url, self::TYPE_COURSE, $shortname, $course->id, new pix_icon('i/course', '')); |
3dabb083 | 2687 | $coursenode->showinflatnavigation = $coursetype == self::COURSE_MY; |
d07373f2 | 2688 | |
3406acde | 2689 | $coursenode->hidden = (!$course->visible); |
a530cb93 | 2690 | $coursenode->title(format_string($course->fullname, true, array('context' => $coursecontext, 'escape' => false))); |
cbd063f4 SH |
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 | } | |
e26507b3 | 2699 | if (!$forcegeneric) { |
082513ba | 2700 | $this->addedcourses[$course->id] = $coursenode; |
e26507b3 | 2701 | } |
e26507b3 | 2702 | |
3406acde | 2703 | return $coursenode; |
7d2a0492 | 2704 | } |
8e8de15f | 2705 | |
cbd063f4 SH |
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) { | |
8b456850 | 2738 | $canexpand = isloggedin() && can_access_course($course, null, '', true); |
cbd063f4 SH |
2739 | $canexpand = (int)$canexpand; |
2740 | $cache->set($course->id, $canexpand); | |
2741 | } | |
2742 | return ($canexpand === 1); | |
2743 | } | |
2744 | ||
8e8de15f SH |
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 | ||
7d2a0492 | 2755 | /** |
3406acde | 2756 | * Adds essential course nodes to the navigation for the given course. |
7d2a0492 | 2757 | * |
3406acde | 2758 | * This method adds nodes such as reports, blogs and participants |
7d2a0492 | 2759 | * |
3406acde SH |
2760 | * @param navigation_node $coursenode |
2761 | * @param stdClass $course | |
31fde54f | 2762 | * @return bool returns true on successful addition of a node. |
7d2a0492 | 2763 | */ |
2027c10e | 2764 | public function add_course_essentials($coursenode, stdClass $course) { |
98556b23 | 2765 | global $CFG, $SITE; |
d6cd89b1 | 2766 | require_once($CFG->dirroot . '/course/lib.php'); |
7d2a0492 | 2767 | |
98556b23 | 2768 | if ($course->id == $SITE->id) { |
3406acde | 2769 | return $this->add_front_page_course_essentials($coursenode, $course); |
7d2a0492 | 2770 | } |
7d2a0492 | 2771 | |
2027c10e | 2772 | if ($coursenode == false || !($coursenode instanceof navigation_node) || $coursenode->get('participants', navigation_node::TYPE_CONTAINER)) { |
3406acde | 2773 | return true; |
7d2a0492 | 2774 | } |
7d2a0492 | 2775 | |
d430a7c3 | 2776 | $navoptions = course_get_user_navigation_options($this->page->context, $course); |
d6cd89b1 | 2777 | |
3406acde | 2778 | //Participants |
d6cd89b1 | 2779 | if ($navoptions->participants) { |
64448e1e KO |
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', '')); | |
d6cd89b1 JL |
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); | |
24f4bfcf | 2789 | } |
d6cd89b1 | 2790 | $participants->add(get_string('blogscourse', 'blog'), $blogsurls->out(), self::TYPE_SETTING, null, 'courseblogs'); |
3406acde | 2791 | } |
d6cd89b1 JL |
2792 | |
2793 | if ($navoptions->notes) { | |
327e68e6 | 2794 | $participants->add(get_string('notes', 'notes'), new moodle_url('/notes/index.php', array('filtertype' => 'course', 'filterselect' => $course->id)), self::TYPE_SETTING, null, 'currentcoursenotes'); |
3406acde | 2795 | } |
533299cb | 2796 | } else if (count($this->extendforuser) > 0 || $this->page->course->id == $course->id) { |
3406acde SH |
2797 | $participants = $coursenode->add(get_string('participants'), null, self::TYPE_CONTAINER, get_string('participants'), 'participants'); |
2798 | } | |
2799 | ||
27806552 | 2800 | // Badges. |
d6cd89b1 | 2801 | if ($navoptions->badges) { |
1dcb9ff1 | 2802 | $url = new moodle_url('/badges/view.php', array('type' => 2, 'id' => $course->id)); |
27806552 | 2803 | |
99061152 | 2804 | $coursenode->add(get_string('coursebadges', 'badges'), $url, |
27806552 | 2805 | navigation_node::TYPE_SETTING, null, 'badgesview', |
99061152 DW |
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)); | |
f592063f AB |
2814 | $coursenode->add($title, $path, navigation_node::TYPE_SETTING, null, 'competencies', |
2815 | new pix_icon('i/competencies', '')); | |
99061152 DW |
2816 | } |
2817 | if ($navoptions->grades) { | |
2818 | $url = new moodle_url('/grade/report/index.php', array('id'=>$course->id)); | |
f37c3ec8 KO |
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. | |
fd2b5d5c | 2822 | if ($this->page->context->contextlevel < CONTEXT_MODULE && strpos($this->page->pagetype, 'grade-') === 0) { |
f37c3ec8 KO |
2823 | $gradenode->make_active(); |
2824 | } | |
27806552 YB |
2825 | } |
2826 | ||
7d2a0492 | 2827 | return true; |
2828 | } | |
deaf04c7 | 2829 | /** |
31fde54f | 2830 | * This generates the structure of the course that won't be generated when |
deaf04c7 SH |
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 | */ | |
3406acde | 2840 | public function add_front_page_course_essentials(navigation_node $coursenode, stdClass $course) { |
a5327e44 | 2841 | global $CFG, $USER, $COURSE, $SITE; |
d6cd89b1 | 2842 | require_once($CFG->dirroot . '/course/lib.php'); |
7d2a0492 | 2843 | |
1fa692ed | 2844 | if ($coursenode == false || $coursenode->get('frontpageloaded', navigation_node::TYPE_CUSTOM)) { |
3406acde | 2845 | return true; |
7a7e209d SH |
2846 | } |
2847 | ||
f915dcbe MG |
2848 | $systemcontext = context_system::instance(); |
2849 | $navoptions = course_get_user_navigation_options($systemcontext, $course); | |
ea9431eb | 2850 | |
1fa692ed SH |
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 | ||
ea9431eb | 2855 | // Participants. |
d6cd89b1 | 2856 | if ($navoptions->participants) { |
3406acde SH |
2857 | $coursenode->add(get_string('participants'), new moodle_url('/user/index.php?id='.$course->id), self::TYPE_CUSTOM, get_string('participants'), 'participants'); |
2858 | } | |
435a512e | 2859 | |
ea9431eb | 2860 | // Blogs. |
d6cd89b1 | 2861 | if ($navoptions->blogs) { |
c1f97c77 | 2862 | $blogsurls = new moodle_url('/blog/index.php'); |
45367bdf | 2863 | $coursenode->add(get_string('blogssite', 'blog'), $blogsurls->out(), self::TYPE_SYSTEM, null, 'siteblog'); |
7d2a0492 | 2864 | } |
593270c6 | 2865 | |
c1f97c77 DC |
2866 | $filterselect = 0; |
2867 | ||
ea9431eb | 2868 | // Badges. |
d6cd89b1 | 2869 | if ($navoptions->badges) { |
27806552 YB |
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 | ||
ea9431eb | 2874 | // Notes. |
d6cd89b1 | 2875 | if ($navoptions->notes) { |
45367bdf AG |
2876 | $coursenode->add(get_string('notes', 'notes'), new moodle_url('/notes/index.php', |
2877 | array('filtertype' => 'course', 'filterselect' => $filterselect)), self::TYPE_SETTING, null, 'notes'); | |
3406acde | 2878 | } |
593270c6 MD |
2879 | |
2880 | // Tags | |
d6cd89b1 | 2881 | if ($navoptions->tags) { |
45367bdf AG |
2882 | $node = $coursenode->add(get_string('tags', 'tag'), new moodle_url('/tag/search.php'), |
2883 | self::TYPE_SETTING, null, 'tags'); | |
7d2a0492 | 2884 | } |
6644741d | 2885 | |
db48207e | 2886 | // Search. |
d6cd89b1 | 2887 | if ($navoptions->search) { |
db48207e DM |
2888 | $node = $coursenode->add(get_string('search', 'search'), new moodle_url('/search/index.php'), |
2889 | self::TYPE_SETTING, null, 'search'); | |
2890 | } | |
2891 | ||
d6cd89b1 | 2892 | if ($navoptions->calendar) { |
a5327e44 DW |
2893 | $courseid = $COURSE->id; |
2894 | $params = array('view' => 'month'); | |
2895 | if ($courseid != $SITE->id) { | |
2896 | $params['course'] = $courseid; | |
2897 | } | |
2898 | ||
e7f9b7ab | 2899 | // Calendar |
a5327e44 | 2900 | $calendarurl = new moodle_url('/calendar/view.php', $params); |
64448e1e KO |
2901 | $node = $coursenode->add(get_string('calendar', 'calendar'), $calendarurl, |
2902 | self::TYPE_CUSTOM, null, 'calendar', new pix_icon('i/calendar', '')); | |
99061152 DW |
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'); | |
64448e1e KO |
2910 | $node = $coursenode->add(get_string('privatefiles'), $url, |
2911 | self::TYPE_SETTING, null, 'privatefiles', new pix_icon('i/privatefiles', '')); | |
99061152 DW |
2912 | $node->display = false; |
2913 | $node->showinflatnavigation = true; | |
2914 | } | |
e7f9b7ab | 2915 | } |
6644741d | 2916 | |
33b8ca26 AA |
2917 | if (isloggedin()) { |
2918 | $context = $this->page->context; | |
2919 | switch ($context->contextlevel) { | |
2920 | case CONTEXT_COURSECAT: | |
cd1a8787 | 2921 | // OK, expected context level. |
33b8ca26 AA |
2922 | break; |
2923 | case CONTEXT_COURSE: | |
cd1a8787 | 2924 | // OK, expected context level if not on frontpage. |
33b8ca26 AA |
2925 | if ($COURSE->id != $SITE->id) { |
2926 | break; | |
2927 | } | |
2928 | default: | |
f915dcbe MG |
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 | } | |
33b8ca26 AA |
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, | |
cd1a8787 | 2943 | self::TYPE_CUSTOM, null, 'contentbank', new pix_icon('i/contentbank', '')); |
33b8ca26 AA |
2944 | $node->showinflatnavigation = true; |
2945 | } | |
2946 | } | |
2947 | ||
3406acde | 2948 | return true; |
6644741d | 2949 | } |
da3ab9c4 | 2950 | |
3406acde SH |
2951 | /** |
2952 | * Clears the navigation cache | |
2953 | */ | |
2954 | public function clear_cache() { | |
2955 | $this->cache->clear(); | |
da3ab9c4 | 2956 | } |
88f77c3c | 2957 | |
deaf04c7 SH |
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_* | |
31fde54f | 2969 | * @return bool true when complete. |
deaf04c7 | 2970 | */ |
88f77c3c | 2971 | public function set_expansion_limit($type) { |
98556b23 | 2972 | global $SITE; |
88f77c3c | 2973 | $nodes = $this->find_all_of_type($type); |
8718ffba SH |
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 | ||
ba7b1ca5 | 2985 | foreach ($nodes as $node) { |
1af67ecb | 2986 | // We need to generate the full site node |
98556b23 | 2987 | if ($type == self::TYPE_COURSE && $node->key == $SITE->id) { |
1af67ecb SH |
2988 | continue; |
2989 | } | |
ba7b1ca5 | 2990 | foreach ($node->children as $child) { |
8718ffba | 2991 | $child->hide($typestohide); |
88f77c3c SH |
2992 | } |
2993 | } | |
2994 | return true; | |
2995 | } | |
deaf04c7 SH |
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 | * | |
93123b17 | 3002 | * If you need to search recursivily then use the {@link global_navigation::find()} method. |
deaf04c7 | 3003 | * |
93123b17 | 3004 | * Note: If you are trying to set the active node {@link navigation_node::override_active_url()} |
deaf04c7 SH |
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 | */ | |
e2b436d0 | 3011 | public function get($key, $type = null) { |
246a9b05 SH |
3012 | if (!$this->initialised) { |
3013 | $this->initialise(); | |
3014 | } | |
54dc15ab | 3015 | return parent::get($key, $type); |
e2b436d0 SH |
3016 | } |
3017 | ||
deaf04c7 | 3018 | /** |
31fde54f | 3019 | * Searches this nodes children and their children to find a navigation node |
deaf04c7 SH |
3020 | * with the matching key and type. |
3021 | * | |
3022 | * This method is recursive and searches children so until either a node is | |
31fde54f | 3023 | * found or there are no more nodes to search. |
deaf04c7 SH |
3024 | * |
3025 | * If you know that the node being searched for is a child of this node | |
93123b17 | 3026 | * then use the {@link global_navigation::get()} method instead. |
deaf04c7 | 3027 | * |
93123b17 | 3028 | * Note: If you are trying to set the active node {@link navigation_node::override_active_url()} |
deaf04c7 SH |
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 | */ | |
e2b436d0 | 3035 | public function find($key, $type) { |
246a9b05 SH |
3036 | if (!$this->initialised) { |
3037 | $this->initialise(); | |
3038 | } | |
58b602da SH |
3039 | if ($type == self::TYPE_ROOTNODE && array_key_exists($key, $this->rootnodes)) { |
3040 | return $this->rootnodes[$key]; | |
3041 | } | |
54dc15ab | 3042 | return parent::find($key, $type); |
e2b436d0 | 3043 | } |
f87ce4e9 SH |
3044 | |
3045 | /** | |
3046 | * They've expanded the 'my courses' branch. | |
3047 | */ | |
3048 | protected function load_courses_enrolled() { | |
8aedc037 | 3049 | global $CFG; |
fa238237 MN |
3050 | |
3051 | $limit = (int) $CFG->navcourselimit; | |
3052 | ||
04985346 | 3053 | $courses = enrol_get_my_courses('*'); |
3dabb083 MN |
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()) { | |
8aedc037 | 3077 | // Generate an array containing unique values of all the courses' categories. |
f87ce4e9 SH |
3078 | $categoryids = array(); |
3079 | foreach ($courses as $course) { | |
8aedc037 JP |
3080 | if (in_array($course->category, $categoryids)) { |
3081 | continue; | |
3082 | } | |
f87ce4e9 SH |
3083 | $categoryids[] = $course->category; |
3084 | } | |
f87ce4e9 | 3085 | |
8aedc037 JP |