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 | * |
78bfb562 PS |
20 | * @since 2.0 |
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'); | |
32 | ||
7d2a0492 | 33 | /** |
34 | * This class is used to represent a node in a navigation tree | |
35 | * | |
36 | * This class is used to represent a node in a navigation tree within Moodle, | |
37 | * the tree could be one of global navigation, settings navigation, or the navbar. | |
38 | * Each node can be one of two types either a Leaf (default) or a branch. | |
39 | * When a node is first created it is created as a leaf, when/if children are added | |
40 | * the node then becomes a branch. | |
41 | * | |
31fde54f AG |
42 | * @package core |
43 | * @category navigation | |
7d2a0492 | 44 | * @copyright 2009 Sam Hemelryk |
45 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | |
46 | */ | |
3406acde SH |
47 | class navigation_node implements renderable { |
48 | /** @var int Used to identify this node a leaf (default) 0 */ | |
507a7a9a | 49 | const NODETYPE_LEAF = 0; |
3406acde | 50 | /** @var int Used to identify this node a branch, happens with children 1 */ |
7d2a0492 | 51 | const NODETYPE_BRANCH = 1; |
3406acde | 52 | /** @var null Unknown node type null */ |
7d2a0492 | 53 | const TYPE_UNKNOWN = null; |
3406acde SH |
54 | /** @var int System node type 0 */ |
55 | const TYPE_ROOTNODE = 0; | |
56 | /** @var int System node type 1 */ | |
57 | const TYPE_SYSTEM = 1; | |
58 | /** @var int Category node type 10 */ | |
7d2a0492 | 59 | const TYPE_CATEGORY = 10; |
db47f68f RT |
60 | /** var int Category displayed in MyHome navigation node */ |
61 | const TYPE_MY_CATEGORY = 11; | |
3406acde | 62 | /** @var int Course node type 20 */ |
7d2a0492 | 63 | const TYPE_COURSE = 20; |
3406acde | 64 | /** @var int Course Structure node type 30 */ |
507a7a9a | 65 | const TYPE_SECTION = 30; |
3406acde | 66 | /** @var int Activity node type, e.g. Forum, Quiz 40 */ |
7d2a0492 | 67 | const TYPE_ACTIVITY = 40; |
3406acde | 68 | /** @var int Resource node type, e.g. Link to a file, or label 50 */ |
7d2a0492 | 69 | const TYPE_RESOURCE = 50; |
3406acde | 70 | /** @var int A custom node type, default when adding without specifing type 60 */ |
7d2a0492 | 71 | const TYPE_CUSTOM = 60; |
3406acde | 72 | /** @var int Setting node type, used only within settings nav 70 */ |
7d2a0492 | 73 | const TYPE_SETTING = 70; |
3406acde | 74 | /** @var int Setting node type, used only within settings nav 80 */ |
507a7a9a | 75 | const TYPE_USER = 80; |
3406acde SH |
76 | /** @var int Setting node type, used for containers of no importance 90 */ |
77 | const TYPE_CONTAINER = 90; | |
6caf3f5c ARN |
78 | /** var int Course the current user is not enrolled in */ |
79 | const COURSE_OTHER = 0; | |
80 | /** var int Course the current user is enrolled in but not viewing */ | |
81 | const COURSE_MY = 1; | |
82 | /** var int Course the current user is currently viewing */ | |
83 | const COURSE_CURRENT = 2; | |
7d2a0492 | 84 | |
85 | /** @var int Parameter to aid the coder in tracking [optional] */ | |
86 | public $id = null; | |
87 | /** @var string|int The identifier for the node, used to retrieve the node */ | |
88 | public $key = null; | |
89 | /** @var string The text to use for the node */ | |
90 | public $text = null; | |
91 | /** @var string Short text to use if requested [optional] */ | |
92 | public $shorttext = null; | |
93 | /** @var string The title attribute for an action if one is defined */ | |
94 | public $title = null; | |
95 | /** @var string A string that can be used to build a help button */ | |
96 | public $helpbutton = null; | |
3406acde | 97 | /** @var moodle_url|action_link|null An action for the node (link) */ |
7d2a0492 | 98 | public $action = null; |
f9fc1461 | 99 | /** @var pix_icon The path to an icon to use for this node */ |
7d2a0492 | 100 | public $icon = null; |
101 | /** @var int See TYPE_* constants defined for this class */ | |
102 | public $type = self::TYPE_UNKNOWN; | |
103 | /** @var int See NODETYPE_* constants defined for this class */ | |
104 | public $nodetype = self::NODETYPE_LEAF; | |
105 | /** @var bool If set to true the node will be collapsed by default */ | |
106 | public $collapse = false; | |
107 | /** @var bool If set to true the node will be expanded by default */ | |
108 | public $forceopen = false; | |
3406acde | 109 | /** @var array An array of CSS classes for the node */ |
7d2a0492 | 110 | public $classes = array(); |
3406acde | 111 | /** @var navigation_node_collection An array of child nodes */ |
7d2a0492 | 112 | public $children = array(); |
113 | /** @var bool If set to true the node will be recognised as active */ | |
114 | public $isactive = false; | |
3406acde | 115 | /** @var bool If set to true the node will be dimmed */ |
7d2a0492 | 116 | public $hidden = false; |
117 | /** @var bool If set to false the node will not be displayed */ | |
118 | public $display = true; | |
119 | /** @var bool If set to true then an HR will be printed before the node */ | |
120 | public $preceedwithhr = false; | |
121 | /** @var bool If set to true the the navigation bar should ignore this node */ | |
122 | public $mainnavonly = false; | |
123 | /** @var bool If set to true a title will be added to the action no matter what */ | |
124 | public $forcetitle = false; | |
d38a4849 | 125 | /** @var navigation_node A reference to the node parent, you should never set this directly you should always call set_parent */ |
3406acde | 126 | public $parent = null; |
493a48f3 SH |
127 | /** @var bool Override to not display the icon even if one is provided **/ |
128 | public $hideicon = false; | |
58b602da SH |
129 | /** @var bool Set to true if we KNOW that this node can be expanded. */ |
130 | public $isexpandable = false; | |
7d2a0492 | 131 | /** @var array */ |
b14ae498 | 132 | protected $namedtypes = array(0=>'system',10=>'category',20=>'course',30=>'structure',40=>'activity',50=>'resource',60=>'custom',70=>'setting', 80=>'user'); |
7d2a0492 | 133 | /** @var moodle_url */ |
134 | protected static $fullmeurl = null; | |
d9d2817a SH |
135 | /** @var bool toogles auto matching of active node */ |
136 | public static $autofindactive = true; | |
d67e4821 | 137 | /** @var mixed If set to an int, that section will be included even if it has no activities */ |
138 | public $includesectionnum = false; | |
7d2a0492 | 139 | |
140 | /** | |
3406acde | 141 | * Constructs a new navigation_node |
7d2a0492 | 142 | * |
3406acde SH |
143 | * @param array|string $properties Either an array of properties or a string to use |
144 | * as the text for the node | |
7d2a0492 | 145 | */ |
146 | public function __construct($properties) { | |
7d2a0492 | 147 | if (is_array($properties)) { |
3406acde SH |
148 | // Check the array for each property that we allow to set at construction. |
149 | // text - The main content for the node | |
150 | // shorttext - A short text if required for the node | |
151 | // icon - The icon to display for the node | |
152 | // type - The type of the node | |
153 | // key - The key to use to identify the node | |
154 | // parent - A reference to the nodes parent | |
155 | // action - The action to attribute to this node, usually a URL to link to | |
7d2a0492 | 156 | if (array_key_exists('text', $properties)) { |
157 | $this->text = $properties['text']; | |
158 | } | |
159 | if (array_key_exists('shorttext', $properties)) { | |
160 | $this->shorttext = $properties['shorttext']; | |
161 | } | |
7081714d | 162 | if (!array_key_exists('icon', $properties)) { |
775fe700 | 163 | $properties['icon'] = new pix_icon('i/navigationitem', ''); |
7081714d SH |
164 | } |
165 | $this->icon = $properties['icon']; | |
166 | if ($this->icon instanceof pix_icon) { | |
167 | if (empty($this->icon->attributes['class'])) { | |
168 | $this->icon->attributes['class'] = 'navicon'; | |
169 | } else { | |
170 | $this->icon->attributes['class'] .= ' navicon'; | |
7da3a799 | 171 | } |
7d2a0492 | 172 | } |
173 | if (array_key_exists('type', $properties)) { | |
174 | $this->type = $properties['type']; | |
175 | } else { | |
176 | $this->type = self::TYPE_CUSTOM; | |
177 | } | |
178 | if (array_key_exists('key', $properties)) { | |
179 | $this->key = $properties['key']; | |
180 | } | |
3406acde SH |
181 | // This needs to happen last because of the check_if_active call that occurs |
182 | if (array_key_exists('action', $properties)) { | |
183 | $this->action = $properties['action']; | |
184 | if (is_string($this->action)) { | |
185 | $this->action = new moodle_url($this->action); | |
186 | } | |
187 | if (self::$autofindactive) { | |
188 | $this->check_if_active(); | |
189 | } | |
190 | } | |
d38a4849 SH |
191 | if (array_key_exists('parent', $properties)) { |
192 | $this->set_parent($properties['parent']); | |
193 | } | |
7d2a0492 | 194 | } else if (is_string($properties)) { |
195 | $this->text = $properties; | |
196 | } | |
197 | if ($this->text === null) { | |
198 | throw new coding_exception('You must set the text for the node when you create it.'); | |
199 | } | |
3406acde SH |
200 | // Instantiate a new navigation node collection for this nodes children |
201 | $this->children = new navigation_node_collection(); | |
7d2a0492 | 202 | } |
203 | ||
95b97515 | 204 | /** |
3406acde | 205 | * Checks if this node is the active node. |
0baa5d46 | 206 | * |
3406acde SH |
207 | * This is determined by comparing the action for the node against the |
208 | * defined URL for the page. A match will see this node marked as active. | |
0baa5d46 | 209 | * |
3406acde SH |
210 | * @param int $strength One of URL_MATCH_EXACT, URL_MATCH_PARAMS, or URL_MATCH_BASE |
211 | * @return bool | |
7d2a0492 | 212 | */ |
bf6c37c7 | 213 | public function check_if_active($strength=URL_MATCH_EXACT) { |
214 | global $FULLME, $PAGE; | |
3406acde | 215 | // Set fullmeurl if it hasn't already been set |
7d2a0492 | 216 | if (self::$fullmeurl == null) { |
bf6c37c7 | 217 | if ($PAGE->has_set_url()) { |
3406acde | 218 | self::override_active_url(new moodle_url($PAGE->url)); |
bf6c37c7 | 219 | } else { |
3406acde | 220 | self::override_active_url(new moodle_url($FULLME)); |
7d2a0492 | 221 | } |
222 | } | |
bf6c37c7 | 223 | |
3406acde | 224 | // Compare the action of this node against the fullmeurl |
bf6c37c7 | 225 | if ($this->action instanceof moodle_url && $this->action->compare(self::$fullmeurl, $strength)) { |
7d2a0492 | 226 | $this->make_active(); |
227 | return true; | |
7d2a0492 | 228 | } |
229 | return false; | |
230 | } | |
3406acde | 231 | |
7d2a0492 | 232 | /** |
d0cfbab3 SH |
233 | * This sets the URL that the URL of new nodes get compared to when locating |
234 | * the active node. | |
235 | * | |
236 | * The active node is the node that matches the URL set here. By default this | |
237 | * is either $PAGE->url or if that hasn't been set $FULLME. | |
7d2a0492 | 238 | * |
3406acde SH |
239 | * @param moodle_url $url The url to use for the fullmeurl. |
240 | */ | |
241 | public static function override_active_url(moodle_url $url) { | |
633c0843 TH |
242 | // Clone the URL, in case the calling script changes their URL later. |
243 | self::$fullmeurl = new moodle_url($url); | |
3406acde SH |
244 | } |
245 | ||
246 | /** | |
188a8127 | 247 | * Creates a navigation node, ready to add it as a child using add_node |
248 | * function. (The created node needs to be added before you can use it.) | |
3406acde SH |
249 | * @param string $text |
250 | * @param moodle_url|action_link $action | |
251 | * @param int $type | |
252 | * @param string $shorttext | |
253 | * @param string|int $key | |
254 | * @param pix_icon $icon | |
255 | * @return navigation_node | |
7d2a0492 | 256 | */ |
188a8127 | 257 | public static function create($text, $action=null, $type=self::TYPE_CUSTOM, |
258 | $shorttext=null, $key=null, pix_icon $icon=null) { | |
3406acde SH |
259 | // Properties array used when creating the new navigation node |
260 | $itemarray = array( | |
261 | 'text' => $text, | |
262 | 'type' => $type | |
263 | ); | |
264 | // Set the action if one was provided | |
7d2a0492 | 265 | if ($action!==null) { |
266 | $itemarray['action'] = $action; | |
267 | } | |
3406acde | 268 | // Set the shorttext if one was provided |
7d2a0492 | 269 | if ($shorttext!==null) { |
270 | $itemarray['shorttext'] = $shorttext; | |
271 | } | |
3406acde | 272 | // Set the icon if one was provided |
7d2a0492 | 273 | if ($icon!==null) { |
274 | $itemarray['icon'] = $icon; | |
275 | } | |
3406acde | 276 | // Set the key |
7d2a0492 | 277 | $itemarray['key'] = $key; |
188a8127 | 278 | // Construct and return |
279 | return new navigation_node($itemarray); | |
280 | } | |
281 | ||
282 | /** | |
283 | * Adds a navigation node as a child of this node. | |
284 | * | |
285 | * @param string $text | |
286 | * @param moodle_url|action_link $action | |
287 | * @param int $type | |
288 | * @param string $shorttext | |
289 | * @param string|int $key | |
290 | * @param pix_icon $icon | |
291 | * @return navigation_node | |
292 | */ | |
293 | public function add($text, $action=null, $type=self::TYPE_CUSTOM, $shorttext=null, $key=null, pix_icon $icon=null) { | |
294 | // Create child node | |
295 | $childnode = self::create($text, $action, $type, $shorttext, $key, $icon); | |
296 | ||
297 | // Add the child to end and return | |
298 | return $this->add_node($childnode); | |
299 | } | |
300 | ||
301 | /** | |
302 | * Adds a navigation node as a child of this one, given a $node object | |
303 | * created using the create function. | |
304 | * @param navigation_node $childnode Node to add | |
31fde54f | 305 | * @param string $beforekey |
188a8127 | 306 | * @return navigation_node The added node |
307 | */ | |
308 | public function add_node(navigation_node $childnode, $beforekey=null) { | |
309 | // First convert the nodetype for this node to a branch as it will now have children | |
310 | if ($this->nodetype !== self::NODETYPE_BRANCH) { | |
311 | $this->nodetype = self::NODETYPE_BRANCH; | |
312 | } | |
3406acde | 313 | // Set the parent to this node |
d38a4849 | 314 | $childnode->set_parent($this); |
188a8127 | 315 | |
316 | // Default the key to the number of children if not provided | |
317 | if ($childnode->key === null) { | |
318 | $childnode->key = $this->children->count(); | |
319 | } | |
320 | ||
3406acde | 321 | // Add the child using the navigation_node_collections add method |
188a8127 | 322 | $node = $this->children->add($childnode, $beforekey); |
323 | ||
324 | // If added node is a category node or the user is logged in and it's a course | |
325 | // then mark added node as a branch (makes it expandable by AJAX) | |
326 | $type = $childnode->type; | |
db47f68f | 327 | if (($type == self::TYPE_CATEGORY) || (isloggedin() && ($type == self::TYPE_COURSE)) || ($type == self::TYPE_MY_CATEGORY)) { |
3406acde | 328 | $node->nodetype = self::NODETYPE_BRANCH; |
7d2a0492 | 329 | } |
3406acde | 330 | // If this node is hidden mark it's children as hidden also |
7d2a0492 | 331 | if ($this->hidden) { |
3406acde | 332 | $node->hidden = true; |
7d2a0492 | 333 | } |
188a8127 | 334 | // Return added node (reference returned by $this->children->add() |
3406acde | 335 | return $node; |
7d2a0492 | 336 | } |
337 | ||
0c2f94e0 TH |
338 | /** |
339 | * Return a list of all the keys of all the child nodes. | |
340 | * @return array the keys. | |
341 | */ | |
342 | public function get_children_key_list() { | |
343 | return $this->children->get_key_list(); | |
344 | } | |
345 | ||
7d2a0492 | 346 | /** |
3406acde | 347 | * Searches for a node of the given type with the given key. |
7d2a0492 | 348 | * |
3406acde SH |
349 | * This searches this node plus all of its children, and their children.... |
350 | * If you know the node you are looking for is a child of this node then please | |
351 | * use the get method instead. | |
352 | * | |
353 | * @param int|string $key The key of the node we are looking for | |
354 | * @param int $type One of navigation_node::TYPE_* | |
355 | * @return navigation_node|false | |
7d2a0492 | 356 | */ |
3406acde SH |
357 | public function find($key, $type) { |
358 | return $this->children->find($key, $type); | |
359 | } | |
360 | ||
361 | /** | |
31fde54f | 362 | * Get the child of this node that has the given key + (optional) type. |
3406acde SH |
363 | * |
364 | * If you are looking for a node and want to search all children + thier children | |
365 | * then please use the find method instead. | |
366 | * | |
367 | * @param int|string $key The key of the node we are looking for | |
368 | * @param int $type One of navigation_node::TYPE_* | |
369 | * @return navigation_node|false | |
370 | */ | |
371 | public function get($key, $type=null) { | |
372 | return $this->children->get($key, $type); | |
373 | } | |
374 | ||
375 | /** | |
376 | * Removes this node. | |
377 | * | |
378 | * @return bool | |
379 | */ | |
380 | public function remove() { | |
381 | return $this->parent->children->remove($this->key, $this->type); | |
382 | } | |
383 | ||
384 | /** | |
385 | * Checks if this node has or could have any children | |
386 | * | |
387 | * @return bool Returns true if it has children or could have (by AJAX expansion) | |
388 | */ | |
389 | public function has_children() { | |
58b602da | 390 | return ($this->nodetype === navigation_node::NODETYPE_BRANCH || $this->children->count()>0 || $this->isexpandable); |
3406acde SH |
391 | } |
392 | ||
393 | /** | |
394 | * Marks this node as active and forces it open. | |
d0cfbab3 SH |
395 | * |
396 | * Important: If you are here because you need to mark a node active to get | |
93123b17 | 397 | * the navigation to do what you want have you looked at {@link navigation_node::override_active_url()}? |
d0cfbab3 SH |
398 | * You can use it to specify a different URL to match the active navigation node on |
399 | * rather than having to locate and manually mark a node active. | |
3406acde SH |
400 | */ |
401 | public function make_active() { | |
402 | $this->isactive = true; | |
403 | $this->add_class('active_tree_node'); | |
404 | $this->force_open(); | |
405 | if ($this->parent !== null) { | |
406 | $this->parent->make_inactive(); | |
407 | } | |
408 | } | |
409 | ||
410 | /** | |
411 | * Marks a node as inactive and recusised back to the base of the tree | |
412 | * doing the same to all parents. | |
413 | */ | |
414 | public function make_inactive() { | |
415 | $this->isactive = false; | |
416 | $this->remove_class('active_tree_node'); | |
417 | if ($this->parent !== null) { | |
418 | $this->parent->make_inactive(); | |
7d2a0492 | 419 | } |
420 | } | |
421 | ||
422 | /** | |
3406acde SH |
423 | * Forces this node to be open and at the same time forces open all |
424 | * parents until the root node. | |
117bd748 | 425 | * |
3406acde SH |
426 | * Recursive. |
427 | */ | |
428 | public function force_open() { | |
429 | $this->forceopen = true; | |
430 | if ($this->parent !== null) { | |
431 | $this->parent->force_open(); | |
432 | } | |
433 | } | |
434 | ||
435 | /** | |
436 | * Adds a CSS class to this node. | |
437 | * | |
438 | * @param string $class | |
439 | * @return bool | |
7d2a0492 | 440 | */ |
441 | public function add_class($class) { | |
442 | if (!in_array($class, $this->classes)) { | |
443 | $this->classes[] = $class; | |
444 | } | |
445 | return true; | |
446 | } | |
447 | ||
448 | /** | |
3406acde | 449 | * Removes a CSS class from this node. |
7d2a0492 | 450 | * |
451 | * @param string $class | |
3406acde | 452 | * @return bool True if the class was successfully removed. |
7d2a0492 | 453 | */ |
454 | public function remove_class($class) { | |
455 | if (in_array($class, $this->classes)) { | |
456 | $key = array_search($class,$this->classes); | |
457 | if ($key!==false) { | |
458 | unset($this->classes[$key]); | |
459 | return true; | |
460 | } | |
461 | } | |
462 | return false; | |
463 | } | |
464 | ||
465 | /** | |
3406acde SH |
466 | * Sets the title for this node and forces Moodle to utilise it. |
467 | * @param string $title | |
468 | */ | |
469 | public function title($title) { | |
470 | $this->title = $title; | |
471 | $this->forcetitle = true; | |
472 | } | |
473 | ||
474 | /** | |
475 | * Resets the page specific information on this node if it is being unserialised. | |
476 | */ | |
477 | public function __wakeup(){ | |
478 | $this->forceopen = false; | |
479 | $this->isactive = false; | |
480 | $this->remove_class('active_tree_node'); | |
481 | } | |
482 | ||
483 | /** | |
484 | * Checks if this node or any of its children contain the active node. | |
435a512e | 485 | * |
3406acde | 486 | * Recursive. |
7d2a0492 | 487 | * |
3406acde | 488 | * @return bool |
7d2a0492 | 489 | */ |
3406acde SH |
490 | public function contains_active_node() { |
491 | if ($this->isactive) { | |
7d2a0492 | 492 | return true; |
493 | } else { | |
3406acde SH |
494 | foreach ($this->children as $child) { |
495 | if ($child->isactive || $child->contains_active_node()) { | |
496 | return true; | |
497 | } | |
498 | } | |
7d2a0492 | 499 | } |
3406acde | 500 | return false; |
7d2a0492 | 501 | } |
502 | ||
503 | /** | |
3406acde SH |
504 | * Finds the active node. |
505 | * | |
506 | * Searches this nodes children plus all of the children for the active node | |
507 | * and returns it if found. | |
7d2a0492 | 508 | * |
3406acde SH |
509 | * Recursive. |
510 | * | |
511 | * @return navigation_node|false | |
7d2a0492 | 512 | */ |
3406acde SH |
513 | public function find_active_node() { |
514 | if ($this->isactive) { | |
515 | return $this; | |
516 | } else { | |
7d2a0492 | 517 | foreach ($this->children as &$child) { |
3406acde SH |
518 | $outcome = $child->find_active_node(); |
519 | if ($outcome !== false) { | |
520 | return $outcome; | |
7d2a0492 | 521 | } |
522 | } | |
7d2a0492 | 523 | } |
3406acde | 524 | return false; |
7d2a0492 | 525 | } |
526 | ||
7c4efe3b SH |
527 | /** |
528 | * Searches all children for the best matching active node | |
529 | * @return navigation_node|false | |
530 | */ | |
531 | public function search_for_active_node() { | |
532 | if ($this->check_if_active(URL_MATCH_BASE)) { | |
533 | return $this; | |
534 | } else { | |
535 | foreach ($this->children as &$child) { | |
536 | $outcome = $child->search_for_active_node(); | |
537 | if ($outcome !== false) { | |
538 | return $outcome; | |
539 | } | |
540 | } | |
541 | } | |
542 | return false; | |
543 | } | |
544 | ||
7d2a0492 | 545 | /** |
3406acde | 546 | * Gets the content for this node. |
7d2a0492 | 547 | * |
3406acde SH |
548 | * @param bool $shorttext If true shorttext is used rather than the normal text |
549 | * @return string | |
7d2a0492 | 550 | */ |
3406acde | 551 | public function get_content($shorttext=false) { |
7d2a0492 | 552 | if ($shorttext && $this->shorttext!==null) { |
3406acde | 553 | return format_string($this->shorttext); |
7d2a0492 | 554 | } else { |
3406acde | 555 | return format_string($this->text); |
1c4eef57 | 556 | } |
3406acde | 557 | } |
1c4eef57 | 558 | |
3406acde SH |
559 | /** |
560 | * Gets the title to use for this node. | |
435a512e | 561 | * |
3406acde SH |
562 | * @return string |
563 | */ | |
564 | public function get_title() { | |
bbfa9be0 | 565 | if ($this->forcetitle || $this->action != null){ |
3406acde SH |
566 | return $this->title; |
567 | } else { | |
9bf16314 PS |
568 | return ''; |
569 | } | |
7d2a0492 | 570 | } |
117bd748 | 571 | |
7d2a0492 | 572 | /** |
3406acde | 573 | * Gets the CSS class to add to this node to describe its type |
435a512e | 574 | * |
7d2a0492 | 575 | * @return string |
576 | */ | |
577 | public function get_css_type() { | |
578 | if (array_key_exists($this->type, $this->namedtypes)) { | |
579 | return 'type_'.$this->namedtypes[$this->type]; | |
580 | } | |
581 | return 'type_unknown'; | |
582 | } | |
583 | ||
584 | /** | |
3406acde | 585 | * Finds all nodes that are expandable by AJAX |
7d2a0492 | 586 | * |
3406acde | 587 | * @param array $expandable An array by reference to populate with expandable nodes. |
7d2a0492 | 588 | */ |
3406acde | 589 | public function find_expandable(array &$expandable) { |
3406acde | 590 | foreach ($this->children as &$child) { |
58b602da | 591 | if ($child->display && $child->has_children() && $child->children->count() == 0) { |
3406acde SH |
592 | $child->id = 'expandable_branch_'.(count($expandable)+1); |
593 | $this->add_class('canexpand'); | |
8ad24c1a | 594 | $expandable[] = array('id' => $child->id, 'key' => $child->key, 'type' => $child->type); |
7d2a0492 | 595 | } |
3406acde | 596 | $child->find_expandable($expandable); |
7d2a0492 | 597 | } |
7d2a0492 | 598 | } |
88f77c3c | 599 | |
4766a50c SH |
600 | /** |
601 | * Finds all nodes of a given type (recursive) | |
602 | * | |
31fde54f | 603 | * @param int $type One of navigation_node::TYPE_* |
4766a50c SH |
604 | * @return array |
605 | */ | |
88f77c3c SH |
606 | public function find_all_of_type($type) { |
607 | $nodes = $this->children->type($type); | |
608 | foreach ($this->children as &$node) { | |
609 | $childnodes = $node->find_all_of_type($type); | |
610 | $nodes = array_merge($nodes, $childnodes); | |
611 | } | |
612 | return $nodes; | |
613 | } | |
56ed242b SH |
614 | |
615 | /** | |
616 | * Removes this node if it is empty | |
617 | */ | |
618 | public function trim_if_empty() { | |
619 | if ($this->children->count() == 0) { | |
620 | $this->remove(); | |
621 | } | |
622 | } | |
623 | ||
624 | /** | |
625 | * Creates a tab representation of this nodes children that can be used | |
626 | * with print_tabs to produce the tabs on a page. | |
627 | * | |
628 | * call_user_func_array('print_tabs', $node->get_tabs_array()); | |
629 | * | |
630 | * @param array $inactive | |
631 | * @param bool $return | |
632 | * @return array Array (tabs, selected, inactive, activated, return) | |
633 | */ | |
634 | public function get_tabs_array(array $inactive=array(), $return=false) { | |
635 | $tabs = array(); | |
636 | $rows = array(); | |
637 | $selected = null; | |
638 | $activated = array(); | |
639 | foreach ($this->children as $node) { | |
640 | $tabs[] = new tabobject($node->key, $node->action, $node->get_content(), $node->get_title()); | |
641 | if ($node->contains_active_node()) { | |
642 | if ($node->children->count() > 0) { | |
643 | $activated[] = $node->key; | |
644 | foreach ($node->children as $child) { | |
645 | if ($child->contains_active_node()) { | |
646 | $selected = $child->key; | |
647 | } | |
648 | $rows[] = new tabobject($child->key, $child->action, $child->get_content(), $child->get_title()); | |
649 | } | |
650 | } else { | |
651 | $selected = $node->key; | |
652 | } | |
653 | } | |
654 | } | |
655 | return array(array($tabs, $rows), $selected, $inactive, $activated, $return); | |
656 | } | |
d38a4849 SH |
657 | |
658 | /** | |
659 | * Sets the parent for this node and if this node is active ensures that the tree is properly | |
660 | * adjusted as well. | |
661 | * | |
662 | * @param navigation_node $parent | |
663 | */ | |
664 | public function set_parent(navigation_node $parent) { | |
665 | // Set the parent (thats the easy part) | |
666 | $this->parent = $parent; | |
667 | // Check if this node is active (this is checked during construction) | |
668 | if ($this->isactive) { | |
669 | // Force all of the parent nodes open so you can see this node | |
670 | $this->parent->force_open(); | |
671 | // Make all parents inactive so that its clear where we are. | |
672 | $this->parent->make_inactive(); | |
673 | } | |
674 | } | |
ba7b1ca5 SH |
675 | |
676 | /** | |
677 | * Hides the node and any children it has. | |
678 | * | |
679 | * @since 2.5 | |
8718ffba SH |
680 | * @param array $typestohide Optional. An array of node types that should be hidden. |
681 | * If null all nodes will be hidden. | |
682 | * If an array is given then nodes will only be hidden if their type mtatches an element in the array. | |
683 | * e.g. array(navigation_node::TYPE_COURSE) would hide only course nodes. | |
684 | */ | |
685 | public function hide(array $typestohide = null) { | |
686 | if ($typestohide === null || in_array($this->type, $typestohide)) { | |
687 | $this->display = false; | |
688 | if ($this->has_children()) { | |
689 | foreach ($this->children as $child) { | |
690 | $child->hide($typestohide); | |
691 | } | |
ba7b1ca5 SH |
692 | } |
693 | } | |
694 | } | |
3406acde | 695 | } |
7d2a0492 | 696 | |
3406acde SH |
697 | /** |
698 | * Navigation node collection | |
699 | * | |
700 | * This class is responsible for managing a collection of navigation nodes. | |
701 | * It is required because a node's unique identifier is a combination of both its | |
702 | * key and its type. | |
703 | * | |
704 | * Originally an array was used with a string key that was a combination of the two | |
705 | * however it was decided that a better solution would be to use a class that | |
706 | * implements the standard IteratorAggregate interface. | |
707 | * | |
31fde54f AG |
708 | * @package core |
709 | * @category navigation | |
3406acde SH |
710 | * @copyright 2010 Sam Hemelryk |
711 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | |
712 | */ | |
713 | class navigation_node_collection implements IteratorAggregate { | |
7d2a0492 | 714 | /** |
3406acde SH |
715 | * A multidimensional array to where the first key is the type and the second |
716 | * key is the nodes key. | |
717 | * @var array | |
7d2a0492 | 718 | */ |
3406acde | 719 | protected $collection = array(); |
7d2a0492 | 720 | /** |
3406acde SH |
721 | * An array that contains references to nodes in the same order they were added. |
722 | * This is maintained as a progressive array. | |
723 | * @var array | |
7d2a0492 | 724 | */ |
3406acde | 725 | protected $orderedcollection = array(); |
da3ab9c4 | 726 | /** |
3406acde SH |
727 | * A reference to the last node that was added to the collection |
728 | * @var navigation_node | |
da3ab9c4 | 729 | */ |
3406acde | 730 | protected $last = null; |
6644741d | 731 | /** |
3406acde SH |
732 | * The total number of items added to this array. |
733 | * @var int | |
6644741d | 734 | */ |
3406acde | 735 | protected $count = 0; |
0c2f94e0 | 736 | |
7d2a0492 | 737 | /** |
3406acde | 738 | * Adds a navigation node to the collection |
7d2a0492 | 739 | * |
188a8127 | 740 | * @param navigation_node $node Node to add |
741 | * @param string $beforekey If specified, adds before a node with this key, | |
742 | * otherwise adds at end | |
743 | * @return navigation_node Added node | |
7d2a0492 | 744 | */ |
188a8127 | 745 | public function add(navigation_node $node, $beforekey=null) { |
3406acde SH |
746 | global $CFG; |
747 | $key = $node->key; | |
748 | $type = $node->type; | |
188a8127 | 749 | |
3406acde SH |
750 | // First check we have a 2nd dimension for this type |
751 | if (!array_key_exists($type, $this->orderedcollection)) { | |
752 | $this->orderedcollection[$type] = array(); | |
7d2a0492 | 753 | } |
3406acde SH |
754 | // Check for a collision and report if debugging is turned on |
755 | if ($CFG->debug && array_key_exists($key, $this->orderedcollection[$type])) { | |
756 | debugging('Navigation node intersect: Adding a node that already exists '.$key, DEBUG_DEVELOPER); | |
7d2a0492 | 757 | } |
188a8127 | 758 | |
759 | // Find the key to add before | |
760 | $newindex = $this->count; | |
761 | $last = true; | |
762 | if ($beforekey !== null) { | |
763 | foreach ($this->collection as $index => $othernode) { | |
764 | if ($othernode->key === $beforekey) { | |
765 | $newindex = $index; | |
766 | $last = false; | |
767 | break; | |
768 | } | |
769 | } | |
770 | if ($newindex === $this->count) { | |
188a8127 | 771 | debugging('Navigation node add_before: Reference node not found ' . $beforekey . |
0c2f94e0 | 772 | ', options: ' . implode(' ', $this->get_key_list()), DEBUG_DEVELOPER); |
188a8127 | 773 | } |
774 | } | |
775 | ||
776 | // Add the node to the appropriate place in the by-type structure (which | |
777 | // is not ordered, despite the variable name) | |
3406acde | 778 | $this->orderedcollection[$type][$key] = $node; |
188a8127 | 779 | if (!$last) { |
780 | // Update existing references in the ordered collection (which is the | |
781 | // one that isn't called 'ordered') to shuffle them along if required | |
782 | for ($oldindex = $this->count; $oldindex > $newindex; $oldindex--) { | |
783 | $this->collection[$oldindex] = $this->collection[$oldindex - 1]; | |
784 | } | |
785 | } | |
3406acde | 786 | // Add a reference to the node to the progressive collection. |
082513ba | 787 | $this->collection[$newindex] = $this->orderedcollection[$type][$key]; |
3406acde | 788 | // Update the last property to a reference to this new node. |
082513ba | 789 | $this->last = $this->orderedcollection[$type][$key]; |
5436561c | 790 | |
188a8127 | 791 | // Reorder the array by index if needed |
792 | if (!$last) { | |
793 | ksort($this->collection); | |
188a8127 | 794 | } |
3406acde SH |
795 | $this->count++; |
796 | // Return the reference to the now added node | |
188a8127 | 797 | return $node; |
7d2a0492 | 798 | } |
799 | ||
0c2f94e0 TH |
800 | /** |
801 | * Return a list of all the keys of all the nodes. | |
802 | * @return array the keys. | |
803 | */ | |
804 | public function get_key_list() { | |
805 | $keys = array(); | |
806 | foreach ($this->collection as $node) { | |
807 | $keys[] = $node->key; | |
808 | } | |
809 | return $keys; | |
810 | } | |
811 | ||
7d2a0492 | 812 | /** |
3406acde | 813 | * Fetches a node from this collection. |
7d2a0492 | 814 | * |
3406acde SH |
815 | * @param string|int $key The key of the node we want to find. |
816 | * @param int $type One of navigation_node::TYPE_*. | |
817 | * @return navigation_node|null | |
7d2a0492 | 818 | */ |
3406acde SH |
819 | public function get($key, $type=null) { |
820 | if ($type !== null) { | |
821 | // If the type is known then we can simply check and fetch | |
822 | if (!empty($this->orderedcollection[$type][$key])) { | |
823 | return $this->orderedcollection[$type][$key]; | |
824 | } | |
825 | } else { | |
826 | // Because we don't know the type we look in the progressive array | |
827 | foreach ($this->collection as $node) { | |
828 | if ($node->key === $key) { | |
829 | return $node; | |
7d2a0492 | 830 | } |
831 | } | |
832 | } | |
833 | return false; | |
834 | } | |
0c2f94e0 | 835 | |
7d2a0492 | 836 | /** |
3406acde | 837 | * Searches for a node with matching key and type. |
7d2a0492 | 838 | * |
3406acde SH |
839 | * This function searches both the nodes in this collection and all of |
840 | * the nodes in each collection belonging to the nodes in this collection. | |
7d2a0492 | 841 | * |
3406acde | 842 | * Recursive. |
7d2a0492 | 843 | * |
3406acde SH |
844 | * @param string|int $key The key of the node we want to find. |
845 | * @param int $type One of navigation_node::TYPE_*. | |
846 | * @return navigation_node|null | |
7d2a0492 | 847 | */ |
3406acde SH |
848 | public function find($key, $type=null) { |
849 | if ($type !== null && array_key_exists($type, $this->orderedcollection) && array_key_exists($key, $this->orderedcollection[$type])) { | |
850 | return $this->orderedcollection[$type][$key]; | |
851 | } else { | |
852 | $nodes = $this->getIterator(); | |
853 | // Search immediate children first | |
854 | foreach ($nodes as &$node) { | |
d9219fc9 | 855 | if ($node->key === $key && ($type === null || $type === $node->type)) { |
3406acde | 856 | return $node; |
d926f4b1 | 857 | } |
3406acde SH |
858 | } |
859 | // Now search each childs children | |
860 | foreach ($nodes as &$node) { | |
861 | $result = $node->children->find($key, $type); | |
862 | if ($result !== false) { | |
863 | return $result; | |
d926f4b1 | 864 | } |
7d2a0492 | 865 | } |
866 | } | |
867 | return false; | |
868 | } | |
869 | ||
d926f4b1 | 870 | /** |
3406acde | 871 | * Fetches the last node that was added to this collection |
435a512e | 872 | * |
3406acde | 873 | * @return navigation_node |
d926f4b1 | 874 | */ |
3406acde SH |
875 | public function last() { |
876 | return $this->last; | |
877 | } | |
0c2f94e0 | 878 | |
3406acde SH |
879 | /** |
880 | * Fetches all nodes of a given type from this collection | |
31fde54f AG |
881 | * |
882 | * @param string|int $type node type being searched for. | |
883 | * @return array ordered collection | |
3406acde SH |
884 | */ |
885 | public function type($type) { | |
886 | if (!array_key_exists($type, $this->orderedcollection)) { | |
887 | $this->orderedcollection[$type] = array(); | |
d926f4b1 | 888 | } |
3406acde | 889 | return $this->orderedcollection[$type]; |
d926f4b1 | 890 | } |
7d2a0492 | 891 | /** |
3406acde | 892 | * Removes the node with the given key and type from the collection |
7d2a0492 | 893 | * |
31fde54f | 894 | * @param string|int $key The key of the node we want to find. |
3406acde SH |
895 | * @param int $type |
896 | * @return bool | |
7d2a0492 | 897 | */ |
3406acde SH |
898 | public function remove($key, $type=null) { |
899 | $child = $this->get($key, $type); | |
900 | if ($child !== false) { | |
901 | foreach ($this->collection as $colkey => $node) { | |
e3b11a6e | 902 | if ($node->key === $key && $node->type == $type) { |
3406acde | 903 | unset($this->collection[$colkey]); |
5150c302 | 904 | $this->collection = array_values($this->collection); |
3406acde SH |
905 | break; |
906 | } | |
7d2a0492 | 907 | } |
3406acde SH |
908 | unset($this->orderedcollection[$child->type][$child->key]); |
909 | $this->count--; | |
910 | return true; | |
7d2a0492 | 911 | } |
3406acde | 912 | return false; |
7d2a0492 | 913 | } |
914 | ||
9da1ec27 | 915 | /** |
3406acde | 916 | * Gets the number of nodes in this collection |
e26507b3 SH |
917 | * |
918 | * This option uses an internal count rather than counting the actual options to avoid | |
919 | * a performance hit through the count function. | |
920 | * | |
3406acde | 921 | * @return int |
7d2a0492 | 922 | */ |
3406acde | 923 | public function count() { |
e26507b3 | 924 | return $this->count; |
7d2a0492 | 925 | } |
7d2a0492 | 926 | /** |
3406acde | 927 | * Gets an array iterator for the collection. |
7d2a0492 | 928 | * |
3406acde SH |
929 | * This is required by the IteratorAggregator interface and is used by routines |
930 | * such as the foreach loop. | |
7d2a0492 | 931 | * |
3406acde | 932 | * @return ArrayIterator |
7d2a0492 | 933 | */ |
3406acde SH |
934 | public function getIterator() { |
935 | return new ArrayIterator($this->collection); | |
7d2a0492 | 936 | } |
937 | } | |
938 | ||
939 | /** | |
940 | * The global navigation class used for... the global navigation | |
941 | * | |
942 | * This class is used by PAGE to store the global navigation for the site | |
943 | * and is then used by the settings nav and navbar to save on processing and DB calls | |
944 | * | |
945 | * See | |
93123b17 | 946 | * {@link lib/pagelib.php} {@link moodle_page::initialise_theme_and_output()} |
31fde54f | 947 | * {@link lib/ajax/getnavbranch.php} Called by ajax |
7d2a0492 | 948 | * |
31fde54f AG |
949 | * @package core |
950 | * @category navigation | |
7d2a0492 | 951 | * @copyright 2009 Sam Hemelryk |
952 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | |
953 | */ | |
954 | class global_navigation extends navigation_node { | |
31fde54f | 955 | /** @var moodle_page The Moodle page this navigation object belongs to. */ |
3406acde | 956 | protected $page; |
31fde54f | 957 | /** @var bool switch to let us know if the navigation object is initialised*/ |
7d2a0492 | 958 | protected $initialised = false; |
31fde54f | 959 | /** @var array An array of course information */ |
3406acde | 960 | protected $mycourses = array(); |
31fde54f | 961 | /** @var array An array for containing root navigation nodes */ |
3406acde | 962 | protected $rootnodes = array(); |
31fde54f | 963 | /** @var bool A switch for whether to show empty sections in the navigation */ |
fe221dd5 | 964 | protected $showemptysections = true; |
31fde54f | 965 | /** @var bool A switch for whether courses should be shown within categories on the navigation. */ |
9bf5af21 | 966 | protected $showcategories = null; |
58b602da SH |
967 | /** @var null@var bool A switch for whether or not to show categories in the my courses branch. */ |
968 | protected $showmycategories = null; | |
31fde54f | 969 | /** @var array An array of stdClasses for users that the navigation is extended for */ |
7a7e209d | 970 | protected $extendforuser = array(); |
3406acde SH |
971 | /** @var navigation_cache */ |
972 | protected $cache; | |
31fde54f | 973 | /** @var array An array of course ids that are present in the navigation */ |
3406acde | 974 | protected $addedcourses = array(); |
8e8de15f SH |
975 | /** @var bool */ |
976 | protected $allcategoriesloaded = false; | |
31fde54f | 977 | /** @var array An array of category ids that are included in the navigation */ |
e26507b3 | 978 | protected $addedcategories = array(); |
31fde54f | 979 | /** @var int expansion limit */ |
88f77c3c | 980 | protected $expansionlimit = 0; |
31fde54f | 981 | /** @var int userid to allow parent to see child's profile page navigation */ |
870815fa | 982 | protected $useridtouseforparentchecks = 0; |
88f77c3c | 983 | |
8e8de15f SH |
984 | /** Used when loading categories to load all top level categories [parent = 0] **/ |
985 | const LOAD_ROOT_CATEGORIES = 0; | |
986 | /** Used when loading categories to load all categories **/ | |
987 | const LOAD_ALL_CATEGORIES = -1; | |
988 | ||
7d2a0492 | 989 | /** |
3406acde SH |
990 | * Constructs a new global navigation |
991 | * | |
3406acde | 992 | * @param moodle_page $page The page this navigation object belongs to |
7d2a0492 | 993 | */ |
3406acde | 994 | public function __construct(moodle_page $page) { |
4766a50c | 995 | global $CFG, $SITE, $USER; |
3406acde | 996 | |
7d2a0492 | 997 | if (during_initial_install()) { |
3406acde | 998 | return; |
7d2a0492 | 999 | } |
3406acde | 1000 | |
4766a50c SH |
1001 | if (get_home_page() == HOMEPAGE_SITE) { |
1002 | // We are using the site home for the root element | |
1003 | $properties = array( | |
1004 | 'key' => 'home', | |
1005 | 'type' => navigation_node::TYPE_SYSTEM, | |
1006 | 'text' => get_string('home'), | |
1007 | 'action' => new moodle_url('/') | |
1008 | ); | |
1009 | } else { | |
1010 | // We are using the users my moodle for the root element | |
1011 | $properties = array( | |
1012 | 'key' => 'myhome', | |
1013 | 'type' => navigation_node::TYPE_SYSTEM, | |
1014 | 'text' => get_string('myhome'), | |
1015 | 'action' => new moodle_url('/my/') | |
1016 | ); | |
dd8e5011 | 1017 | } |
4766a50c | 1018 | |
31fde54f | 1019 | // Use the parents constructor.... good good reuse |
3406acde SH |
1020 | parent::__construct($properties); |
1021 | ||
1022 | // Initalise and set defaults | |
1023 | $this->page = $page; | |
7d2a0492 | 1024 | $this->forceopen = true; |
f5b5a822 | 1025 | $this->cache = new navigation_cache(NAVIGATION_CACHE_NAME); |
7d2a0492 | 1026 | } |
1027 | ||
b9bcad24 AB |
1028 | /** |
1029 | * Mutator to set userid to allow parent to see child's profile | |
1030 | * page navigation. See MDL-25805 for initial issue. Linked to it | |
1031 | * is an issue explaining why this is a REALLY UGLY HACK thats not | |
1032 | * for you to use! | |
1033 | * | |
5607036a | 1034 | * @param int $userid userid of profile page that parent wants to navigate around. |
b9bcad24 | 1035 | */ |
870815fa SH |
1036 | public function set_userid_for_parent_checks($userid) { |
1037 | $this->useridtouseforparentchecks = $userid; | |
b9bcad24 AB |
1038 | } |
1039 | ||
1040 | ||
7d2a0492 | 1041 | /** |
3406acde | 1042 | * Initialises the navigation object. |
7d2a0492 | 1043 | * |
3406acde SH |
1044 | * This causes the navigation object to look at the current state of the page |
1045 | * that it is associated with and then load the appropriate content. | |
7d2a0492 | 1046 | * |
3406acde SH |
1047 | * This should only occur the first time that the navigation structure is utilised |
1048 | * which will normally be either when the navbar is called to be displayed or | |
1049 | * when a block makes use of it. | |
7d2a0492 | 1050 | * |
3406acde | 1051 | * @return bool |
7d2a0492 | 1052 | */ |
3406acde | 1053 | public function initialise() { |
58b602da SH |
1054 | global $CFG, $SITE, $USER; |
1055 | // Check if it has already been initialised | |
7d2a0492 | 1056 | if ($this->initialised || during_initial_install()) { |
1057 | return true; | |
1058 | } | |
e2b436d0 | 1059 | $this->initialised = true; |
3406acde SH |
1060 | |
1061 | // Set up the five base root nodes. These are nodes where we will put our | |
1062 | // content and are as follows: | |
58b602da SH |
1063 | // site: Navigation for the front page. |
1064 | // myprofile: User profile information goes here. | |
1065 | // currentcourse: The course being currently viewed. | |
1066 | // mycourses: The users courses get added here. | |
1067 | // courses: Additional courses are added here. | |
1068 | // users: Other users information loaded here. | |
3406acde | 1069 | $this->rootnodes = array(); |
4766a50c | 1070 | if (get_home_page() == HOMEPAGE_SITE) { |
3f9ccf85 | 1071 | // The home element should be my moodle because the root element is the site |
b9d4c7d3 | 1072 | if (isloggedin() && !isguestuser()) { // Makes no sense if you aren't logged in |
e26507b3 | 1073 | $this->rootnodes['home'] = $this->add(get_string('myhome'), new moodle_url('/my/'), self::TYPE_SETTING, null, 'home'); |
3f9ccf85 | 1074 | } |
4766a50c SH |
1075 | } else { |
1076 | // The home element should be the site because the root node is my moodle | |
e26507b3 | 1077 | $this->rootnodes['home'] = $this->add(get_string('sitehome'), new moodle_url('/'), self::TYPE_SETTING, null, 'home'); |
ab6ec58a | 1078 | if (!empty($CFG->defaulthomepage) && ($CFG->defaulthomepage == HOMEPAGE_MY)) { |
4766a50c SH |
1079 | // We need to stop automatic redirection |
1080 | $this->rootnodes['home']->action->param('redirect', '0'); | |
1081 | } | |
1082 | } | |
58b602da | 1083 | $this->rootnodes['site'] = $this->add_course($SITE); |
e26507b3 | 1084 | $this->rootnodes['myprofile'] = $this->add(get_string('myprofile'), null, self::TYPE_USER, null, 'myprofile'); |
6caf3f5c | 1085 | $this->rootnodes['currentcourse'] = $this->add(get_string('currentcourse'), null, self::TYPE_ROOTNODE, null, 'currentcourse'); |
f87ce4e9 | 1086 | $this->rootnodes['mycourses'] = $this->add(get_string('mycourses'), new moodle_url('/my/'), self::TYPE_ROOTNODE, null, 'mycourses'); |
58b602da SH |
1087 | $this->rootnodes['courses'] = $this->add(get_string('courses'), new moodle_url('/course/index.php'), self::TYPE_ROOTNODE, null, 'courses'); |
1088 | $this->rootnodes['users'] = $this->add(get_string('users'), null, self::TYPE_ROOTNODE, null, 'users'); | |
3406acde | 1089 | |
80c69522 SH |
1090 | // We always load the frontpage course to ensure it is available without |
1091 | // JavaScript enabled. | |
1092 | $this->add_front_page_course_essentials($this->rootnodes['site'], $SITE); | |
1093 | $this->load_course_sections($SITE, $this->rootnodes['site']); | |
1094 | ||
58b602da SH |
1095 | $course = $this->page->course; |
1096 | ||
8e8de15f | 1097 | // $issite gets set to true if the current pages course is the sites frontpage course |
98556b23 | 1098 | $issite = ($this->page->course->id == $SITE->id); |
58b602da SH |
1099 | // Determine if the user is enrolled in any course. |
1100 | $enrolledinanycourse = enrol_user_sees_own_courses(); | |
4766a50c | 1101 | |
58b602da SH |
1102 | $this->rootnodes['currentcourse']->mainnavonly = true; |
1103 | if ($enrolledinanycourse) { | |
1104 | $this->rootnodes['mycourses']->isexpandable = true; | |
1105 | if ($CFG->navshowallcourses) { | |
1106 | // When we show all courses we need to show both the my courses and the regular courses branch. | |
1107 | $this->rootnodes['courses']->isexpandable = true; | |
8e8de15f | 1108 | } |
58b602da SH |
1109 | } else { |
1110 | $this->rootnodes['courses']->isexpandable = true; | |
3406acde SH |
1111 | } |
1112 | ||
f87ce4e9 SH |
1113 | if ($this->rootnodes['mycourses']->isactive) { |
1114 | $this->load_courses_enrolled(); | |
1115 | } | |
1116 | ||
cede87e0 SH |
1117 | $canviewcourseprofile = true; |
1118 | ||
6a16e136 SH |
1119 | // Next load context specific content into the navigation |
1120 | switch ($this->page->context->contextlevel) { | |
1121 | case CONTEXT_SYSTEM : | |
58b602da | 1122 | // Nothing left to do here I feel. |
6a16e136 SH |
1123 | break; |
1124 | case CONTEXT_COURSECAT : | |
58b602da SH |
1125 | // This is essential, we must load categories. |
1126 | $this->load_all_categories($this->page->context->instanceid, true); | |
6a16e136 SH |
1127 | break; |
1128 | case CONTEXT_BLOCK : | |
1129 | case CONTEXT_COURSE : | |
1130 | if ($issite) { | |
58b602da | 1131 | // Nothing left to do here. |
56c062a4 | 1132 | break; |
6a16e136 | 1133 | } |
b9bcad24 | 1134 | |
6caf3f5c | 1135 | // Load the course associated with the current page into the navigation. |
6caf3f5c | 1136 | $coursenode = $this->add_course($course, false, self::COURSE_CURRENT); |
6a16e136 SH |
1137 | // If the course wasn't added then don't try going any further. |
1138 | if (!$coursenode) { | |
1139 | $canviewcourseprofile = false; | |
1140 | break; | |
1141 | } | |
e26507b3 | 1142 | |
6a16e136 SH |
1143 | // If the user is not enrolled then we only want to show the |
1144 | // course node and not populate it. | |
1145 | ||
1146 | // Not enrolled, can't view, and hasn't switched roles | |
1147 | if (!can_access_course($course)) { | |
58b602da | 1148 | // Very ugly hack - do not force "parents" to enrol into course their child is enrolled in, |
6a16e136 | 1149 | // this hack has been propagated from user/view.php to display the navigation node. (MDL-25805) |
58b602da | 1150 | if (!$this->current_user_is_parent_role()) { |
56c062a4 | 1151 | $coursenode->make_active(); |
6a16e136 SH |
1152 | $canviewcourseprofile = false; |
1153 | break; | |
1154 | } | |
1155 | } | |
6caf3f5c | 1156 | |
6a16e136 SH |
1157 | // Add the essentials such as reports etc... |
1158 | $this->add_course_essentials($coursenode, $course); | |
ee7084e9 MG |
1159 | // Extend course navigation with it's sections/activities |
1160 | $this->load_course_sections($course, $coursenode); | |
6a16e136 SH |
1161 | if (!$coursenode->contains_active_node() && !$coursenode->search_for_active_node()) { |
1162 | $coursenode->make_active(); | |
1163 | } | |
6caf3f5c | 1164 | |
6a16e136 SH |
1165 | break; |
1166 | case CONTEXT_MODULE : | |
1167 | if ($issite) { | |
1168 | // If this is the site course then most information will have | |
1169 | // already been loaded. | |
1170 | // However we need to check if there is more content that can | |
1171 | // yet be loaded for the specific module instance. | |
e010b850 | 1172 | $activitynode = $this->rootnodes['site']->find($this->page->cm->id, navigation_node::TYPE_ACTIVITY); |
6a16e136 SH |
1173 | if ($activitynode) { |
1174 | $this->load_activity($this->page->cm, $this->page->course, $activitynode); | |
56c062a4 | 1175 | } |
2027c10e | 1176 | break; |
6a16e136 | 1177 | } |
2027c10e | 1178 | |
6a16e136 SH |
1179 | $course = $this->page->course; |
1180 | $cm = $this->page->cm; | |
cede87e0 | 1181 | |
6a16e136 | 1182 | // Load the course associated with the page into the navigation |
58b602da | 1183 | $coursenode = $this->add_course($course, false, self::COURSE_CURRENT); |
56c062a4 | 1184 | |
6a16e136 SH |
1185 | // If the course wasn't added then don't try going any further. |
1186 | if (!$coursenode) { | |
1187 | $canviewcourseprofile = false; | |
1188 | break; | |
1189 | } | |
1190 | ||
1191 | // If the user is not enrolled then we only want to show the | |
1192 | // course node and not populate it. | |
1193 | if (!can_access_course($course)) { | |
1194 | $coursenode->make_active(); | |
1195 | $canviewcourseprofile = false; | |
1196 | break; | |
1197 | } | |
56c062a4 | 1198 | |
6a16e136 | 1199 | $this->add_course_essentials($coursenode, $course); |
56c062a4 | 1200 | |
6a16e136 | 1201 | // Load the course sections into the page |
e010b850 MG |
1202 | $this->load_course_sections($course, $coursenode, null, $cm); |
1203 | $activity = $coursenode->find($cm->id, navigation_node::TYPE_ACTIVITY); | |
1204 | // Finally load the cm specific navigaton information | |
1205 | $this->load_activity($cm, $course, $activity); | |
1206 | // Check if we have an active ndoe | |
1207 | if (!$activity->contains_active_node() && !$activity->search_for_active_node()) { | |
1208 | // And make the activity node active. | |
1209 | $activity->make_active(); | |
6a16e136 SH |
1210 | } |
1211 | break; | |
1212 | case CONTEXT_USER : | |
1213 | if ($issite) { | |
1214 | // The users profile information etc is already loaded | |
1215 | // for the front page. | |
96e78552 | 1216 | break; |
6a16e136 SH |
1217 | } |
1218 | $course = $this->page->course; | |
6a16e136 | 1219 | // Load the course associated with the user into the navigation |
58b602da | 1220 | $coursenode = $this->add_course($course, false, self::COURSE_CURRENT); |
2027c10e | 1221 | |
6a16e136 SH |
1222 | // If the course wasn't added then don't try going any further. |
1223 | if (!$coursenode) { | |
1224 | $canviewcourseprofile = false; | |
1225 | break; | |
1226 | } | |
2027c10e | 1227 | |
6a16e136 SH |
1228 | // If the user is not enrolled then we only want to show the |
1229 | // course node and not populate it. | |
1230 | if (!can_access_course($course)) { | |
1231 | $coursenode->make_active(); | |
1232 | $canviewcourseprofile = false; | |
56c062a4 | 1233 | break; |
e52a5d3a | 1234 | } |
6a16e136 | 1235 | $this->add_course_essentials($coursenode, $course); |
ee7084e9 | 1236 | $this->load_course_sections($course, $coursenode); |
6a16e136 | 1237 | break; |
7d2a0492 | 1238 | } |
7a7e209d | 1239 | |
3406acde SH |
1240 | // Load for the current user |
1241 | $this->load_for_user(); | |
98556b23 | 1242 | if ($this->page->context->contextlevel >= CONTEXT_COURSE && $this->page->context->instanceid != $SITE->id && $canviewcourseprofile) { |
87c215de SH |
1243 | $this->load_for_user(null, true); |
1244 | } | |
7a7e209d SH |
1245 | // Load each extending user into the navigation. |
1246 | foreach ($this->extendforuser as $user) { | |
44303ca6 | 1247 | if ($user->id != $USER->id) { |
7a7e209d SH |
1248 | $this->load_for_user($user); |
1249 | } | |
1250 | } | |
7a7e209d | 1251 | |
a683da3c | 1252 | // Give the local plugins a chance to include some navigation if they want. |
5c1f0d1f SH |
1253 | foreach (get_plugin_list_with_file('local', 'lib.php', true) as $plugin => $file) { |
1254 | $function = "local_{$plugin}_extends_navigation"; | |
1255 | $oldfunction = "{$plugin}_extends_navigation"; | |
a683da3c | 1256 | if (function_exists($function)) { |
5c1f0d1f | 1257 | // This is the preferred function name as there is less chance of conflicts |
a683da3c | 1258 | $function($this); |
5c1f0d1f | 1259 | } else if (function_exists($oldfunction)) { |
58b602da | 1260 | // We continue to support the old function name to ensure backwards compatibility |
22ba3f08 | 1261 | debugging("Deprecated local plugin navigation callback: Please rename '{$oldfunction}' to '{$function}'. Support for the old callback will be dropped after the release of 2.4", DEBUG_DEVELOPER); |
5c1f0d1f | 1262 | $oldfunction($this); |
a683da3c SH |
1263 | } |
1264 | } | |
1265 | ||
3406acde SH |
1266 | // Remove any empty root nodes |
1267 | foreach ($this->rootnodes as $node) { | |
4766a50c SH |
1268 | // Dont remove the home node |
1269 | if ($node->key !== 'home' && !$node->has_children()) { | |
3406acde SH |
1270 | $node->remove(); |
1271 | } | |
1272 | } | |
1273 | ||
7c4efe3b SH |
1274 | if (!$this->contains_active_node()) { |
1275 | $this->search_for_active_node(); | |
1276 | } | |
1277 | ||
3406acde | 1278 | // If the user is not logged in modify the navigation structure as detailed |
728ebac7 | 1279 | // in {@link http://docs.moodle.org/dev/Navigation_2.0_structure} |
3406acde SH |
1280 | if (!isloggedin()) { |
1281 | $activities = clone($this->rootnodes['site']->children); | |
1282 | $this->rootnodes['site']->remove(); | |
1283 | $children = clone($this->children); | |
1284 | $this->children = new navigation_node_collection(); | |
1285 | foreach ($activities as $child) { | |
1286 | $this->children->add($child); | |
1287 | } | |
1288 | foreach ($children as $child) { | |
1289 | $this->children->add($child); | |
1290 | } | |
3406acde | 1291 | } |
7d2a0492 | 1292 | return true; |
1293 | } | |
9bf5af21 | 1294 | |
58b602da SH |
1295 | /** |
1296 | * Returns true if the current user is a parent of the user being currently viewed. | |
1297 | * | |
1298 | * If the current user is not viewing another user, or if the current user does not hold any parent roles over the | |
1299 | * other user being viewed this function returns false. | |
1300 | * In order to set the user for whom we are checking against you must call {@link set_userid_for_parent_checks()} | |
1301 | * | |
1302 | * @since 2.4 | |
1303 | * @return bool | |
1304 | */ | |
1305 | protected function current_user_is_parent_role() { | |
1306 | global $USER, $DB; | |
1307 | if ($this->useridtouseforparentchecks && $this->useridtouseforparentchecks != $USER->id) { | |
1308 | $usercontext = context_user::instance($this->useridtouseforparentchecks, MUST_EXIST); | |
1309 | if (!has_capability('moodle/user:viewdetails', $usercontext)) { | |
1310 | return false; | |
1311 | } | |
1312 | if ($DB->record_exists('role_assignments', array('userid' => $USER->id, 'contextid' => $usercontext->id))) { | |
1313 | return true; | |
1314 | } | |
1315 | } | |
1316 | return false; | |
1317 | } | |
1318 | ||
9bf5af21 | 1319 | /** |
31fde54f | 1320 | * Returns true if courses should be shown within categories on the navigation. |
9bf5af21 | 1321 | * |
58b602da | 1322 | * @param bool $ismycourse Set to true if you are calculating this for a course. |
9bf5af21 SH |
1323 | * @return bool |
1324 | */ | |
58b602da | 1325 | protected function show_categories($ismycourse = false) { |
9bf5af21 | 1326 | global $CFG, $DB; |
58b602da SH |
1327 | if ($ismycourse) { |
1328 | return $this->show_my_categories(); | |
1329 | } | |
9bf5af21 | 1330 | if ($this->showcategories === null) { |
58b602da SH |
1331 | $show = false; |
1332 | if ($this->page->context->contextlevel == CONTEXT_COURSECAT) { | |
1333 | $show = true; | |
1334 | } else if (!empty($CFG->navshowcategories) && $DB->count_records('course_categories') > 1) { | |
1335 | $show = true; | |
1336 | } | |
6a16e136 | 1337 | $this->showcategories = $show; |
9bf5af21 SH |
1338 | } |
1339 | return $this->showcategories; | |
1340 | } | |
1341 | ||
58b602da SH |
1342 | /** |
1343 | * Returns true if we should show categories in the My Courses branch. | |
1344 | * @return bool | |
1345 | */ | |
1346 | protected function show_my_categories() { | |
1347 | global $CFG, $DB; | |
1348 | if ($this->showmycategories === null) { | |
1349 | $this->showmycategories = !empty($CFG->navshowmycoursecategories) && $DB->count_records('course_categories') > 1; | |
1350 | } | |
1351 | return $this->showmycategories; | |
1352 | } | |
1353 | ||
3406acde | 1354 | /** |
31fde54f | 1355 | * Loads the courses in Moodle into the navigation. |
3406acde | 1356 | * |
8e8de15f SH |
1357 | * @global moodle_database $DB |
1358 | * @param string|array $categoryids An array containing categories to load courses | |
1359 | * for, OR null to load courses for all categories. | |
1360 | * @return array An array of navigation_nodes one for each course | |
3406acde | 1361 | */ |
8e8de15f | 1362 | protected function load_all_courses($categoryids = null) { |
98556b23 | 1363 | global $CFG, $DB, $SITE; |
4766a50c | 1364 | |
8e8de15f | 1365 | // Work out the limit of courses. |
4766a50c SH |
1366 | $limit = 20; |
1367 | if (!empty($CFG->navcourselimit)) { | |
1368 | $limit = $CFG->navcourselimit; | |
1369 | } | |
4766a50c | 1370 | |
8e8de15f SH |
1371 | $toload = (empty($CFG->navshowallcourses))?self::LOAD_ROOT_CATEGORIES:self::LOAD_ALL_CATEGORIES; |
1372 | ||
1373 | // If we are going to show all courses AND we are showing categories then | |
1374 | // to save us repeated DB calls load all of the categories now | |
1375 | if ($this->show_categories()) { | |
1376 | $this->load_all_categories($toload); | |
1377 | } | |
1378 | ||
1379 | // Will be the return of our efforts | |
3406acde | 1380 | $coursenodes = array(); |
8e8de15f | 1381 | |
f7ee4baa SH |
1382 | // Check if we need to show categories. |
1383 | if ($this->show_categories()) { | |
1384 | // Hmmm we need to show categories... this is going to be painful. | |
1385 | // We now need to fetch up to $limit courses for each category to | |
1386 | // be displayed. | |
1387 | if ($categoryids !== null) { | |
1388 | if (!is_array($categoryids)) { | |
1389 | $categoryids = array($categoryids); | |
8e8de15f | 1390 | } |
f7ee4baa SH |
1391 | list($categorywhere, $categoryparams) = $DB->get_in_or_equal($categoryids, SQL_PARAMS_NAMED, 'cc'); |
1392 | $categorywhere = 'WHERE cc.id '.$categorywhere; | |
1393 | } else if ($toload == self::LOAD_ROOT_CATEGORIES) { | |
1394 | $categorywhere = 'WHERE cc.depth = 1 OR cc.depth = 2'; | |
1395 | $categoryparams = array(); | |
1396 | } else { | |
1397 | $categorywhere = ''; | |
1398 | $categoryparams = array(); | |
1399 | } | |
8e8de15f | 1400 | |
f7ee4baa SH |
1401 | // First up we are going to get the categories that we are going to |
1402 | // need so that we can determine how best to load the courses from them. | |
1403 | $sql = "SELECT cc.id, COUNT(c.id) AS coursecount | |
1404 | FROM {course_categories} cc | |
1405 | LEFT JOIN {course} c ON c.category = cc.id | |
1406 | {$categorywhere} | |
1407 | GROUP BY cc.id"; | |
1408 | $categories = $DB->get_recordset_sql($sql, $categoryparams); | |
1409 | $fullfetch = array(); | |
1410 | $partfetch = array(); | |
1411 | foreach ($categories as $category) { | |
1412 | if (!$this->can_add_more_courses_to_category($category->id)) { | |
1413 | continue; | |
1414 | } | |
1415 | if ($category->coursecount > $limit * 5) { | |
1416 | $partfetch[] = $category->id; | |
1417 | } else if ($category->coursecount > 0) { | |
1418 | $fullfetch[] = $category->id; | |
1419 | } | |
1420 | } | |
1421 | $categories->close(); | |
1422 | ||
1423 | if (count($fullfetch)) { | |
1424 | // First up fetch all of the courses in categories where we know that we are going to | |
1425 | // need the majority of courses. | |
1426 | list($ccselect, $ccjoin) = context_instance_preload_sql('c.id', CONTEXT_COURSE, 'ctx'); | |
f7ee4baa SH |
1427 | list($categoryids, $categoryparams) = $DB->get_in_or_equal($fullfetch, SQL_PARAMS_NAMED, 'lcategory'); |
1428 | $sql = "SELECT c.id, c.sortorder, c.visible, c.fullname, c.shortname, c.category $ccselect | |
1429 | FROM {course} c | |
1430 | $ccjoin | |
fca2d3d7 | 1431 | WHERE c.category {$categoryids} |
f7ee4baa | 1432 | ORDER BY c.sortorder ASC"; |
fca2d3d7 | 1433 | $coursesrs = $DB->get_recordset_sql($sql, $categoryparams); |
f7ee4baa | 1434 | foreach ($coursesrs as $course) { |
fca2d3d7 PS |
1435 | if ($course->id == $SITE->id) { |
1436 | // This should not be necessary, frontpage is not in any category. | |
1437 | continue; | |
1438 | } | |
1439 | if (array_key_exists($course->id, $this->addedcourses)) { | |
1440 | // It is probably better to not include the already loaded courses | |
1441 | // directly in SQL because inequalities may confuse query optimisers | |
1442 | // and may interfere with query caching. | |
1443 | continue; | |
1444 | } | |
f7ee4baa | 1445 | if (!$this->can_add_more_courses_to_category($course->category)) { |
8e8de15f SH |
1446 | continue; |
1447 | } | |
f7ee4baa | 1448 | context_instance_preload($course); |
b0c6dc1c | 1449 | if (!$course->visible && !is_role_switched($course->id) && !has_capability('moodle/course:viewhiddencourses', context_course::instance($course->id))) { |
f7ee4baa | 1450 | continue; |
8e8de15f | 1451 | } |
f7ee4baa | 1452 | $coursenodes[$course->id] = $this->add_course($course); |
8e8de15f | 1453 | } |
f7ee4baa SH |
1454 | $coursesrs->close(); |
1455 | } | |
8e8de15f | 1456 | |
f7ee4baa SH |
1457 | if (count($partfetch)) { |
1458 | // Next we will work our way through the categories where we will likely only need a small | |
1459 | // proportion of the courses. | |
1460 | foreach ($partfetch as $categoryid) { | |
8e8de15f | 1461 | list($ccselect, $ccjoin) = context_instance_preload_sql('c.id', CONTEXT_COURSE, 'ctx'); |
8e8de15f | 1462 | $sql = "SELECT c.id, c.sortorder, c.visible, c.fullname, c.shortname, c.category $ccselect |
f7ee4baa SH |
1463 | FROM {course} c |
1464 | $ccjoin | |
fca2d3d7 | 1465 | WHERE c.category = :categoryid |
f7ee4baa | 1466 | ORDER BY c.sortorder ASC"; |
fca2d3d7 | 1467 | $courseparams = array('categoryid' => $categoryid); |
f7ee4baa | 1468 | $coursesrs = $DB->get_recordset_sql($sql, $courseparams, 0, $limit * 5); |
8e8de15f | 1469 | foreach ($coursesrs as $course) { |
fca2d3d7 PS |
1470 | if ($course->id == $SITE->id) { |
1471 | // This should not be necessary, frontpage is not in any category. | |
1472 | continue; | |
1473 | } | |
1474 | if (array_key_exists($course->id, $this->addedcourses)) { | |
1475 | // It is probably better to not include the already loaded courses | |
1476 | // directly in SQL because inequalities may confuse query optimisers | |
1477 | // and may interfere with query caching. | |
1478 | // This also helps to respect expected $limit on repeated executions. | |
1479 | continue; | |
1480 | } | |
8e8de15f | 1481 | if (!$this->can_add_more_courses_to_category($course->category)) { |
f7ee4baa | 1482 | break; |
8e8de15f SH |
1483 | } |
1484 | context_instance_preload($course); | |
b0c6dc1c | 1485 | if (!$course->visible && !is_role_switched($course->id) && !has_capability('moodle/course:viewhiddencourses', context_course::instance($course->id))) { |
8e8de15f SH |
1486 | continue; |
1487 | } | |
1488 | $coursenodes[$course->id] = $this->add_course($course); | |
1489 | } | |
1490 | $coursesrs->close(); | |
1491 | } | |
8e8de15f | 1492 | } |
8e8de15f | 1493 | } else { |
8e8de15f SH |
1494 | // Prepare the SQL to load the courses and their contexts |
1495 | list($ccselect, $ccjoin) = context_instance_preload_sql('c.id', CONTEXT_COURSE, 'ctx'); | |
f7ee4baa | 1496 | list($courseids, $courseparams) = $DB->get_in_or_equal(array_keys($this->addedcourses), SQL_PARAMS_NAMED, 'lc', false); |
8e8de15f | 1497 | $sql = "SELECT c.id, c.sortorder, c.visible, c.fullname, c.shortname, c.category $ccselect |
f7ee4baa SH |
1498 | FROM {course} c |
1499 | $ccjoin | |
1500 | WHERE c.id {$courseids} | |
1501 | ORDER BY c.sortorder ASC"; | |
8e8de15f SH |
1502 | $coursesrs = $DB->get_recordset_sql($sql, $courseparams); |
1503 | foreach ($coursesrs as $course) { | |
fca2d3d7 PS |
1504 | if ($course->id == $SITE->id) { |
1505 | // frotpage is not wanted here | |
1506 | continue; | |
1507 | } | |
6caf3f5c ARN |
1508 | if ($this->page->course && ($this->page->course->id == $course->id)) { |
1509 | // Don't include the currentcourse in this nodelist - it's displayed in the Current course node | |
1510 | continue; | |
1511 | } | |
8e8de15f | 1512 | context_instance_preload($course); |
b0c6dc1c | 1513 | if (!$course->visible && !is_role_switched($course->id) && !has_capability('moodle/course:viewhiddencourses', context_course::instance($course->id))) { |
f7ee4baa SH |
1514 | continue; |
1515 | } | |
8e8de15f | 1516 | $coursenodes[$course->id] = $this->add_course($course); |
f7ee4baa SH |
1517 | if (count($coursenodes) >= $limit) { |
1518 | break; | |
1519 | } | |
8e8de15f SH |
1520 | } |
1521 | $coursesrs->close(); | |
3406acde | 1522 | } |
f7ee4baa | 1523 | |
3406acde SH |
1524 | return $coursenodes; |
1525 | } | |
1526 | ||
8e8de15f SH |
1527 | /** |
1528 | * Returns true if more courses can be added to the provided category. | |
1529 | * | |
98556b23 SH |
1530 | * @param int|navigation_node|stdClass $category |
1531 | * @return bool | |
8e8de15f SH |
1532 | */ |
1533 | protected function can_add_more_courses_to_category($category) { | |
1534 | global $CFG; | |
1535 | $limit = 20; | |
1536 | if (!empty($CFG->navcourselimit)) { | |
1537 | $limit = (int)$CFG->navcourselimit; | |
1538 | } | |
1539 | if (is_numeric($category)) { | |
1540 | if (!array_key_exists($category, $this->addedcategories)) { | |
1541 | return true; | |
1542 | } | |
1543 | $coursecount = count($this->addedcategories[$category]->children->type(self::TYPE_COURSE)); | |
1544 | } else if ($category instanceof navigation_node) { | |
db47f68f | 1545 | if (($category->type != self::TYPE_CATEGORY) || ($category->type != self::TYPE_MY_CATEGORY)) { |
8e8de15f SH |
1546 | return false; |
1547 | } | |
1548 | $coursecount = count($category->children->type(self::TYPE_COURSE)); | |
1549 | } else if (is_object($category) && property_exists($category,'id')) { | |
1550 | $coursecount = count($this->addedcategories[$category->id]->children->type(self::TYPE_COURSE)); | |
1551 | } | |
d4bb6462 | 1552 | return ($coursecount <= $limit); |
8e8de15f SH |
1553 | } |
1554 | ||
4766a50c SH |
1555 | /** |
1556 | * Loads all categories (top level or if an id is specified for that category) | |
1557 | * | |
e26507b3 SH |
1558 | * @param int $categoryid The category id to load or null/0 to load all base level categories |
1559 | * @param bool $showbasecategories If set to true all base level categories will be loaded as well | |
1560 | * as the requested category and any parent categories. | |
31fde54f | 1561 | * @return navigation_node|void returns a navigation node if a category has been loaded. |
4766a50c | 1562 | */ |
8e8de15f | 1563 | protected function load_all_categories($categoryid = self::LOAD_ROOT_CATEGORIES, $showbasecategories = false) { |
58b602da | 1564 | global $CFG, $DB; |
e26507b3 SH |
1565 | |
1566 | // Check if this category has already been loaded | |
8e8de15f SH |
1567 | if ($this->allcategoriesloaded || ($categoryid < 1 && $this->is_category_fully_loaded($categoryid))) { |
1568 | return true; | |
e26507b3 SH |
1569 | } |
1570 | ||
176b75b5 SH |
1571 | $catcontextsql = context_helper::get_preload_record_columns_sql('ctx'); |
1572 | $sqlselect = "SELECT cc.*, $catcontextsql | |
1573 | FROM {course_categories} cc | |
1574 | JOIN {context} ctx ON cc.id = ctx.instanceid"; | |
1575 | $sqlwhere = "WHERE ctx.contextlevel = ".CONTEXT_COURSECAT; | |
957983b7 | 1576 | $sqlorder = "ORDER BY cc.depth ASC, cc.sortorder ASC, cc.id ASC"; |
176b75b5 SH |
1577 | $params = array(); |
1578 | ||
8e8de15f SH |
1579 | $categoriestoload = array(); |
1580 | if ($categoryid == self::LOAD_ALL_CATEGORIES) { | |
176b75b5 SH |
1581 | // We are going to load all categories regardless... prepare to fire |
1582 | // on the database server! | |
8e8de15f | 1583 | } else if ($categoryid == self::LOAD_ROOT_CATEGORIES) { // can be 0 |
e26507b3 | 1584 | // We are going to load all of the first level categories (categories without parents) |
176b75b5 | 1585 | $sqlwhere .= " AND cc.parent = 0"; |
e26507b3 SH |
1586 | } else if (array_key_exists($categoryid, $this->addedcategories)) { |
1587 | // The category itself has been loaded already so we just need to ensure its subcategories | |
1588 | // have been loaded | |
1589 | list($sql, $params) = $DB->get_in_or_equal(array_keys($this->addedcategories), SQL_PARAMS_NAMED, 'parent', false); | |
1590 | if ($showbasecategories) { | |
1591 | // We need to include categories with parent = 0 as well | |
d4bb6462 | 1592 | $sqlwhere .= " AND (cc.parent = :categoryid OR cc.parent = 0) AND cc.parent {$sql}"; |
e26507b3 | 1593 | } else { |
176b75b5 | 1594 | // All we need is categories that match the parent |
d4bb6462 | 1595 | $sqlwhere .= " AND cc.parent = :categoryid AND cc.parent {$sql}"; |
e26507b3 SH |
1596 | } |
1597 | $params['categoryid'] = $categoryid; | |
4766a50c | 1598 | } else { |
e26507b3 SH |
1599 | // This category hasn't been loaded yet so we need to fetch it, work out its category path |
1600 | // and load this category plus all its parents and subcategories | |
3992a46e | 1601 | $category = $DB->get_record('course_categories', array('id' => $categoryid), 'path', MUST_EXIST); |
8e8de15f SH |
1602 | $categoriestoload = explode('/', trim($category->path, '/')); |
1603 | list($select, $params) = $DB->get_in_or_equal($categoriestoload); | |
176b75b5 | 1604 | // We are going to use select twice so double the params |
4766a50c | 1605 | $params = array_merge($params, $params); |
d4bb6462 SH |
1606 | $basecategorysql = ($showbasecategories)?' OR cc.depth = 1':''; |
1607 | $sqlwhere .= " AND (cc.id {$select} OR cc.parent {$select}{$basecategorysql})"; | |
176b75b5 SH |
1608 | } |
1609 | ||
1610 | $categoriesrs = $DB->get_recordset_sql("$sqlselect $sqlwhere $sqlorder", $params); | |
1611 | $categories = array(); | |
1612 | foreach ($categoriesrs as $category) { | |
1613 | // Preload the context.. we'll need it when adding the category in order | |
1614 | // to format the category name. | |
1615 | context_helper::preload_from_record($category); | |
1616 | if (array_key_exists($category->id, $this->addedcategories)) { | |
1617 | // Do nothing, its already been added. | |
1618 | } else if ($category->parent == '0') { | |
1619 | // This is a root category lets add it immediately | |
1620 | $this->add_category($category, $this->rootnodes['courses']); | |
1621 | } else if (array_key_exists($category->parent, $this->addedcategories)) { | |
1622 | // This categories parent has already been added we can add this immediately | |
1623 | $this->add_category($category, $this->addedcategories[$category->parent]); | |
1624 | } else { | |
1625 | $categories[] = $category; | |
1626 | } | |
4766a50c | 1627 | } |
176b75b5 | 1628 | $categoriesrs->close(); |
e26507b3 SH |
1629 | |
1630 | // Now we have an array of categories we need to add them to the navigation. | |
1631 | while (!empty($categories)) { | |
1632 | $category = reset($categories); | |
1633 | if (array_key_exists($category->id, $this->addedcategories)) { | |
1634 | // Do nothing | |
1635 | } else if ($category->parent == '0') { | |
1636 | $this->add_category($category, $this->rootnodes['courses']); | |
1637 | } else if (array_key_exists($category->parent, $this->addedcategories)) { | |
1638 | $this->add_category($category, $this->addedcategories[$category->parent]); | |
4766a50c | 1639 | } else { |
e26507b3 SH |
1640 | // This category isn't in the navigation and niether is it's parent (yet). |
1641 | // We need to go through the category path and add all of its components in order. | |
1642 | $path = explode('/', trim($category->path, '/')); | |
1643 | foreach ($path as $catid) { | |
1644 | if (!array_key_exists($catid, $this->addedcategories)) { | |
1645 | // This category isn't in the navigation yet so add it. | |
1646 | $subcategory = $categories[$catid]; | |
c4afcf84 SH |
1647 | if ($subcategory->parent == '0') { |
1648 | // Yay we have a root category - this likely means we will now be able | |
1649 | // to add categories without problems. | |
1650 | $this->add_category($subcategory, $this->rootnodes['courses']); | |
1651 | } else if (array_key_exists($subcategory->parent, $this->addedcategories)) { | |
e26507b3 SH |
1652 | // The parent is in the category (as we'd expect) so add it now. |
1653 | $this->add_category($subcategory, $this->addedcategories[$subcategory->parent]); | |
1654 | // Remove the category from the categories array. | |
1655 | unset($categories[$catid]); | |
1656 | } else { | |
1657 | // We should never ever arrive here - if we have then there is a bigger | |
1658 | // problem at hand. | |
5607036a | 1659 | throw new coding_exception('Category path order is incorrect and/or there are missing categories'); |
e26507b3 | 1660 | } |
4766a50c | 1661 | } |
4766a50c SH |
1662 | } |
1663 | } | |
e26507b3 SH |
1664 | // Remove the category from the categories array now that we know it has been added. |
1665 | unset($categories[$category->id]); | |
4766a50c | 1666 | } |
8e8de15f SH |
1667 | if ($categoryid === self::LOAD_ALL_CATEGORIES) { |
1668 | $this->allcategoriesloaded = true; | |
1669 | } | |
e26507b3 | 1670 | // Check if there are any categories to load. |
8e8de15f SH |
1671 | if (count($categoriestoload) > 0) { |
1672 | $readytoloadcourses = array(); | |
1673 | foreach ($categoriestoload as $category) { | |
1674 | if ($this->can_add_more_courses_to_category($category)) { | |
1675 | $readytoloadcourses[] = $category; | |
1676 | } | |
1677 | } | |
1678 | if (count($readytoloadcourses)) { | |
1679 | $this->load_all_courses($readytoloadcourses); | |
1680 | } | |
4766a50c | 1681 | } |
58b602da SH |
1682 | |
1683 | // Look for all categories which have been loaded | |
1684 | if (!empty($this->addedcategories)) { | |
1685 | $categoryids = array(); | |
1686 | foreach ($this->addedcategories as $category) { | |
1687 | if ($this->can_add_more_courses_to_category($category)) { | |
1688 | $categoryids[] = $category->key; | |
1689 | } | |
1690 | } | |
bb900d13 PS |
1691 | if ($categoryids) { |
1692 | list($categoriessql, $params) = $DB->get_in_or_equal($categoryids, SQL_PARAMS_NAMED); | |
1693 | $params['limit'] = (!empty($CFG->navcourselimit))?$CFG->navcourselimit:20; | |
1694 | $sql = "SELECT cc.id, COUNT(c.id) AS coursecount | |
1695 | FROM {course_categories} cc | |
1696 | JOIN {course} c ON c.category = cc.id | |
1697 | WHERE cc.id {$categoriessql} | |
1698 | GROUP BY cc.id | |
1699 | HAVING COUNT(c.id) > :limit"; | |
1700 | $excessivecategories = $DB->get_records_sql($sql, $params); | |
1701 | foreach ($categories as &$category) { | |
1702 | if (array_key_exists($category->key, $excessivecategories) && !$this->can_add_more_courses_to_category($category)) { | |
30142c19 | 1703 | $url = new moodle_url('/course/index.php', array('categoryid' => $category->key)); |
bb900d13 PS |
1704 | $category->add(get_string('viewallcourses'), $url, self::TYPE_SETTING); |
1705 | } | |
58b602da SH |
1706 | } |
1707 | } | |
1708 | } | |
4766a50c SH |
1709 | } |
1710 | ||
1711 | /** | |
1712 | * Adds a structured category to the navigation in the correct order/place | |
1713 | * | |
db47f68f RT |
1714 | * @param stdClass $category category to be added in navigation. |
1715 | * @param navigation_node $parent parent navigation node | |
1716 | * @param int $nodetype type of node, if category is under MyHome then it's TYPE_MY_CATEGORY | |
1717 | * @return void. | |
4766a50c | 1718 | */ |
db47f68f | 1719 | protected function add_category(stdClass $category, navigation_node $parent, $nodetype = self::TYPE_CATEGORY) { |
e26507b3 | 1720 | if (array_key_exists($category->id, $this->addedcategories)) { |
563329e8 | 1721 | return; |
e26507b3 | 1722 | } |
30142c19 | 1723 | $url = new moodle_url('/course/index.php', array('categoryid' => $category->id)); |
b0c6dc1c | 1724 | $context = context_coursecat::instance($category->id); |
63390481 | 1725 | $categoryname = format_string($category->name, true, array('context' => $context)); |
db47f68f | 1726 | $categorynode = $parent->add($categoryname, $url, $nodetype, $categoryname, $category->id); |
e26507b3 SH |
1727 | if (empty($category->visible)) { |
1728 | if (has_capability('moodle/category:viewhiddencategories', get_system_context())) { | |
1729 | $categorynode->hidden = true; | |
1730 | } else { | |
1731 | $categorynode->display = false; | |
14337688 | 1732 | } |
4766a50c | 1733 | } |
082513ba | 1734 | $this->addedcategories[$category->id] = $categorynode; |
4766a50c SH |
1735 | } |
1736 | ||
3406acde SH |
1737 | /** |
1738 | * Loads the given course into the navigation | |
7d2a0492 | 1739 | * |
3406acde SH |
1740 | * @param stdClass $course |
1741 | * @return navigation_node | |
1742 | */ | |
1743 | protected function load_course(stdClass $course) { | |
98556b23 SH |
1744 | global $SITE; |
1745 | if ($course->id == $SITE->id) { | |
80c69522 | 1746 | // This is always loaded during initialisation |
e26507b3 SH |
1747 | return $this->rootnodes['site']; |
1748 | } else if (array_key_exists($course->id, $this->addedcourses)) { | |
80c69522 | 1749 | // The course has already been loaded so return a reference |
e26507b3 | 1750 | return $this->addedcourses[$course->id]; |
3406acde | 1751 | } else { |
80c69522 | 1752 | // Add the course |
e26507b3 | 1753 | return $this->add_course($course); |
3406acde | 1754 | } |
3406acde SH |
1755 | } |
1756 | ||
1757 | /** | |
1758 | * Loads all of the courses section into the navigation. | |
1759 | * | |
e010b850 MG |
1760 | * This function calls method from current course format, see |
1761 | * {@link format_base::extend_course_navigation()} | |
1762 | * If course module ($cm) is specified but course format failed to create the node, | |
1763 | * the activity node is created anyway. | |
3406acde | 1764 | * |
e010b850 | 1765 | * By default course formats call the method {@link global_navigation::load_generic_course_sections()} |
3406acde | 1766 | * |
3406acde SH |
1767 | * @param stdClass $course Database record for the course |
1768 | * @param navigation_node $coursenode The course node within the navigation | |
e010b850 MG |
1769 | * @param null|int $sectionnum If specified load the contents of section with this relative number |
1770 | * @param null|cm_info $cm If specified make sure that activity node is created (either | |
1771 | * in containg section or by calling load_stealth_activity() ) | |
3406acde | 1772 | */ |
e010b850 MG |
1773 | protected function load_course_sections(stdClass $course, navigation_node $coursenode, $sectionnum = null, $cm = null) { |
1774 | global $CFG, $SITE; | |
ee7084e9 | 1775 | require_once($CFG->dirroot.'/course/lib.php'); |
e010b850 MG |
1776 | if (isset($cm->sectionnum)) { |
1777 | $sectionnum = $cm->sectionnum; | |
1778 | } | |
1779 | if ($sectionnum !== null) { | |
1780 | $this->includesectionnum = $sectionnum; | |
1781 | } | |
1782 | course_get_format($course)->extend_course_navigation($this, $coursenode, $sectionnum, $cm); | |
1783 | if (isset($cm->id)) { | |
1784 | $activity = $coursenode->find($cm->id, self::TYPE_ACTIVITY); | |
1785 | if (empty($activity)) { | |
1786 | $activity = $this->load_stealth_activity($coursenode, get_fast_modinfo($course)); | |
1787 | } | |
1788 | } | |
1789 | } | |
3406acde | 1790 | |
e26507b3 SH |
1791 | /** |
1792 | * Generates an array of sections and an array of activities for the given course. | |
1793 | * | |
1794 | * This method uses the cache to improve performance and avoid the get_fast_modinfo call | |
1795 | * | |
1796 | * @param stdClass $course | |
1797 | * @return array Array($sections, $activities) | |
1798 | */ | |
1799 | protected function generate_sections_and_activities(stdClass $course) { | |
1800 | global $CFG; | |
1801 | require_once($CFG->dirroot.'/course/lib.php'); | |
1802 | ||
c18facf2 | 1803 | $modinfo = get_fast_modinfo($course); |
b5cf83f0 | 1804 | $sections = $modinfo->get_section_info_all(); |
850acb35 MG |
1805 | |
1806 | // For course formats using 'numsections' trim the sections list | |
1807 | $courseformatoptions = course_get_format($course)->get_format_options(); | |
1808 | if (isset($courseformatoptions['numsections'])) { | |
1809 | $sections = array_slice($sections, 0, $courseformatoptions['numsections']+1, true); | |
b5cf83f0 | 1810 | } |
850acb35 | 1811 | |
c18facf2 | 1812 | $activities = array(); |
e26507b3 | 1813 | |
c18facf2 | 1814 | foreach ($sections as $key => $section) { |
2d9c0d11 | 1815 | // Clone and unset summary to prevent $SESSION bloat (MDL-31802). |
51591b2c AO |
1816 | $sections[$key] = clone($section); |
1817 | unset($sections[$key]->summary); | |
c18facf2 SH |
1818 | $sections[$key]->hasactivites = false; |
1819 | if (!array_key_exists($section->section, $modinfo->sections)) { | |
1820 | continue; | |
1821 | } | |
1822 | foreach ($modinfo->sections[$section->section] as $cmid) { | |
1823 | $cm = $modinfo->cms[$cmid]; | |
1824 | if (!$cm->uservisible) { | |
e26507b3 SH |
1825 | continue; |
1826 | } | |
c18facf2 SH |
1827 | $activity = new stdClass; |
1828 | $activity->id = $cm->id; | |
1829 | $activity->course = $course->id; | |
1830 | $activity->section = $section->section; | |
1831 | $activity->name = $cm->name; | |
1832 | $activity->icon = $cm->icon; | |
1833 | $activity->iconcomponent = $cm->iconcomponent; | |
1834 | $activity->hidden = (!$cm->visible); | |
1835 | $activity->modname = $cm->modname; | |
1836 | $activity->nodetype = navigation_node::NODETYPE_LEAF; | |
1837 | $activity->onclick = $cm->get_on_click(); | |
1838 | $url = $cm->get_url(); | |
1839 | if (!$url) { | |
1840 | $activity->url = null; | |
1841 | $activity->display = false; | |
1842 | } else { | |
1843 | $activity->url = $cm->get_url()->out(); | |
1844 | $activity->display = true; | |
1845 | if (self::module_extends_navigation($cm->modname)) { | |
1846 | $activity->nodetype = navigation_node::NODETYPE_BRANCH; | |
d67e4821 | 1847 | } |
e26507b3 | 1848 | } |
c18facf2 SH |
1849 | $activities[$cmid] = $activity; |
1850 | if ($activity->display) { | |
1851 | $sections[$key]->hasactivites = true; | |
1852 | } | |
e26507b3 | 1853 | } |
e26507b3 | 1854 | } |
c18facf2 | 1855 | |
e26507b3 SH |
1856 | return array($sections, $activities); |
1857 | } | |
1858 | ||
3406acde SH |
1859 | /** |
1860 | * Generically loads the course sections into the course's navigation. | |
1861 | * | |
1862 | * @param stdClass $course | |
1863 | * @param navigation_node $coursenode | |
3406acde SH |
1864 | * @return array An array of course section nodes |
1865 | */ | |
ee7084e9 | 1866 | public function load_generic_course_sections(stdClass $course, navigation_node $coursenode) { |
98556b23 | 1867 | global $CFG, $DB, $USER, $SITE; |
df997f84 | 1868 | require_once($CFG->dirroot.'/course/lib.php'); |
435a512e | 1869 | |
e26507b3 | 1870 | list($sections, $activities) = $this->generate_sections_and_activities($course); |
0f4ab67d | 1871 | |
7487c856 | 1872 | $navigationsections = array(); |
e26507b3 | 1873 | foreach ($sections as $sectionid => $section) { |
7487c856 | 1874 | $section = clone($section); |
98556b23 | 1875 | if ($course->id == $SITE->id) { |
e26507b3 | 1876 | $this->load_section_activities($coursenode, $section->section, $activities); |
3406acde | 1877 | } else { |
ce4dfd27 | 1878 | if (!$section->uservisible || (!$this->showemptysections && |
d67e4821 | 1879 | !$section->hasactivites && $this->includesectionnum !== $section->section)) { |
3406acde SH |
1880 | continue; |
1881 | } | |
ce4dfd27 | 1882 | |
ee7084e9 MG |
1883 | $sectionname = get_section_name($course, $section); |
1884 | $url = course_get_url($course, $section->section, array('navigation' => true)); | |
ad470097 | 1885 | |
3406acde SH |
1886 | $sectionnode = $coursenode->add($sectionname, $url, navigation_node::TYPE_SECTION, null, $section->id); |
1887 | $sectionnode->nodetype = navigation_node::NODETYPE_BRANCH; | |
ce4dfd27 | 1888 | $sectionnode->hidden = (!$section->visible || !$section->available); |
e010b850 | 1889 | if ($this->includesectionnum !== false && $this->includesectionnum == $section->section) { |
e26507b3 | 1890 | $this->load_section_activities($sectionnode, $section->section, $activities); |
3406acde SH |
1891 | } |
1892 | $section->sectionnode = $sectionnode; | |
7487c856 | 1893 | $navigationsections[$sectionid] = $section; |
3406acde SH |
1894 | } |
1895 | } | |
7487c856 | 1896 | return $navigationsections; |
3406acde | 1897 | } |
e010b850 | 1898 | |
3406acde SH |
1899 | /** |
1900 | * Loads all of the activities for a section into the navigation structure. | |
1901 | * | |
1902 | * @param navigation_node $sectionnode | |
1903 | * @param int $sectionnumber | |
93123b17 | 1904 | * @param array $activities An array of activites as returned by {@link global_navigation::generate_sections_and_activities()} |
1580cfdb | 1905 | * @param stdClass $course The course object the section and activities relate to. |
3406acde SH |
1906 | * @return array Array of activity nodes |
1907 | */ | |
1580cfdb | 1908 | protected function load_section_activities(navigation_node $sectionnode, $sectionnumber, array $activities, $course = null) { |
98556b23 | 1909 | global $CFG, $SITE; |
dee1a0fd SH |
1910 | // A static counter for JS function naming |
1911 | static $legacyonclickcounter = 0; | |
3406acde | 1912 | |
e26507b3 | 1913 | $activitynodes = array(); |
4037098e SH |
1914 | if (empty($activities)) { |
1915 | return $activitynodes; | |
1916 | } | |
1917 | ||
1918 | if (!is_object($course)) { | |
1919 | $activity = reset($activities); | |
1920 | $courseid = $activity->course; | |
1921 | } else { | |
1922 | $courseid = $course->id; | |
1923 | } | |
98556b23 | 1924 | $showactivities = ($courseid != $SITE->id || !empty($CFG->navshowfrontpagemods)); |
4037098e | 1925 | |
e26507b3 SH |
1926 | foreach ($activities as $activity) { |
1927 | if ($activity->section != $sectionnumber) { | |
3406acde SH |
1928 | continue; |
1929 | } | |
e26507b3 SH |
1930 | if ($activity->icon) { |
1931 | $icon = new pix_icon($activity->icon, get_string('modulename', $activity->modname), $activity->iconcomponent); | |
3406acde | 1932 | } else { |
e26507b3 | 1933 | $icon = new pix_icon('icon', get_string('modulename', $activity->modname), $activity->modname); |
3406acde | 1934 | } |
dee1a0fd SH |
1935 | |
1936 | // Prepare the default name and url for the node | |
b0c6dc1c | 1937 | $activityname = format_string($activity->name, true, array('context' => context_module::instance($activity->id))); |
dee1a0fd SH |
1938 | $action = new moodle_url($activity->url); |
1939 | ||
1940 | // Check if the onclick property is set (puke!) | |
1941 | if (!empty($activity->onclick)) { | |
1942 | // Increment the counter so that we have a unique number. | |
1943 | $legacyonclickcounter++; | |
1944 | // Generate the function name we will use | |
1945 | $functionname = 'legacy_activity_onclick_handler_'.$legacyonclickcounter; | |
1946 | $propogrationhandler = ''; | |
1947 | // Check if we need to cancel propogation. Remember inline onclick | |
1948 | // events would return false if they wanted to prevent propogation and the | |
1949 | // default action. | |
1950 | if (strpos($activity->onclick, 'return false')) { | |
1951 | $propogrationhandler = 'e.halt();'; | |
1952 | } | |
1953 | // Decode the onclick - it has already been encoded for display (puke) | |
e5ad278a | 1954 | $onclick = htmlspecialchars_decode($activity->onclick, ENT_QUOTES); |
dee1a0fd SH |
1955 | // Build the JS function the click event will call |
1956 | $jscode = "function {$functionname}(e) { $propogrationhandler $onclick }"; | |
1957 | $this->page->requires->js_init_code($jscode); | |
1958 | // Override the default url with the new action link | |
1959 | $action = new action_link($action, $activityname, new component_action('click', $functionname)); | |
1960 | } | |
1961 | ||
1962 | $activitynode = $sectionnode->add($activityname, $action, navigation_node::TYPE_ACTIVITY, null, $activity->id, $icon); | |
e26507b3 SH |
1963 | $activitynode->title(get_string('modulename', $activity->modname)); |
1964 | $activitynode->hidden = $activity->hidden; | |
4037098e | 1965 | $activitynode->display = $showactivities && $activity->display; |
e26507b3 SH |
1966 | $activitynode->nodetype = $activity->nodetype; |
1967 | $activitynodes[$activity->id] = $activitynode; | |
3406acde SH |
1968 | } |
1969 | ||
e26507b3 | 1970 | return $activitynodes; |
3406acde | 1971 | } |
2a62743c PS |
1972 | /** |
1973 | * Loads a stealth module from unavailable section | |
1974 | * @param navigation_node $coursenode | |
1975 | * @param stdClass $modinfo | |
1976 | * @return navigation_node or null if not accessible | |
1977 | */ | |
1978 | protected function load_stealth_activity(navigation_node $coursenode, $modinfo) { | |
1979 | if (empty($modinfo->cms[$this->page->cm->id])) { | |
1980 | return null; | |
1981 | } | |
1982 | $cm = $modinfo->cms[$this->page->cm->id]; | |
1983 | if (!$cm->uservisible) { | |
1984 | return null; | |
1985 | } | |
1986 | if ($cm->icon) { | |
1987 | $icon = new pix_icon($cm->icon, get_string('modulename', $cm->modname), $cm->iconcomponent); | |
1988 | } else { | |
1989 | $icon = new pix_icon('icon', get_string('modulename', $cm->modname), $cm->modname); | |
1990 | } | |
0d8b6a69 | 1991 | $url = $cm->get_url(); |
2a62743c PS |
1992 | $activitynode = $coursenode->add(format_string($cm->name), $url, navigation_node::TYPE_ACTIVITY, null, $cm->id, $icon); |
1993 | $activitynode->title(get_string('modulename', $cm->modname)); | |
1994 | $activitynode->hidden = (!$cm->visible); | |
0d8b6a69 | 1995 | if (!$url) { |
1996 | // Don't show activities that don't have links! | |
2a62743c | 1997 | $activitynode->display = false; |
e26507b3 | 1998 | } else if (self::module_extends_navigation($cm->modname)) { |
2a62743c PS |
1999 | $activitynode->nodetype = navigation_node::NODETYPE_BRANCH; |
2000 | } | |
2001 | return $activitynode; | |
2002 | } | |
3406acde SH |
2003 | /** |
2004 | * Loads the navigation structure for the given activity into the activities node. | |
2005 | * | |
2006 | * This method utilises a callback within the modules lib.php file to load the | |
2007 | * content specific to activity given. | |
2008 | * | |
2009 | * The callback is a method: {modulename}_extend_navigation() | |
2010 | * Examples: | |
93123b17 EL |
2011 | * * {@link forum_extend_navigation()} |
2012 | * * {@link workshop_extend_navigation()} | |
3406acde | 2013 | * |
f0dcc212 | 2014 | * @param cm_info|stdClass $cm |
3406acde SH |
2015 | * @param stdClass $course |
2016 | * @param navigation_node $activity | |
2017 | * @return bool | |
2018 | */ | |
0d8b6a69 | 2019 | protected function load_activity($cm, stdClass $course, navigation_node $activity) { |
3406acde | 2020 | global $CFG, $DB; |
44303ca6 | 2021 | |
f0dcc212 SH |
2022 | // make sure we have a $cm from get_fast_modinfo as this contains activity access details |
2023 | if (!($cm instanceof cm_info)) { | |
2024 | $modinfo = get_fast_modinfo($course); | |
2025 | $cm = $modinfo->get_cm($cm->id); | |
2026 | } | |
577c8964 | 2027 | $activity->nodetype = navigation_node::NODETYPE_LEAF; |
3406acde SH |
2028 | $activity->make_active(); |
2029 | $file = $CFG->dirroot.'/mod/'.$cm->modname.'/lib.php'; | |
2030 | $function = $cm->modname.'_extend_navigation'; | |
2031 | ||
2032 | if (file_exists($file)) { | |
2033 | require_once($file); | |
2034 | if (function_exists($function)) { | |
2035 | $activtyrecord = $DB->get_record($cm->modname, array('id' => $cm->instance), '*', MUST_EXIST); | |
2036 | $function($activity, $course, $activtyrecord, $cm); | |
3406acde SH |
2037 | } |
2038 | } | |
577c8964 MG |
2039 | |
2040 | // Allow the active advanced grading method plugin to append module navigation | |
2041 | $featuresfunc = $cm->modname.'_supports'; | |
2042 | if (function_exists($featuresfunc) && $featuresfunc(FEATURE_ADVANCED_GRADING)) { | |
2043 | require_once($CFG->dirroot.'/grade/grading/lib.php'); | |
2044 | $gradingman = get_grading_manager($cm->context, $cm->modname); | |
2045 | $gradingman->extend_navigation($this, $activity); | |
2046 | } | |
2047 | ||
2048 | return $activity->has_children(); | |
3406acde SH |
2049 | } |
2050 | /** | |
4d00fded | 2051 | * Loads user specific information into the navigation in the appropriate place. |
3406acde SH |
2052 | * |
2053 | * If no user is provided the current user is assumed. | |
2054 | * | |
3406acde | 2055 | * @param stdClass $user |
4d00fded | 2056 | * @param bool $forceforcontext probably force something to be loaded somewhere (ask SamH if not sure what this means) |
3406acde | 2057 | * @return bool |
7a7e209d | 2058 | */ |
87c215de | 2059 | protected function load_for_user($user=null, $forceforcontext=false) { |
98556b23 | 2060 | global $DB, $CFG, $USER, $SITE; |
4f0c2d00 | 2061 | |
7a7e209d SH |
2062 | if ($user === null) { |
2063 | // We can't require login here but if the user isn't logged in we don't | |
2064 | // want to show anything | |
b9d4c7d3 | 2065 | if (!isloggedin() || isguestuser()) { |
7a7e209d SH |
2066 | return false; |
2067 | } | |
2068 | $user = $USER; | |
7a7e209d SH |
2069 | } else if (!is_object($user)) { |
2070 | // If the user is not an object then get them from the database | |
e141bc81 SH |
2071 | $select = context_helper::get_preload_record_columns_sql('ctx'); |
2072 | $sql = "SELECT u.*, $select | |
2073 | FROM {user} u | |
2074 | JOIN {context} ctx ON u.id = ctx.instanceid | |
2075 | WHERE u.id = :userid AND | |
2076 | ctx.contextlevel = :contextlevel"; | |
2077 | $user = $DB->get_record_sql($sql, array('userid' => (int)$user, 'contextlevel' => CONTEXT_USER), MUST_EXIST); | |
2078 | context_helper::preload_from_record($user); | |
7a7e209d | 2079 | } |
136ca7c8 SH |
2080 | |
2081 | $iscurrentuser = ($user->id == $USER->id); | |
2082 | ||
b0c6dc1c | 2083 | $usercontext = context_user::instance($user->id); |
7a7e209d SH |
2084 | |
2085 | // Get the course set against the page, by default this will be the site | |
3406acde SH |
2086 | $course = $this->page->course; |
2087 | $baseargs = array('id'=>$user->id); | |
98556b23 | 2088 | if ($course->id != $SITE->id && (!$iscurrentuser || $forceforcontext)) { |
58b602da | 2089 | $coursenode = $this->add_course($course, false, self::COURSE_CURRENT); |
7a7e209d | 2090 | $baseargs['course'] = $course->id; |
b0c6dc1c | 2091 | $coursecontext = context_course::instance($course->id); |
7a7e209d | 2092 | $issitecourse = false; |
7d2a0492 | 2093 | } else { |
7a7e209d | 2094 | // Load all categories and get the context for the system |
b0c6dc1c | 2095 | $coursecontext = context_system::instance(); |
7a7e209d SH |
2096 | $issitecourse = true; |
2097 | } | |
2098 | ||
2099 | // Create a node to add user information under. | |
87c215de | 2100 | if ($iscurrentuser && !$forceforcontext) { |
3406acde SH |
2101 | // If it's the current user the information will go under the profile root node |
2102 | $usernode = $this->rootnodes['myprofile']; | |
e96bc3f9 | 2103 | $course = get_site(); |
b0c6dc1c | 2104 | $coursecontext = context_course::instance($course->id); |
e96bc3f9 | 2105 | $issitecourse = true; |
7a7e209d SH |
2106 | } else { |
2107 | if (!$issitecourse) { | |
2108 | // Not the current user so add it to the participants node for the current course | |
3406acde | 2109 | $usersnode = $coursenode->get('participants', navigation_node::TYPE_CONTAINER); |
ad93ddd5 | 2110 | $userviewurl = new moodle_url('/user/view.php', $baseargs); |
7a7e209d SH |
2111 | } else { |
2112 | // This is the site so add a users node to the root branch | |
3406acde | 2113 | $usersnode = $this->rootnodes['users']; |
8b0614d9 DP |
2114 | if (has_capability('moodle/course:viewparticipants', $coursecontext)) { |
2115 | $usersnode->action = new moodle_url('/user/index.php', array('id'=>$course->id)); | |
2116 | } | |
ad93ddd5 | 2117 | $userviewurl = new moodle_url('/user/profile.php', $baseargs); |
7a7e209d | 2118 | } |
f5c1e621 SH |
2119 | if (!$usersnode) { |
2120 | // We should NEVER get here, if the course hasn't been populated | |
2121 | // with a participants node then the navigaiton either wasn't generated | |
2122 | // for it (you are missing a require_login or set_context call) or | |
2123 | // you don't have access.... in the interests of no leaking informatin | |
2124 | // we simply quit... | |
2125 | return false; | |
2126 | } | |
7a7e209d | 2127 | // Add a branch for the current user |
76565fa1 AG |
2128 | $canseefullname = has_capability('moodle/site:viewfullnames', $coursecontext); |
2129 | $usernode = $usersnode->add(fullname($user, $canseefullname), $userviewurl, self::TYPE_USER, null, $user->id); | |
3406acde | 2130 | |
5ac851fb SH |
2131 | if ($this->page->context->contextlevel == CONTEXT_USER && $user->id == $this->page->context->instanceid) { |
2132 | $usernode->make_active(); | |
2133 | } | |
7a7e209d SH |
2134 | } |
2135 | ||
2136 | // If the user is the current user or has permission to view the details of the requested | |
2137 | // user than add a view profile link. | |
507a7a9a | 2138 | if ($iscurrentuser || has_capability('moodle/user:viewdetails', $coursecontext) || has_capability('moodle/user:viewdetails', $usercontext)) { |
87c215de | 2139 | if ($issitecourse || ($iscurrentuser && !$forceforcontext)) { |
03d9401e MD |
2140 | $usernode->add(get_string('viewprofile'), new moodle_url('/user/profile.php',$baseargs)); |
2141 | } else { | |
2142 | $usernode->add(get_string('viewprofile'), new moodle_url('/user/view.php',$baseargs)); | |
2143 | } | |
7a7e209d SH |
2144 | } |
2145 | ||
356a6de3 SH |
2146 | if (!empty($CFG->navadduserpostslinks)) { |
2147 | // Add nodes for forum posts and discussions if the user can view either or both | |
2148 | // There are no capability checks here as the content of the page is based | |
2149 | // purely on the forums the current user has access too. | |
2150 | $forumtab = $usernode->add(get_string('forumposts', 'forum')); | |
2151 | $forumtab->add(get_string('posts', 'forum'), new moodle_url('/mod/forum/user.php', $baseargs)); | |
2152 | $forumtab->add(get_string('discussions', 'forum'), new moodle_url('/mod/forum/user.php', array_merge($baseargs, array('mode'=>'discussions')))); | |
2153 | } | |
7a7e209d | 2154 | |
27bad0a6 | 2155 | // Add blog nodes |
850d2db8 | 2156 | if (!empty($CFG->enableblogs)) { |
e26507b3 SH |
2157 | if (!$this->cache->cached('userblogoptions'.$user->id)) { |
2158 | require_once($CFG->dirroot.'/blog/lib.php'); | |
2159 | // Get all options for the user | |
2160 | $options = blog_get_options_for_user($user); | |
2161 | $this->cache->set('userblogoptions'.$user->id, $options); | |
2162 | } else { | |
2163 | $options = $this->cache->{'userblogoptions'.$user->id}; | |
2164 | } | |
2165 | ||
27bad0a6 SH |
2166 | if (count($options) > 0) { |
2167 | $blogs = $usernode->add(get_string('blogs', 'blog'), null, navigation_node::TYPE_CONTAINER); | |
c000545d JF |
2168 | foreach ($options as $type => $option) { |
2169 | if ($type == "rss") { | |
2170 | $blogs->add($option['string'], $option['link'], settings_navigation::TYPE_SETTING, null, null, new pix_icon('i/rss', '')); | |
2171 | } else { | |
2172 | $blogs->add($option['string'], $option['link']); | |
2173 | } | |
27bad0a6 SH |
2174 | } |
2175 | } | |
2176 | } | |
2177 | ||
5ac851fb SH |
2178 | if (!empty($CFG->messaging)) { |
2179 | $messageargs = null; | |
447df209 AD |
2180 | if ($USER->id != $user->id) { |
2181 | $messageargs = array('user1' => $user->id); | |
5ac851fb SH |
2182 | } |
2183 | $url = new moodle_url('/message/index.php',$messageargs); | |
2184 | $usernode->add(get_string('messages', 'message'), $url, self::TYPE_SETTING, null, 'messages'); | |
c81b9f69 | 2185 | } |
c81b9f69 | 2186 | |
d422fb22 | 2187 | if ($iscurrentuser && has_capability('moodle/user:manageownfiles', context_user::instance($USER->id))) { |
4c886459 | 2188 | $url = new moodle_url('/user/files.php'); |
82af55d7 | 2189 | $usernode->add(get_string('myfiles'), $url, self::TYPE_SETTING); |
78765507 DC |
2190 | } |
2191 | ||
2188a697 | 2192 | if (!empty($CFG->enablebadges) && $iscurrentuser && |
27806552 YB |
2193 | has_capability('moodle/badges:manageownbadges', context_user::instance($USER->id))) { |
2194 | $url = new moodle_url('/badges/mybadges.php'); | |
2195 | $usernode->add(get_string('mybadges', 'badges'), $url, self::TYPE_SETTING); | |
2196 | } | |
2197 | ||
7a7e209d | 2198 | // Add a node to view the users notes if permitted |
507a7a9a | 2199 | if (!empty($CFG->enablenotes) && has_any_capability(array('moodle/notes:manage', 'moodle/notes:view'), $coursecontext)) { |
3406acde SH |
2200 | $url = new moodle_url('/notes/index.php',array('user'=>$user->id)); |
2201 | if ($coursecontext->instanceid) { | |
2202 | $url->param('course', $coursecontext->instanceid); | |
2203 | } | |
2204 | $usernode->add(get_string('notes', 'notes'), $url); | |
7a7e209d SH |
2205 | } |
2206 | ||
7a7e209d | 2207 | // If the user is the current user add the repositories for the current user |
9acb8241 | 2208 | $hiddenfields = array_flip(explode(',', $CFG->hiddenuserfields)); |
7a7e209d | 2209 | if ($iscurrentuser) { |
e26507b3 SH |
2210 | if (!$this->cache->cached('contexthasrepos'.$usercontext->id)) { |
2211 | require_once($CFG->dirroot . '/repository/lib.php'); | |
2212 | $editabletypes = repository::get_editable_types($usercontext); | |
2213 | $haseditabletypes = !empty($editabletypes); | |
2214 | unset($editabletypes); | |
2215 | $this->cache->set('contexthasrepos'.$usercontext->id, $haseditabletypes); | |
2216 | } else { | |
2217 | $haseditabletypes = $this->cache->{'contexthasrepos'.$usercontext->id}; | |
2218 | } | |
2219 | if ($haseditabletypes) { | |
ad70376c | 2220 | $usernode->add(get_string('repositories', 'repository'), new moodle_url('/repository/manage_instances.php', array('contextid' => $usercontext->id))); |
7a7e209d | 2221 | } |
98556b23 | 2222 | } else if ($course->id == $SITE->id && has_capability('moodle/user:viewdetails', $usercontext) && (!in_array('mycourses', $hiddenfields) || has_capability('moodle/user:viewhiddendetails', $coursecontext))) { |
9acb8241 SH |
2223 | |
2224 | // Add view grade report is permitted | |
2225 | $reports = get_plugin_list('gradereport'); | |
2226 | arsort($reports); // user is last, we want to test it first | |
2227 | ||
2228 | $userscourses = enrol_get_users_courses($user->id); | |
2229 | $userscoursesnode = $usernode->add(get_string('courses')); | |
69816a5c | 2230 | |
9acb8241 | 2231 | foreach ($userscourses as $usercourse) { |
b0c6dc1c | 2232 | $usercoursecontext = context_course::instance($usercourse->id); |
8ebbb06a SH |
2233 | $usercourseshortname = format_string($usercourse->shortname, true, array('context' => $usercoursecontext)); |
2234 | $usercoursenode = $userscoursesnode->add($usercourseshortname, new moodle_url('/user/view.php', array('id'=>$user->id, 'course'=>$usercourse->id)), self::TYPE_CONTAINER); | |
9acb8241 SH |
2235 | |
2236 | $gradeavailable = has_capability('moodle/grade:viewall', $usercoursecontext); | |
2237 | if (!$gradeavailable && !empty($usercourse->showgrades) && is_array($reports) && !empty($reports)) { | |
2238 | foreach ($reports as $plugin => $plugindir) { | |
2239 | if (has_capability('gradereport/'.$plugin.':view', $usercoursecontext)) { | |
2240 | //stop when the first visible plugin is found | |
2241 | $gradeavailable = true; | |
2242 | break; | |
deaf04c7 | 2243 | } |
9acb8241 SH |
2244 | } |
2245 | } | |
2246 | ||
2247 | if ($gradeavailable) { | |
2248 | $url = new moodle_url('/grade/report/index.php', array('id'=>$usercourse->id)); | |
2249 | $usercoursenode->add(get_string('grades'), $url, self::TYPE_SETTING, null, null, new pix_icon('i/grades', '')); | |
2250 | } | |
2251 | ||
2252 | // Add a node to view the users notes if permitted | |
2253 | if (!empty($CFG->enablenotes) && has_any_capability(array('moodle/notes:manage', 'moodle/notes:view'), $usercoursecontext)) { | |
2254 | $url = new moodle_url('/notes/index.php',array('user'=>$user->id, 'course'=>$usercourse->id)); | |
2255 | $usercoursenode->add(get_string('notes', 'notes'), $url, self::TYPE_SETTING); | |
2256 | } | |
2257 | ||
1344b0ca | 2258 | if (can_access_course($usercourse, $user->id)) { |
9acb8241 SH |
2259 | $usercoursenode->add(get_string('entercourse'), new moodle_url('/course/view.php', array('id'=>$usercourse->id)), self::TYPE_SETTING, null, null, new pix_icon('i/course', '')); |
2260 | } | |
2261 | ||
4d00fded PS |
2262 | $reporttab = $usercoursenode->add(get_string('activityreports')); |
2263 | ||
2264 | $reports = get_plugin_list_with_function('report', 'extend_navigation_user', 'lib.php'); | |
2265 | foreach ($reports as $reportfunction) { | |
2266 | $reportfunction($reporttab, $user, $usercourse); | |
2267 | } | |
2268 | ||
4d00fded | 2269 | $reporttab->trim_if_empty(); |
9acb8241 | 2270 | } |
7a7e209d SH |
2271 | } |
2272 | return true; | |
2273 | } | |
2274 | ||
2275 | /** | |
3406acde | 2276 | * This method simply checks to see if a given module can extend the navigation. |
7d2a0492 | 2277 | * |
31fde54f | 2278 | * @todo (MDL-25290) A shared caching solution should be used to save details on what extends navigation. |
e26507b3 | 2279 | * |
7d2a0492 | 2280 | * @param string $modname |
2281 | * @return bool | |
2282 | */ | |
e010b850 | 2283 | public static function module_extends_navigation($modname) { |
7d2a0492 | 2284 | global $CFG; |
e26507b3 SH |
2285 | static $extendingmodules = array(); |
2286 | if (!array_key_exists($modname, $extendingmodules)) { | |
2287 | $extendingmodules[$modname] = false; | |
2288 | $file = $CFG->dirroot.'/mod/'.$modname.'/lib.php'; | |
2289 | if (file_exists($file)) { | |
2290 | $function = $modname.'_extend_navigation'; | |
2291 | require_once($file); | |
2292 | $extendingmodules[$modname] = (function_exists($function)); | |
7d2a0492 | 2293 | } |
2294 | } | |
e26507b3 | 2295 | return $extendingmodules[$modname]; |
7d2a0492 | 2296 | } |
2297 | /** | |
3406acde | 2298 | * Extends the navigation for the given user. |
435a512e | 2299 | * |
3406acde | 2300 | * @param stdClass $user A user from the database |
7d2a0492 | 2301 | */ |
3406acde SH |
2302 | public function extend_for_user($user) { |
2303 | $this->extendforuser[] = $user; | |
5d07e957 SH |
2304 | } |
2305 | ||
2306 | /** | |
2307 | * Returns all of the users the navigation is being extended for | |
2308 | * | |
31fde54f | 2309 | * @return array An array of extending users. |
5d07e957 SH |
2310 | */ |
2311 | public function get_extending_users() { | |
2312 | return $this->extendforuser; | |
7d2a0492 | 2313 | } |
7d2a0492 | 2314 | /** |
3406acde | 2315 | * Adds the given course to the navigation structure. |
7d2a0492 | 2316 | * |
3406acde | 2317 | * @param stdClass $course |
8e8de15f SH |
2318 | * @param bool $forcegeneric |
2319 | * @param bool $ismycourse | |
3406acde | 2320 | * @return navigation_node |
7d2a0492 | 2321 | */ |
6caf3f5c | 2322 | public function add_course(stdClass $course, $forcegeneric = false, $coursetype = self::COURSE_OTHER) { |
98556b23 | 2323 | global $CFG, $SITE; |
44303ca6 | 2324 | |
e26507b3 SH |
2325 | // We found the course... we can return it now :) |
2326 | if (!$forcegeneric && array_key_exists($course->id, $this->addedcourses)) { | |
2327 | return $this->addedcourses[$course->id]; | |
2328 | } | |
2329 | ||
b0c6dc1c | 2330 | $coursecontext = context_course::instance($course->id); |
8ebbb06a | 2331 | |
98556b23 | 2332 | if ($course->id != $SITE->id && !$course->visible) { |
e26507b3 SH |
2333 | if (is_role_switched($course->id)) { |
2334 | // user has to be able to access course in order to switch, let's skip the visibility test here | |
8ebbb06a | 2335 | } else if (!has_capability('moodle/course:viewhiddencourses', $coursecontext)) { |
e26507b3 | 2336 | return false; |
44303ca6 | 2337 | } |
7d2a0492 | 2338 | } |
7d2a0492 | 2339 | |
98556b23 | 2340 | $issite = ($course->id == $SITE->id); |
8ebbb06a | 2341 | $shortname = format_string($course->shortname, true, array('context' => $coursecontext)); |
d20337f1 SH |
2342 | $fullname = format_string($course->fullname, true, array('context' => $coursecontext)); |
2343 | // This is the name that will be shown for the course. | |
2344 | $coursename = empty($CFG->navshowfullcoursenames) ? $shortname : $fullname; | |
4766a50c SH |
2345 | |
2346 | if ($issite) { | |
3406acde | 2347 | $parent = $this; |
4766a50c | 2348 | $url = null; |
016ba638 | 2349 | if (empty($CFG->usesitenameforsitepages)) { |
d20337f1 | 2350 | $coursename = get_string('sitepages'); |
016ba638 | 2351 | } |
6caf3f5c ARN |
2352 | } else if ($coursetype == self::COURSE_CURRENT) { |
2353 | $parent = $this->rootnodes['currentcourse']; | |
2354 | $url = new moodle_url('/course/view.php', array('id'=>$course->id)); | |
2355 | } else if ($coursetype == self::COURSE_MY && !$forcegeneric) { | |
db47f68f | 2356 | if (!empty($CFG->navshowmycoursecategories) && ($parent = $this->rootnodes['mycourses']->find($course->category, self::TYPE_MY_CATEGORY))) { |
b0712163 SH |
2357 | // Nothing to do here the above statement set $parent to the category within mycourses. |
2358 | } else { | |
2359 | $parent = $this->rootnodes['mycourses']; | |
2360 | } | |
3406acde | 2361 | $url = new moodle_url('/course/view.php', array('id'=>$course->id)); |
7a7e209d | 2362 | } else { |
3406acde | 2363 | $parent = $this->rootnodes['courses']; |
a6855934 | 2364 | $url = new moodle_url('/course/view.php', array('id'=>$course->id)); |
58b602da SH |
2365 | if (!empty($course->category) && $this->show_categories($coursetype == self::COURSE_MY)) { |
2366 | if (!$this->is_category_fully_loaded($course->category)) { | |
8e8de15f | 2367 | // We need to load the category structure for this course |
80c69522 | 2368 | $this->load_all_categories($course->category, false); |
8e8de15f SH |
2369 | } |
2370 | if (array_key_exists($course->category, $this->addedcategories)) { | |
2371 | $parent = $this->addedcategories[$course->category]; | |
2372 | // This could lead to the course being created so we should check whether it is the case again | |
2373 | if (!$forcegeneric && array_key_exists($course->id, $this->addedcourses)) { | |
2374 | return $this->addedcourses[$course->id]; | |
2375 | } | |
3992a46e | 2376 | } |
4766a50c SH |
2377 | } |
2378 | } | |
2379 | ||
d20337f1 | 2380 | $coursenode = $parent->add($coursename, $url, self::TYPE_COURSE, $shortname, $course->id); |
3406acde SH |
2381 | $coursenode->nodetype = self::NODETYPE_BRANCH; |
2382 | $coursenode->hidden = (!$course->visible); | |
1d8a1a87 SH |
2383 | // We need to decode &'s here as they will have been added by format_string above and attributes will be encoded again |
2384 | // later. | |
2385 | $coursenode->title(str_replace('&', '&', $fullname)); | |
e26507b3 | 2386 | if (!$forcegeneric) { |
082513ba | 2387 | $this->addedcourses[$course->id] = $coursenode; |
e26507b3 | 2388 | } |
e26507b3 | 2389 | |
3406acde | 2390 | return $coursenode; |
7d2a0492 | 2391 | } |
8e8de15f SH |
2392 | |
2393 | /** | |
2394 | * Returns true if the category has already been loaded as have any child categories | |
2395 | * | |
2396 | * @param int $categoryid | |
2397 | * @return bool | |
2398 | */ | |
2399 | protected function is_category_fully_loaded($categoryid) { | |
2400 | return (array_key_exists($categoryid, $this->addedcategories) && ($this->allcategoriesloaded || $this->addedcategories[$categoryid]->children->count() > 0)); | |
2401 | } | |
2402 | ||
7d2a0492 | 2403 | /** |
3406acde | 2404 | * Adds essential course nodes to the navigation for the given course. |
7d2a0492 | 2405 | * |
3406acde | 2406 | * This method adds nodes such as reports, blogs and participants |
7d2a0492 | 2407 | * |
3406acde SH |
2408 | * @param navigation_node $coursenode |
2409 | * @param stdClass $course | |
31fde54f | 2410 | * @return bool returns true on successful addition of a node. |
7d2a0492 | 2411 | */ |
2027c10e | 2412 | public function add_course_essentials($coursenode, stdClass $course) { |
98556b23 | 2413 | global $CFG, $SITE; |
7d2a0492 | 2414 | |
98556b23 | 2415 | if ($course->id == $SITE->id) { |
3406acde | 2416 | return $this->add_front_page_course_essentials($coursenode, $course); |
7d2a0492 | 2417 | } |
7d2a0492 | 2418 | |
2027c10e | 2419 | if ($coursenode == false || !($coursenode instanceof navigation_node) || $coursenode->get('participants', navigation_node::TYPE_CONTAINER)) { |
3406acde | 2420 | return true; |
7d2a0492 | 2421 | } |
7d2a0492 | 2422 | |
3406acde SH |
2423 | //Participants |
2424 | if (has_capability('moodle/course:viewparticipants', $this->page->context)) { | |
3406acde SH |
2425 | $participants = $coursenode->add(get_string('participants'), new moodle_url('/user/index.php?id='.$course->id), self::TYPE_CONTAINER, get_string('participants'), 'participants'); |
2426 | $currentgroup = groups_get_course_group($course, true); | |
98556b23 | 2427 | if ($course->id == $SITE->id) { |
8bc8ab6e | 2428 | $filtervar = 'courseid'; |
3406acde SH |
2429 | $filterselect = ''; |
2430 | } else if ($course->id && !$currentgroup) { | |
8bc8ab6e | 2431 | $filtervar = 'courseid'; |
3406acde SH |
2432 | $filterselect = $course->id; |
2433 | } else { | |
8bc8ab6e | 2434 | $filtervar = 'groupid'; |
3406acde SH |
2435 | $filterselect = $currentgroup; |
2436 | } | |
2437 | $filterselect = clean_param($filterselect, PARAM_INT); | |
8f6c1f34 | 2438 | if (($CFG->bloglevel == BLOG_GLOBAL_LEVEL or ($CFG->bloglevel == BLOG_SITE_LEVEL and (isloggedin() and !isguestuser()))) |
b0c6dc1c | 2439 | and has_capability('moodle/blog:view', context_system::instance())) { |
8bc8ab6e | 2440 | $blogsurls = new moodle_url('/blog/index.php', array($filtervar => $filterselect)); |
ec21eaba | 2441 | $participants->add(get_string('blogscourse','blog'), $blogsurls->out()); |
3406acde SH |
2442 | } |
2443 | if (!empty($CFG->enablenotes) && (has_capability('moodle/notes:manage', $this->page->context) || has_capability('moodle/notes:view', $this->page->context))) { | |
41eae0d9 | 2444 | $participants->add(get_string('notes','notes'), new moodle_url('/notes/index.php', array('filtertype'=>'course', 'filterselect'=>$course->id))); |
3406acde | 2445 | } |
533299cb | 2446 | } else if (count($this->extendforuser) > 0 || $this->page->course->id == $course->id) { |
3406acde SH |
2447 | $participants = $coursenode->add(get_string('participants'), null, self::TYPE_CONTAINER, get_string('participants'), 'participants'); |
2448 | } | |
2449 | ||
27806552 | 2450 | // Badges. |
1dcb9ff1 YB |
2451 | if (!empty($CFG->enablebadges) && !empty($CFG->badges_allowcoursebadges) && |
2452 | has_capability('moodle/badges:viewbadges', $this->page->context)) { | |
2453 | $url = new moodle_url('/badges/view.php', array('type' => 2, 'id' => $course->id)); | |
27806552 YB |
2454 | |
2455 | $coursenode->add(get_string('coursebadges', 'badges'), null, | |
2456 | navigation_node::TYPE_CONTAINER, null, 'coursebadges'); | |
2457 | $coursenode->get('coursebadges')->add(get_string('badgesview', 'badges'), $url, | |
2458 | navigation_node::TYPE_SETTING, null, 'badgesview', | |
2459 | new pix_icon('i/badge', get_string('badgesview', 'badges'))); | |
2460 | } | |
2461 | ||
7d2a0492 | 2462 | return true; |
2463 | } | |
deaf04c7 | 2464 | /** |
31fde54f | 2465 | * This generates the structure of the course that won't be generated when |
deaf04c7 SH |
2466 | * the modules and sections are added. |
2467 | * | |
2468 | * Things such as the reports branch, the participants branch, blogs... get | |
2469 | * added to the course node by this method. | |
2470 | * | |
2471 | * @param navigation_node $coursenode | |
2472 | * @param stdClass $course | |
2473 | * @return bool True for successfull generation | |
2474 | */ | |
3406acde SH |
2475 | public function add_front_page_course_essentials(navigation_node $coursenode, stdClass $course) { |
2476 | global $CFG; | |
7d2a0492 | 2477 | |
1fa692ed | 2478 | if ($coursenode == false || $coursenode->get('frontpageloaded', navigation_node::TYPE_CUSTOM)) { |
3406acde | 2479 | return true; |
7a7e209d SH |
2480 | } |
2481 | ||
1fa692ed SH |
2482 | // Hidden node that we use to determine if the front page navigation is loaded. |
2483 | // This required as there are not other guaranteed nodes that may be loaded. | |
2484 | $coursenode->add('frontpageloaded', null, self::TYPE_CUSTOM, null, 'frontpageloaded')->display = false; | |
2485 | ||
3406acde | 2486 | //Participants |
b475cf4c | 2487 | if (has_capability('moodle/course:viewparticipants', get_system_context())) { |
3406acde SH |
2488 | $coursenode->add(get_string('participants'), new moodle_url('/user/index.php?id='.$course->id), self::TYPE_CUSTOM, get_string('participants'), 'participants'); |
2489 | } | |
435a512e | 2490 | |
83a5e4fc | 2491 | $filterselect = 0; |
593270c6 MD |
2492 | |
2493 | // Blogs | |
850d2db8 | 2494 | if (!empty($CFG->enableblogs) |
8f6c1f34 | 2495 | and ($CFG->bloglevel == BLOG_GLOBAL_LEVEL or ($CFG->bloglevel == BLOG_SITE_LEVEL and (isloggedin() and !isguestuser()))) |
b0c6dc1c | 2496 | and has_capability('moodle/blog:view', context_system::instance())) { |
8f6c1f34 | 2497 | $blogsurls = new moodle_url('/blog/index.php', array('courseid' => $filterselect)); |
ec21eaba | 2498 | $coursenode->add(get_string('blogssite','blog'), $blogsurls->out()); |
7d2a0492 | 2499 | } |
593270c6 | 2500 | |
27806552 | 2501 | //Badges |
1dcb9ff1 | 2502 | if (!empty($CFG->enablebadges) && has_capability('moodle/badges:viewbadges', $this->page->context)) { |
27806552 YB |
2503 | $url = new moodle_url($CFG->wwwroot . '/badges/view.php', array('type' => 1)); |
2504 | $coursenode->add(get_string('sitebadges', 'badges'), $url, navigation_node::TYPE_CUSTOM); | |
2505 | } | |
2506 | ||
593270c6 | 2507 | // Notes |
3406acde SH |
2508 | if (!empty($CFG->enablenotes) && (has_capability('moodle/notes:manage', $this->page->context) || has_capability('moodle/notes:view', $this->page->context))) { |
2509 | $coursenode->add(get_string('notes','notes'), new moodle_url('/notes/index.php', array('filtertype'=>'course', 'filterselect'=>$filterselect))); | |
2510 | } | |
593270c6 MD |
2511 | |
2512 | // Tags | |
2513 | if (!empty($CFG->usetags) && isloggedin()) { | |
3406acde | 2514 | $coursenode->add(get_string('tags', 'tag'), new moodle_url('/tag/search.php')); |
7d2a0492 | 2515 | } |
6644741d | 2516 | |
e7f9b7ab SH |
2517 | if (isloggedin()) { |
2518 | // Calendar | |
2519 | $calendarurl = new moodle_url('/calendar/view.php', array('view' => 'month')); | |
2520 | $coursenode->add(get_string('calendar', 'calendar'), $calendarurl, self::TYPE_CUSTOM, null, 'calendar'); | |
2521 | } | |
6644741d | 2522 | |
3406acde | 2523 | return true; |
6644741d | 2524 | } |
da3ab9c4 | 2525 | |
3406acde SH |
2526 | /** |
2527 | * Clears the navigation cache | |
2528 | */ | |
2529 | public function clear_cache() { | |
2530 | $this->cache->clear(); | |
da3ab9c4 | 2531 | } |
88f77c3c | 2532 | |
deaf04c7 SH |
2533 | /** |
2534 | * Sets an expansion limit for the navigation | |
2535 | * | |
2536 | * The expansion limit is used to prevent the display of content that has a type | |
2537 | * greater than the provided $type. | |
2538 | * | |
2539 | * Can be used to ensure things such as activities or activity content don't get | |
2540 | * shown on the navigation. | |
2541 | * They are still generated in order to ensure the navbar still makes sense. | |
2542 | * | |
2543 | * @param int $type One of navigation_node::TYPE_* | |
31fde54f | 2544 | * @return bool true when complete. |
deaf04c7 | 2545 | */ |
88f77c3c | 2546 | public function set_expansion_limit($type) { |
98556b23 | 2547 | global $SITE; |
88f77c3c | 2548 | $nodes = $this->find_all_of_type($type); |
8718ffba SH |
2549 | |
2550 | // We only want to hide specific types of nodes. | |
2551 | // Only nodes that represent "structure" in the navigation tree should be hidden. | |
2552 | // If we hide all nodes then we risk hiding vital information. | |
2553 | $typestohide = array( | |
2554 | self::TYPE_CATEGORY, | |
2555 | self::TYPE_COURSE, | |
2556 | self::TYPE_SECTION, | |
2557 | self::TYPE_ACTIVITY | |
2558 | ); | |
2559 | ||
ba7b1ca5 | 2560 | foreach ($nodes as $node) { |
1af67ecb | 2561 | // We need to generate the full site node |
98556b23 | 2562 | if ($type == self::TYPE_COURSE && $node->key == $SITE->id) { |
1af67ecb SH |
2563 | continue; |
2564 | } | |
ba7b1ca5 | 2565 | foreach ($node->children as $child) { |
8718ffba | 2566 | $child->hide($typestohide); |
88f77c3c SH |
2567 | } |
2568 | } | |
2569 | return true; | |
2570 | } | |
deaf04c7 SH |
2571 | /** |
2572 | * Attempts to get the navigation with the given key from this nodes children. | |
2573 | * | |
2574 | * This function only looks at this nodes children, it does NOT look recursivily. | |
2575 | * If the node can't be found then false is returned. | |
2576 | * | |
93123b17 | 2577 | * If you need to search recursivily then use the {@link global_navigation::find()} method. |
deaf04c7 | 2578 | * |
93123b17 | 2579 | * Note: If you are trying to set the active node {@link navigation_node::override_active_url()} |
deaf04c7 SH |
2580 | * may be of more use to you. |
2581 | * | |
2582 | * @param string|int $key The key of the node you wish to receive. | |
2583 | * @param int $type One of navigation_node::TYPE_* | |
2584 | * @return navigation_node|false | |
2585 | */ | |
e2b436d0 | 2586 | public function get($key, $type = null) { |
246a9b05 SH |
2587 | if (!$this->initialised) { |
2588 | $this->initialise(); | |
2589 | } | |
54dc15ab | 2590 | return parent::get($key, $type); |
e2b436d0 SH |
2591 | } |
2592 | ||
deaf04c7 | 2593 | /** |
31fde54f | 2594 | * Searches this nodes children and their children to find a navigation node |
deaf04c7 SH |
2595 | * with the matching key and type. |
2596 | * | |
2597 | * This method is recursive and searches children so until either a node is | |
31fde54f | 2598 | * found or there are no more nodes to search. |
deaf04c7 SH |
2599 | * |
2600 | * If you know that the node being searched for is a child of this node | |
93123b17 | 2601 | * then use the {@link global_navigation::get()} method instead. |
deaf04c7 | 2602 | * |
93123b17 | 2603 | * Note: If you are trying to set the active node {@link navigation_node::override_active_url()} |
deaf04c7 SH |
2604 | * may be of more use to you. |
2605 | * | |
2606 | * @param string|int $key The key of the node you wish to receive. | |
2607 | * @param int $type One of navigation_node::TYPE_* | |
2608 | * @return navigation_node|false | |
2609 | */ | |
e2b436d0 | 2610 | public function find($key, $type) { |
246a9b05 SH |
2611 | if (!$this->initialised) { |
2612 | $this->initialise(); | |
2613 | } | |
58b602da SH |
2614 | if ($type == self::TYPE_ROOTNODE && array_key_exists($key, $this->rootnodes)) { |
2615 | return $this->rootnodes[$key]; | |
2616 | } | |
54dc15ab | 2617 | return parent::find($key, $type); |
e2b436d0 | 2618 | } |
f87ce4e9 SH |
2619 | |
2620 | /** | |
2621 | * They've expanded the 'my courses' branch. | |
2622 | */ | |
2623 | protected function load_courses_enrolled() { | |
2624 | global $CFG, $DB; | |
2625 | $sortorder = 'visible DESC'; | |
a75dfd6f EL |
2626 | // Prevent undefined $CFG->navsortmycoursessort errors. |
2627 | if (empty($CFG->navsortmycoursessort)) { | |
2628 | $CFG->navsortmycoursessort = 'sortorder'; | |
2629 | } | |
f87ce4e9 SH |
2630 | // Append the chosen sortorder. |
2631 | $sortorder = $sortorder . ',' . $CFG->navsortmycoursessort . ' ASC'; | |
2632 | $courses = enrol_get_my_courses(null, $sortorder); | |
c629c91f | 2633 | if (count($courses) && $this->show_my_categories()) { |
f87ce4e9 SH |
2634 | // OK Actually we are loading categories. We only want to load categories that have a parent of 0. |
2635 | // In order to make sure we load everything required we must first find the categories that are not | |
2636 | // base categories and work out the bottom category in thier path. | |
2637 | $categoryids = array(); | |
2638 | foreach ($courses as $course) { | |
2639 | $categoryids[] = $course->category; | |
2640 | } | |
2641 | $categoryids = array_unique($categoryids); | |
2642 | list($sql, $params) = $DB->get_in_or_equal($categoryids); | |
2643 | $categories = $DB->get_recordset_select('course_categories', 'id '.$sql.' AND parent <> 0', $params, 'sortorder, id', 'id, path'); | |
2644 | foreach ($categories as $category) { | |
2645 | $bits = explode('/', trim($category->path,'/')); | |
2646 | $categoryids[] = array_shift($bits); | |
2647 | } | |
2648 | $categoryids = array_unique($categoryids); | |
2649 | $categories->close(); | |
2650 | ||
2651 | // Now we load the base categories. | |
2652 | list($sql, $params) = $DB->get_in_or_equal($categoryids); | |
2653 | $categories = $DB->get_recordset_select('course_categories', 'id '.$sql.' AND parent = 0', $params, 'sortorder, id'); | |
2654 | foreach ($categories as $category) { | |
257b59e7 | 2655 | $this->add_category($category, $this->rootnodes['mycourses'], self::TYPE_MY_CATEGORY); |
f87ce4e9 SH |
2656 | } |
2657 | $categories->close(); | |
2658 | } else { | |
2659 | foreach ($courses as $course) { | |
2660 | $this->add_course($course, false, self::COURSE_MY); | |
2661 | } | |
2662 | } | |
2663 | } | |
7d2a0492 | 2664 | } |
2665 | ||
2666 | /** | |
98556b23 | 2667 | * The global navigation class used especially for AJAX requests. |
7d2a0492 | 2668 | * |
2669 | * The primary methods that are used in the global navigation class have been overriden | |
2670 | * to ensure that only the relevant branch is generated at the root of the tree. | |
2671 | * This can be done because AJAX is only used when the backwards structure for the | |
2672 | * requested branch exists. | |
2673 | * This has been done only because it shortens the amounts of information that is generated | |
2674 | * which of course will speed up the response time.. because no one likes laggy AJAX. | |
2675 | * | |
31fde54f AG |
2676 | * @package core |
2677 | * @category navigation | |
7d2a0492 | 2678 | * @copyright 2009 Sam Hemelryk |
2679 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | |
2680 | */ | |
507a7a9a | 2681 | class global_navigation_for_ajax extends global_navigation { |
3406acde | 2682 | |
31fde54f | 2683 | /** @var int used for determining what type of navigation_node::TYPE_* is being used */ |
39ae5e54 | 2684 | protected $branchtype; |
31fde54f AG |
2685 | |
2686 | /** @var int the instance id */ | |
39ae5e54 SH |
2687 | protected $instanceid; |
2688 | ||
31fde54f | 2689 | /** @var array Holds an array of expandable nodes */ |
3406acde SH |
2690 | protected $expandable = array(); |
2691 | ||
7d2a0492 | 2692 | /** |
31fde54f AG |
2693 | * Constructs the navigation for use in an AJAX request |
2694 | * | |
2695 | * @param moodle_page $page moodle_page object | |
2696 | * @param int $branchtype | |
2697 | * @param int $id | |
3406acde | 2698 | */ |
246a9b05 | 2699 | public function __construct($page, $branchtype, $id) { |
4766a50c | 2700 | $this->page = $page; |
3406acde SH |
2701 | $this->cache = new navigation_cache(NAVIGATION_CACHE_NAME); |
2702 | $this->children = new navigation_node_collection(); | |
39ae5e54 SH |
2703 | $this->branchtype = $branchtype; |
2704 | $this->instanceid = $id; | |
2705 | $this->initialise(); | |
3406acde SH |
2706 | } |
2707 | /** | |
2708 | * Initialise the navigation given the type and id for the branch to expand. | |
7d2a0492 | 2709 | * |
31fde54f | 2710 | * @return array An array of the expandable nodes |
7d2a0492 | 2711 | */ |
39ae5e54 | 2712 | public function initialise() { |
58b602da | 2713 | global $DB, $SITE; |
507a7a9a | 2714 | |
7d2a0492 | 2715 | if ($this->initialised || during_initial_install()) { |
95b97515 | 2716 | return $this->expandable; |
7d2a0492 | 2717 | } |
246a9b05 SH |
2718 | $this->initialised = true; |
2719 | ||
2720 | $this->rootnodes = array(); | |
e26507b3 | 2721 | $this->rootnodes['site'] = $this->add_course($SITE); |
58b602da | 2722 | $this->rootnodes['mycourses'] = $this->add(get_string('mycourses'), new moodle_url('/my'), self::TYPE_ROOTNODE, null, 'mycourses'); |
246a9b05 | 2723 | $this->rootnodes['courses'] = $this->add(get_string('courses'), null, self::TYPE_ROOTNODE, null, 'courses'); |
507a7a9a SH |
2724 | |
2725 | // Branchtype will be one of navigation_node::TYPE_* | |
39ae5e54 | 2726 | switch ($this->branchtype) { |
58b602da SH |
2727 | case 0: |
2728 | if ($this->instanceid === 'mycourses') { | |
2729 | $this->load_courses_enrolled(); | |
2730 | } else if ($this->instanceid === 'courses') { | |
2731 | $this->load_courses_other(); | |
2732 | } | |
2733 | break; | |
4766a50c | 2734 | case self::TYPE_CATEGORY : |
d4bb6462 | 2735 | $this->load_category($this->instanceid); |
4766a50c | 2736 | break; |
db47f68f RT |
2737 | case self::TYPE_MY_CATEGORY : |
2738 | $this->load_category($this->instanceid, self::TYPE_MY_CATEGORY); | |
2739 | break; | |
507a7a9a | 2740 | case self::TYPE_COURSE : |
39ae5e54 | 2741 | $course = $DB->get_record('course', array('id' => $this->instanceid), '*', MUST_EXIST); |
04bcd1ce | 2742 | require_course_login($course, true, null, false, true); |
b0c6dc1c | 2743 | $this->page->set_context(context_course::instance($course->id)); |
3406acde SH |
2744 | $coursenode = $this->add_course($course); |
2745 | $this->add_course_essentials($coursenode, $course); | |
ee7084e9 | 2746 | $this->load_course_sections($course, $coursenode); |
7d2a0492 | 2747 | break; |
507a7a9a | 2748 | case self::TYPE_SECTION : |
3406acde | 2749 | $sql = 'SELECT c.*, cs.section AS sectionnumber |
507a7a9a SH |
2750 | FROM {course} c |
2751 | LEFT JOIN {course_sections} cs ON cs.course = c.id | |
2752 | WHERE cs.id = ?'; | |
39ae5e54 | 2753 | $course = $DB->get_record_sql($sql, array($this->instanceid), MUST_EXIST); |
04bcd1ce | 2754 | require_course_login($course, true, null, false, true); |
b0c6dc1c | 2755 | $this->page->set_context(context_course::instance($course->id)); |
3406acde SH |
2756 | $coursenode = $this->add_course($course); |
2757 | $this->add_course_essentials($coursenode, $course); | |
e010b850 | 2758 | $this->load_course_sections($course, $coursenode, $course->sectionnumber); |
7d2a0492 | 2759 | break; |
507a7a9a | 2760 | case self::TYPE_ACTIVITY : |
c78262b5 SH |
2761 | $sql = "SELECT c.* |
2762 | FROM {course} c | |
2763 | JOIN {course_modules} cm ON cm.course = c.id | |
2764 | WHERE cm.id = :cmid"; | |
2765 | $params = array('cmid' => $this->instanceid); | |
2766 | $course = $DB->get_record_sql($sql, $params, MUST_EXIST); | |
f0dcc212 SH |
2767 | $modinfo = get_fast_modinfo($course); |
2768 | $cm = $modinfo->get_cm($this->instanceid); | |
04bcd1ce | 2769 | require_course_login($course, true, $cm, false, true); |
b0c6dc1c | 2770 | $this->page->set_context(context_module::instance($cm->id)); |
3406acde | 2771 | $coursenode = $this->load_course($course); |
e010b850 MG |
2772 | if ($course->id != $SITE->id) { |
2773 | $this->load_course_sections($course, $coursenode, null, $cm); | |
1aa1e9b5 | 2774 | } |
e010b850 | 2775 | $modulenode = $this->load_activity($cm, $course, $coursenode->find($cm->id, self::TYPE_ACTIVITY)); |
7d2a0492 | 2776 | break; |
507a7a9a | 2777 | default: |
3406acde | 2778 | throw new Exception('Unknown type'); |
507a7a9a | 2779 | return $this->expandable; |
7d2a0492 | 2780 | } |
588a3953 | 2781 | |
98556b23 | 2782 | if ($this->page->context->contextlevel == CONTEXT_COURSE && $this->page->context->instanceid != $SITE->id) { |
588a3953 SH |
2783 | $this->load_for_user(null, true); |
2784 | } | |
2785 | ||
507a7a9a | 2786 | $this->find_expandable($this->expandable); |
507a7a9a | 2787 | return $this->expandable; |
246a9b05 SH |
2788 | } |
2789 | ||
58b602da SH |
2790 | /** |
2791 | * They've expanded the general 'courses' branch. | |
2792 | */ | |
2793 | protected function load_courses_other() { | |
2794 | $this->load_all_courses(); | |
2795 | } | |
2796 | ||
d4bb6462 SH |
2797 | /** |
2798 | * Loads a single category into the AJAX navigation. | |
2799 | * | |
2800 | * This function is special in that it doesn't concern itself with the parent of | |
2801 | * the requested category or its siblings. | |
2802 | * This is because with the AJAX navigation we know exactly what is wanted and only need to | |
2803 | * request that. | |
2804 | * | |
2805 | * @global moodle_database $DB | |
db47f68f RT |
2806 | * @param int $categoryid id of category to load in navigation. |
2807 | * @param int $nodetype type of node, if category is under MyHome then it's TYPE_MY_CATEGORY | |
2808 | * @return void. | |
d4bb6462 | 2809 | */ |
db47f68f | 2810 | protected function load_category($categoryid, $nodetype = self::TYPE_CATEGORY) { |
d4bb6462 SH |
2811 | global $CFG, $DB; |
2812 | ||
2813 | $limit = 20; | |
2814 | if (!empty($CFG->navcourselimit)) { | |
2815 | $limit = (int)$CFG->navcourselimit; | |
2816 | } | |
2817 | ||
2818 | $catcontextsql = context_helper::get_preload_record_columns_sql('ctx'); | |
2819 | $sql = "SELECT cc.*, $catcontextsql | |
2820 | FROM {course_categories} cc | |
2821 | JOIN {context} ctx ON cc.id = ctx.instanceid | |
2822 | WHERE ctx.contextlevel = ".CONTEXT_COURSECAT." AND | |
2823 | (cc.id = :categoryid1 OR cc.parent = :categoryid2) | |
957983b7 | 2824 | ORDER BY cc.depth ASC, cc.sortorder ASC, cc.id ASC"; |
d4bb6462 SH |
2825 | $params = array('categoryid1' => $categoryid, 'categoryid2' => $categoryid); |
2826 | $categories = $DB->get_recordset_sql($sql, $params, 0, $limit); | |
0627e1ce | 2827 | $categorylist = array(); |
d4bb6462 SH |
2828 | $subcategories = array(); |
2829 | $basecategory = null; | |
2830 | foreach ($categories as $category) { | |
0627e1ce | 2831 | $categorylist[] = $category->id; |
d4bb6462 SH |
2832 | context_helper::preload_from_record($category); |
2833 | if ($category->id == $categoryid) { | |
db47f68f | 2834 | $this->add_category($category, $this, $nodetype); |
d4bb6462 SH |
2835 | $basecategory = $this->addedcategories[$category->id]; |
2836 | } else { | |
2837 | $subcategories[] = $category; | |
2838 | } | |
2839 | } | |
2840 | $categories->close(); | |
2841 | ||
2842 | if (!is_null($basecategory)) { | |
d4bb6462 | 2843 | foreach ($subcategories as $category) { |
db47f68f | 2844 | $this->add_category($category, $basecategory, $nodetype); |
d4bb6462 SH |
2845 | } |
2846 | } | |
2847 | ||
db47f68f RT |
2848 | // If category is shown in MyHome then only show enrolled courses, else show all courses. |
2849 | if ($nodetype === self::TYPE_MY_CATEGORY) { | |
2850 | $courses = enrol_get_my_courses(); | |
2851 | foreach ($courses as $course) { | |
0627e1ce RT |
2852 | // Add course if it's in category. |
2853 | if (in_array($course->category, $categorylist)) { | |
2854 | $this->add_course($course, true, self::COURSE_MY); | |
2855 | } | |
db47f68f RT |
2856 | } |
2857 | } else { | |
2858 | $courses = $DB->get_recordset('course', array('category' => $categoryid), 'sortorder', '*' , 0, $limit); | |
2859 | foreach ($courses as $course) { | |
2860 | $this->add_course($course); | |
2861 | } | |
2862 | $courses->close(); | |
d4bb6462 | 2863 | } |
d4bb6462 SH |
2864 | } |
2865 | ||
deaf04c7 SH |
2866 | /** |
2867 | * Returns an array of expandable nodes | |
2868 | * @return array | |
2869 | */ | |
246a9b05 SH |
2870 | public function get_expandable() { |
2871 | return $this->expandable; | |
7d2a0492 | 2872 | } |
7d2a0492 | 2873 | } |
2874 | ||
2875 | /** | |
2876 | * Navbar class | |
2877 | * | |
2878 | * This class is used to manage the navbar, which is initialised from the navigation | |
2879 | * object held by PAGE | |
2880 | * | |
31fde54f AG |
2881 | * @package core |
2882 | * @category navigation | |
7d2a0492 | 2883 | * @copyright 2009 Sam Hemelryk |
2884 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | |
2885 | */ | |
2886 | class navbar extends navigation_node { | |
31fde54f | 2887 | /** @var bool A switch for whether the navbar is initialised or not */ |
7d2a0492 | 2888 | protected $initialised = false; |
31fde54f | 2889 | /** @var mixed keys used to reference the nodes on the navbar */ |
7d2a0492 | 2890 | protected $keys = array(); |
31fde54f | 2891 | /** @var null|string content of the navbar */ |
7d2a0492 | 2892 | protected $content = null; |
31fde54f | 2893 | /** @var moodle_page object the moodle page that this navbar belongs to */ |
7d2a0492 | 2894 | protected $page; |
31fde54f | 2895 | /** @var bool A switch for whether to ignore the active navigation information */ |
31759089 | 2896 | protected $ignoreactive = false; |
31fde54f | 2897 | /** @var bool A switch to let us know if we are in the middle of an install */ |
7d2a0492 | 2898 | protected $duringinstall = false; |
31fde54f | 2899 | /** @var bool A switch for whether the navbar has items */ |
7a7e209d | 2900 | protected $hasitems = false; |
31fde54f | 2901 | /** @var array An array of navigation nodes for the navbar */ |
3406acde | 2902 | protected $items; |
31fde54f | 2903 | /** @var array An array of child node objects */ |
3406acde | 2904 | public $children = array(); |
31fde54f | 2905 | /** @var bool A switch for whether we want to include the root node in the navbar */ |
4d5059d4 | 2906 | public $includesettingsbase = false; |
7d2a0492 | 2907 | /** |
2908 | * The almighty constructor | |
3406acde SH |
2909 | * |
2910 | * @param moodle_page $page | |
7d2a0492 | 2911 | */ |
3406acde | 2912 | public function __construct(moodle_page $page) { |
507a7a9a | 2913 | global $CFG; |
7d2a0492 | 2914 | if (during_initial_install()) { |
2915 | $this->duringinstall = true; | |
2916 | return false; | |
2917 | } | |
2918 | $this->page = $page; | |
2919 | $this->text = get_string('home'); | |
2920 | $this->shorttext = get_string('home'); | |
2921 | $this->action = new moodle_url($CFG->wwwroot); | |
2922 | $this->nodetype = self::NODETYPE_BRANCH; | |
2923 | $this->type = self::TYPE_SYSTEM; | |
2924 | } | |
2925 | ||
2926 | /** | |
2927 | * Quick check to see if the navbar will have items in. | |
2928 | * | |
2929 | * @return bool Returns true if the navbar will have items, false otherwise | |
2930 | */ | |
2931 | public function has_items() { | |
2932 | if ($this->duringinstall) { | |
2933 | return false; | |
7a7e209d SH |
2934 | } else if ($this->hasitems !== false) { |
2935 | return true; | |
7d2a0492 | 2936 | } |
3406acde | 2937 | $this->page->navigation->initialise($this->page); |
bf6c37c7 | 2938 | |
7a7e209d | 2939 | $activenodefound = ($this->page->navigation->contains_active_node() || |
3406acde | 2940 | $this->page->settingsnav->contains_active_node()); |
bf6c37c7 | 2941 | |
3406acde | 2942 | $outcome = (count($this->children)>0 || (!$this->ignoreactive && $activenodefound)); |
7a7e209d | 2943 | $this->hasitems = $outcome; |
bf6c37c7 | 2944 | return $outcome; |
31759089 | 2945 | } |
2946 | ||
3406acde SH |
2947 | /** |
2948 | * Turn on/off ignore active | |
2949 | * | |
2950 | * @param bool $setting | |
2951 | */ | |
31759089 | 2952 | public function ignore_active($setting=true) { |
2953 | $this->ignoreactive = ($setting); | |
7d2a0492 | 2954 | } |
31fde54f AG |
2955 | |
2956 | /** | |
2957 | * Gets a navigation node | |
2958 | * | |
2959 | * @param string|int $key for referencing the navbar nodes | |
2960 | * @param int $type navigation_node::TYPE_* | |
2961 | * @return navigation_node|bool | |
2962 | */ | |
3406acde SH |
2963 | public function get($key, $type = null) { |
2964 | foreach ($this->children as &$child) { | |
2965 | if ($child->key === $key && ($type == null || $type == $child->type)) { | |
2966 | return $child; | |
2967 | } | |
2968 | } | |
2969 | return false; | |
2970 | } | |
7d2a0492 | 2971 | /** |
3406acde | 2972 | * Returns an array of navigation_node's that make up the navbar. |
435a512e | 2973 | * |
3406acde | 2974 | * @return array |
7d2a0492 | 2975 | */ |
3406acde | 2976 | public function get_items() { |
abb29010 | 2977 | global $CFG; |
3406acde | 2978 | $items = array(); |
7d2a0492 | 2979 | // Make sure that navigation is initialised |
7a7e209d | 2980 | if (!$this->has_items()) { |
3406acde | 2981 | return $items; |
7a7e209d | 2982 | } |
3406acde SH |
2983 | if ($this->items !== null) { |
2984 | return $this->items; | |
7d2a0492 | 2985 | } |
2986 | ||
3406acde SH |
2987 | if (count($this->children) > 0) { |
2988 | // Add the custom children | |
2989 | $items = array_reverse($this->children); | |
2990 | } | |
117bd748 | 2991 | |
3406acde SH |
2992 | $navigationactivenode = $this->page->navigation->find_active_node(); |
2993 | $settingsactivenode = $this->page->settingsnav->find_active_node(); | |
0b29477b | 2994 | |
7d2a0492 | 2995 | // Check if navigation contains the active node |
0b29477b | 2996 | if (!$this->ignoreactive) { |
435a512e | 2997 | |
3406acde | 2998 | if ($navigationactivenode && $settingsactivenode) { |
0b29477b | 2999 | // Parse a combined navigation tree |
3406acde SH |
3000 | while ($settingsactivenode && $settingsactivenode->parent !== null) { |
3001 | if (!$settingsactivenode->mainnavonly) { | |
3002 | $items[] = $settingsactivenode; | |
3003 | } | |
3004 | $settingsactivenode = $settingsactivenode->parent; | |
3005 | } | |
4d5059d4 SH |
3006 | if (!$this->includesettingsbase) { |
3007 | // Removes the first node from the settings (root node) from the list | |
3008 | array_pop($items); | |
3009 | } | |
3406acde SH |
3010 | while ($navigationactivenode && $navigationactivenode->parent !== null) { |
3011 | if (!$navigationactivenode->mainnavonly) { | |
3012 | $items[] = $navigationactivenode; | |
3013 | } | |
823efced DW |
3014 | if (!empty($CFG->navshowcategories) && |
3015 | $navigationactivenode->type === self::TYPE_COURSE && | |
3016 | $navigationactivenode->parent->key === 'currentcourse') { | |
abb29010 | 3017 | $items = array_merge($items, $this->get_course_categories()); |
75bdae9f | 3018 | } |
3406acde | 3019 | $navigationactivenode = $navigationactivenode->parent; |
0b29477b | 3020 | } |
3406acde | 3021 | } else if ($navigationactivenode) { |
0b29477b | 3022 | // Parse the navigation tree to get the active node |
3406acde SH |
3023 | while ($navigationactivenode && $navigationactivenode->parent !== null) { |
3024 | if (!$navigationactivenode->mainnavonly) { | |
3025 | $items[] = $navigationactivenode; | |
3026 | } | |
823efced DW |
3027 | if (!empty($CFG->navshowcategories) && |
3028 | $navigationactivenode->type === self::TYPE_COURSE && | |
3029 | $navigationactivenode->parent->key === 'currentcourse') { | |
abb29010 | 3030 | $items = array_merge($items, $this->get_course_categories()); |
75bdae9f | 3031 | } |
3406acde SH |
3032 | $navigationactivenode = $navigationactivenode->parent; |
3033 | } | |
3034 | } else if ($settingsactivenode) { | |
0b29477b | 3035 | // Parse the settings navigation to get the active node |
3406acde SH |
3036 | while ($settingsactivenode && $settingsactivenode->parent !== null) { |
3037 | if (!$settingsactivenode->mainnavonly) { | |
3038 | $items[] = $settingsactivenode; | |
3039 | } | |
3040 | $settingsactivenode = $settingsactivenode->parent; | |
3041 | } | |
0b29477b | 3042 | } |
7d2a0492 | 3043 | } |
a3bbac8b | 3044 | |
3406acde SH |
3045 | $items[] = new navigation_node(array( |
3046 | 'text'=>$this->page->navigation->text, | |
3047 | 'shorttext'=>$this->page->navigation->shorttext, | |
3048 | 'key'=>$this->page->navigation->key, | |
3049 | 'action'=>$this->page->navigation->action | |
3050 | )); | |
a3bbac8b | 3051 | |
3406acde SH |
3052 | $this->items = array_reverse($items); |
3053 | return $this->items; | |
7d2a0492 | 3054 | } |
507a7a9a | 3055 | |
75bdae9f PN |
3056 | /** |
3057 | * Get the list of categories leading to this course. | |
3058 | * | |
3059 | * This function is used by {@link navbar::get_items()} to add back the "courses" | |
3060 | * node and category chain leading to the current course. Note that this is only ever | |
3061 | * called for the current course, so we don't need to bother taking in any parameters. |