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