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