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