Commit | Line | Data |
---|---|---|
7d2a0492 | 1 | <?php |
2 | ||
3 | // This file is part of Moodle - http://moodle.org/ | |
4 | // | |
5 | // Moodle is free software: you can redistribute it and/or modify | |
6 | // it under the terms of the GNU General Public License as published by | |
7 | // the Free Software Foundation, either version 3 of the License, or | |
8 | // (at your option) any later version. | |
9 | // | |
10 | // Moodle is distributed in the hope that it will be useful, | |
11 | // but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 | // GNU General Public License for more details. | |
14 | // | |
15 | // You should have received a copy of the GNU General Public License | |
16 | // along with Moodle. If not, see <http://www.gnu.org/licenses/>. | |
17 | ||
18 | /** | |
19 | * This file contains classes used to manage the navigation structures in Moodle | |
20 | * and was introduced as part of the changes occuring in Moodle 2.0 | |
21 | * | |
22 | * @since 2.0 | |
23 | * @package moodlecore | |
24 | * @subpackage navigation | |
25 | * @copyright 2009 Sam Hemelryk | |
26 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | |
27 | */ | |
28 | ||
f5b5a822 | 29 | /** |
2561c4e9 | 30 | * The name that will be used to separate the navigation cache within SESSION |
f5b5a822 | 31 | */ |
32 | define('NAVIGATION_CACHE_NAME', 'navigation'); | |
33 | ||
7d2a0492 | 34 | /** |
35 | * This class is used to represent a node in a navigation tree | |
36 | * | |
37 | * This class is used to represent a node in a navigation tree within Moodle, | |
38 | * the tree could be one of global navigation, settings navigation, or the navbar. | |
39 | * Each node can be one of two types either a Leaf (default) or a branch. | |
40 | * When a node is first created it is created as a leaf, when/if children are added | |
41 | * the node then becomes a branch. | |
42 | * | |
43 | * @package moodlecore | |
babb3911 | 44 | * @subpackage navigation |
7d2a0492 | 45 | * @copyright 2009 Sam Hemelryk |
46 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | |
47 | */ | |
3406acde SH |
48 | class navigation_node implements renderable { |
49 | /** @var int Used to identify this node a leaf (default) 0 */ | |
507a7a9a | 50 | const NODETYPE_LEAF = 0; |
3406acde | 51 | /** @var int Used to identify this node a branch, happens with children 1 */ |
7d2a0492 | 52 | const NODETYPE_BRANCH = 1; |
3406acde | 53 | /** @var null Unknown node type null */ |
7d2a0492 | 54 | const TYPE_UNKNOWN = null; |
3406acde SH |
55 | /** @var int System node type 0 */ |
56 | const TYPE_ROOTNODE = 0; | |
57 | /** @var int System node type 1 */ | |
58 | const TYPE_SYSTEM = 1; | |
59 | /** @var int Category node type 10 */ | |
7d2a0492 | 60 | const TYPE_CATEGORY = 10; |
3406acde | 61 | /** @var int Course node type 20 */ |
7d2a0492 | 62 | const TYPE_COURSE = 20; |
3406acde | 63 | /** @var int Course Structure node type 30 */ |
507a7a9a | 64 | const TYPE_SECTION = 30; |
3406acde | 65 | /** @var int Activity node type, e.g. Forum, Quiz 40 */ |
7d2a0492 | 66 | const TYPE_ACTIVITY = 40; |
3406acde | 67 | /** @var int Resource node type, e.g. Link to a file, or label 50 */ |
7d2a0492 | 68 | const TYPE_RESOURCE = 50; |
3406acde | 69 | /** @var int A custom node type, default when adding without specifing type 60 */ |
7d2a0492 | 70 | const TYPE_CUSTOM = 60; |
3406acde | 71 | /** @var int Setting node type, used only within settings nav 70 */ |
7d2a0492 | 72 | const TYPE_SETTING = 70; |
3406acde | 73 | /** @var int Setting node type, used only within settings nav 80 */ |
507a7a9a | 74 | const TYPE_USER = 80; |
3406acde SH |
75 | /** @var int Setting node type, used for containers of no importance 90 */ |
76 | const TYPE_CONTAINER = 90; | |
7d2a0492 | 77 | |
78 | /** @var int Parameter to aid the coder in tracking [optional] */ | |
79 | public $id = null; | |
80 | /** @var string|int The identifier for the node, used to retrieve the node */ | |
81 | public $key = null; | |
82 | /** @var string The text to use for the node */ | |
83 | public $text = null; | |
84 | /** @var string Short text to use if requested [optional] */ | |
85 | public $shorttext = null; | |
86 | /** @var string The title attribute for an action if one is defined */ | |
87 | public $title = null; | |
88 | /** @var string A string that can be used to build a help button */ | |
89 | public $helpbutton = null; | |
3406acde | 90 | /** @var moodle_url|action_link|null An action for the node (link) */ |
7d2a0492 | 91 | public $action = null; |
f9fc1461 | 92 | /** @var pix_icon The path to an icon to use for this node */ |
7d2a0492 | 93 | public $icon = null; |
94 | /** @var int See TYPE_* constants defined for this class */ | |
95 | public $type = self::TYPE_UNKNOWN; | |
96 | /** @var int See NODETYPE_* constants defined for this class */ | |
97 | public $nodetype = self::NODETYPE_LEAF; | |
98 | /** @var bool If set to true the node will be collapsed by default */ | |
99 | public $collapse = false; | |
100 | /** @var bool If set to true the node will be expanded by default */ | |
101 | public $forceopen = false; | |
3406acde | 102 | /** @var array An array of CSS classes for the node */ |
7d2a0492 | 103 | public $classes = array(); |
3406acde | 104 | /** @var navigation_node_collection An array of child nodes */ |
7d2a0492 | 105 | public $children = array(); |
106 | /** @var bool If set to true the node will be recognised as active */ | |
107 | public $isactive = false; | |
3406acde | 108 | /** @var bool If set to true the node will be dimmed */ |
7d2a0492 | 109 | public $hidden = false; |
110 | /** @var bool If set to false the node will not be displayed */ | |
111 | public $display = true; | |
112 | /** @var bool If set to true then an HR will be printed before the node */ | |
113 | public $preceedwithhr = false; | |
114 | /** @var bool If set to true the the navigation bar should ignore this node */ | |
115 | public $mainnavonly = false; | |
116 | /** @var bool If set to true a title will be added to the action no matter what */ | |
117 | public $forcetitle = false; | |
3406acde SH |
118 | /** @var navigation_node A reference to the node parent */ |
119 | public $parent = null; | |
493a48f3 SH |
120 | /** @var bool Override to not display the icon even if one is provided **/ |
121 | public $hideicon = false; | |
7d2a0492 | 122 | /** @var array */ |
b14ae498 | 123 | protected $namedtypes = array(0=>'system',10=>'category',20=>'course',30=>'structure',40=>'activity',50=>'resource',60=>'custom',70=>'setting', 80=>'user'); |
7d2a0492 | 124 | /** @var moodle_url */ |
125 | protected static $fullmeurl = null; | |
d9d2817a SH |
126 | /** @var bool toogles auto matching of active node */ |
127 | public static $autofindactive = true; | |
7d2a0492 | 128 | |
129 | /** | |
3406acde | 130 | * Constructs a new navigation_node |
7d2a0492 | 131 | * |
3406acde SH |
132 | * @param array|string $properties Either an array of properties or a string to use |
133 | * as the text for the node | |
7d2a0492 | 134 | */ |
135 | public function __construct($properties) { | |
7d2a0492 | 136 | if (is_array($properties)) { |
3406acde SH |
137 | // Check the array for each property that we allow to set at construction. |
138 | // text - The main content for the node | |
139 | // shorttext - A short text if required for the node | |
140 | // icon - The icon to display for the node | |
141 | // type - The type of the node | |
142 | // key - The key to use to identify the node | |
143 | // parent - A reference to the nodes parent | |
144 | // action - The action to attribute to this node, usually a URL to link to | |
7d2a0492 | 145 | if (array_key_exists('text', $properties)) { |
146 | $this->text = $properties['text']; | |
147 | } | |
148 | if (array_key_exists('shorttext', $properties)) { | |
149 | $this->shorttext = $properties['shorttext']; | |
150 | } | |
7081714d SH |
151 | if (!array_key_exists('icon', $properties)) { |
152 | $properties['icon'] = new pix_icon('i/navigationitem', 'moodle'); | |
153 | } | |
154 | $this->icon = $properties['icon']; | |
155 | if ($this->icon instanceof pix_icon) { | |
156 | if (empty($this->icon->attributes['class'])) { | |
157 | $this->icon->attributes['class'] = 'navicon'; | |
158 | } else { | |
159 | $this->icon->attributes['class'] .= ' navicon'; | |
7da3a799 | 160 | } |
7d2a0492 | 161 | } |
162 | if (array_key_exists('type', $properties)) { | |
163 | $this->type = $properties['type']; | |
164 | } else { | |
165 | $this->type = self::TYPE_CUSTOM; | |
166 | } | |
167 | if (array_key_exists('key', $properties)) { | |
168 | $this->key = $properties['key']; | |
169 | } | |
3406acde SH |
170 | if (array_key_exists('parent', $properties)) { |
171 | $this->parent = $properties['parent']; | |
172 | } | |
173 | // This needs to happen last because of the check_if_active call that occurs | |
174 | if (array_key_exists('action', $properties)) { | |
175 | $this->action = $properties['action']; | |
176 | if (is_string($this->action)) { | |
177 | $this->action = new moodle_url($this->action); | |
178 | } | |
179 | if (self::$autofindactive) { | |
180 | $this->check_if_active(); | |
181 | } | |
182 | } | |
7d2a0492 | 183 | } else if (is_string($properties)) { |
184 | $this->text = $properties; | |
185 | } | |
186 | if ($this->text === null) { | |
187 | throw new coding_exception('You must set the text for the node when you create it.'); | |
188 | } | |
3406acde | 189 | // Default the title to the text |
7d2a0492 | 190 | $this->title = $this->text; |
3406acde SH |
191 | // Instantiate a new navigation node collection for this nodes children |
192 | $this->children = new navigation_node_collection(); | |
7d2a0492 | 193 | } |
194 | ||
95b97515 | 195 | /** |
3406acde | 196 | * Checks if this node is the active node. |
0baa5d46 | 197 | * |
3406acde SH |
198 | * This is determined by comparing the action for the node against the |
199 | * defined URL for the page. A match will see this node marked as active. | |
0baa5d46 | 200 | * |
3406acde SH |
201 | * @param int $strength One of URL_MATCH_EXACT, URL_MATCH_PARAMS, or URL_MATCH_BASE |
202 | * @return bool | |
7d2a0492 | 203 | */ |
bf6c37c7 | 204 | public function check_if_active($strength=URL_MATCH_EXACT) { |
205 | global $FULLME, $PAGE; | |
3406acde | 206 | // Set fullmeurl if it hasn't already been set |
7d2a0492 | 207 | if (self::$fullmeurl == null) { |
bf6c37c7 | 208 | if ($PAGE->has_set_url()) { |
3406acde | 209 | self::override_active_url(new moodle_url($PAGE->url)); |
bf6c37c7 | 210 | } else { |
3406acde | 211 | self::override_active_url(new moodle_url($FULLME)); |
7d2a0492 | 212 | } |
213 | } | |
bf6c37c7 | 214 | |
3406acde | 215 | // Compare the action of this node against the fullmeurl |
bf6c37c7 | 216 | if ($this->action instanceof moodle_url && $this->action->compare(self::$fullmeurl, $strength)) { |
7d2a0492 | 217 | $this->make_active(); |
218 | return true; | |
7d2a0492 | 219 | } |
220 | return false; | |
221 | } | |
3406acde | 222 | |
7d2a0492 | 223 | /** |
3406acde | 224 | * Overrides the fullmeurl variable providing |
7d2a0492 | 225 | * |
3406acde SH |
226 | * @param moodle_url $url The url to use for the fullmeurl. |
227 | */ | |
228 | public static function override_active_url(moodle_url $url) { | |
229 | self::$fullmeurl = $url; | |
230 | } | |
231 | ||
232 | /** | |
233 | * Adds a navigation node as a child of this node. | |
435a512e | 234 | * |
3406acde SH |
235 | * @param string $text |
236 | * @param moodle_url|action_link $action | |
237 | * @param int $type | |
238 | * @param string $shorttext | |
239 | * @param string|int $key | |
240 | * @param pix_icon $icon | |
241 | * @return navigation_node | |
7d2a0492 | 242 | */ |
3406acde SH |
243 | public function add($text, $action=null, $type=self::TYPE_CUSTOM, $shorttext=null, $key=null, pix_icon $icon=null) { |
244 | // First convert the nodetype for this node to a branch as it will now have children | |
7d2a0492 | 245 | if ($this->nodetype !== self::NODETYPE_BRANCH) { |
246 | $this->nodetype = self::NODETYPE_BRANCH; | |
247 | } | |
3406acde SH |
248 | // Properties array used when creating the new navigation node |
249 | $itemarray = array( | |
250 | 'text' => $text, | |
251 | 'type' => $type | |
252 | ); | |
253 | // Set the action if one was provided | |
7d2a0492 | 254 | if ($action!==null) { |
255 | $itemarray['action'] = $action; | |
256 | } | |
3406acde | 257 | // Set the shorttext if one was provided |
7d2a0492 | 258 | if ($shorttext!==null) { |
259 | $itemarray['shorttext'] = $shorttext; | |
260 | } | |
3406acde | 261 | // Set the icon if one was provided |
7d2a0492 | 262 | if ($icon!==null) { |
263 | $itemarray['icon'] = $icon; | |
264 | } | |
3406acde SH |
265 | // Default the key to the number of children if not provided |
266 | if ($key === null) { | |
267 | $key = $this->children->count(); | |
7d2a0492 | 268 | } |
3406acde | 269 | // Set the key |
7d2a0492 | 270 | $itemarray['key'] = $key; |
3406acde SH |
271 | // Set the parent to this node |
272 | $itemarray['parent'] = $this; | |
273 | // Add the child using the navigation_node_collections add method | |
274 | $node = $this->children->add(new navigation_node($itemarray)); | |
275 | // If the node is a category node or the user is logged in and its a course | |
276 | // then mark this node as a branch (makes it expandable by AJAX) | |
c73e37e0 | 277 | if (($type==self::TYPE_CATEGORY) || (isloggedin() && $type==self::TYPE_COURSE)) { |
3406acde | 278 | $node->nodetype = self::NODETYPE_BRANCH; |
7d2a0492 | 279 | } |
3406acde | 280 | // If this node is hidden mark it's children as hidden also |
7d2a0492 | 281 | if ($this->hidden) { |
3406acde | 282 | $node->hidden = true; |
7d2a0492 | 283 | } |
3406acde SH |
284 | // Return the node (reference returned by $this->children->add() |
285 | return $node; | |
7d2a0492 | 286 | } |
287 | ||
288 | /** | |
3406acde | 289 | * Searches for a node of the given type with the given key. |
7d2a0492 | 290 | * |
3406acde SH |
291 | * This searches this node plus all of its children, and their children.... |
292 | * If you know the node you are looking for is a child of this node then please | |
293 | * use the get method instead. | |
294 | * | |
295 | * @param int|string $key The key of the node we are looking for | |
296 | * @param int $type One of navigation_node::TYPE_* | |
297 | * @return navigation_node|false | |
7d2a0492 | 298 | */ |
3406acde SH |
299 | public function find($key, $type) { |
300 | return $this->children->find($key, $type); | |
301 | } | |
302 | ||
303 | /** | |
304 | * Get ths child of this node that has the given key + (optional) type. | |
305 | * | |
306 | * If you are looking for a node and want to search all children + thier children | |
307 | * then please use the find method instead. | |
308 | * | |
309 | * @param int|string $key The key of the node we are looking for | |
310 | * @param int $type One of navigation_node::TYPE_* | |
311 | * @return navigation_node|false | |
312 | */ | |
313 | public function get($key, $type=null) { | |
314 | return $this->children->get($key, $type); | |
315 | } | |
316 | ||
317 | /** | |
318 | * Removes this node. | |
319 | * | |
320 | * @return bool | |
321 | */ | |
322 | public function remove() { | |
323 | return $this->parent->children->remove($this->key, $this->type); | |
324 | } | |
325 | ||
326 | /** | |
327 | * Checks if this node has or could have any children | |
328 | * | |
329 | * @return bool Returns true if it has children or could have (by AJAX expansion) | |
330 | */ | |
331 | public function has_children() { | |
332 | return ($this->nodetype === navigation_node::NODETYPE_BRANCH || $this->children->count()>0); | |
333 | } | |
334 | ||
335 | /** | |
336 | * Marks this node as active and forces it open. | |
337 | */ | |
338 | public function make_active() { | |
339 | $this->isactive = true; | |
340 | $this->add_class('active_tree_node'); | |
341 | $this->force_open(); | |
342 | if ($this->parent !== null) { | |
343 | $this->parent->make_inactive(); | |
344 | } | |
345 | } | |
346 | ||
347 | /** | |
348 | * Marks a node as inactive and recusised back to the base of the tree | |
349 | * doing the same to all parents. | |
350 | */ | |
351 | public function make_inactive() { | |
352 | $this->isactive = false; | |
353 | $this->remove_class('active_tree_node'); | |
354 | if ($this->parent !== null) { | |
355 | $this->parent->make_inactive(); | |
7d2a0492 | 356 | } |
357 | } | |
358 | ||
359 | /** | |
3406acde SH |
360 | * Forces this node to be open and at the same time forces open all |
361 | * parents until the root node. | |
117bd748 | 362 | * |
3406acde SH |
363 | * Recursive. |
364 | */ | |
365 | public function force_open() { | |
366 | $this->forceopen = true; | |
367 | if ($this->parent !== null) { | |
368 | $this->parent->force_open(); | |
369 | } | |
370 | } | |
371 | ||
372 | /** | |
373 | * Adds a CSS class to this node. | |
374 | * | |
375 | * @param string $class | |
376 | * @return bool | |
7d2a0492 | 377 | */ |
378 | public function add_class($class) { | |
379 | if (!in_array($class, $this->classes)) { | |
380 | $this->classes[] = $class; | |
381 | } | |
382 | return true; | |
383 | } | |
384 | ||
385 | /** | |
3406acde | 386 | * Removes a CSS class from this node. |
7d2a0492 | 387 | * |
388 | * @param string $class | |
3406acde | 389 | * @return bool True if the class was successfully removed. |
7d2a0492 | 390 | */ |
391 | public function remove_class($class) { | |
392 | if (in_array($class, $this->classes)) { | |
393 | $key = array_search($class,$this->classes); | |
394 | if ($key!==false) { | |
395 | unset($this->classes[$key]); | |
396 | return true; | |
397 | } | |
398 | } | |
399 | return false; | |
400 | } | |
401 | ||
402 | /** | |
3406acde SH |
403 | * Sets the title for this node and forces Moodle to utilise it. |
404 | * @param string $title | |
405 | */ | |
406 | public function title($title) { | |
407 | $this->title = $title; | |
408 | $this->forcetitle = true; | |
409 | } | |
410 | ||
411 | /** | |
412 | * Resets the page specific information on this node if it is being unserialised. | |
413 | */ | |
414 | public function __wakeup(){ | |
415 | $this->forceopen = false; | |
416 | $this->isactive = false; | |
417 | $this->remove_class('active_tree_node'); | |
418 | } | |
419 | ||
420 | /** | |
421 | * Checks if this node or any of its children contain the active node. | |
435a512e | 422 | * |
3406acde | 423 | * Recursive. |
7d2a0492 | 424 | * |
3406acde | 425 | * @return bool |
7d2a0492 | 426 | */ |
3406acde SH |
427 | public function contains_active_node() { |
428 | if ($this->isactive) { | |
7d2a0492 | 429 | return true; |
430 | } else { | |
3406acde SH |
431 | foreach ($this->children as $child) { |
432 | if ($child->isactive || $child->contains_active_node()) { | |
433 | return true; | |
434 | } | |
435 | } | |
7d2a0492 | 436 | } |
3406acde | 437 | return false; |
7d2a0492 | 438 | } |
439 | ||
440 | /** | |
3406acde SH |
441 | * Finds the active node. |
442 | * | |
443 | * Searches this nodes children plus all of the children for the active node | |
444 | * and returns it if found. | |
7d2a0492 | 445 | * |
3406acde SH |
446 | * Recursive. |
447 | * | |
448 | * @return navigation_node|false | |
7d2a0492 | 449 | */ |
3406acde SH |
450 | public function find_active_node() { |
451 | if ($this->isactive) { | |
452 | return $this; | |
453 | } else { | |
7d2a0492 | 454 | foreach ($this->children as &$child) { |
3406acde SH |
455 | $outcome = $child->find_active_node(); |
456 | if ($outcome !== false) { | |
457 | return $outcome; | |
7d2a0492 | 458 | } |
459 | } | |
7d2a0492 | 460 | } |
3406acde | 461 | return false; |
7d2a0492 | 462 | } |
463 | ||
7c4efe3b SH |
464 | /** |
465 | * Searches all children for the best matching active node | |
466 | * @return navigation_node|false | |
467 | */ | |
468 | public function search_for_active_node() { | |
469 | if ($this->check_if_active(URL_MATCH_BASE)) { | |
470 | return $this; | |
471 | } else { | |
472 | foreach ($this->children as &$child) { | |
473 | $outcome = $child->search_for_active_node(); | |
474 | if ($outcome !== false) { | |
475 | return $outcome; | |
476 | } | |
477 | } | |
478 | } | |
479 | return false; | |
480 | } | |
481 | ||
7d2a0492 | 482 | /** |
3406acde | 483 | * Gets the content for this node. |
7d2a0492 | 484 | * |
3406acde SH |
485 | * @param bool $shorttext If true shorttext is used rather than the normal text |
486 | * @return string | |
7d2a0492 | 487 | */ |
3406acde | 488 | public function get_content($shorttext=false) { |
7d2a0492 | 489 | if ($shorttext && $this->shorttext!==null) { |
3406acde | 490 | return format_string($this->shorttext); |
7d2a0492 | 491 | } else { |
3406acde | 492 | return format_string($this->text); |
1c4eef57 | 493 | } |
3406acde | 494 | } |
1c4eef57 | 495 | |
3406acde SH |
496 | /** |
497 | * Gets the title to use for this node. | |
435a512e | 498 | * |
3406acde SH |
499 | * @return string |
500 | */ | |
501 | public function get_title() { | |
bbfa9be0 | 502 | if ($this->forcetitle || $this->action != null){ |
3406acde SH |
503 | return $this->title; |
504 | } else { | |
9bf16314 PS |
505 | return ''; |
506 | } | |
7d2a0492 | 507 | } |
117bd748 | 508 | |
7d2a0492 | 509 | /** |
3406acde | 510 | * Gets the CSS class to add to this node to describe its type |
435a512e | 511 | * |
7d2a0492 | 512 | * @return string |
513 | */ | |
514 | public function get_css_type() { | |
515 | if (array_key_exists($this->type, $this->namedtypes)) { | |
516 | return 'type_'.$this->namedtypes[$this->type]; | |
517 | } | |
518 | return 'type_unknown'; | |
519 | } | |
520 | ||
521 | /** | |
3406acde | 522 | * Finds all nodes that are expandable by AJAX |
7d2a0492 | 523 | * |
3406acde | 524 | * @param array $expandable An array by reference to populate with expandable nodes. |
7d2a0492 | 525 | */ |
3406acde | 526 | public function find_expandable(array &$expandable) { |
4766a50c SH |
527 | $isloggedin = (isloggedin() && !isguestuser()); |
528 | if (!$isloggedin && $this->type > self::TYPE_CATEGORY) { | |
3406acde SH |
529 | return; |
530 | } | |
531 | foreach ($this->children as &$child) { | |
4766a50c SH |
532 | if (!$isloggedin && $child->type > self::TYPE_CATEGORY) { |
533 | continue; | |
534 | } | |
88f77c3c | 535 | if ($child->nodetype == self::NODETYPE_BRANCH && $child->children->count()==0 && $child->display) { |
3406acde SH |
536 | $child->id = 'expandable_branch_'.(count($expandable)+1); |
537 | $this->add_class('canexpand'); | |
538 | $expandable[] = array('id'=>$child->id,'branchid'=>$child->key,'type'=>$child->type); | |
7d2a0492 | 539 | } |
3406acde | 540 | $child->find_expandable($expandable); |
7d2a0492 | 541 | } |
7d2a0492 | 542 | } |
88f77c3c | 543 | |
4766a50c SH |
544 | /** |
545 | * Finds all nodes of a given type (recursive) | |
546 | * | |
547 | * @param int $type On of navigation_node::TYPE_* | |
548 | * @return array | |
549 | */ | |
88f77c3c SH |
550 | public function find_all_of_type($type) { |
551 | $nodes = $this->children->type($type); | |
552 | foreach ($this->children as &$node) { | |
553 | $childnodes = $node->find_all_of_type($type); | |
554 | $nodes = array_merge($nodes, $childnodes); | |
555 | } | |
556 | return $nodes; | |
557 | } | |
56ed242b SH |
558 | |
559 | /** | |
560 | * Removes this node if it is empty | |
561 | */ | |
562 | public function trim_if_empty() { | |
563 | if ($this->children->count() == 0) { | |
564 | $this->remove(); | |
565 | } | |
566 | } | |
567 | ||
568 | /** | |
569 | * Creates a tab representation of this nodes children that can be used | |
570 | * with print_tabs to produce the tabs on a page. | |
571 | * | |
572 | * call_user_func_array('print_tabs', $node->get_tabs_array()); | |
573 | * | |
574 | * @param array $inactive | |
575 | * @param bool $return | |
576 | * @return array Array (tabs, selected, inactive, activated, return) | |
577 | */ | |
578 | public function get_tabs_array(array $inactive=array(), $return=false) { | |
579 | $tabs = array(); | |
580 | $rows = array(); | |
581 | $selected = null; | |
582 | $activated = array(); | |
583 | foreach ($this->children as $node) { | |
584 | $tabs[] = new tabobject($node->key, $node->action, $node->get_content(), $node->get_title()); | |
585 | if ($node->contains_active_node()) { | |
586 | if ($node->children->count() > 0) { | |
587 | $activated[] = $node->key; | |
588 | foreach ($node->children as $child) { | |
589 | if ($child->contains_active_node()) { | |
590 | $selected = $child->key; | |
591 | } | |
592 | $rows[] = new tabobject($child->key, $child->action, $child->get_content(), $child->get_title()); | |
593 | } | |
594 | } else { | |
595 | $selected = $node->key; | |
596 | } | |
597 | } | |
598 | } | |
599 | return array(array($tabs, $rows), $selected, $inactive, $activated, $return); | |
600 | } | |
3406acde | 601 | } |
7d2a0492 | 602 | |
3406acde SH |
603 | /** |
604 | * Navigation node collection | |
605 | * | |
606 | * This class is responsible for managing a collection of navigation nodes. | |
607 | * It is required because a node's unique identifier is a combination of both its | |
608 | * key and its type. | |
609 | * | |
610 | * Originally an array was used with a string key that was a combination of the two | |
611 | * however it was decided that a better solution would be to use a class that | |
612 | * implements the standard IteratorAggregate interface. | |
613 | * | |
614 | * @package moodlecore | |
615 | * @subpackage navigation | |
616 | * @copyright 2010 Sam Hemelryk | |
617 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | |
618 | */ | |
619 | class navigation_node_collection implements IteratorAggregate { | |
7d2a0492 | 620 | /** |
3406acde SH |
621 | * A multidimensional array to where the first key is the type and the second |
622 | * key is the nodes key. | |
623 | * @var array | |
7d2a0492 | 624 | */ |
3406acde | 625 | protected $collection = array(); |
7d2a0492 | 626 | /** |
3406acde SH |
627 | * An array that contains references to nodes in the same order they were added. |
628 | * This is maintained as a progressive array. | |
629 | * @var array | |
7d2a0492 | 630 | */ |
3406acde | 631 | protected $orderedcollection = array(); |
da3ab9c4 | 632 | /** |
3406acde SH |
633 | * A reference to the last node that was added to the collection |
634 | * @var navigation_node | |
da3ab9c4 | 635 | */ |
3406acde | 636 | protected $last = null; |
6644741d | 637 | /** |
3406acde SH |
638 | * The total number of items added to this array. |
639 | * @var int | |
6644741d | 640 | */ |
3406acde | 641 | protected $count = 0; |
7d2a0492 | 642 | /** |
3406acde | 643 | * Adds a navigation node to the collection |
7d2a0492 | 644 | * |
3406acde SH |
645 | * @param navigation_node $node |
646 | * @return navigation_node | |
7d2a0492 | 647 | */ |
3406acde SH |
648 | public function add(navigation_node $node) { |
649 | global $CFG; | |
650 | $key = $node->key; | |
651 | $type = $node->type; | |
652 | // First check we have a 2nd dimension for this type | |
653 | if (!array_key_exists($type, $this->orderedcollection)) { | |
654 | $this->orderedcollection[$type] = array(); | |
7d2a0492 | 655 | } |
3406acde SH |
656 | // Check for a collision and report if debugging is turned on |
657 | if ($CFG->debug && array_key_exists($key, $this->orderedcollection[$type])) { | |
658 | debugging('Navigation node intersect: Adding a node that already exists '.$key, DEBUG_DEVELOPER); | |
7d2a0492 | 659 | } |
3406acde SH |
660 | // Add the node to the appropriate place in the ordered structure. |
661 | $this->orderedcollection[$type][$key] = $node; | |
662 | // Add a reference to the node to the progressive collection. | |
663 | $this->collection[$this->count] = &$this->orderedcollection[$type][$key]; | |
664 | // Update the last property to a reference to this new node. | |
665 | $this->last = &$this->orderedcollection[$type][$key]; | |
666 | $this->count++; | |
667 | // Return the reference to the now added node | |
668 | return $this->last; | |
7d2a0492 | 669 | } |
670 | ||
671 | /** | |
3406acde | 672 | * Fetches a node from this collection. |
7d2a0492 | 673 | * |
3406acde SH |
674 | * @param string|int $key The key of the node we want to find. |
675 | * @param int $type One of navigation_node::TYPE_*. | |
676 | * @return navigation_node|null | |
7d2a0492 | 677 | */ |
3406acde SH |
678 | public function get($key, $type=null) { |
679 | if ($type !== null) { | |
680 | // If the type is known then we can simply check and fetch | |
681 | if (!empty($this->orderedcollection[$type][$key])) { | |
682 | return $this->orderedcollection[$type][$key]; | |
683 | } | |
684 | } else { | |
685 | // Because we don't know the type we look in the progressive array | |
686 | foreach ($this->collection as $node) { | |
687 | if ($node->key === $key) { | |
688 | return $node; | |
7d2a0492 | 689 | } |
690 | } | |
691 | } | |
692 | return false; | |
693 | } | |
7d2a0492 | 694 | /** |
3406acde | 695 | * Searches for a node with matching key and type. |
7d2a0492 | 696 | * |
3406acde SH |
697 | * This function searches both the nodes in this collection and all of |
698 | * the nodes in each collection belonging to the nodes in this collection. | |
7d2a0492 | 699 | * |
3406acde | 700 | * Recursive. |
7d2a0492 | 701 | * |
3406acde SH |
702 | * @param string|int $key The key of the node we want to find. |
703 | * @param int $type One of navigation_node::TYPE_*. | |
704 | * @return navigation_node|null | |
7d2a0492 | 705 | */ |
3406acde SH |
706 | public function find($key, $type=null) { |
707 | if ($type !== null && array_key_exists($type, $this->orderedcollection) && array_key_exists($key, $this->orderedcollection[$type])) { | |
708 | return $this->orderedcollection[$type][$key]; | |
709 | } else { | |
710 | $nodes = $this->getIterator(); | |
711 | // Search immediate children first | |
712 | foreach ($nodes as &$node) { | |
713 | if ($node->key == $key && ($type == null || $type === $node->type)) { | |
714 | return $node; | |
d926f4b1 | 715 | } |
3406acde SH |
716 | } |
717 | // Now search each childs children | |
718 | foreach ($nodes as &$node) { | |
719 | $result = $node->children->find($key, $type); | |
720 | if ($result !== false) { | |
721 | return $result; | |
d926f4b1 | 722 | } |
7d2a0492 | 723 | } |
724 | } | |
725 | return false; | |
726 | } | |
727 | ||
d926f4b1 | 728 | /** |
3406acde | 729 | * Fetches the last node that was added to this collection |
435a512e | 730 | * |
3406acde | 731 | * @return navigation_node |
d926f4b1 | 732 | */ |
3406acde SH |
733 | public function last() { |
734 | return $this->last; | |
735 | } | |
736 | /** | |
737 | * Fetches all nodes of a given type from this collection | |
738 | */ | |
739 | public function type($type) { | |
740 | if (!array_key_exists($type, $this->orderedcollection)) { | |
741 | $this->orderedcollection[$type] = array(); | |
d926f4b1 | 742 | } |
3406acde | 743 | return $this->orderedcollection[$type]; |
d926f4b1 | 744 | } |
7d2a0492 | 745 | /** |
3406acde | 746 | * Removes the node with the given key and type from the collection |
7d2a0492 | 747 | * |
3406acde SH |
748 | * @param string|int $key |
749 | * @param int $type | |
750 | * @return bool | |
7d2a0492 | 751 | */ |
3406acde SH |
752 | public function remove($key, $type=null) { |
753 | $child = $this->get($key, $type); | |
754 | if ($child !== false) { | |
755 | foreach ($this->collection as $colkey => $node) { | |
756 | if ($node->key == $key && $node->type == $type) { | |
757 | unset($this->collection[$colkey]); | |
758 | break; | |
759 | } | |
7d2a0492 | 760 | } |
3406acde SH |
761 | unset($this->orderedcollection[$child->type][$child->key]); |
762 | $this->count--; | |
763 | return true; | |
7d2a0492 | 764 | } |
3406acde | 765 | return false; |
7d2a0492 | 766 | } |
767 | ||
9da1ec27 | 768 | /** |
3406acde SH |
769 | * Gets the number of nodes in this collection |
770 | * @return int | |
7d2a0492 | 771 | */ |
3406acde SH |
772 | public function count() { |
773 | return count($this->collection); | |
7d2a0492 | 774 | } |
7d2a0492 | 775 | /** |
3406acde | 776 | * Gets an array iterator for the collection. |
7d2a0492 | 777 | * |
3406acde SH |
778 | * This is required by the IteratorAggregator interface and is used by routines |
779 | * such as the foreach loop. | |
7d2a0492 | 780 | * |
3406acde | 781 | * @return ArrayIterator |
7d2a0492 | 782 | */ |
3406acde SH |
783 | public function getIterator() { |
784 | return new ArrayIterator($this->collection); | |
7d2a0492 | 785 | } |
786 | } | |
787 | ||
788 | /** | |
789 | * The global navigation class used for... the global navigation | |
790 | * | |
791 | * This class is used by PAGE to store the global navigation for the site | |
792 | * and is then used by the settings nav and navbar to save on processing and DB calls | |
793 | * | |
794 | * See | |
795 | * <ul> | |
796 | * <li><b>{@link lib/pagelib.php}</b> {@link moodle_page::initialise_theme_and_output()}<li> | |
797 | * <li><b>{@link lib/ajax/getnavbranch.php}</b> Called by ajax<li> | |
798 | * </ul> | |
799 | * | |
800 | * @package moodlecore | |
babb3911 | 801 | * @subpackage navigation |
7d2a0492 | 802 | * @copyright 2009 Sam Hemelryk |
803 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | |
804 | */ | |
805 | class global_navigation extends navigation_node { | |
3406acde SH |
806 | /** |
807 | * The Moodle page this navigation object belongs to. | |
808 | * @var moodle_page | |
809 | */ | |
810 | protected $page; | |
7d2a0492 | 811 | /** @var bool */ |
812 | protected $initialised = false; | |
3406acde SH |
813 | /** @var array */ |
814 | protected $mycourses = array(); | |
815 | /** @var array */ | |
816 | protected $rootnodes = array(); | |
7d2a0492 | 817 | /** @var bool */ |
3406acde SH |
818 | protected $showemptysections = false; |
819 | /** @var array */ | |
7a7e209d | 820 | protected $extendforuser = array(); |
3406acde SH |
821 | /** @var navigation_cache */ |
822 | protected $cache; | |
823 | /** @var array */ | |
824 | protected $addedcourses = array(); | |
88f77c3c SH |
825 | /** @var int */ |
826 | protected $expansionlimit = 0; | |
827 | ||
7d2a0492 | 828 | /** |
3406acde SH |
829 | * Constructs a new global navigation |
830 | * | |
3406acde | 831 | * @param moodle_page $page The page this navigation object belongs to |
7d2a0492 | 832 | */ |
3406acde | 833 | public function __construct(moodle_page $page) { |
4766a50c | 834 | global $CFG, $SITE, $USER; |
3406acde | 835 | |
7d2a0492 | 836 | if (during_initial_install()) { |
3406acde | 837 | return; |
7d2a0492 | 838 | } |
3406acde | 839 | |
4766a50c SH |
840 | if (get_home_page() == HOMEPAGE_SITE) { |
841 | // We are using the site home for the root element | |
842 | $properties = array( | |
843 | 'key' => 'home', | |
844 | 'type' => navigation_node::TYPE_SYSTEM, | |
845 | 'text' => get_string('home'), | |
846 | 'action' => new moodle_url('/') | |
847 | ); | |
848 | } else { | |
849 | // We are using the users my moodle for the root element | |
850 | $properties = array( | |
851 | 'key' => 'myhome', | |
852 | 'type' => navigation_node::TYPE_SYSTEM, | |
853 | 'text' => get_string('myhome'), | |
854 | 'action' => new moodle_url('/my/') | |
855 | ); | |
dd8e5011 | 856 | } |
4766a50c SH |
857 | |
858 | // Use the parents consturctor.... good good reuse | |
3406acde SH |
859 | parent::__construct($properties); |
860 | ||
861 | // Initalise and set defaults | |
862 | $this->page = $page; | |
7d2a0492 | 863 | $this->forceopen = true; |
f5b5a822 | 864 | $this->cache = new navigation_cache(NAVIGATION_CACHE_NAME); |
3406acde SH |
865 | |
866 | // Check if we need to clear the cache | |
7d2a0492 | 867 | $regenerate = optional_param('regenerate', null, PARAM_TEXT); |
3406acde | 868 | if ($regenerate === 'navigation') { |
7d2a0492 | 869 | $this->cache->clear(); |
870 | } | |
871 | } | |
872 | ||
873 | /** | |
3406acde | 874 | * Initialises the navigation object. |
7d2a0492 | 875 | * |
3406acde SH |
876 | * This causes the navigation object to look at the current state of the page |
877 | * that it is associated with and then load the appropriate content. | |
7d2a0492 | 878 | * |
3406acde SH |
879 | * This should only occur the first time that the navigation structure is utilised |
880 | * which will normally be either when the navbar is called to be displayed or | |
881 | * when a block makes use of it. | |
7d2a0492 | 882 | * |
3406acde | 883 | * @return bool |
7d2a0492 | 884 | */ |
3406acde | 885 | public function initialise() { |
4766a50c | 886 | global $CFG, $SITE, $USER, $DB; |
3406acde | 887 | // Check if it has alread been initialised |
7d2a0492 | 888 | if ($this->initialised || during_initial_install()) { |
889 | return true; | |
890 | } | |
e2b436d0 | 891 | $this->initialised = true; |
3406acde SH |
892 | |
893 | // Set up the five base root nodes. These are nodes where we will put our | |
894 | // content and are as follows: | |
895 | // site: Navigation for the front page. | |
896 | // myprofile: User profile information goes here. | |
897 | // mycourses: The users courses get added here. | |
898 | // courses: Additional courses are added here. | |
899 | // users: Other users information loaded here. | |
900 | $this->rootnodes = array(); | |
4766a50c | 901 | if (get_home_page() == HOMEPAGE_SITE) { |
3f9ccf85 | 902 | // The home element should be my moodle because the root element is the site |
b9d4c7d3 | 903 | if (isloggedin() && !isguestuser()) { // Makes no sense if you aren't logged in |
3f9ccf85 MD |
904 | $this->rootnodes['home'] = $this->add(get_string('myhome'), new moodle_url('/my/'), self::TYPE_SETTING, null, 'home'); |
905 | } | |
4766a50c SH |
906 | } else { |
907 | // The home element should be the site because the root node is my moodle | |
908 | $this->rootnodes['home'] = $this->add(get_string('sitehome'), new moodle_url('/'), self::TYPE_SETTING, null, 'home'); | |
909 | if ($CFG->defaulthomepage == HOMEPAGE_MY) { | |
910 | // We need to stop automatic redirection | |
911 | $this->rootnodes['home']->action->param('redirect', '0'); | |
912 | } | |
913 | } | |
3406acde SH |
914 | $this->rootnodes['site'] = $this->add_course($SITE); |
915 | $this->rootnodes['myprofile'] = $this->add(get_string('myprofile'), null, self::TYPE_USER, null, 'myprofile'); | |
916 | $this->rootnodes['mycourses'] = $this->add(get_string('mycourses'), null, self::TYPE_ROOTNODE, null, 'mycourses'); | |
917 | $this->rootnodes['courses'] = $this->add(get_string('courses'), null, self::TYPE_ROOTNODE, null, 'courses'); | |
918 | $this->rootnodes['users'] = $this->add(get_string('users'), null, self::TYPE_ROOTNODE, null, 'users'); | |
919 | ||
920 | // Fetch all of the users courses. | |
4766a50c SH |
921 | $limit = 20; |
922 | if (!empty($CFG->navcourselimit)) { | |
923 | $limit = $CFG->navcourselimit; | |
924 | } | |
925 | ||
9a18a77e SH |
926 | if (!empty($CFG->navshowcategories) && $DB->count_records('course_categories') == 1) { |
927 | // There is only one category so we don't want to show categories | |
928 | $CFG->navshowcategories = false; | |
929 | } | |
930 | ||
df997f84 | 931 | $this->mycourses = enrol_get_my_courses(NULL, 'visible DESC,sortorder ASC', $limit); |
ba2789c1 SH |
932 | $showallcourses = (count($this->mycourses) == 0 || !empty($CFG->navshowallcourses)); |
933 | $showcategories = ($showallcourses && !empty($CFG->navshowcategories)); | |
934 | ||
3406acde SH |
935 | // Check if any courses were returned. |
936 | if (count($this->mycourses) > 0) { | |
937 | // Add all of the users courses to the navigation | |
938 | foreach ($this->mycourses as &$course) { | |
4766a50c | 939 | $course->coursenode = $this->add_course($course); |
3406acde | 940 | } |
4766a50c SH |
941 | } |
942 | ||
4766a50c SH |
943 | if ($showcategories) { |
944 | // Load all categories (ensures we get the base categories) | |
945 | $this->load_all_categories(); | |
ba2789c1 SH |
946 | } else if ($showallcourses) { |
947 | // Load all courses | |
948 | $this->load_all_courses(); | |
3406acde SH |
949 | } |
950 | ||
951 | // Next load context specific content into the navigation | |
952 | switch ($this->page->context->contextlevel) { | |
953 | case CONTEXT_SYSTEM : | |
954 | case CONTEXT_COURSECAT : | |
955 | // Load the front page course navigation | |
7c4efe3b SH |
956 | $coursenode = $this->load_course($SITE); |
957 | $this->add_front_page_course_essentials($coursenode, $SITE); | |
7d2a0492 | 958 | break; |
3406acde SH |
959 | case CONTEXT_BLOCK : |
960 | case CONTEXT_COURSE : | |
961 | // Load the course associated with the page into the navigation | |
962 | $course = $this->page->course; | |
963 | $coursenode = $this->load_course($course); | |
3406acde SH |
964 | // Add the essentials such as reports etc... |
965 | $this->add_course_essentials($coursenode, $course); | |
966 | if ($this->format_display_course_content($course->format)) { | |
967 | // Load the course sections | |
968 | $sections = $this->load_course_sections($course, $coursenode); | |
969 | } | |
7c4efe3b SH |
970 | if (!$coursenode->contains_active_node() && !$coursenode->search_for_active_node()) { |
971 | $coursenode->make_active(); | |
972 | } | |
7d2a0492 | 973 | break; |
3406acde SH |
974 | case CONTEXT_MODULE : |
975 | $course = $this->page->course; | |
976 | $cm = $this->page->cm; | |
977 | // Load the course associated with the page into the navigation | |
978 | $coursenode = $this->load_course($course); | |
979 | $this->add_course_essentials($coursenode, $course); | |
980 | // Load the course sections into the page | |
981 | $sections = $this->load_course_sections($course, $coursenode); | |
982 | if ($course->id !== SITEID) { | |
983 | // Find the section for the $CM associated with the page and collect | |
984 | // its section number. | |
985 | foreach ($sections as $section) { | |
986 | if ($section->id == $cm->section) { | |
987 | $cm->sectionnumber = $section->section; | |
988 | break; | |
989 | } | |
990 | } | |
991 | ||
992 | // Load all of the section activities for the section the cm belongs to. | |
993 | $activities = $this->load_section_activities($sections[$cm->sectionnumber]->sectionnode, $cm->sectionnumber, get_fast_modinfo($course)); | |
3406acde SH |
994 | } else { |
995 | $activities = array(); | |
996 | $activities[$cm->id] = $coursenode->get($cm->id, navigation_node::TYPE_ACTIVITY); | |
997 | } | |
91eab03d | 998 | // Finally load the cm specific navigaton information |
3406acde | 999 | $this->load_activity($cm, $course, $activities[$cm->id]); |
7c4efe3b SH |
1000 | // Check if we have an active ndoe |
1001 | if (!$activities[$cm->id]->contains_active_node() && !$activities[$cm->id]->search_for_active_node()) { | |
1002 | // And make the activity node active. | |
1003 | $activities[$cm->id]->make_active(); | |
1004 | } | |
7d2a0492 | 1005 | break; |
3406acde SH |
1006 | case CONTEXT_USER : |
1007 | $course = $this->page->course; | |
1008 | if ($course->id != SITEID) { | |
1009 | // Load the course associated with the user into the navigation | |
1010 | $coursenode = $this->load_course($course); | |
1011 | $this->add_course_essentials($coursenode, $course); | |
1012 | $sections = $this->load_course_sections($course, $coursenode); | |
7a7e209d | 1013 | } |
7d2a0492 | 1014 | break; |
1015 | } | |
7a7e209d | 1016 | |
ba2789c1 SH |
1017 | $limit = 20; |
1018 | if (!empty($CFG->navcourselimit)) { | |
1019 | $limit = $CFG->navcourselimit; | |
1020 | } | |
1021 | if ($showcategories) { | |
1022 | $categories = $this->find_all_of_type(self::TYPE_CATEGORY); | |
1023 | foreach ($categories as &$category) { | |
1024 | if ($category->children->count() >= $limit) { | |
1025 | $url = new moodle_url('/course/category.php', array('id'=>$category->key)); | |
1026 | $category->add(get_string('viewallcourses'), $url, self::TYPE_SETTING); | |
1027 | } | |
1028 | } | |
1029 | } else if ($this->rootnodes['courses']->children->count() >= $limit) { | |
1030 | $this->rootnodes['courses']->add(get_string('viewallcoursescategories'), new moodle_url('/course/index.php'), self::TYPE_SETTING); | |
1031 | } | |
1032 | ||
3406acde SH |
1033 | // Load for the current user |
1034 | $this->load_for_user(); | |
25ad1f47 | 1035 | if ($this->page->context->contextlevel >= CONTEXT_COURSE && $this->page->context->instanceid != SITEID) { |
87c215de SH |
1036 | $this->load_for_user(null, true); |
1037 | } | |
7a7e209d SH |
1038 | // Load each extending user into the navigation. |
1039 | foreach ($this->extendforuser as $user) { | |
1040 | if ($user->id !== $USER->id) { | |
1041 | $this->load_for_user($user); | |
1042 | } | |
1043 | } | |
7a7e209d | 1044 | |
a683da3c SH |
1045 | // Give the local plugins a chance to include some navigation if they want. |
1046 | foreach (get_list_of_plugins('local') as $plugin) { | |
1047 | if (!file_exists($CFG->dirroot.'/local/'.$plugin.'/lib.php')) { | |
1048 | continue; | |
1049 | } | |
1050 | require_once($CFG->dirroot.'/local/'.$plugin.'/lib.php'); | |
1051 | $function = $plugin.'_extends_navigation'; | |
1052 | if (function_exists($function)) { | |
1053 | $function($this); | |
1054 | } | |
1055 | } | |
1056 | ||
3406acde SH |
1057 | // Remove any empty root nodes |
1058 | foreach ($this->rootnodes as $node) { | |
4766a50c SH |
1059 | // Dont remove the home node |
1060 | if ($node->key !== 'home' && !$node->has_children()) { | |
3406acde SH |
1061 | $node->remove(); |
1062 | } | |
1063 | } | |
1064 | ||
7c4efe3b SH |
1065 | if (!$this->contains_active_node()) { |
1066 | $this->search_for_active_node(); | |
1067 | } | |
1068 | ||
3406acde SH |
1069 | // If the user is not logged in modify the navigation structure as detailed |
1070 | // in {@link http://docs.moodle.org/en/Development:Navigation_2.0_structure} | |
1071 | if (!isloggedin()) { | |
1072 | $activities = clone($this->rootnodes['site']->children); | |
1073 | $this->rootnodes['site']->remove(); | |
1074 | $children = clone($this->children); | |
1075 | $this->children = new navigation_node_collection(); | |
1076 | foreach ($activities as $child) { | |
1077 | $this->children->add($child); | |
1078 | } | |
1079 | foreach ($children as $child) { | |
1080 | $this->children->add($child); | |
1081 | } | |
3406acde | 1082 | } |
7d2a0492 | 1083 | return true; |
1084 | } | |
1085 | /** | |
3406acde SH |
1086 | * Checks the course format to see whether it wants the navigation to load |
1087 | * additional information for the course. | |
1088 | * | |
1089 | * This function utilises a callback that can exist within the course format lib.php file | |
1090 | * The callback should be a function called: | |
1091 | * callback_{formatname}_display_content() | |
1092 | * It doesn't get any arguments and should return true if additional content is | |
1093 | * desired. If the callback doesn't exist we assume additional content is wanted. | |
1094 | * | |
3406acde SH |
1095 | * @param string $format The course format |
1096 | * @return bool | |
1097 | */ | |
1098 | protected function format_display_course_content($format) { | |
1099 | global $CFG; | |
1100 | $formatlib = $CFG->dirroot.'/course/format/'.$format.'/lib.php'; | |
1101 | if (file_exists($formatlib)) { | |
1102 | require_once($formatlib); | |
1103 | $displayfunc = 'callback_'.$format.'_display_content'; | |
1104 | if (function_exists($displayfunc) && !$displayfunc()) { | |
1105 | return $displayfunc(); | |
1106 | } | |
1107 | } | |
1108 | return true; | |
1109 | } | |
1110 | ||
1111 | /** | |
1112 | * Loads of the the courses in Moodle into the navigation. | |
1113 | * | |
4766a50c | 1114 | * @param string|array $categoryids Either a string or array of category ids to load courses for |
3406acde SH |
1115 | * @return array An array of navigation_node |
1116 | */ | |
4766a50c SH |
1117 | protected function load_all_courses($categoryids=null) { |
1118 | global $CFG, $DB, $USER; | |
1119 | ||
1120 | if ($categoryids !== null) { | |
1121 | if (is_array($categoryids)) { | |
1122 | list ($select, $params) = $DB->get_in_or_equal($categoryids); | |
1123 | } else { | |
1124 | $select = '= ?'; | |
1125 | $params = array($categoryids); | |
1126 | } | |
1127 | array_unshift($params, SITEID); | |
1128 | $select = ' AND c.category '.$select; | |
1129 | } else { | |
1130 | $params = array(SITEID); | |
1131 | $select = ''; | |
1132 | } | |
435a512e | 1133 | |
3406acde SH |
1134 | list($ccselect, $ccjoin) = context_instance_preload_sql('c.id', CONTEXT_COURSE, 'ctx'); |
1135 | $sql = "SELECT c.id,c.sortorder,c.visible,c.fullname,c.shortname,c.category,cat.path AS categorypath $ccselect | |
1136 | FROM {course} c | |
1137 | $ccjoin | |
1138 | LEFT JOIN {course_categories} cat ON cat.id=c.category | |
4766a50c | 1139 | WHERE c.id <> ?$select |
3406acde | 1140 | ORDER BY c.sortorder ASC"; |
4766a50c SH |
1141 | $limit = 20; |
1142 | if (!empty($CFG->navcourselimit)) { | |
1143 | $limit = $CFG->navcourselimit; | |
1144 | } | |
1145 | $courses = $DB->get_records_sql($sql, $params, 0, $limit); | |
1146 | ||
3406acde SH |
1147 | $coursenodes = array(); |
1148 | foreach ($courses as $course) { | |
1149 | context_instance_preload($course); | |
1150 | $coursenodes[$course->id] = $this->add_course($course); | |
1151 | } | |
1152 | return $coursenodes; | |
1153 | } | |
1154 | ||
4766a50c SH |
1155 | /** |
1156 | * Loads all categories (top level or if an id is specified for that category) | |
1157 | * | |
1158 | * @param int $categoryid | |
1159 | * @return void | |
1160 | */ | |
1161 | protected function load_all_categories($categoryid=null) { | |
1162 | global $DB; | |
1163 | if ($categoryid == null) { | |
1164 | $categories = $DB->get_records('course_categories', array('parent'=>'0'), 'sortorder'); | |
1165 | } else { | |
1166 | $category = $DB->get_record('course_categories', array('id'=>$categoryid), '*', MUST_EXIST); | |
1167 | $wantedids = explode('/', trim($category->path, '/')); | |
1168 | list($select, $params) = $DB->get_in_or_equal($wantedids); | |
1169 | $select = 'id '.$select.' OR parent '.$select; | |
1170 | $params = array_merge($params, $params); | |
1171 | $categories = $DB->get_records_select('course_categories', $select, $params, 'sortorder'); | |
1172 | } | |
1173 | $structured = array(); | |
1174 | $categoriestoload = array(); | |
1175 | foreach ($categories as $category) { | |
1176 | if ($category->parent == '0') { | |
1177 | $structured[$category->id] = array('category'=>$category, 'children'=>array()); | |
1178 | } else { | |
1179 | if ($category->parent == $categoryid) { | |
1180 | $categoriestoload[] = $category->id; | |
1181 | } | |
1182 | $parents = array(); | |
1183 | $id = $category->parent; | |
1184 | while ($id != '0') { | |
1185 | $parents[] = $id; | |
1186 | if (!array_key_exists($id, $categories)) { | |
1187 | $categories[$id] = $DB->get_record('course_categories', array('id'=>$id), '*', MUST_EXIST); | |
1188 | } | |
1189 | $id = $categories[$id]->parent; | |
1190 | } | |
1191 | $parents = array_reverse($parents); | |
1192 | $parentref = &$structured[array_shift($parents)]; | |
1193 | foreach ($parents as $parent) { | |
1194 | if (!array_key_exists($parent, $parentref['children'])) { | |
1195 | $parentref['children'][$parent] = array('category'=>$categories[$parent], 'children'=>array()); | |
1196 | } | |
1197 | $parentref = &$parentref['children'][$parent]; | |
1198 | } | |
1199 | if (!array_key_exists($category->id, $parentref['children'])) { | |
1200 | $parentref['children'][$category->id] = array('category'=>$category, 'children'=>array()); | |
1201 | } | |
1202 | } | |
1203 | } | |
1204 | ||
1205 | foreach ($structured as $category) { | |
1206 | $this->add_category($category, $this->rootnodes['courses']); | |
1207 | } | |
1208 | ||
1209 | if ($categoryid !== null && count($wantedids)) { | |
1210 | foreach ($wantedids as $catid) { | |
1211 | $this->load_all_courses($catid); | |
1212 | } | |
1213 | } | |
1214 | } | |
1215 | ||
1216 | /** | |
1217 | * Adds a structured category to the navigation in the correct order/place | |
1218 | * | |
1219 | * @param object $cat | |
435a512e | 1220 | * @param navigation_node $parent |
4766a50c SH |
1221 | */ |
1222 | protected function add_category($cat, navigation_node $parent) { | |
1223 | $category = $parent->get($cat['category']->id, navigation_node::TYPE_CATEGORY); | |
1224 | if (!$category) { | |
1225 | $category = $cat['category']; | |
1226 | $url = new moodle_url('/course/category.php', array('id'=>$category->id)); | |
1227 | $category = $parent->add($category->name, null, self::TYPE_CATEGORY, $category->name, $category->id); | |
1228 | } | |
1229 | foreach ($cat['children'] as $child) { | |
1230 | $this->add_category($child, $category); | |
1231 | } | |
1232 | } | |
1233 | ||
3406acde SH |
1234 | /** |
1235 | * Loads the given course into the navigation | |
7d2a0492 | 1236 | * |
3406acde SH |
1237 | * @param stdClass $course |
1238 | * @return navigation_node | |
1239 | */ | |
1240 | protected function load_course(stdClass $course) { | |
1241 | if ($course->id == SITEID) { | |
1242 | $coursenode = $this->rootnodes['site']; | |
1243 | } else if (array_key_exists($course->id, $this->mycourses)) { | |
1244 | if (!isset($this->mycourses[$course->id]->coursenode)) { | |
1245 | $this->mycourses[$course->id]->coursenode = $this->add_course($course); | |
1246 | } | |
1247 | $coursenode = $this->mycourses[$course->id]->coursenode; | |
1248 | } else { | |
1249 | $coursenode = $this->add_course($course); | |
1250 | } | |
1251 | return $coursenode; | |
1252 | } | |
1253 | ||
1254 | /** | |
1255 | * Loads all of the courses section into the navigation. | |
1256 | * | |
1257 | * This function utilisies a callback that can be implemented within the course | |
1258 | * formats lib.php file to customise the navigation that is generated at this | |
1259 | * point for the course. | |
1260 | * | |
1261 | * By default (if not defined) the method {@see load_generic_course_sections} is | |
1262 | * called instead. | |
1263 | * | |
3406acde SH |
1264 | * @param stdClass $course Database record for the course |
1265 | * @param navigation_node $coursenode The course node within the navigation | |
1266 | * @return array Array of navigation nodes for the section with key = section id | |
1267 | */ | |
1268 | protected function load_course_sections(stdClass $course, navigation_node $coursenode) { | |
1269 | global $CFG; | |
1270 | $structurefile = $CFG->dirroot.'/course/format/'.$course->format.'/lib.php'; | |
1271 | $structurefunc = 'callback_'.$course->format.'_load_content'; | |
1272 | if (function_exists($structurefunc)) { | |
1273 | return $structurefunc($this, $course, $coursenode); | |
1274 | } else if (file_exists($structurefile)) { | |
1275 | require_once $structurefile; | |
1276 | if (function_exists($structurefunc)) { | |
1277 | return $structurefunc($this, $course, $coursenode); | |
1278 | } else { | |
0f4ab67d | 1279 | return $this->load_generic_course_sections($course, $coursenode); |
3406acde SH |
1280 | } |
1281 | } else { | |
0f4ab67d | 1282 | return $this->load_generic_course_sections($course, $coursenode); |
3406acde SH |
1283 | } |
1284 | } | |
1285 | ||
1286 | /** | |
1287 | * Generically loads the course sections into the course's navigation. | |
1288 | * | |
1289 | * @param stdClass $course | |
1290 | * @param navigation_node $coursenode | |
1291 | * @param string $name The string that identifies each section. e.g Topic, or Week | |
1292 | * @param string $activeparam The url used to identify the active section | |
1293 | * @return array An array of course section nodes | |
1294 | */ | |
0f4ab67d | 1295 | public function load_generic_course_sections(stdClass $course, navigation_node $coursenode, $courseformat='unknown') { |
df997f84 PS |
1296 | global $CFG, $DB, $USER; |
1297 | ||
1298 | require_once($CFG->dirroot.'/course/lib.php'); | |
435a512e | 1299 | |
3406acde SH |
1300 | $modinfo = get_fast_modinfo($course); |
1301 | $sections = array_slice(get_all_sections($course->id), 0, $course->numsections+1, true); | |
1302 | $viewhiddensections = has_capability('moodle/course:viewhiddensections', $this->page->context); | |
1303 | ||
0f4ab67d SH |
1304 | if (isloggedin() && !isguestuser()) { |
1305 | $activesection = $DB->get_field("course_display", "display", array("userid"=>$USER->id, "course"=>$course->id)); | |
1306 | } else { | |
1307 | $activesection = null; | |
1308 | } | |
1309 | ||
1310 | $namingfunction = 'callback_'.$courseformat.'_get_section_name'; | |
1311 | $namingfunctionexists = (function_exists($namingfunction)); | |
435a512e | 1312 | |
0f4ab67d SH |
1313 | $activeparamfunction = 'callback_'.$courseformat.'_request_key'; |
1314 | if (function_exists($activeparamfunction)) { | |
1315 | $activeparam = $activeparamfunction(); | |
1316 | } else { | |
1317 | $activeparam = 'section'; | |
dc076831 | 1318 | } |
7487c856 SH |
1319 | $navigationsections = array(); |
1320 | foreach ($sections as $sectionid=>$section) { | |
1321 | $section = clone($section); | |
3406acde SH |
1322 | if ($course->id == SITEID) { |
1323 | $this->load_section_activities($coursenode, $section->section, $modinfo); | |
1324 | } else { | |
1325 | if ((!$viewhiddensections && !$section->visible) || (!$this->showemptysections && !array_key_exists($section->section, $modinfo->sections))) { | |
1326 | continue; | |
1327 | } | |
0f4ab67d SH |
1328 | if ($namingfunctionexists) { |
1329 | $sectionname = $namingfunction($course, $section, $sections); | |
3406acde | 1330 | } else { |
0f4ab67d | 1331 | $sectionname = get_string('section').' '.$section->section; |
3406acde SH |
1332 | } |
1333 | $url = new moodle_url('/course/view.php', array('id'=>$course->id, $activeparam=>$section->section)); | |
1334 | $sectionnode = $coursenode->add($sectionname, $url, navigation_node::TYPE_SECTION, null, $section->id); | |
1335 | $sectionnode->nodetype = navigation_node::NODETYPE_BRANCH; | |
1336 | $sectionnode->hidden = (!$section->visible); | |
91eab03d | 1337 | if ($this->page->context->contextlevel != CONTEXT_MODULE && ($sectionnode->isactive || ($activesection != null && $section->section == $activesection))) { |
0f4ab67d | 1338 | $sectionnode->force_open(); |
3406acde SH |
1339 | $this->load_section_activities($sectionnode, $section->section, $modinfo); |
1340 | } | |
1341 | $section->sectionnode = $sectionnode; | |
7487c856 | 1342 | $navigationsections[$sectionid] = $section; |
3406acde SH |
1343 | } |
1344 | } | |
7487c856 | 1345 | return $navigationsections; |
3406acde SH |
1346 | } |
1347 | /** | |
1348 | * Loads all of the activities for a section into the navigation structure. | |
1349 | * | |
1350 | * @param navigation_node $sectionnode | |
1351 | * @param int $sectionnumber | |
1352 | * @param stdClass $modinfo Object returned from {@see get_fast_modinfo()} | |
1353 | * @return array Array of activity nodes | |
1354 | */ | |
1355 | protected function load_section_activities(navigation_node $sectionnode, $sectionnumber, $modinfo) { | |
1356 | if (!array_key_exists($sectionnumber, $modinfo->sections)) { | |
1357 | return true; | |
1358 | } | |
1359 | ||
1360 | $viewhiddenactivities = has_capability('moodle/course:viewhiddenactivities', $this->page->context); | |
1361 | ||
1362 | $activities = array(); | |
1363 | ||
1364 | foreach ($modinfo->sections[$sectionnumber] as $cmid) { | |
1365 | $cm = $modinfo->cms[$cmid]; | |
1366 | if (!$viewhiddenactivities && !$cm->visible) { | |
1367 | continue; | |
1368 | } | |
1369 | if ($cm->icon) { | |
1370 | $icon = new pix_icon($cm->icon, '', $cm->iconcomponent); | |
1371 | } else { | |
1372 | $icon = new pix_icon('icon', '', $cm->modname); | |
1373 | } | |
1374 | $url = new moodle_url('/mod/'.$cm->modname.'/view.php', array('id'=>$cm->id)); | |
1375 | $activitynode = $sectionnode->add($cm->name, $url, navigation_node::TYPE_ACTIVITY, $cm->name, $cm->id, $icon); | |
1376 | $activitynode->title(get_string('modulename', $cm->modname)); | |
1377 | $activitynode->hidden = (!$cm->visible); | |
1378 | if ($this->module_extends_navigation($cm->modname)) { | |
8f57314b | 1379 | $activitynode->nodetype = navigation_node::NODETYPE_BRANCH; |
3406acde SH |
1380 | } |
1381 | $activities[$cmid] = $activitynode; | |
1382 | } | |
1383 | ||
1384 | return $activities; | |
1385 | } | |
1386 | /** | |
1387 | * Loads the navigation structure for the given activity into the activities node. | |
1388 | * | |
1389 | * This method utilises a callback within the modules lib.php file to load the | |
1390 | * content specific to activity given. | |
1391 | * | |
1392 | * The callback is a method: {modulename}_extend_navigation() | |
1393 | * Examples: | |
1394 | * * {@see forum_extend_navigation()} | |
1395 | * * {@see workshop_extend_navigation()} | |
1396 | * | |
3406acde SH |
1397 | * @param stdClass $cm |
1398 | * @param stdClass $course | |
1399 | * @param navigation_node $activity | |
1400 | * @return bool | |
1401 | */ | |
1402 | protected function load_activity(stdClass $cm, stdClass $course, navigation_node $activity) { | |
1403 | global $CFG, $DB; | |
1404 | ||
1405 | $activity->make_active(); | |
1406 | $file = $CFG->dirroot.'/mod/'.$cm->modname.'/lib.php'; | |
1407 | $function = $cm->modname.'_extend_navigation'; | |
1408 | ||
1409 | if (file_exists($file)) { | |
1410 | require_once($file); | |
1411 | if (function_exists($function)) { | |
1412 | $activtyrecord = $DB->get_record($cm->modname, array('id' => $cm->instance), '*', MUST_EXIST); | |
1413 | $function($activity, $course, $activtyrecord, $cm); | |
1414 | return true; | |
1415 | } | |
1416 | } | |
1417 | $activity->nodetype = navigation_node::NODETYPE_LEAF; | |
1418 | return false; | |
1419 | } | |
1420 | /** | |
1421 | * Loads user specific information into the navigation in the appopriate place. | |
1422 | * | |
1423 | * If no user is provided the current user is assumed. | |
1424 | * | |
3406acde SH |
1425 | * @param stdClass $user |
1426 | * @return bool | |
7a7e209d | 1427 | */ |
87c215de | 1428 | protected function load_for_user($user=null, $forceforcontext=false) { |
3406acde | 1429 | global $DB, $CFG, $USER; |
4f0c2d00 | 1430 | |
7a7e209d SH |
1431 | $iscurrentuser = false; |
1432 | if ($user === null) { | |
1433 | // We can't require login here but if the user isn't logged in we don't | |
1434 | // want to show anything | |
b9d4c7d3 | 1435 | if (!isloggedin() || isguestuser()) { |
7a7e209d SH |
1436 | return false; |
1437 | } | |
1438 | $user = $USER; | |
1439 | $iscurrentuser = true; | |
1440 | } else if (!is_object($user)) { | |
1441 | // If the user is not an object then get them from the database | |
1442 | $user = $DB->get_record('user', array('id'=>(int)$user), '*', MUST_EXIST); | |
1443 | } | |
507a7a9a | 1444 | $usercontext = get_context_instance(CONTEXT_USER, $user->id); |
7a7e209d SH |
1445 | |
1446 | // Get the course set against the page, by default this will be the site | |
3406acde SH |
1447 | $course = $this->page->course; |
1448 | $baseargs = array('id'=>$user->id); | |
87c215de | 1449 | if ($course->id !== SITEID && (!$iscurrentuser || $forceforcontext)) { |
3406acde SH |
1450 | if (array_key_exists($course->id, $this->mycourses)) { |
1451 | $coursenode = $this->mycourses[$course->id]->coursenode; | |
1452 | } else { | |
1453 | $coursenode = $this->rootnodes['courses']->find($course->id, navigation_node::TYPE_COURSE); | |
1454 | if (!$coursenode) { | |
1455 | $coursenode = $this->load_course($course); | |
1456 | } | |
1457 | } | |
7a7e209d | 1458 | $baseargs['course'] = $course->id; |
3406acde | 1459 | $coursecontext = get_context_instance(CONTEXT_COURSE, $course->id); |
7a7e209d | 1460 | $issitecourse = false; |
7d2a0492 | 1461 | } else { |
7a7e209d | 1462 | // Load all categories and get the context for the system |
507a7a9a | 1463 | $coursecontext = get_context_instance(CONTEXT_SYSTEM); |
7a7e209d SH |
1464 | $issitecourse = true; |
1465 | } | |
1466 | ||
1467 | // Create a node to add user information under. | |
87c215de | 1468 | if ($iscurrentuser && !$forceforcontext) { |
3406acde SH |
1469 | // If it's the current user the information will go under the profile root node |
1470 | $usernode = $this->rootnodes['myprofile']; | |
7a7e209d SH |
1471 | } else { |
1472 | if (!$issitecourse) { | |
1473 | // Not the current user so add it to the participants node for the current course | |
3406acde | 1474 | $usersnode = $coursenode->get('participants', navigation_node::TYPE_CONTAINER); |
7a7e209d SH |
1475 | } else { |
1476 | // This is the site so add a users node to the root branch | |
3406acde SH |
1477 | $usersnode = $this->rootnodes['users']; |
1478 | $usersnode->action = new moodle_url('/user/index.php', array('id'=>$course->id)); | |
7a7e209d SH |
1479 | } |
1480 | // Add a branch for the current user | |
25aad2c4 | 1481 | $usernode = $usersnode->add(fullname($user, true), null, self::TYPE_USER, null, $user->id); |
3406acde SH |
1482 | } |
1483 | ||
1484 | if ($this->page->context->contextlevel == CONTEXT_USER && $user->id == $this->page->context->instanceid) { | |
1485 | $usernode->force_open(); | |
7a7e209d SH |
1486 | } |
1487 | ||
1488 | // If the user is the current user or has permission to view the details of the requested | |
1489 | // user than add a view profile link. | |
507a7a9a | 1490 | if ($iscurrentuser || has_capability('moodle/user:viewdetails', $coursecontext) || has_capability('moodle/user:viewdetails', $usercontext)) { |
87c215de | 1491 | if ($issitecourse || ($iscurrentuser && !$forceforcontext)) { |
03d9401e MD |
1492 | $usernode->add(get_string('viewprofile'), new moodle_url('/user/profile.php',$baseargs)); |
1493 | } else { | |
1494 | $usernode->add(get_string('viewprofile'), new moodle_url('/user/view.php',$baseargs)); | |
1495 | } | |
7a7e209d SH |
1496 | } |
1497 | ||
1498 | // Add nodes for forum posts and discussions if the user can view either or both | |
507a7a9a SH |
1499 | $canviewposts = has_capability('moodle/user:readuserposts', $usercontext); |
1500 | $canviewdiscussions = has_capability('mod/forum:viewdiscussion', $coursecontext); | |
7a7e209d SH |
1501 | if ($canviewposts || $canviewdiscussions) { |
1502 | $forumtab = $usernode->add(get_string('forumposts', 'forum')); | |
7a7e209d | 1503 | if ($canviewposts) { |
a6855934 | 1504 | $forumtab->add(get_string('posts', 'forum'), new moodle_url('/mod/forum/user.php', $baseargs)); |
7a7e209d SH |
1505 | } |
1506 | if ($canviewdiscussions) { | |
03d9401e | 1507 | $forumtab->add(get_string('discussions', 'forum'), new moodle_url('/mod/forum/user.php', array_merge($baseargs, array('mode'=>'discussions', 'course'=>$course->id)))); |
7a7e209d SH |
1508 | } |
1509 | } | |
1510 | ||
27bad0a6 SH |
1511 | // Add blog nodes |
1512 | if (!empty($CFG->bloglevel)) { | |
1513 | require_once($CFG->dirroot.'/blog/lib.php'); | |
1514 | // Get all options for the user | |
1515 | $options = blog_get_options_for_user($user); | |
1516 | if (count($options) > 0) { | |
1517 | $blogs = $usernode->add(get_string('blogs', 'blog'), null, navigation_node::TYPE_CONTAINER); | |
1518 | foreach ($options as $option) { | |
1519 | $blogs->add($option['string'], $option['link']); | |
1520 | } | |
1521 | } | |
1522 | } | |
1523 | ||
c81b9f69 AD |
1524 | $messageargs = null; |
1525 | if ($USER->id!=$user->id) { | |
1526 | $messageargs = array('id'=>$user->id); | |
1527 | } | |
02f2c7bd | 1528 | $url = new moodle_url('/message/index.php',$messageargs); |
c81b9f69 AD |
1529 | $usernode->add(get_string('messages', 'message'), $url, self::TYPE_SETTING, null, 'messages'); |
1530 | ||
78765507 DC |
1531 | // TODO: Private file capability check |
1532 | if ($iscurrentuser) { | |
1533 | $url = new moodle_url('/blocks/private_files/edit.php'); | |
1534 | $usernode->add(get_string('privatefiles', 'block_private_files'), $url, self::TYPE_SETTING); | |
1535 | } | |
1536 | ||
7a7e209d | 1537 | // Add a node to view the users notes if permitted |
507a7a9a | 1538 | if (!empty($CFG->enablenotes) && has_any_capability(array('moodle/notes:manage', 'moodle/notes:view'), $coursecontext)) { |
3406acde SH |
1539 | $url = new moodle_url('/notes/index.php',array('user'=>$user->id)); |
1540 | if ($coursecontext->instanceid) { | |
1541 | $url->param('course', $coursecontext->instanceid); | |
1542 | } | |
1543 | $usernode->add(get_string('notes', 'notes'), $url); | |
7a7e209d SH |
1544 | } |
1545 | ||
1546 | // Add a reports tab and then add reports the the user has permission to see. | |
507a7a9a | 1547 | $anyreport = has_capability('moodle/user:viewuseractivitiesreport', $usercontext); |
7a7e209d | 1548 | $viewreports = ($anyreport || ($course->showreports && $iscurrentuser)); |
03d9401e MD |
1549 | if ($viewreports) { |
1550 | $reporttab = $usernode->add(get_string('activityreports')); | |
1551 | $reportargs = array('user'=>$user->id); | |
1552 | if (!empty($course->id)) { | |
1553 | $reportargs['id'] = $course->id; | |
1554 | } else { | |
1555 | $reportargs['id'] = SITEID; | |
1556 | } | |
1557 | if ($viewreports || has_capability('coursereport/outline:view', $coursecontext)) { | |
1558 | $reporttab->add(get_string('outlinereport'), new moodle_url('/course/user.php', array_merge($reportargs, array('mode'=>'outline')))); | |
1559 | $reporttab->add(get_string('completereport'), new moodle_url('/course/user.php', array_merge($reportargs, array('mode'=>'complete')))); | |
1560 | } | |
4f0c2d00 | 1561 | |
03d9401e MD |
1562 | if ($viewreports || has_capability('coursereport/log:viewtoday', $coursecontext)) { |
1563 | $reporttab->add(get_string('todaylogs'), new moodle_url('/course/user.php', array_merge($reportargs, array('mode'=>'todaylogs')))); | |
1564 | } | |
7a7e209d | 1565 | |
03d9401e MD |
1566 | if ($viewreports || has_capability('coursereport/log:view', $coursecontext)) { |
1567 | $reporttab->add(get_string('alllogs'), new moodle_url('/course/user.php', array_merge($reportargs, array('mode'=>'alllogs')))); | |
1568 | } | |
7a7e209d | 1569 | |
03d9401e MD |
1570 | if (!empty($CFG->enablestats)) { |
1571 | if ($viewreports || has_capability('coursereport/stats:view', $coursecontext)) { | |
1572 | $reporttab->add(get_string('stats'), new moodle_url('/course/user.php', array_merge($reportargs, array('mode'=>'stats')))); | |
1573 | } | |
7a7e209d | 1574 | } |
7a7e209d | 1575 | |
03d9401e MD |
1576 | $gradeaccess = false; |
1577 | if (has_capability('moodle/grade:viewall', $coursecontext)) { | |
1578 | //ok - can view all course grades | |
7a7e209d | 1579 | $gradeaccess = true; |
03d9401e MD |
1580 | } else if ($course->showgrades) { |
1581 | if ($iscurrentuser && has_capability('moodle/grade:view', $coursecontext)) { | |
1582 | //ok - can view own grades | |
1583 | $gradeaccess = true; | |
1584 | } else if (has_capability('moodle/grade:viewall', $usercontext)) { | |
1585 | // ok - can view grades of this user - parent most probably | |
1586 | $gradeaccess = true; | |
1587 | } else if ($anyreport) { | |
1588 | // ok - can view grades of this user - parent most probably | |
1589 | $gradeaccess = true; | |
1590 | } | |
1591 | } | |
1592 | if ($gradeaccess) { | |
1593 | $reporttab->add(get_string('grade'), new moodle_url('/course/user.php', array_merge($reportargs, array('mode'=>'grade')))); | |
7a7e209d | 1594 | } |
7a7e209d | 1595 | |
03d9401e MD |
1596 | // Check the number of nodes in the report node... if there are none remove |
1597 | // the node | |
1598 | if (count($reporttab->children)===0) { | |
1599 | $usernode->remove_child($reporttab); | |
1600 | } | |
7a7e209d SH |
1601 | } |
1602 | ||
03d9401e | 1603 | |
7a7e209d SH |
1604 | // If the user is the current user add the repositories for the current user |
1605 | if ($iscurrentuser) { | |
78765507 | 1606 | |
7a7e209d | 1607 | require_once($CFG->dirroot . '/repository/lib.php'); |
507a7a9a | 1608 | $editabletypes = repository::get_editable_types($usercontext); |
7a7e209d | 1609 | if (!empty($editabletypes)) { |
ad70376c | 1610 | $usernode->add(get_string('repositories', 'repository'), new moodle_url('/repository/manage_instances.php', array('contextid' => $usercontext->id))); |
7a7e209d SH |
1611 | } |
1612 | } | |
1613 | return true; | |
1614 | } | |
1615 | ||
1616 | /** | |
3406acde | 1617 | * This method simply checks to see if a given module can extend the navigation. |
7d2a0492 | 1618 | * |
1619 | * @param string $modname | |
1620 | * @return bool | |
1621 | */ | |
1622 | protected function module_extends_navigation($modname) { | |
1623 | global $CFG; | |
1624 | if ($this->cache->cached($modname.'_extends_navigation')) { | |
1625 | return $this->cache->{$modname.'_extends_navigation'}; | |
1626 | } | |
1627 | $file = $CFG->dirroot.'/mod/'.$modname.'/lib.php'; | |
1628 | $function = $modname.'_extend_navigation'; | |
1629 | if (function_exists($function)) { | |
1630 | $this->cache->{$modname.'_extends_navigation'} = true; | |
1631 | return true; | |
1632 | } else if (file_exists($file)) { | |
1633 | require_once($file); | |
1634 | if (function_exists($function)) { | |
1635 | $this->cache->{$modname.'_extends_navigation'} = true; | |
1636 | return true; | |
1637 | } | |
1638 | } | |
1639 | $this->cache->{$modname.'_extends_navigation'} = false; | |
1640 | return false; | |
1641 | } | |
1642 | /** | |
3406acde | 1643 | * Extends the navigation for the given user. |
435a512e | 1644 | * |
3406acde | 1645 | * @param stdClass $user A user from the database |
7d2a0492 | 1646 | */ |
3406acde SH |
1647 | public function extend_for_user($user) { |
1648 | $this->extendforuser[] = $user; | |
5d07e957 SH |
1649 | } |
1650 | ||
1651 | /** | |
1652 | * Returns all of the users the navigation is being extended for | |
1653 | * | |
1654 | * @return array | |
1655 | */ | |
1656 | public function get_extending_users() { | |
1657 | return $this->extendforuser; | |
7d2a0492 | 1658 | } |
7d2a0492 | 1659 | /** |
3406acde | 1660 | * Adds the given course to the navigation structure. |
7d2a0492 | 1661 | * |
3406acde SH |
1662 | * @param stdClass $course |
1663 | * @return navigation_node | |
7d2a0492 | 1664 | */ |
4766a50c SH |
1665 | public function add_course(stdClass $course, $forcegeneric = false) { |
1666 | global $CFG; | |
3406acde | 1667 | $canviewhidden = has_capability('moodle/course:viewhiddencourses', $this->page->context); |
df997f84 | 1668 | if ($course->id !== SITEID && !$canviewhidden && !$course->visible) { |
3406acde | 1669 | return false; |
7d2a0492 | 1670 | } |
7d2a0492 | 1671 | |
4766a50c SH |
1672 | $issite = ($course->id == SITEID); |
1673 | $ismycourse = (array_key_exists($course->id, $this->mycourses) && !$forcegeneric); | |
1674 | $displaycategories = (!$ismycourse && !$issite && !empty($CFG->navshowcategories)); | |
95892197 | 1675 | $shortname = $course->shortname; |
4766a50c SH |
1676 | |
1677 | if ($issite) { | |
3406acde | 1678 | $parent = $this; |
4766a50c | 1679 | $url = null; |
95892197 | 1680 | $shortname = get_string('sitepages'); |
4766a50c | 1681 | } else if ($ismycourse) { |
3406acde SH |
1682 | $parent = $this->rootnodes['mycourses']; |
1683 | $url = new moodle_url('/course/view.php', array('id'=>$course->id)); | |
7a7e209d | 1684 | } else { |
3406acde | 1685 | $parent = $this->rootnodes['courses']; |
a6855934 | 1686 | $url = new moodle_url('/course/view.php', array('id'=>$course->id)); |
7d2a0492 | 1687 | } |
4766a50c SH |
1688 | |
1689 | if ($displaycategories) { | |
1690 | // We need to load the category structure for this course | |
1691 | $categoryfound = false; | |
1692 | if (!empty($course->categorypath)) { | |
1693 | $categories = explode('/', trim($course->categorypath, '/')); | |
1694 | $category = $parent; | |
1695 | while ($category && $categoryid = array_shift($categories)) { | |
1696 | $category = $category->get($categoryid, self::TYPE_CATEGORY); | |
1697 | } | |
1698 | if ($category instanceof navigation_node) { | |
1699 | $parent = $category; | |
1700 | $categoryfound = true; | |
1701 | } | |
1702 | if (!$categoryfound && $forcegeneric) { | |
1703 | $this->load_all_categories($course->category); | |
1704 | if ($category = $parent->find($course->category, self::TYPE_CATEGORY)) { | |
1705 | $parent = $category; | |
1706 | $categoryfound = true; | |
1707 | } | |
1708 | } | |
1709 | } else if (!empty($course->category)) { | |
1710 | $this->load_all_categories($course->category); | |
1711 | if ($category = $parent->find($course->category, self::TYPE_CATEGORY)) { | |
1712 | $parent = $category; | |
1713 | $categoryfound = true; | |
1714 | } | |
1715 | if (!$categoryfound && !$forcegeneric) { | |
1716 | $this->load_all_categories($course->category); | |
1717 | if ($category = $parent->find($course->category, self::TYPE_CATEGORY)) { | |
1718 | $parent = $category; | |
1719 | $categoryfound = true; | |
1720 | } | |
1721 | } | |
1722 | } | |
1723 | } | |
1724 | ||
1725 | // We found the course... we can return it now :) | |
1726 | if ($coursenode = $parent->get($course->id, self::TYPE_COURSE)) { | |
1727 | return $coursenode; | |
1728 | } | |
1729 | ||
95892197 | 1730 | $coursenode = $parent->add($shortname, $url, self::TYPE_COURSE, $shortname, $course->id); |
3406acde SH |
1731 | $coursenode->nodetype = self::NODETYPE_BRANCH; |
1732 | $coursenode->hidden = (!$course->visible); | |
4766a50c | 1733 | $coursenode->title($course->fullname); |
3406acde | 1734 | $this->addedcourses[$course->id] = &$coursenode; |
4766a50c SH |
1735 | if ($ismycourse && !empty($CFG->navshowallcourses)) { |
1736 | // We need to add this course to the general courses node as well as the | |
1737 | // my courses node, rerun the function with the kill param | |
1738 | $genericcourse = $this->add_course($course, true); | |
1739 | if ($genericcourse->isactive) { | |
1740 | $genericcourse->make_inactive(); | |
1741 | $genericcourse->collapse = true; | |
1742 | if ($genericcourse->parent && $genericcourse->parent->type == self::TYPE_CATEGORY) { | |
1743 | $parent = $genericcourse->parent; | |
1744 | while ($parent && $parent->type == self::TYPE_CATEGORY) { | |
1745 | $parent->collapse = true; | |
1746 | $parent = $parent->parent; | |
1747 | } | |
1748 | } | |
1749 | } | |
1750 | } | |
3406acde | 1751 | return $coursenode; |
7d2a0492 | 1752 | } |
1753 | /** | |
3406acde | 1754 | * Adds essential course nodes to the navigation for the given course. |
7d2a0492 | 1755 | * |
3406acde | 1756 | * This method adds nodes such as reports, blogs and participants |
7d2a0492 | 1757 | * |
3406acde SH |
1758 | * @param navigation_node $coursenode |
1759 | * @param stdClass $course | |
7d2a0492 | 1760 | * @return bool |
1761 | */ | |
3406acde SH |
1762 | public function add_course_essentials(navigation_node $coursenode, stdClass $course) { |
1763 | global $CFG; | |
7d2a0492 | 1764 | |
4766a50c | 1765 | if ($course->id == SITEID) { |
3406acde | 1766 | return $this->add_front_page_course_essentials($coursenode, $course); |
7d2a0492 | 1767 | } |
7d2a0492 | 1768 | |
3406acde SH |
1769 | if ($coursenode == false || $coursenode->get('participants', navigation_node::TYPE_CONTAINER)) { |
1770 | return true; | |
7d2a0492 | 1771 | } |
7d2a0492 | 1772 | |
3406acde SH |
1773 | //Participants |
1774 | if (has_capability('moodle/course:viewparticipants', $this->page->context)) { | |
1775 | require_once($CFG->dirroot.'/blog/lib.php'); | |
1776 | $participants = $coursenode->add(get_string('participants'), new moodle_url('/user/index.php?id='.$course->id), self::TYPE_CONTAINER, get_string('participants'), 'participants'); | |
1777 | $currentgroup = groups_get_course_group($course, true); | |
1778 | if ($course->id == SITEID) { | |
1779 | $filterselect = ''; | |
1780 | } else if ($course->id && !$currentgroup) { | |
1781 | $filterselect = $course->id; | |
1782 | } else { | |
1783 | $filterselect = $currentgroup; | |
1784 | } | |
1785 | $filterselect = clean_param($filterselect, PARAM_INT); | |
1786 | if ($CFG->bloglevel >= 3) { | |
1787 | $blogsurls = new moodle_url('/blog/index.php', array('courseid' => $filterselect)); | |
1788 | $participants->add(get_string('blogs','blog'), $blogsurls->out()); | |
1789 | } | |
1790 | if (!empty($CFG->enablenotes) && (has_capability('moodle/notes:manage', $this->page->context) || has_capability('moodle/notes:view', $this->page->context))) { | |
1791 | $participants->add(get_string('notes','notes'), new moodle_url('/notes/index.php', array('filtertype'=>'course', 'filterselect'=>$filterselect))); | |
1792 | } | |
533299cb | 1793 | } else if (count($this->extendforuser) > 0 || $this->page->course->id == $course->id) { |
3406acde SH |
1794 | $participants = $coursenode->add(get_string('participants'), null, self::TYPE_CONTAINER, get_string('participants'), 'participants'); |
1795 | } | |
1796 | ||
1797 | // View course reports | |
1798 | if (has_capability('moodle/site:viewreports', $this->page->context)) { // basic capability for listing of reports | |
1799 | $reportnav = $coursenode->add(get_string('reports'), new moodle_url('/course/report.php', array('id'=>$course->id)), self::TYPE_CONTAINER, null, null, new pix_icon('i/stats', '')); | |
1800 | $coursereports = get_plugin_list('coursereport'); | |
1801 | foreach ($coursereports as $report=>$dir) { | |
1802 | $libfile = $CFG->dirroot.'/course/report/'.$report.'/lib.php'; | |
1803 | if (file_exists($libfile)) { | |
1804 | require_once($libfile); | |
1805 | $reportfunction = $report.'_report_extend_navigation'; | |
1806 | if (function_exists($report.'_report_extend_navigation')) { | |
1807 | $reportfunction($reportnav, $course, $this->page->context); | |
7d2a0492 | 1808 | } |
1809 | } | |
1810 | } | |
1811 | } | |
1812 | return true; | |
1813 | } | |
1814 | ||
3406acde SH |
1815 | public function add_front_page_course_essentials(navigation_node $coursenode, stdClass $course) { |
1816 | global $CFG; | |
7d2a0492 | 1817 | |
3406acde SH |
1818 | if ($coursenode == false || $coursenode->get('participants', navigation_node::TYPE_CUSTOM)) { |
1819 | return true; | |
7a7e209d SH |
1820 | } |
1821 | ||
3406acde SH |
1822 | //Participants |
1823 | if (has_capability('moodle/course:viewparticipants', $this->page->context)) { | |
3406acde SH |
1824 | $coursenode->add(get_string('participants'), new moodle_url('/user/index.php?id='.$course->id), self::TYPE_CUSTOM, get_string('participants'), 'participants'); |
1825 | } | |
435a512e | 1826 | |
3406acde SH |
1827 | $currentgroup = groups_get_course_group($course, true); |
1828 | if ($course->id == SITEID) { | |
1829 | $filterselect = ''; | |
1830 | } else if ($course->id && !$currentgroup) { | |
1831 | $filterselect = $course->id; | |
7d2a0492 | 1832 | } else { |
3406acde | 1833 | $filterselect = $currentgroup; |
7d2a0492 | 1834 | } |
3406acde | 1835 | $filterselect = clean_param($filterselect, PARAM_INT); |
593270c6 MD |
1836 | |
1837 | // Blogs | |
1838 | if (has_capability('moodle/blog:view', $this->page->context)) { | |
1839 | require_once($CFG->dirroot.'/blog/lib.php'); | |
1840 | if (blog_is_enabled_for_user()) { | |
1841 | $blogsurls = new moodle_url('/blog/index.php', array('courseid' => $filterselect)); | |
1842 | $coursenode->add(get_string('blogs','blog'), $blogsurls->out()); | |
1843 | } | |
7d2a0492 | 1844 | } |
593270c6 MD |
1845 | |
1846 | // Notes | |
3406acde SH |
1847 | if (!empty($CFG->enablenotes) && (has_capability('moodle/notes:manage', $this->page->context) || has_capability('moodle/notes:view', $this->page->context))) { |
1848 | $coursenode->add(get_string('notes','notes'), new moodle_url('/notes/index.php', array('filtertype'=>'course', 'filterselect'=>$filterselect))); | |
1849 | } | |
593270c6 MD |
1850 | |
1851 | // Tags | |
1852 | if (!empty($CFG->usetags) && isloggedin()) { | |
3406acde | 1853 | $coursenode->add(get_string('tags', 'tag'), new moodle_url('/tag/search.php')); |
7d2a0492 | 1854 | } |
6644741d | 1855 | |
6644741d | 1856 | |
3406acde SH |
1857 | // View course reports |
1858 | if (has_capability('moodle/site:viewreports', $this->page->context)) { // basic capability for listing of reports | |
1859 | $reportnav = $coursenode->add(get_string('reports'), new moodle_url('/course/report.php', array('id'=>$course->id)), self::TYPE_CONTAINER, null, null, new pix_icon('i/stats', '')); | |
1860 | $coursereports = get_plugin_list('coursereport'); | |
1861 | foreach ($coursereports as $report=>$dir) { | |
1862 | $libfile = $CFG->dirroot.'/course/report/'.$report.'/lib.php'; | |
1863 | if (file_exists($libfile)) { | |
1864 | require_once($libfile); | |
1865 | $reportfunction = $report.'_report_extend_navigation'; | |
1866 | if (function_exists($report.'_report_extend_navigation')) { | |
1867 | $reportfunction($reportnav, $course, $this->page->context); | |
1868 | } | |
6644741d | 1869 | } |
6644741d | 1870 | } |
1871 | } | |
3406acde | 1872 | return true; |
6644741d | 1873 | } |
da3ab9c4 | 1874 | |
3406acde SH |
1875 | /** |
1876 | * Clears the navigation cache | |
1877 | */ | |
1878 | public function clear_cache() { | |
1879 | $this->cache->clear(); | |
da3ab9c4 | 1880 | } |
88f77c3c SH |
1881 | |
1882 | public function set_expansion_limit($type) { | |
1883 | $nodes = $this->find_all_of_type($type); | |
1884 | foreach ($nodes as &$node) { | |
1af67ecb SH |
1885 | // We need to generate the full site node |
1886 | if ($type == self::TYPE_COURSE && $node->key == SITEID) { | |
1887 | continue; | |
1888 | } | |
88f77c3c | 1889 | foreach ($node->children as &$child) { |
1af67ecb SH |
1890 | // We still want to show course reports and participants containers |
1891 | // or there will be navigation missing. | |
1892 | if ($type == self::TYPE_COURSE && $child->type === self::TYPE_CONTAINER) { | |
1893 | continue; | |
1894 | } | |
88f77c3c SH |
1895 | $child->display = false; |
1896 | } | |
1897 | } | |
1898 | return true; | |
1899 | } | |
e2b436d0 SH |
1900 | |
1901 | public function get($key, $type = null) { | |
246a9b05 SH |
1902 | if (!$this->initialised) { |
1903 | $this->initialise(); | |
1904 | } | |
54dc15ab | 1905 | return parent::get($key, $type); |
e2b436d0 SH |
1906 | } |
1907 | ||
1908 | public function find($key, $type) { | |
246a9b05 SH |
1909 | if (!$this->initialised) { |
1910 | $this->initialise(); | |
1911 | } | |
54dc15ab | 1912 | return parent::find($key, $type); |
e2b436d0 | 1913 | } |
7d2a0492 | 1914 | } |
1915 | ||
1916 | /** | |
1917 | * The limited global navigation class used for the AJAX extension of the global | |
1918 | * navigation class. | |
1919 | * | |
1920 | * The primary methods that are used in the global navigation class have been overriden | |
1921 | * to ensure that only the relevant branch is generated at the root of the tree. | |
1922 | * This can be done because AJAX is only used when the backwards structure for the | |
1923 | * requested branch exists. | |
1924 | * This has been done only because it shortens the amounts of information that is generated | |
1925 | * which of course will speed up the response time.. because no one likes laggy AJAX. | |
1926 | * | |
1927 | * @package moodlecore | |
babb3911 | 1928 | * @subpackage navigation |
7d2a0492 | 1929 | * @copyright 2009 Sam Hemelryk |
1930 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | |
1931 | */ | |
507a7a9a | 1932 | class global_navigation_for_ajax extends global_navigation { |
3406acde SH |
1933 | |
1934 | /** @var array */ | |
1935 | protected $expandable = array(); | |
1936 | ||
7d2a0492 | 1937 | /** |
3406acde | 1938 | * Constructs the navigation for use in AJAX request |
3406acde | 1939 | */ |
246a9b05 | 1940 | public function __construct($page, $branchtype, $id) { |
4766a50c | 1941 | $this->page = $page; |
3406acde SH |
1942 | $this->cache = new navigation_cache(NAVIGATION_CACHE_NAME); |
1943 | $this->children = new navigation_node_collection(); | |
246a9b05 | 1944 | $this->initialise($branchtype, $id); |
3406acde SH |
1945 | } |
1946 | /** | |
1947 | * Initialise the navigation given the type and id for the branch to expand. | |
7d2a0492 | 1948 | * |
3406acde SH |
1949 | * @param int $branchtype One of navigation_node::TYPE_* |
1950 | * @param int $id | |
1951 | * @return array The expandable nodes | |
7d2a0492 | 1952 | */ |
507a7a9a | 1953 | public function initialise($branchtype, $id) { |
246a9b05 | 1954 | global $CFG, $DB, $PAGE, $SITE; |
507a7a9a | 1955 | |
7d2a0492 | 1956 | if ($this->initialised || during_initial_install()) { |
95b97515 | 1957 | return $this->expandable; |
7d2a0492 | 1958 | } |
246a9b05 SH |
1959 | $this->initialised = true; |
1960 | ||
1961 | $this->rootnodes = array(); | |
1962 | $this->rootnodes['site'] = $this->add_course($SITE); | |
1963 | $this->rootnodes['courses'] = $this->add(get_string('courses'), null, self::TYPE_ROOTNODE, null, 'courses'); | |
507a7a9a SH |
1964 | |
1965 | // Branchtype will be one of navigation_node::TYPE_* | |
1966 | switch ($branchtype) { | |
4766a50c SH |
1967 | case self::TYPE_CATEGORY : |
1968 | $this->load_all_categories($id); | |
1969 | $limit = 20; | |
1970 | if (!empty($CFG->navcourselimit)) { | |
1971 | $limit = (int)$CFG->navcourselimit; | |
1972 | } | |
1973 | $courses = $DB->get_records('course', array('category' => $id), 'sortorder','*', 0, $limit); | |
1974 | foreach ($courses as $course) { | |
1975 | $this->add_course($course); | |
1976 | } | |
1977 | break; | |
507a7a9a SH |
1978 | case self::TYPE_COURSE : |
1979 | $course = $DB->get_record('course', array('id' => $id), '*', MUST_EXIST); | |
1980 | require_course_login($course); | |
3406acde | 1981 | $this->page = $PAGE; |
87c215de | 1982 | $this->page->set_context(get_context_instance(CONTEXT_COURSE, $course->id)); |
3406acde SH |
1983 | $coursenode = $this->add_course($course); |
1984 | $this->add_course_essentials($coursenode, $course); | |
1985 | if ($this->format_display_course_content($course->format)) { | |
1986 | $this->load_course_sections($course, $coursenode); | |
1987 | } | |
7d2a0492 | 1988 | break; |
507a7a9a | 1989 | case self::TYPE_SECTION : |
3406acde | 1990 | $sql = 'SELECT c.*, cs.section AS sectionnumber |
507a7a9a SH |
1991 | FROM {course} c |
1992 | LEFT JOIN {course_sections} cs ON cs.course = c.id | |
1993 | WHERE cs.id = ?'; | |
1994 | $course = $DB->get_record_sql($sql, array($id), MUST_EXIST); | |
1995 | require_course_login($course); | |
3406acde | 1996 | $this->page = $PAGE; |
87c215de | 1997 | $this->page->set_context(get_context_instance(CONTEXT_COURSE, $course->id)); |
3406acde SH |
1998 | $coursenode = $this->add_course($course); |
1999 | $this->add_course_essentials($coursenode, $course); | |
2000 | $sections = $this->load_course_sections($course, $coursenode); | |
2001 | $this->load_section_activities($sections[$course->sectionnumber]->sectionnode, $course->sectionnumber, get_fast_modinfo($course)); | |
7d2a0492 | 2002 | break; |
507a7a9a SH |
2003 | case self::TYPE_ACTIVITY : |
2004 | $cm = get_coursemodule_from_id(false, $id, 0, false, MUST_EXIST); | |
2005 | $course = $DB->get_record('course', array('id'=>$cm->course), '*', MUST_EXIST); | |
2006 | require_course_login($course, true, $cm); | |
3406acde | 2007 | $this->page = $PAGE; |
87c215de | 2008 | $this->page->set_context(get_context_instance(CONTEXT_MODULE, $cm->id)); |
3406acde SH |
2009 | $coursenode = $this->load_course($course); |
2010 | $sections = $this->load_course_sections($course, $coursenode); | |
2011 | foreach ($sections as $section) { | |
2012 | if ($section->id == $cm->section) { | |
2013 | $cm->sectionnumber = $section->section; | |
2014 | break; | |
2015 | } | |
2016 | } | |
1aa1e9b5 SH |
2017 | if ($course->id == SITEID) { |
2018 | $modulenode = $this->load_activity($cm, $course, $coursenode->find($cm->id, self::TYPE_ACTIVITY)); | |
2019 | } else { | |
2020 | $activities = $this->load_section_activities($sections[$cm->sectionnumber]->sectionnode, $cm->sectionnumber, get_fast_modinfo($course)); | |
2021 | $modulenode = $this->load_activity($cm, $course, $activities[$cm->id]); | |
2022 | } | |
7d2a0492 | 2023 | break; |
507a7a9a | 2024 | default: |
3406acde | 2025 | throw new Exception('Unknown type'); |
507a7a9a | 2026 | return $this->expandable; |
7d2a0492 | 2027 | } |
507a7a9a | 2028 | $this->find_expandable($this->expandable); |
507a7a9a | 2029 | return $this->expandable; |
246a9b05 SH |
2030 | } |
2031 | ||
2032 | public function get_expandable() { | |
2033 | return $this->expandable; | |
7d2a0492 | 2034 | } |
7d2a0492 | 2035 | } |
2036 | ||
2037 | /** | |
2038 | * Navbar class | |
2039 | * | |
2040 | * This class is used to manage the navbar, which is initialised from the navigation | |
2041 | * object held by PAGE | |
2042 | * | |
2043 | * @package moodlecore | |
babb3911 | 2044 | * @subpackage navigation |
7d2a0492 | 2045 | * @copyright 2009 Sam Hemelryk |
2046 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | |
2047 | */ | |
2048 | class navbar extends navigation_node { | |
2049 | /** @var bool */ | |
2050 | protected $initialised = false; | |
2051 | /** @var mixed */ | |
2052 | protected $keys = array(); | |
2053 | /** @var null|string */ | |
2054 | protected $content = null; | |
3406acde | 2055 | /** @var moodle_page object */ |
7d2a0492 | 2056 | protected $page; |
2057 | /** @var bool */ | |
31759089 | 2058 | protected $ignoreactive = false; |
2059 | /** @var bool */ | |
7d2a0492 | 2060 | protected $duringinstall = false; |
7a7e209d SH |
2061 | /** @var bool */ |
2062 | protected $hasitems = false; | |
3406acde SH |
2063 | /** @var array */ |
2064 | protected $items; | |
2065 | /** @var array */ | |
2066 | public $children = array(); | |
4d5059d4 SH |
2067 | /** @var bool */ |
2068 | public $includesettingsbase = false; | |
7d2a0492 | 2069 | /** |
2070 | * The almighty constructor | |
3406acde SH |
2071 | * |
2072 | * @param moodle_page $page | |
7d2a0492 | 2073 | */ |
3406acde | 2074 | public function __construct(moodle_page $page) { |
507a7a9a | 2075 | global $CFG; |
7d2a0492 | 2076 | if (during_initial_install()) { |
2077 | $this->duringinstall = true; | |
2078 | return false; | |
2079 | } | |
2080 | $this->page = $page; | |
2081 | $this->text = get_string('home'); | |
2082 | $this->shorttext = get_string('home'); | |
2083 | $this->action = new moodle_url($CFG->wwwroot); | |
2084 | $this->nodetype = self::NODETYPE_BRANCH; | |
2085 | $this->type = self::TYPE_SYSTEM; | |
2086 | } | |
2087 | ||
2088 | /** | |
2089 | * Quick check to see if the navbar will have items in. | |
2090 | * | |
2091 | * @return bool Returns true if the navbar will have items, false otherwise | |
2092 | */ | |
2093 | public function has_items() { | |
2094 | if ($this->duringinstall) { | |
2095 | return false; | |
7a7e209d SH |
2096 | } else if ($this->hasitems !== false) { |
2097 | return true; | |
7d2a0492 | 2098 | } |
3406acde | 2099 | $this->page->navigation->initialise($this->page); |
bf6c37c7 | 2100 | |
7a7e209d | 2101 | $activenodefound = ($this->page->navigation->contains_active_node() || |
3406acde | 2102 | $this->page->settingsnav->contains_active_node()); |
bf6c37c7 | 2103 | |
3406acde | 2104 | $outcome = (count($this->children)>0 || (!$this->ignoreactive && $activenodefound)); |
7a7e209d | 2105 | $this->hasitems = $outcome; |
bf6c37c7 | 2106 | return $outcome; |
31759089 | 2107 | } |
2108 | ||
3406acde SH |
2109 | /** |
2110 | * Turn on/off ignore active | |
2111 | * | |
2112 | * @param bool $setting | |
2113 | */ | |
31759089 | 2114 | public function ignore_active($setting=true) { |
2115 | $this->ignoreactive = ($setting); | |
7d2a0492 | 2116 | } |
3406acde SH |
2117 | public function get($key, $type = null) { |
2118 | foreach ($this->children as &$child) { | |
2119 | if ($child->key === $key && ($type == null || $type == $child->type)) { | |
2120 | return $child; | |
2121 | } | |
2122 | } | |
2123 | return false; | |
2124 | } | |
7d2a0492 | 2125 | /** |
3406acde | 2126 | * Returns an array of navigation_node's that make up the navbar. |
435a512e | 2127 | * |
3406acde | 2128 | * @return array |
7d2a0492 | 2129 | */ |
3406acde SH |
2130 | public function get_items() { |
2131 | $items = array(); | |
7d2a0492 | 2132 | // Make sure that navigation is initialised |
7a7e209d | 2133 | if (!$this->has_items()) { |
3406acde | 2134 | return $items; |
7a7e209d | 2135 | } |
3406acde SH |
2136 | if ($this->items !== null) { |
2137 | return $this->items; | |
7d2a0492 | 2138 | } |
2139 | ||
3406acde SH |
2140 | if (count($this->children) > 0) { |
2141 | // Add the custom children | |
2142 | $items = array_reverse($this->children); | |
2143 | } | |
117bd748 | 2144 | |
3406acde SH |
2145 | $navigationactivenode = $this->page->navigation->find_active_node(); |
2146 | $settingsactivenode = $this->page->settingsnav->find_active_node(); | |
0b29477b | 2147 | |
7d2a0492 | 2148 | // Check if navigation contains the active node |
0b29477b | 2149 | if (!$this->ignoreactive) { |
435a512e | 2150 | |
3406acde | 2151 | if ($navigationactivenode && $settingsactivenode) { |
0b29477b | 2152 | // Parse a combined navigation tree |
3406acde SH |
2153 | while ($settingsactivenode && $settingsactivenode->parent !== null) { |
2154 | if (!$settingsactivenode->mainnavonly) { | |
2155 | $items[] = $settingsactivenode; | |
2156 | } | |
2157 | $settingsactivenode = $settingsactivenode->parent; | |
2158 | } | |
4d5059d4 SH |
2159 | if (!$this->includesettingsbase) { |
2160 | // Removes the first node from the settings (root node) from the list | |
2161 | array_pop($items); | |
2162 | } | |
3406acde SH |
2163 | while ($navigationactivenode && $navigationactivenode->parent !== null) { |
2164 | if (!$navigationactivenode->mainnavonly) { | |
2165 | $items[] = $navigationactivenode; | |
2166 | } | |
2167 | $navigationactivenode = $navigationactivenode->parent; | |
0b29477b | 2168 | } |
3406acde | 2169 | } else if ($navigationactivenode) { |
0b29477b | 2170 | // Parse the navigation tree to get the active node |
3406acde SH |
2171 | while ($navigationactivenode && $navigationactivenode->parent !== null) { |
2172 | if (!$navigationactivenode->mainnavonly) { | |
2173 | $items[] = $navigationactivenode; | |
2174 | } | |
2175 | $navigationactivenode = $navigationactivenode->parent; | |
2176 | } | |
2177 | } else if ($settingsactivenode) { | |
0b29477b | 2178 | // Parse the settings navigation to get the active node |
3406acde SH |
2179 | while ($settingsactivenode && $settingsactivenode->parent !== null) { |
2180 | if (!$settingsactivenode->mainnavonly) { | |
2181 | $items[] = $settingsactivenode; | |
2182 | } | |
2183 | $settingsactivenode = $settingsactivenode->parent; | |
2184 | } | |
0b29477b | 2185 | } |
7d2a0492 | 2186 | } |
a3bbac8b | 2187 | |
3406acde SH |
2188 | $items[] = new navigation_node(array( |
2189 | 'text'=>$this->page->navigation->text, | |
2190 | 'shorttext'=>$this->page->navigation->shorttext, | |
2191 | 'key'=>$this->page->navigation->key, | |
2192 | 'action'=>$this->page->navigation->action | |
2193 | )); | |
a3bbac8b | 2194 | |
3406acde SH |
2195 | $this->items = array_reverse($items); |
2196 | return $this->items; | |
7d2a0492 | 2197 | } |
507a7a9a | 2198 | |
7d2a0492 | 2199 | /** |
3406acde | 2200 | * Add a new navigation_node to the navbar, overrides parent::add |
7d2a0492 | 2201 | * |
2202 | * This function overrides {@link navigation_node::add()} so that we can change | |
2203 | * the way nodes get added to allow us to simply call add and have the node added to the | |
2204 | * end of the navbar | |
2205 | * | |
2206 | * @param string $text | |
7d2a0492 | 2207 | * @param string|moodle_url $action |
d972bad1 | 2208 | * @param int $type |
2209 | * @param string|int $key | |
2210 | * @param string $shorttext | |
7d2a0492 | 2211 | * @param string $icon |
3406acde | 2212 | * @return navigation_node |
7d2a0492 | 2213 | */ |
f9fc1461 | 2214 | public function add($text, $action=null, $type=self::TYPE_CUSTOM, $shorttext=null, $key=null, pix_icon $icon=null) { |
3406acde SH |
2215 | if ($this->content !== null) { |
2216 | debugging('Nav bar items must be printed before $OUTPUT->header() has been called', DEBUG_DEVELOPER); | |
2217 | } | |
435a512e | 2218 | |
3406acde SH |
2219 | // Properties array used when creating the new navigation node |
2220 | $itemarray = array( | |
2221 | 'text' => $text, | |
2222 | 'type' => $type | |
2223 | ); | |
2224 | // Set the action if one was provided | |
2225 | if ($action!==null) { | |
2226 | $itemarray['action'] = $action; | |
2227 | } | |
2228 | // Set the shorttext if one was provided | |
2229 | if ($shorttext!==null) { | |
2230 | $itemarray['shorttext'] = $shorttext; | |
2231 | } | |
2232 | // Set the icon if one was provided | |
2233 | if ($icon!==null) { | |
2234 | $itemarray['icon'] = $icon; | |
7d2a0492 | 2235 | } |
3406acde SH |
2236 | // Default the key to the number of children if not provided |
2237 | if ($key === null) { | |
2238 | $key = count($this->children); | |
7d2a0492 | 2239 | } |
3406acde SH |
2240 | // Set the key |
2241 | $itemarray['key'] = $key; | |
2242 | // Set the parent to this node | |
2243 | $itemarray['parent'] = $this; | |
2244 | // Add the child using the navigation_node_collections add method | |
2245 | $this->children[] = new navigation_node($itemarray); | |
2246 | return $this; | |
7d2a0492 | 2247 | } |
2248 | } | |
2249 | ||
2250 | /** | |
2251 | * Class used to manage the settings option for the current page | |
2252 | * | |
2253 | * This class is used to manage the settings options in a tree format (recursively) | |
2254 | * and was created initially for use with the settings blocks. | |
2255 | * | |
2256 | * @package moodlecore | |
babb3911 | 2257 | * @subpackage navigation |
7d2a0492 | 2258 | * @copyright 2009 Sam Hemelryk |
2259 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | |
2260 | */ | |
2261 | class settings_navigation extends navigation_node { | |
2262 | /** @var stdClass */ | |
2263 | protected $context; | |
3406acde | 2264 | /** @var navigation_cache */ |
7d2a0492 | 2265 | protected $cache; |
3406acde | 2266 | /** @var moodle_page */ |
7d2a0492 | 2267 | protected $page; |
3406acde | 2268 | /** @var string */ |
d9d2817a | 2269 | protected $adminsection; |
4766a50c SH |
2270 | /** @var bool */ |
2271 | protected $initialised = false; | |
87c215de SH |
2272 | /** @var array */ |
2273 | protected $userstoextendfor = array(); | |
4766a50c | 2274 | |
7d2a0492 | 2275 | /** |
2276 | * Sets up the object with basic settings and preparse it for use | |
435a512e | 2277 | * |
3406acde | 2278 | * @param moodle_page $page |
7d2a0492 | 2279 | */ |
507a7a9a | 2280 | public function __construct(moodle_page &$page) { |
7d2a0492 | 2281 | if (during_initial_install()) { |
2282 | return false; | |
2283 | } | |
7d2a0492 | 2284 | $this->page = $page; |
2285 | // Initialise the main navigation. It is most important that this is done | |
2286 | // before we try anything | |
2287 | $this->page->navigation->initialise(); | |
2288 | // Initialise the navigation cache | |
f5b5a822 | 2289 | $this->cache = new navigation_cache(NAVIGATION_CACHE_NAME); |
3406acde | 2290 | $this->children = new navigation_node_collection(); |
7d2a0492 | 2291 | } |
2292 | /** | |
2293 | * Initialise the settings navigation based on the current context | |
2294 | * | |
2295 | * This function initialises the settings navigation tree for a given context | |
2296 | * by calling supporting functions to generate major parts of the tree. | |
3406acde | 2297 | * |
7d2a0492 | 2298 | */ |
2299 | public function initialise() { | |
0b29477b | 2300 | global $DB; |
4f0c2d00 | 2301 | |
7d2a0492 | 2302 | if (during_initial_install()) { |
2303 | return false; | |
4766a50c SH |
2304 | } else if ($this->initialised) { |
2305 | return true; | |
7d2a0492 | 2306 | } |
2307 | $this->id = 'settingsnav'; | |
2308 | $this->context = $this->page->context; | |
0b29477b SH |
2309 | |
2310 | $context = $this->context; | |
2311 | if ($context->contextlevel == CONTEXT_BLOCK) { | |
2312 | $this->load_block_settings(); | |
2313 | $context = $DB->get_record_sql('SELECT ctx.* FROM {block_instances} bi LEFT JOIN {context} ctx ON ctx.id=bi.parentcontextid WHERE bi.id=?', array($context->instanceid)); | |
2314 | } | |
2315 | ||
2316 | switch ($context->contextlevel) { | |
7d2a0492 | 2317 | case CONTEXT_SYSTEM: |
0b29477b SH |
2318 | if ($this->page->url->compare(new moodle_url('/admin/settings.php', array('section'=>'frontpagesettings')))) { |
2319 | $this->load_front_page_settings(($context->id == $this->context->id)); | |
2320 | } | |
7d2a0492 | 2321 | break; |
2322 | case CONTEXT_COURSECAT: | |
0b29477b | 2323 | $this->load_category_settings(); |
7d2a0492 | 2324 | break; |
2325 | case CONTEXT_COURSE: | |
0b29477b SH |
2326 | if ($this->page->course->id != SITEID) { |
2327 | $this->load_course_settings(($context->id == $this->context->id)); | |
7d2a0492 | 2328 | } else { |
0b29477b | 2329 | $this->load_front_page_settings(($context->id == $this->context->id)); |
7d2a0492 | 2330 | } |
2331 | break; | |
2332 | case CONTEXT_MODULE: | |
0b29477b SH |
2333 | $this->load_module_settings(); |
2334 | $this->load_course_settings(); | |
7d2a0492 | 2335 | break; |
2336 | case CONTEXT_USER: | |
0b29477b SH |
2337 | if ($this->page->course->id != SITEID) { |
2338 | $this->load_course_settings(); | |
7d2a0492 | 2339 | } |
7d2a0492 | 2340 | break; |
2341 | } | |
2342 | ||
3406acde SH |
2343 | $settings = $this->load_user_settings($this->page->course->id); |
2344 | $admin = $this->load_administration_settings(); | |
0b29477b | 2345 | |
3406acde SH |
2346 | if ($context->contextlevel == CONTEXT_SYSTEM && $admin) { |
2347 | $admin->force_open(); | |
2348 | } else if ($context->contextlevel == CONTEXT_USER && $settings) { | |
2349 | $settings->force_open(); | |
0b29477b SH |
2350 | } |
2351 | ||
7d2a0492 | 2352 | // Check if the user is currently logged in as another user |
2353 | if (session_is_loggedinas()) { | |
2354 | // Get the actual user, we need this so we can display an informative return link | |
2355 | $realuser = session_get_realuser(); | |
2356 | // Add the informative return to original user link | |
a6855934 | 2357 | $url = new moodle_url('/course/loginas.php',array('id'=>$this->page->course->id, 'return'=>1,'sesskey'=>sesskey())); |
f9fc1461 | 2358 | $this->add(get_string('returntooriginaluser', 'moodle', fullname($realuser, true)), $url, self::TYPE_SETTING, null, null, new pix_icon('t/left', '')); |
7d2a0492 | 2359 | } |
2360 | ||
2361 | // Make sure the first child doesnt have proceed with hr set to true | |
7d2a0492 | 2362 | |
3406acde SH |
2363 | foreach ($this->children as $key=>$node) { |
2364 | if ($node->nodetype != self::NODETYPE_BRANCH || $node->children->count()===0) { | |
2365 | $node->remove(); | |
2366 | } | |
2367 | } | |
4766a50c | 2368 | $this->initialised = true; |
7d2a0492 | 2369 | } |
2370 | /** | |
2371 | * Override the parent function so that we can add preceeding hr's and set a | |
2372 | * root node class against all first level element | |
2373 | * | |
2374 | * It does this by first calling the parent's add method {@link navigation_node::add()} | |
117bd748 | 2375 | * and then proceeds to use the key to set class and hr |
7d2a0492 | 2376 | * |
2377 | * @param string $text | |
91152a35 | 2378 | * @param sting|moodle_url $url |
7d2a0492 | 2379 | * @param string $shorttext |
2380 | * @param string|int $key | |
2381 | * @param int $type | |
7d2a0492 | 2382 | * @param string $icon |
3406acde | 2383 | * @return navigation_node |
7d2a0492 | 2384 | */ |
f9fc1461 | 2385 | public function add($text, $url=null, $type=null, $shorttext=null, $key=null, pix_icon $icon=null) { |
3406acde SH |
2386 | $node = parent::add($text, $url, $type, $shorttext, $key, $icon); |
2387 | $node->add_class('root_node'); | |
2388 | return $node; | |
7d2a0492 | 2389 | } |
a6e34701 | 2390 | |
2391 | /** | |
2392 | * This function allows the user to add something to the start of the settings | |
2393 | * navigation, which means it will be at the top of the settings navigation block | |
2394 | * | |
2395 | * @param string $text | |
2396 | * @param sting|moodle_url $url | |
2397 | * @param string $shorttext | |
2398 | * @param string|int $key | |
2399 | * @param int $type | |
2400 | * @param string $icon | |
3406acde | 2401 | * @return navigation_node |
a6e34701 | 2402 | */ |
f9fc1461 | 2403 | public function prepend($text, $url=null, $type=null, $shorttext=null, $key=null, pix_icon $icon=null) { |
a6e34701 | 2404 | $children = $this->children; |
b499db57 SH |
2405 | $childrenclass = get_class($children); |
2406 | $this->children = new $childrenclass; | |
3406acde SH |
2407 | $node = $this->add($text, $url, $type, $shorttext, $key, $icon); |
2408 | foreach ($children as $child) { | |
2409 | $this->children->add($child); | |
a6e34701 | 2410 | } |
3406acde | 2411 | return $node; |
a6e34701 | 2412 | } |
7d2a0492 | 2413 | /** |
2414 | * Load the site administration tree | |
2415 | * | |
2416 | * This function loads the site administration tree by using the lib/adminlib library functions | |
2417 | * | |
3406acde | 2418 | * @param navigation_node $referencebranch A reference to a branch in the settings |
7d2a0492 | 2419 | * navigation tree |
3406acde | 2420 | * @param part_of_admin_tree $adminbranch The branch to add, if null generate the admin |
7d2a0492 | 2421 | * tree and start at the beginning |
2422 | * @return mixed A key to access the admin tree by | |
2423 | */ | |
0e3eee62 | 2424 | protected function load_administration_settings(navigation_node $referencebranch=null, part_of_admin_tree $adminbranch=null) { |
507a7a9a | 2425 | global $CFG; |
0e3eee62 | 2426 | |
7d2a0492 | 2427 | // Check if we are just starting to generate this navigation. |
2428 | if ($referencebranch === null) { | |
0e3eee62 | 2429 | |
d9d2817a | 2430 | // Require the admin lib then get an admin structure |
0e3eee62 SH |
2431 | if (!function_exists('admin_get_root')) { |
2432 | require_once($CFG->dirroot.'/lib/adminlib.php'); | |
2433 | } | |
2434 | $adminroot = admin_get_root(false, false); | |
d9d2817a SH |
2435 | // This is the active section identifier |
2436 | $this->adminsection = $this->page->url->param('section'); | |
4f0c2d00 | 2437 | |
d9d2817a SH |
2438 | // Disable the navigation from automatically finding the active node |
2439 | navigation_node::$autofindactive = false; | |
3406acde | 2440 | $referencebranch = $this->add(get_string('administrationsite'), null, self::TYPE_SETTING, null, 'root'); |
0e3eee62 SH |
2441 | foreach ($adminroot->children as $adminbranch) { |
2442 | $this->load_administration_settings($referencebranch, $adminbranch); | |
2443 | } | |
d9d2817a | 2444 | navigation_node::$autofindactive = true; |
0e3eee62 | 2445 | |
d9d2817a | 2446 | // Use the admin structure to locate the active page |
3406acde SH |
2447 | if (!$this->contains_active_node() && $current = $adminroot->locate($this->adminsection, true)) { |
2448 | $currentnode = $this; | |
2449 | while (($pathkey = array_pop($current->path))!==null && $currentnode) { | |
2450 | $currentnode = $currentnode->get($pathkey); | |
2451 | } | |
2452 | if ($currentnode) { | |
2453 | $currentnode->make_active(); | |
7d2a0492 | 2454 | } |
25b550d2 SH |
2455 | } else { |
2456 | $this->scan_for_active_node($referencebranch); | |
0e3eee62 | 2457 | } |
3406acde | 2458 | return $referencebranch; |
8140c440 | 2459 | } else if ($adminbranch->check_access()) { |
7d2a0492 | 2460 | // We have a reference branch that we can access and is not hidden `hurrah` |
2461 | // Now we need to display it and any children it may have | |
2462 | $url = null; | |
2463 | $icon = null; | |
2464 | if ($adminbranch instanceof admin_settingpage) { | |
a6855934 | 2465 | $url = new moodle_url('/admin/settings.php', array('section'=>$adminbranch->name)); |
7d2a0492 | 2466 | } else if ($adminbranch instanceof admin_externalpage) { |
2467 | $url = $adminbranch->url; | |
2468 | } | |
2469 | ||
2470 | // Add the branch | |
3406acde | 2471 | $reference = $referencebranch->add($adminbranch->visiblename, $url, self::TYPE_SETTING, null, $adminbranch->name, $icon); |
8140c440 | 2472 | |
2473 | if ($adminbranch->is_hidden()) { | |
d9d2817a SH |
2474 | if (($adminbranch instanceof admin_externalpage || $adminbranch instanceof admin_settingpage) && $adminbranch->name == $this->adminsection) { |
2475 | $reference->add_class('hidden'); | |
2476 | } else { | |
2477 | $reference->display = false; | |
2478 | } | |
8140c440 | 2479 | } |
2480 | ||
7d2a0492 | 2481 | // Check if we are generating the admin notifications and whether notificiations exist |
2482 | if ($adminbranch->name === 'adminnotifications' && admin_critical_warnings_present()) { | |
2483 | $reference->add_class('criticalnotification'); | |
2484 | } | |
2485 | // Check if this branch has children | |
2486 | if ($reference && isset($adminbranch->children) && is_array($adminbranch->children) && count($adminbranch->children)>0) { | |
2487 | foreach ($adminbranch->children as $branch) { | |
2488 | // Generate the child branches as well now using this branch as the reference | |
459e384e | 2489 | $this->load_administration_settings($reference, $branch); |
7d2a0492 | 2490 | } |
2491 | } else { | |
f9fc1461 | 2492 | $reference->icon = new pix_icon('i/settings', ''); |
7d2a0492 | 2493 | } |
2494 | } | |
2495 | } | |
2496 | ||
25b550d2 SH |
2497 | /** |
2498 | * This function recursivily scans nodes until it finds the active node or there | |
2499 | * are no more nodes. | |
2500 | * @param navigation_node $node | |
2501 | */ | |
2502 | protected function scan_for_active_node(navigation_node $node) { | |
2503 | if (!$node->check_if_active() && $node->children->count()>0) { | |
2504 | foreach ($node->children as &$child) { | |
2505 | $this->scan_for_active_node($child); | |
2506 | } | |
2507 | } | |
2508 | } | |
2509 | ||
3406acde SH |
2510 | /** |
2511 | * Gets a navigation node given an array of keys that represent the path to | |
2512 | * the desired node. | |
2513 | * | |
2514 | * @param array $path | |
2515 | * @return navigation_node|false | |
2516 | */ | |
2517 | protected function get_by_path(array $path) { | |
2518 | $node = $this->get(array_shift($path)); | |
2519 | foreach ($path as $key) { | |
2520 | $node->get($key); | |
2521 | } | |
2522 | return $node; | |
2523 | } | |
2524 | ||
7d2a0492 | 2525 | /** |
2526 | * Generate the list of modules for the given course. | |
2527 | * | |
2528 | * The array of resources and activities that can be added to a course is then | |
2529 | * stored in the cache so that we can access it for anywhere. | |
2530 | * It saves us generating it all the time | |
2531 | * | |
2532 | * <code php> | |
2533 | * // To get resources: | |
2534 | * $this->cache->{'course'.$courseid.'resources'} | |
2535 | * // To get activities: | |
2536 | * $this->cache->{'course'.$courseid.'activities'} | |
2537 | * </code> | |
2538 | * | |
2539 | * @param stdClass $course The course to get modules for | |
2540 | */ | |
2541 | protected function get_course_modules($course) { | |
2542 | global $CFG; | |
2543 | $mods = $modnames = $modnamesplural = $modnamesused = array(); | |
2544 | // This function is included when we include course/lib.php at the top | |
2545 | // of this file | |
2546 | get_all_mods($course->id, $mods, $modnames, $modnamesplural, $modnamesused); | |
2547 | $resources = array(); | |
2548 | $activities = array(); | |
2549 | foreach($modnames as $modname=>$modnamestr) { | |
2550 | if (!course_allowed_module($course, $modname)) { | |
2551 | continue; | |
2552 | } | |
2553 | ||
2554 | $libfile = "$CFG->dirroot/mod/$modname/lib.php"; | |
2555 | if (!file_exists($libfile)) { | |
2556 | continue; | |
2557 | } | |
2558 | include_once($libfile); | |
2559 | $gettypesfunc = $modname.'_get_types'; | |
2560 | if (function_exists($gettypesfunc)) { | |
2561 | $types = $gettypesfunc(); | |
2562 | foreach($types as $type) { | |
2563 | if (!isset($type->modclass) || !isset($type->typestr)) { | |
2564 | debugging('Incorrect activity type in '.$modname); | |
2565 | continue; | |
2566 | } | |
2567 | if ($type->modclass == MOD_CLASS_RESOURCE) { | |
2568 | $resources[html_entity_decode($type->type)] = $type->typestr; | |
2569 | } else { | |
2570 | $activities[html_entity_decode($type->type)] = $type->typestr; | |
2571 | } | |
2572 | } | |
2573 | } else { | |
2574 | $archetype = plugin_supports('mod', $modname, FEATURE_MOD_ARCHETYPE, MOD_ARCHETYPE_OTHER); | |
2575 | if ($archetype == MOD_ARCHETYPE_RESOURCE) { | |
2576 | $resources[$modname] = $modnamestr; | |
2577 | } else { | |
2578 | // all other archetypes are considered activity | |
2579 | $activities[$modname] = $modnamestr; | |
2580 | } | |
2581 | } | |
2582 | } | |
2583 | $this->cache->{'course'.$course->id.'resources'} = $resources; | |
2584 | $this->cache->{'course'.$course->id.'activities'} = $activities; | |
2585 | } | |
2586 | ||
2587 | /** | |
2588 | * This function loads the course settings that are available for the user | |
2589 | * | |
0b29477b | 2590 | * @param bool $forceopen If set to true the course node will be forced open |
3406acde | 2591 | * @return navigation_node|false |
7d2a0492 | 2592 | */ |
0b29477b | 2593 | protected function load_course_settings($forceopen = false) { |
4f0c2d00 | 2594 | global $CFG, $USER, $SESSION, $OUTPUT; |
7d2a0492 | 2595 | |
2596 | $course = $this->page->course; | |
4f0c2d00 | 2597 | $coursecontext = get_context_instance(CONTEXT_COURSE, $course->id); |
df997f84 PS |
2598 | |
2599 | // note: do not test if enrolled or viewing here because we need the enrol link in Course administration section | |
7d2a0492 | 2600 | |
3406acde SH |
2601 | $coursenode = $this->add(get_string('courseadministration'), null, self::TYPE_COURSE, null, 'courseadmin'); |
2602 | if ($forceopen) { | |
2603 | $coursenode->force_open(); | |
2604 | } | |
117bd748 | 2605 | |
4f0c2d00 | 2606 | if (has_capability('moodle/course:update', $coursecontext)) { |
7d2a0492 | 2607 | // Add the turn on/off settings |
a6855934 | 2608 | $url = new moodle_url('/course/view.php', array('id'=>$course->id, 'sesskey'=>sesskey())); |
7d2a0492 | 2609 | if ($this->page->user_is_editing()) { |
2610 | $url->param('edit', 'off'); | |
2611 | $editstring = get_string('turneditingoff'); | |
2612 | } else { | |
2613 | $url->param('edit', 'on'); | |
2614 | $editstring = get_string('turneditingon'); | |
2615 | } | |
f9fc1461 | 2616 | $coursenode->add($editstring, $url, self::TYPE_SETTING, null, null, new pix_icon('i/edit', '')); |
7d2a0492 | 2617 | |
7d2a0492 | 2618 | if ($this->page->user_is_editing()) { |
538891b5 SH |
2619 | // Removed as per MDL-22732 |
2620 | // $this->add_course_editing_links($course); | |
7d2a0492 | 2621 | } |
2622 | ||
2623 | // Add the course settings link | |
a6855934 | 2624 | $url = new moodle_url('/course/edit.php', array('id'=>$course->id)); |
20d6b374 | 2625 | $coursenode->add(get_string('editsettings'), $url, self::TYPE_SETTING, null, null, new pix_icon('i/settings', '')); |
2be4d090 MD |
2626 | |
2627 | // Add the course completion settings link | |
2628 | if ($CFG->enablecompletion && $course->enablecompletion) { | |
2629 | $url = new moodle_url('/course/completion.php', array('id'=>$course->id)); | |
2630 | $coursenode->add(get_string('completion', 'completion'), $url, self::TYPE_SETTING, null, null, new pix_icon('i/settings', '')); | |
2631 | } | |
7d2a0492 | 2632 | } |
117bd748 | 2633 | |
df997f84 PS |
2634 | // add enrol nodes |
2635 | enrol_add_course_navigation($coursenode, $course); | |
2636 | ||
2637 | // Manage filters | |
2638 | if (has_capability('moodle/filter:manage', $coursecontext) && count(filter_get_available_in_context($coursecontext))>0) { | |
2639 | $url = new moodle_url('/filter/manage.php', array('contextid'=>$coursecontext->id)); | |
2640 | $coursenode->add(get_string('filters', 'admin'), $url, self::TYPE_SETTING, null, null, new pix_icon('i/filter', '')); | |
7d2a0492 | 2641 | } |
2642 | ||
2643 | // Add view grade report is permitted | |
2644 | $reportavailable = false; | |
4f0c2d00 | 2645 | if (has_capability('moodle/grade:viewall', $coursecontext)) { |
7d2a0492 | 2646 | $reportavailable = true; |
2647 | } else if (!empty($course->showgrades)) { | |
2648 | $reports = get_plugin_list('gradereport'); | |
2649 | if (is_array($reports) && count($reports)>0) { // Get all installed reports | |
2650 | arsort($reports); // user is last, we want to test it first | |
2651 | foreach ($reports as $plugin => $plugindir) { | |
4f0c2d00 | 2652 | if (has_capability('gradereport/'.$plugin.':view', $coursecontext)) { |
7d2a0492 | 2653 | //stop when the first visible plugin is found |
2654 | $reportavailable = true; | |
2655 | break; | |
2656 | } | |
2657 | } | |
2658 | } | |
2659 | } | |
2660 | if ($reportavailable) { | |
a6855934 | 2661 | $url = new moodle_url('/grade/report/index.php', array('id'=>$course->id)); |
b499db57 | 2662 | $gradenode = $coursenode->add(get_string('grades'), $url, self::TYPE_SETTING, null, 'grades', new pix_icon('i/grades', '')); |
7d2a0492 | 2663 | } |
2664 | ||
2665 | // Add outcome if permitted | |
4f0c2d00 | 2666 | if (!empty($CFG->enableoutcomes) && has_capability('moodle/course:update', $coursecontext)) { |
a6855934 | 2667 | $url = new moodle_url('/grade/edit/outcome/course.php', array('id'=>$course->id)); |
d6d07a68 | 2668 | $coursenode->add(get_string('outcomes', 'grades'), $url, self::TYPE_SETTING, null, 'outcomes', new pix_icon('i/outcomes', '')); |
7d2a0492 | 2669 | } |
2670 | ||
7d2a0492 | 2671 | // Backup this course |
4f0c2d00 | 2672 | if (has_capability('moodle/backup:backupcourse', $coursecontext)) { |
a6855934 | 2673 | $url = new moodle_url('/backup/backup.php', array('id'=>$course->id)); |
d6d07a68 | 2674 | $coursenode->add(get_string('backup'), $url, self::TYPE_SETTING, null, 'backup', new pix_icon('i/backup', '')); |
7d2a0492 | 2675 | } |
2676 | ||
2677 | // Restore to this course | |
4f0c2d00 | 2678 | if (has_capability('moodle/restore:restorecourse', $coursecontext)) { |
64f93798 | 2679 | $url = new moodle_url('/files/index.php', array('contextid'=>$coursecontext->id, 'itemid'=>0, 'component' => 'backup', 'filearea'=>'course')); |
530fa981 | 2680 | $url = null; // Disabled until restore is implemented. MDL-21432 |
d6d07a68 | 2681 | $coursenode->add(get_string('restore'), $url, self::TYPE_SETTING, null, 'restore', new pix_icon('i/restore', '')); |
7d2a0492 | 2682 | } |
2683 | ||
2684 | // Import data from other courses | |
4f0c2d00 | 2685 | if (has_capability('moodle/restore:restoretargetimport', $coursecontext)) { |
a6855934 | 2686 | $url = new moodle_url('/course/import.php', array('id'=>$course->id)); |
530fa981 | 2687 | $url = null; // Disabled until restore is implemented. MDL-21432 |
d6d07a68 | 2688 | $coursenode->add(get_string('import'), $url, self::TYPE_SETTING, null, 'import', new pix_icon('i/restore', '')); |
7d2a0492 | 2689 | } |
2690 | ||
07ab0c80 | 2691 | // Publish course on a hub |
2692 | if (has_capability('moodle/course:publish', $coursecontext)) { | |
2693 | $url = new moodle_url('/course/publish/index.php', array('id'=>$course->id)); | |
d6d07a68 | 2694 | $coursenode->add(get_string('publish'), $url, self::TYPE_SETTING, null, 'publish', new pix_icon('i/publish', '')); |
07ab0c80 | 2695 | } |
2696 | ||
7d2a0492 | 2697 | // Reset this course |
4f0c2d00 | 2698 | if (has_capability('moodle/course:reset', $coursecontext)) { |
a6855934 | 2699 | $url = new moodle_url('/course/reset.php', array('id'=>$course->id)); |
f9fc1461 | 2700 | $coursenode->add(get_string('reset'), $url, self::TYPE_SETTING, null, null, new pix_icon('i/return', '')); |
7d2a0492 | 2701 | } |
7d2a0492 | 2702 | |
56ed242b SH |
2703 | // Questions |
2704 | require_once($CFG->dirroot.'/question/editlib.php'); | |
2705 | question_extend_settings_navigation($coursenode, $coursecontext)->trim_if_empty(); | |
7d2a0492 | 2706 | |
2707 | // Repository Instances | |
2708 | require_once($CFG->dirroot.'/repository/lib.php'); | |
76046fa8 SH |
2709 | $editabletypes = repository::get_editable_types($coursecontext); |
2710 | if (has_capability('moodle/course:update', $coursecontext) && !empty($editabletypes)) { | |
2711 | $url = new moodle_url('/repository/manage_instances.php', array('contextid'=>$coursecontext->id)); | |
f9fc1461 | 2712 | $coursenode->add(get_string('repositories'), $url, self::TYPE_SETTING, null, null, new pix_icon('i/repository', '')); |
7d2a0492 | 2713 | } |
2714 | ||
2715 | // Manage files | |
bf34822b | 2716 | if ($course->legacyfiles == 2 and has_capability('moodle/course:managefiles', $coursecontext)) { |
64f93798 | 2717 | $url = new moodle_url('/files/index.php', array('contextid'=>$coursecontext->id, 'itemid'=>0, 'component' => 'course', 'filearea'=>'legacy')); |
76046fa8 | 2718 | $coursenode->add(get_string('files'), $url, self::TYPE_SETTING, null, 'coursefiles', new pix_icon('i/files', '')); |
7d2a0492 | 2719 | } |
2720 | ||
7d2a0492 | 2721 | // Switch roles |
2722 | $roles = array(); | |
2723 | $assumedrole = $this->in_alternative_role(); | |
2724 | if ($assumedrole!==false) { | |
2725 | $roles[0] = get_string('switchrolereturn'); | |
2726 | } | |
76046fa8 SH |
2727 | if (has_capability('moodle/role:switchroles', $coursecontext)) { |
2728 | $availableroles = get_switchable_roles($coursecontext); | |
7d2a0492 | 2729 | if (is_array($availableroles)) { |
2730 | foreach ($availableroles as $key=>$role) { | |
2731 | if ($key == $CFG->guestroleid || $assumedrole===(int)$key) { | |
2732 | continue; | |
2733 | } | |
2734 | $roles[$key] = $role; | |
2735 | } | |
2736 | } | |
2737 | } | |
2738 | if (is_array($roles) && count($roles)>0) { | |
3406acde | 2739 | $switchroles = $this->add(get_string('switchroleto')); |
7d2a0492 | 2740 | if ((count($roles)==1 && array_key_exists(0, $roles))|| $assumedrole!==false) { |
3406acde | 2741 | $switchroles->force_open(); |
7d2a0492 | 2742 | } |
2743 | $returnurl = $this->page->url; | |
2744 | $returnurl->param('sesskey', sesskey()); | |
2745 | $SESSION->returnurl = serialize($returnurl); | |
2746 | foreach ($roles as $key=>$name) { | |
a6855934 | 2747 | $url = new moodle_url('/course/switchrole.php', array('id'=>$course->id,'sesskey'=>sesskey(), 'switchrole'=>$key, 'returnurl'=>'1')); |
3406acde | 2748 | $switchroles->add($name, $url, self::TYPE_SETTING, null, $key, new pix_icon('i/roles', '')); |
7d2a0492 | 2749 | } |
2750 | } | |
2751 | // Return we are done | |
3406acde | 2752 | return $coursenode; |
7d2a0492 | 2753 | } |
2754 | ||
538891b5 SH |
2755 | /** |
2756 | * Adds branches and links to the settings navigaiton to add course activities | |
2757 | * and resources. | |
2758 | * | |
2759 | * @param stdClass $course | |
2760 | */ | |
2761 | protected function add_course_editing_links($course) { | |
df997f84 PS |
2762 | global $CFG; |
2763 | ||
2764 | require_once($CFG->dirroot.'/course/lib.php'); | |
2765 | ||
538891b5 SH |
2766 | // Add `add` resources|activities branches |
2767 | $structurefile = $CFG->dirroot.'/course/format/'.$course->format.'/lib.php'; | |
2768 | if (file_exists($structurefile)) { | |
2769 | require_once($structurefile); | |
2770 | $formatstring = call_user_func('callback_'.$course->format.'_definition'); | |
2771 | $formatidentifier = optional_param(call_user_func('callback_'.$course->format.'_request_key'), 0, PARAM_INT); | |
2772 | } else { | |
2773 | $formatstring = get_string('topic'); | |
2774 | $formatidentifier = optional_param('topic', 0, PARAM_INT); | |
2775 | } | |
2776 | ||
2777 | $sections = get_all_sections($course->id); | |
2778 | ||
2779 | $addresource = $this->add(get_string('addresource')); | |
2780 | $addactivity = $this->add(get_string('addactivity')); | |
2781 | if ($formatidentifier!==0) { | |
2782 | $addresource->force_open(); | |
2783 | $addactivity->force_open(); | |
2784 | } | |
2785 | ||
2786 | if (!$this->cache->cached('course'.$course->id.'resources')) { | |
2787 | $this->get_course_modules($course); | |
2788 | } | |
2789 | $resources = $this->cache->{'course'.$course->id.'resources'}; | |
2790 | $activities = $this->cache->{'course'.$course->id.'activities'}; | |
2791 | ||
2792 | $textlib = textlib_get_instance(); | |
2793 | ||
2794 | foreach ($sections as $section) { | |
2795 | if ($formatidentifier !== 0 && $section->section != $formatidentifier) { | |
2796 | continue; | |
2797 | } | |
2798 | $sectionurl = new moodle_url('/course/view.php', array('id'=>$course->id, $formatstring=>$section->section)); | |
2799 | if ($section->section == 0) { | |
2800 | $sectionresources = $addresource->add(get_string('course'), $sectionurl, self::TYPE_SETTING); | |
2801 | $sectionactivities = $addactivity->add(get_string('course'), $sectionurl, self::TYPE_SETTING); | |
2802 | } else { | |
2803 | $sectionresources = $addresource->add($formatstring.' '.$section->section, $sectionurl, self::TYPE_SETTING); | |
2804 | $sectionactivities = $addactivity->add($formatstring.' '.$section->section, $sectionurl, self::TYPE_SETTING); | |
2805 | } | |
2806 | foreach ($resources as $value=>$resource) { | |
2807 | $url = new moodle_url('/course/mod.php', array('id'=>$course->id, 'sesskey'=>sesskey(), 'section'=>$section->section)); | |
2808 | $pos = strpos($value, '&type='); | |
2809 | if ($pos!==false) { | |
2810 | $url->param('add', $textlib->substr($value, 0,$pos)); | |
2811 | $url->param('type', $textlib->substr($value, $pos+6)); | |
2812 | } else { | |
2813 | $url->param('add', $value); | |
2814 | } | |
2815 | $sectionresources->add($resource, $url, self::TYPE_SETTING); | |
2816 | } | |
2817 | $subbranch = false; | |
2818 | foreach ($activities as $activityname=>$activity) { | |
2819 | if ($activity==='--') { | |
2820 | $subbranch = false; | |
2821 | continue; | |
2822 | } | |
2823 | if (strpos($activity, '--')===0) { | |
2824 | $subbranch = $sectionactivities->add(trim($activity, '-')); | |
2825 | continue; | |
2826 | } | |
2827 | $url = new moodle_url('/course/mod.php', array('id'=>$course->id, 'sesskey'=>sesskey(), 'section'=>$section->section)); | |
2828 | $pos = strpos($activityname, '&type='); | |
2829 | if ($pos!==false) { | |
2830 | $url->param('add', $textlib->substr($activityname, 0,$pos)); | |
2831 | $url->param('type', $textlib->substr($activityname, $pos+6)); | |
2832 | } else { | |
2833 | $url->param('add', $activityname); | |
2834 | } | |
2835 | if ($subbranch !== false) { | |
2836 | $subbranch->add($activity, $url, self::TYPE_SETTING); | |
2837 | } else { | |
2838 | $sectionactivities->add($activity, $url, self::TYPE_SETTING); | |
2839 | } | |
2840 | } | |
2841 | } | |
2842 | } | |
2843 | ||
7d2a0492 | 2844 | /** |
2845 | * This function calls the module function to inject module settings into the | |
2846 | * settings navigation tree. | |
2847 | * | |
2848 | * This only gets called if there is a corrosponding function in the modules | |
2849 | * lib file. | |
2850 | * | |
2851 | * For examples mod/forum/lib.php ::: forum_extend_settings_navigation() | |
2852 | * | |
3406acde | 2853 | * @return navigation_node|false |
7d2a0492 | 2854 | */ |
2855 | protected function load_module_settings() { | |
507a7a9a | 2856 | global $CFG; |
4dd5bce8 | 2857 | |
2858 | if (!$this->page->cm && $this->context->contextlevel == CONTEXT_MODULE && $this->context->instanceid) { | |
0b29477b | 2859 | $cm = get_coursemodule_from_id(false, $this->context->instanceid, 0, false, MUST_EXIST); |
507a7a9a | 2860 | $this->page->set_cm($cm, $this->page->course); |
4dd5bce8 | 2861 | } |
2862 | ||
3406acde SH |
2863 | $modulenode = $this->add(get_string($this->page->activityname.'administration', $this->page->activityname)); |
2864 | $modulenode->force_open(); | |
4f0c2d00 | 2865 | |
0b29477b SH |
2866 | // Settings for the module |
2867 | if (has_capability('moodle/course:manageactivities', $this->page->cm->context)) { | |
2868 | $url = new moodle_url('/course/modedit.php', array('update' => $this->page->cm->id, 'return' => true, 'sesskey' => sesskey())); | |
20d6b374 | 2869 | $modulenode->add(get_string('editsettings'), $url, navigation_node::TYPE_SETTING); |
0b29477b SH |
2870 | } |
2871 | // Assign local roles | |
2872 | if (count(get_assignable_roles($this->page->cm->context))>0) { | |
2873 | $url = new moodle_url('/'.$CFG->admin.'/roles/assign.php', array('contextid'=>$this->page->cm->context->id)); | |
2874 | $modulenode->add(get_string('localroles', 'role'), $url, self::TYPE_SETTING); | |
2875 | } | |
2876 | // Override roles | |
2877 | if (has_capability('moodle/role:review', $this->page->cm->context) or count(get_overridable_roles($this->page->cm->context))>0) { | |
2878 | $url = new moodle_url('/'.$CFG->admin.'/roles/permissions.php', array('contextid'=>$this->page->cm->context->id)); | |
2879 | $modulenode->add(get_string('permissions', 'role'), $url, self::TYPE_SETTING); | |
2880 | } | |
2881 | // Check role permissions | |
2882 | if (has_any_capability(array('moodle/role:assign', 'moodle/role:safeoverride','moodle/role:override', 'moodle/role:assign'), $this->page->cm->context)) { | |
2883 | $url = new moodle_url('/'.$CFG->admin.'/roles/check.php', array('contextid'=>$this->page->cm->context->id)); | |
2884 | $modulenode->add(get_string('checkpermissions', 'role'), $url, self::TYPE_SETTING); | |
2885 | } | |
2886 | // Manage filters | |
2887 | if (has_capability('moodle/filter:manage', $this->page->cm->context) && count(filter_get_available_in_context($this->page->cm->context))>0) { | |
2888 | $url = new moodle_url('/filter/manage.php', array('contextid'=>$this->page->cm->context->id)); | |
2889 | $modulenode->add(get_string('filters', 'admin'), $url, self::TYPE_SETTING); | |
2890 | } | |
4f0c2d00 | 2891 | |
18ddfc1e SH |
2892 | if (has_capability('coursereport/log:view', get_context_instance(CONTEXT_COURSE, $this->page->cm->course))) { |
2893 | $url = new moodle_url('/course/report/log/index.php', array('chooselog'=>'1','id'=>$this->page->cm->course,'modid'=>$this->page->cm->id)); | |
2894 | $modulenode->add(get_string('logs'), $url, self::TYPE_SETTING); | |
2895 | } | |
2896 | ||
59c7680f SH |
2897 | // Add a backup link |
2898 | $featuresfunc = $this->page->activityname.'_supports'; | |
2899 | if ($featuresfunc(FEATURE_BACKUP_MOODLE2) && has_capability('moodle/backup:backupactivity', $this->page->cm->context)) { | |
2900 | $url = new moodle_url('/backup/backup.php', array('id'=>$this->page->cm->course, 'cm'=>$this->page->cm->id)); | |
2901 | $modulenode->add(get_string('backup'), $url, self::TYPE_SETTING); | |
2902 | } | |
2903 | ||
507a7a9a SH |
2904 | $file = $CFG->dirroot.'/mod/'.$this->page->activityname.'/lib.php'; |
2905 | $function = $this->page->activityname.'_extend_settings_navigation'; | |
117bd748 | 2906 | |
7d2a0492 | 2907 | if (file_exists($file)) { |
2908 | require_once($file); | |
2909 | } | |
2910 | if (!function_exists($function)) { | |
3406acde | 2911 | return $modulenode; |
7d2a0492 | 2912 | } |
0b29477b SH |
2913 | |
2914 | $function($this, $modulenode); | |
2915 | ||
2916 | // Remove the module node if there are no children | |
2917 | if (empty($modulenode->children)) { | |
3406acde | 2918 | $modulenode->remove(); |
0b29477b SH |
2919 | } |
2920 | ||
3406acde | 2921 | return $modulenode; |
7d2a0492 | 2922 | } |
2923 | ||
2924 | /** | |
2925 | * Loads the user settings block of the settings nav | |
117bd748 | 2926 | * |
7d2a0492 | 2927 | * This function is simply works out the userid and whether we need to load |
117bd748 | 2928 | * just the current users profile settings, or the current user and the user the |
7d2a0492 | 2929 | * current user is viewing. |
117bd748 | 2930 | * |
7d2a0492 | 2931 | * This function has some very ugly code to work out the user, if anyone has |
2932 | * any bright ideas please feel free to intervene. | |
2933 | * | |
2934 | * @param int $courseid The course id of the current course | |
3406acde | 2935 | * @return navigation_node|false |
7d2a0492 | 2936 | */ |
2937 | protected function load_user_settings($courseid=SITEID) { | |
0b29477b | 2938 | global $USER, $FULLME, $CFG; |
7d2a0492 | 2939 | |
2940 | if (isguestuser() || !isloggedin()) { | |
2941 | return false; | |
2942 | } | |
435a512e | 2943 | |
5d07e957 SH |
2944 | $navusers = $this->page->navigation->get_extending_users(); |
2945 | ||
2946 | if (count($this->userstoextendfor) > 0 || count($navusers) > 0) { | |
87c215de SH |
2947 | $usernode = null; |
2948 | foreach ($this->userstoextendfor as $userid) { | |
0f88a46e SH |
2949 | if ($userid == $USER->id) { |
2950 | continue; | |
2951 | } | |
87c215de SH |
2952 | $node = $this->generate_user_settings($courseid, $userid, 'userviewingsettings'); |
2953 | if (is_null($usernode)) { | |
2954 | $usernode = $node; | |
2955 | } | |
2956 | } | |
5d07e957 | 2957 | foreach ($navusers as $user) { |
0f88a46e SH |
2958 | if ($user->id == $USER->id) { |
2959 | continue; | |
2960 | } | |
5d07e957 SH |
2961 | $node = $this->generate_user_settings($courseid, $user->id, 'userviewingsettings'); |
2962 | if (is_null($usernode)) { | |
2963 | $usernode = $node; | |
2964 | } | |
2965 | } | |
0b29477b | 2966 | $this->generate_user_settings($courseid, $USER->id); |
7d2a0492 | 2967 | } else { |
3406acde | 2968 | $usernode = $this->generate_user_settings($courseid, $USER->id); |
7d2a0492 | 2969 | } |
3406acde | 2970 | return $usernode; |
7d2a0492 | 2971 | } |
2972 | ||
87c215de SH |
2973 | public function extend_for_user($userid) { |
2974 | global $CFG; | |
2975 | ||
2976 | if (!in_array($userid, $this->userstoextendfor)) { | |
2977 | $this->userstoextendfor[] = $userid; | |
2978 | if ($this->initialised) { | |
2979 | $this->generate_user_settings($this->page->course->id, $userid, 'userviewingsettings'); | |
2980 | $children = array(); | |
2981 | foreach ($this->children as $child) { | |
2982 | $children[] = $child; | |
2983 | } | |
2984 | array_unshift($children, array_pop($children)); | |
2985 | $this->children = new navigation_node_collection(); | |
2986 | foreach ($children as $child) { | |
2987 | $this->children->add($child); | |
2988 | } | |
2989 | } | |
2990 | } | |
2991 | } | |
2992 | ||
7d2a0492 | 2993 | /** |
2994 | * This function gets called by {@link load_user_settings()} and actually works out | |
2995 | * what can be shown/done | |
2996 | * | |
2997 | * @param int $courseid The current course' id | |
2998 | * @param int $userid The user id to load for | |
2999 | * @param string $gstitle The string to pass to get_string for the branch title | |
3406acde | 3000 | * @return navigation_node|false |
7d2a0492 | 3001 | */ |
3002 | protected function generate_user_settings($courseid, $userid, $gstitle='usercurrentsettings') { | |
507a7a9a | 3003 | global $DB, $CFG, $USER, $SITE; |
7d2a0492 | 3004 | |
507a7a9a SH |
3005 | if ($courseid != SITEID) { |
3006 | if (!empty($this->page->course->id) && $this->page->course->id == $courseid) { | |
3007 | $course = $this->page->course; | |
3008 | } else { | |
3009 | $course = $DB->get_record("course", array("id"=>$courseid), '*', MUST_EXIST); | |
3010 | } | |
3011 | } else { | |
3012 | $course = $SITE; | |
7d2a0492 | 3013 | } |
3014 | ||
3015 | $coursecontext = get_context_instance(CONTEXT_COURSE, $course->id); // Course context | |
507a7a9a | 3016 | $systemcontext = get_system_context(); |
7d2a0492 | 3017 | $currentuser = ($USER->id == $userid); |
4f0c2d00 | 3018 | |
7d2a0492 | 3019 | if ($currentuser) { |
3020 | $user = $USER; | |
3021 | $usercontext = get_context_instance(CONTEXT_USER, $user->id); // User context | |
3022 | } else { | |
7a7e209d | 3023 | if (!$user = $DB->get_record('user', array('id'=>$userid))) { |
7d2a0492 | 3024 | return false; |
3025 | } | |
3026 | // Check that the user can view the profile | |
3027 | $usercontext = get_context_instance(CONTEXT_USER, $user->id); // User context | |
3028 | if ($course->id==SITEID) { | |
e30fabae | 3029 | if ($CFG->forceloginforprofiles && !has_coursecontact_role($user->id) && !has_capability('moodle/user:viewdetails', $usercontext)) { // Reduce possibility of "browsing" userbase at site level |
7d2a0492 | 3030 | // Teachers can browse and be browsed at site level. If not forceloginforprofiles, allow access (bug #4366) |
3031 | return false; | |
3032 | } | |
3033 | } else { | |
df997f84 | 3034 | if ((!has_capability('moodle/user:viewdetails', $coursecontext) && !has_capability('moodle/user:viewdetails', $usercontext)) || !is_enrolled($coursecontext, $user->id)) { |
7d2a0492 | 3035 | return false; |
3036 | } | |
3037 | if (groups_get_course_groupmode($course) == SEPARATEGROUPS && !has_capability('moodle/site:accessallgroups', $coursecontext)) { | |
3038 | // If groups are in use, make sure we can see that group | |
3039 | return false; | |
3040 | } | |
3041 | } | |
3042 | } | |
3043 | ||
3044 | $fullname = fullname($user, has_capability('moodle/site:viewfullnames', $this->page->context)); | |
3045 | ||
5d07e957 SH |
3046 | $key = $gstitle; |
3047 | if ($gstitle != 'usercurrentsettings') { | |
3048 | $key .= $userid; | |
3049 | } | |
3050 | ||
7d2a0492 | 3051 | // Add a user setting branch |
5d07e957 | 3052 | $usersetting = $this->add(get_string($gstitle, 'moodle', $fullname), null, self::TYPE_CONTAINER, null, $key); |
7d2a0492 | 3053 | $usersetting->id = 'usersettings'; |
3054 | ||
7d2a0492 | 3055 | // Check if the user has been deleted |
3056 | if ($user->deleted) { | |
3057 | if (!has_capability('moodle/user:update', $coursecontext)) { | |
3058 | // We can't edit the user so just show the user deleted message | |
d972bad1 | 3059 | $usersetting->add(get_string('userdeleted'), null, self::TYPE_SETTING); |
7d2a0492 | 3060 | } else { |
3061 | // We can edit the user so show the user deleted message and link it to the profile | |
03d9401e MD |
3062 | if ($course->id == SITEID) { |
3063 | $profileurl = new moodle_url('/user/profile.php', array('id'=>$user->id)); | |
3064 | } else { | |
3065 | $profileurl = new moodle_url('/user/view.php', array('id'=>$user->id, 'course'=>$course->id)); | |
3066 | } | |
d972bad1 | 3067 | $usersetting->add(get_string('userdeleted'), $profileurl, self::TYPE_SETTING); |
7d2a0492 | 3068 | } |
3069 | return true; | |
3070 | } | |
3071 | ||
7d2a0492 | 3072 | // Add the profile edit link |
3073 | if (isloggedin() && !isguestuser($user) && !is_mnet_remote_user($user)) { | |
7a7e209d | 3074 | if (($currentuser || !is_primary_admin($user->id)) && has_capability('moodle/user:update', $systemcontext)) { |
a6855934 | 3075 | $url = new moodle_url('/user/editadvanced.php', array('id'=>$user->id, 'course'=>$course->id)); |
7a7e209d | 3076 | $usersetting->add(get_string('editmyprofile'), $url, self::TYPE_SETTING); |
7d2a0492 | 3077 | } else if ((has_capability('moodle/user:editprofile', $usercontext) && !is_primary_admin($user->id)) || ($currentuser && has_capability('moodle/user:editownprofile', $systemcontext))) { |
a6855934 | 3078 | $url = new moodle_url('/user/edit.php', array('id'=>$user->id, 'course'=>$course->id)); |
d972bad1 | 3079 | $usersetting->add(get_string('editmyprofile'), $url, self::TYPE_SETTING); |
7d2a0492 | 3080 | } |
3081 | } | |
3082 | ||
3083 | // Change password link | |
3084 | if (!empty($user->auth)) { | |
3085 | $userauth = get_auth_plugin($user->auth); | |
3086 | if ($currentuser && !session_is_loggedinas() && $userauth->can_change_password() && !isguestuser() && has_capability('moodle/user:changeownpassword', $systemcontext)) { | |
3087 | $passwordchangeurl = $userauth->change_password_url(); | |
3088 | if (!$passwordchangeurl) { | |
3089 | if (empty($CFG->loginhttps)) { | |
3090 | $wwwroot = $CFG->wwwroot; | |
3091 | } else { | |
3092 | $wwwroot = str_replace('http:', 'https:', $CFG->wwwroot); | |
3093 | } | |
a6855934 | 3094 | $passwordchangeurl = new moodle_url('/login/change_password.php'); |
7d2a0492 | 3095 | } else { |
3096 | $urlbits = explode($passwordchangeurl. '?', 1); | |
3097 | $passwordchangeurl = new moodle_url($urlbits[0]); | |
3098 | if (count($urlbits)==2 && preg_match_all('#\&([^\=]*?)\=([^\&]*)#si', '&'.$urlbits[1], $matches)) { | |
3099 | foreach ($matches as $pair) { | |
3100 | $fullmeurl->param($pair[1],$pair[2]); | |
3101 | } | |
3102 | } | |
3103 | } | |
3104 | $passwordchangeurl->param('id', $course->id); | |
d972bad1 | 3105 | $usersetting->add(get_string("changepassword"), $passwordchangeurl, self::TYPE_SETTING); |
7d2a0492 | 3106 | } |
3107 | } | |
3108 | ||
3109 | // View the roles settings | |
3110 | if (has_any_capability(array('moodle/role:assign', 'moodle/role:safeoverride','moodle/role:override', 'moodle/role:manage'), $usercontext)) { | |
3406acde | 3111 | $roles = $usersetting->add(get_string('roles'), null, self::TYPE_SETTING); |
7d2a0492 | 3112 | |
a6855934 | 3113 | $url = new moodle_url('/admin/roles/usersroles.php', array('userid'=>$user->id, 'courseid'=>$course->id)); |
3406acde | 3114 | $roles->add(get_string('thisusersroles', 'role'), $url, self::TYPE_SETTING); |
7d2a0492 | 3115 | |
3116 | $assignableroles = get_assignable_roles($usercontext, ROLENAME_BOTH); | |
7d2a0492 | 3117 | |
3118 | if (!empty($assignableroles)) { | |
a6855934 | 3119 | $url = new moodle_url('/admin/roles/assign.php', array('contextid'=>$usercontext->id,'userid'=>$user->id, 'courseid'=>$course->id)); |
3406acde | 3120 | $roles->add(get_string('assignrolesrelativetothisuser', 'role'), $url, self::TYPE_SETTING); |
7d2a0492 | 3121 | } |
3122 | ||
0b29477b SH |
3123 | if (has_capability('moodle/role:review', $usercontext) || count(get_overridable_roles($usercontext, ROLENAME_BOTH))>0) { |
3124 | $url = new moodle_url('/admin/roles/permissions.php', array('contextid'=>$usercontext->id,'userid'=>$user->id, 'courseid'=>$course->id)); | |
3406acde | 3125 | $roles->add(get_string('permissions', 'role'), $url, self::TYPE_SETTING); |
7d2a0492 | 3126 | } |
3127 | ||
a6855934 | 3128 | $url = new moodle_url('/admin/roles/check.php', array('contextid'=>$usercontext->id,'userid'=>$user->id, 'courseid'=>$course->id)); |
3406acde | 3129 | $roles->add(get_string('checkpermissions', 'role'), $url, self::TYPE_SETTING); |
7d2a0492 | 3130 | } |
3131 | ||
3132 | // Portfolio | |
7a7e209d | 3133 | if ($currentuser && !empty($CFG->enableportfolios) && has_capability('moodle/portfolio:export', $systemcontext)) { |
24ba58ee PL |
3134 | require_once($CFG->libdir . '/portfoliolib.php'); |
3135 | if (portfolio_instances(true, false)) { | |
3406acde | 3136 | $portfolio = $usersetting->add(get_string('portfolios', 'portfolio'), null, self::TYPE_SETTING); |
f6017569 MC |
3137 | |
3138 | $config = optional_param('config', 0, PARAM_INT); | |
3139 | $hide = optional_param('hide', 0, PARAM_INT); | |
3140 | $url = new moodle_url('/user/portfolio.php', array('courseid'=>$course->id)); | |
3141 | if ($hide !== 0) { | |
3142 | $url->param('hide', $hide); | |
3143 | } | |
3144 | if ($config !== 0) { | |
3145 | $url->param('config', $config); | |
3146 | } | |
3147 | $portfolio->add(get_string('configure', 'portfolio'), $url, self::TYPE_SETTING); | |
3148 | ||
3149 | $page = optional_param('page', 0, PARAM_INT); | |
3150 | $perpage = optional_param('perpage', 10, PARAM_INT); | |
3151 | $url = new moodle_url('/user/portfoliologs.php', array('courseid'=>$course->id)); | |
3152 | if ($page !== 0) { | |
3153 | $url->param('page', $page); | |
3154 | } | |
3155 | if ($perpage !== 0) { | |
3156 | $url->param('perpage', $perpage); | |
3157 | } | |
3158 | $portfolio->add(get_string('logs', 'portfolio'), $url, self::TYPE_SETTING); | |
24ba58ee | 3159 | } |
7d2a0492 | 3160 | } |
3161 | ||
4dfdeb76 AD |
3162 | $enablemanagetokens = false; |
3163 | if (!empty($CFG->enablerssfeeds)) { | |
3164 | $enablemanagetokens = true; | |
3165 | } else if (!is_siteadmin($USER->id) | |
3166 | && !empty($CFG->enablewebservices) | |
3167 | && has_capability('moodle/webservice:createtoken', get_system_context()) ) { | |
3168 | $enablemanagetokens = true; | |
3169 | } | |
94e90ab7 | 3170 | // Security keys |
4dfdeb76 | 3171 | if ($currentuser && $enablemanagetokens) { |
a6855934 | 3172 | $url = new moodle_url('/user/managetoken.php', array('sesskey'=>sesskey())); |
94e90ab7 | 3173 | $usersetting->add(get_string('securitykeys', 'webservice'), $url, self::TYPE_SETTING); |
5eacbd4b | 3174 | } |
3175 | ||
7d2a0492 | 3176 | // Repository |
3177 | if (!$currentuser) { | |
3178 | require_once($CFG->dirroot . '/repository/lib.php'); | |
3179 | $editabletypes = repository::get_editable_types($usercontext); | |
3180 | if ($usercontext->contextlevel == CONTEXT_USER && !empty($editabletypes)) { | |
a6855934 | 3181 | $url = new moodle_url('/repository/manage_instances.php', array('contextid'=>$usercontext->id)); |
d972bad1 | 3182 | $usersetting->add(get_string('repositories', 'repository'), $url, self::TYPE_SETTING); |
7d2a0492 | 3183 | } |
3184 | } | |
3185 | ||
3186 | // Messaging | |
03d9401e | 3187 | // TODO this is adding itself to the messaging settings for other people based on one's own setting |
7a7e209d | 3188 | if (has_capability('moodle/user:editownmessageprofile', $systemcontext)) { |
a6855934 | 3189 | $url = new moodle_url('/message/edit.php', array('id'=>$user->id, 'course'=>$course->id)); |
d972bad1 | 3190 | $usersetting->add(get_string('editmymessage', 'message'), $url, self::TYPE_SETTING); |
7d2a0492 | 3191 | } |
3192 | ||
2713c6bd SH |
3193 | // Blogs |
3194 | if (!empty($CFG->bloglevel)) { | |
3195 | $blog = $usersetting->add(get_string('blogs', 'blog'), null, navigation_node::TYPE_CONTAINER, null, 'blogs'); | |
3196 | $blog->add(get_string('preferences', 'blog'), new moodle_url('/blog/preferences.php'), navigation_node::TYPE_SETTING); | |
435a512e | 3197 | if (!empty($CFG->useexternalblogs) && $CFG->maxexternalblogsperuser > 0 && has_capability('moodle/blog:manageexternal', get_context_instance(CONTEXT_SYSTEM))) { |
2713c6bd SH |
3198 | $blog->add(get_string('externalblogs', 'blog'), new moodle_url('/blog/external_blogs.php'), navigation_node::TYPE_SETTING); |
3199 | $blog->add(get_string('addnewexternalblog', 'blog'), new moodle_url('/blog/external_blog_edit.php'), navigation_node::TYPE_SETTING); | |
3200 | } | |
3201 | } | |
3202 | ||
03d9401e MD |
3203 | // Login as ... |
3204 | if (!$user->deleted and !$currentuser && !session_is_loggedinas() && has_capability('moodle/user:loginas', $coursecontext) && !is_siteadmin($user->id)) { | |
3205 | $url = new moodle_url('/course/loginas.php', array('id'=>$course->id, 'user'=>$user->id, 'sesskey'=>sesskey())); | |
3206 | $usersetting->add(get_string('loginas'), $url, self::TYPE_SETTING); | |
3207 | } | |
3208 | ||
3406acde | 3209 | return $usersetting; |
7d2a0492 | 3210 | } |
3211 | ||
0b29477b SH |
3212 | /** |
3213 | * Loads block specific settings in the navigation | |
4f0c2d00 | 3214 | * |