navigation MDL-22044 Fixed bug preventing module AJAX expansion
[moodle.git] / blocks / navigation / block_navigation.php
CommitLineData
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 blocks
24 * @copyright 2009 Sam Hemelryk
25 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 */
27
28/**
29 * The global navigation tree block class
30 *
31 * Used to produce the global navigation block new to Moodle 2.0
32 *
33 * @package blocks
34 * @copyright 2009 Sam Hemelryk
35 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
36 */
3406acde 37class block_navigation extends block_base {
7d2a0492 38
39 /** @var int */
40 public static $navcount;
41 /** @var string */
42 public $blockname = null;
d7319652 43 /** @var bool */
7d2a0492 44 protected $contentgenerated = false;
d7319652 45 /** @var bool|null */
46 protected $docked = null;
7d2a0492 47
a9535f79
SH
48 /** @var int Trim characters from the right */
49 const TRIM_RIGHT = 1;
50 /** @var int Trim characters from the left */
51 const TRIM_LEFT = 2;
52 /** @var int Trim characters from the center */
53 const TRIM_CENTER = 3;
54
7d2a0492 55 /**
56 * Set the initial properties for the block
57 */
58 function init() {
59 global $CFG;
60 $this->blockname = get_class($this);
621f2f28 61 $this->title = get_string('pluginname', $this->blockname);
7d2a0492 62 $this->version = 2009082800;
63 }
64
65 /**
66 * All multiple instances of this block
67 * @return bool Returns true
68 */
69 function instance_allow_multiple() {
70 return false;
71 }
72
73 /**
74 * Set the applicable formats for this block to all
75 * @return array
76 */
77 function applicable_formats() {
78 return array('all' => true);
79 }
80
81 /**
82 * Allow the user to configure a block instance
83 * @return bool Returns true
84 */
85 function instance_allow_config() {
86 return true;
87 }
88
3406acde
SH
89 function instance_can_be_docked() {
90 return (parent::instance_can_be_docked() && (empty($this->config->enabledock) || $this->config->enabledock=='yes'));
91 }
92
9d897331 93 function get_required_javascript() {
1ce15fda 94 global $CFG;
7c964cdf 95 $this->page->requires->js_module(array('name'=>'core_dock', 'fullpath'=>'/blocks/dock.js', 'requires'=>array('base', 'cookie', 'dom', 'io', 'node', 'event-custom', 'event-mouseenter', 'yui2-container')));
3406acde 96 $this->page->requires->js_module(array('name'=>'block_navigation', 'fullpath'=>'/blocks/navigation/navigation.js', 'requires'=>array('core_dock', 'io', 'node', 'dom', 'event-custom', 'json-parse')));
9d897331
SH
97 user_preference_allow_ajax_update('docked_block_instance_'.$this->instance->id, PARAM_INT);
98 }
99
7d2a0492 100 /**
101 * Gets the content for this block by grabbing it from $this->page
102 */
103 function get_content() {
104 global $CFG, $OUTPUT;
105 // First check if we have already generated, don't waste cycles
106 if ($this->contentgenerated === true) {
4f0c2d00 107 return $this->content;
7d2a0492 108 }
f44b10ed 109 $this->page->requires->yui2_lib('dom');
2574fae6 110 // JS for navigation moved to the standard theme, the code will probably have to depend on the actual page structure
9dec75db 111 // $this->page->requires->js('/lib/javascript-navigation.js');
7d2a0492 112 // Navcount is used to allow us to have multiple trees although I dont' know why
113 // you would want to trees the same
c1c0cecf 114
3406acde 115 block_navigation::$navcount++;
c1c0cecf 116
d7319652 117 // Check if this block has been docked
118 if ($this->docked === null) {
3406acde 119 $this->docked = get_user_preferences('nav_in_tab_panel_globalnav'.block_navigation::$navcount, 0);
d7319652 120 }
121
122 // Check if there is a param to change the docked state
123 if ($this->docked && optional_param('undock', null, PARAM_INT)==$this->instance->id) {
3406acde 124 unset_user_preference('nav_in_tab_panel_globalnav'.block_navigation::$navcount);
d7319652 125 $url = $this->page->url;
126 $url->remove_params(array('undock'));
127 redirect($url);
128 } else if (!$this->docked && optional_param('dock', null, PARAM_INT)==$this->instance->id) {
3406acde 129 set_user_preferences(array('nav_in_tab_panel_globalnav'.block_navigation::$navcount=>1));
d7319652 130 $url = $this->page->url;
131 $url->remove_params(array('dock'));
132 redirect($url);
133 }
134
a9535f79
SH
135 $trimmode = self::TRIM_LEFT;
136 $trimlength = 50;
137
138 if (!empty($this->config->trimmode)) {
139 $trimmode = (int)$this->config->trimmode;
140 }
141
142 if (!empty($this->config->trimlength)) {
143 $trimlength = (int)$this->config->trimlength;
144 }
145
7d2a0492 146 // Initialise (only actually happens if it hasn't already been done yet
147 $this->page->navigation->initialise();
a9535f79
SH
148 $navigation = clone($this->page->navigation);
149 $this->trim($navigation, $trimmode, $trimlength, ceil($trimlength/2));
7d2a0492 150
7d2a0492 151 if (!empty($this->config->showmyhistory) && $this->config->showmyhistory=='yes') {
152 $this->showmyhistory();
153 }
154
7d2a0492 155 // Get the expandable items so we can pass them to JS
156 $expandable = array();
157 $this->page->navigation->find_expandable($expandable);
d2c394f3 158
7d2a0492 159 // Initialise the JS tree object
3406acde 160 $module = array('name'=>'block_navigation', 'fullpath'=>'/blocks/navigation/navigation.js', 'requires'=>array('core_dock', 'io', 'node', 'dom', 'event-custom', 'json-parse'));
781bd8ae
PS
161 $arguments = array($this->instance->id, array('expansions'=>$expandable, 'instance'=>$this->instance->id, 'candock'=>$this->instance_can_be_docked()));
162 $this->page->requires->js_init_call('M.block_navigation.init_add_tree', $arguments, false, $module);
4f0c2d00 163
7d2a0492 164 // Grab the items to display
3406acde
SH
165 $renderer = $this->page->get_renderer('block_navigation');
166 $this->content->text = $renderer->navigation_tree($this->page->navigation);
4ca6cfbf 167
3ea5951e 168 $reloadlink = new moodle_url($this->page->url, array('regenerate'=>'navigation'));
17390771 169
3406acde 170 $this->content->footer .= $OUTPUT->action_icon($reloadlink, new pix_icon('t/reload', get_string('reload')), null, array('class'=>'customcommand reloadnavigation'));
4ca6cfbf 171
7d2a0492 172 // Set content generated to true so that we know it has been done
173 $this->contentgenerated = true;
4f0c2d00
PS
174
175 return $this->content;
7d2a0492 176 }
177
178 /**
179 * Returns the attributes to set for this block
180 *
181 * This function returns an array of HTML attributes for this block including
182 * the defaults
183 * {@link block_tree->html_attributes()} is used to get the default arguments
184 * and then we check whether the user has enabled hover expansion and add the
185 * appropriate hover class if it has
186 *
187 * @return array An array of HTML attributes
188 */
189 public function html_attributes() {
190 $attributes = parent::html_attributes();
191 if (!empty($this->config->enablehoverexpansion) && $this->config->enablehoverexpansion == 'yes') {
9d897331 192 $attributes['class'] .= ' block_js_expansion';
7d2a0492 193 }
7d2a0492 194 return $attributes;
195 }
196
197 /**
198 * This function maintains a history of the active pages that a user has visited
199 * and displays it back to the user as part of the navigation structure
200 *
201 * @return bool
202 */
203 protected function showmyhistory() {
a6855934 204 global $USER, $PAGE;
7d2a0492 205
206 // Create a navigation cache so that we can store the history
207 $cache = new navigation_cache('navigationhistory', 60*60);
208
209 // If the user isn't logged in or is a guest we don't want to display anything
4cdb8d70 210 if (!isloggedin() || isguestuser()) {
7d2a0492 211 return false;
212 }
213
214 // Check the cache to see if we have loaded my courses already
215 // there is a very good chance that we have
216 if (!$cache->cached('history')) {
217 $cache->history = array();
218 }
219 $history = $cache->history;
220 $historycount = count($history);
221
222 // Find the initial active node
223 $child = false;
224 if ($PAGE->navigation->contains_active_node()) {
225 $child = $PAGE->navigation->find_active_node();
226 } else if ($PAGE->settingsnav->contains_active_node()) {
227 $child = $PAGE->settingsnav->find_active_node();
228 }
229 // Check that we found an active child node
230 if ($child!==false) {
231 $properties = array();
232 // Check whether this child contains another active child node
233 // this can happen if we are looking at a module
234 if ($child->contains_active_node()) {
235 $titlebits = array();
236 // Loop while the child contains active nodes and in each iteration
237 // find the next node in the correct direction
238 while ($child!==null && $child->contains_active_node()) {
239 if (!empty($child->shorttext)) {
240 $titlebits[] = $child->shorttext;
241 } else {
242 $titlebits[] = $child->text;
243 }
244 foreach ($child->children as $child) {
245 if ($child->contains_active_node() || $child->isactive) {
246 // We have found the active child or one of its parents
247 // so break the foreach so we can proceed in the while
248 break;
249 }
250 }
251 }
252 if (!empty($child->shorttext)) {
253 $titlebits[] = $child->shorttext;
254 } else {
255 $titlebits[] = $child->text;
256 }
257 $properties['text'] = join(' - ', $titlebits);
258 $properties['shorttext'] = join(' - ', $titlebits);
259 } else {
260 $properties['text'] = $child->text;
261 $properties['shorttext'] = $child->shorttext;
262 }
263 $properties['action'] = $child->action;
264 $properties['key'] = $child->key;
265 $properties['type'] = $child->type;
266 $properties['icon'] = $child->icon;
267
268 // Create a new navigation node object free of the main structure
269 // so that it is easily storeable and customised
270 $child = new navigation_node($properties);
271
272 // Check that this page isn't already in the history array. If it is
273 // we will remove it so that it gets added at the top and we dont get
274 // duplicate entries
275 foreach ($history as $key=>$node) {
276 if ($node->key == $child->key && $node->type == $child->type) {
277 if ($node->action instanceof moodle_url && $child->action instanceof moodle_url && $node->action->compare($child->action)) {
278 unset($history[$key]);
eb788065 279 } else if ($child->action instanceof moodle_url && $child->action->out_omit_querystring() == $node->action) {
7d2a0492 280 unset($history[$key]);
281 } else if ($child->action == $node->action) {
282 unset($history[$key]);
283 }
284 }
285 }
286 // If there is more than 5 elements in the array remove the first one
287 // We want a fifo array
288 if (count($history) > 5) {
289 array_shift($history);
290 }
291 $child->nodetype = navigation_node::NODETYPE_LEAF;
292 $child->children = array();
293 // Add the child to the history array
294 array_push($history,$child);
295 }
296
297 // If we have `more than nothing` in the history display it :D
298 if ($historycount > 0) {
299 // Add a branch to hold the users history
a9535f79 300 $mymoodle = $PAGE->navigation->get('myprofile', navigation_node::TYPE_USER);
7a7e209d 301 $myhistorybranch = $mymoodle->add(get_string('showmyhistorytitle', $this->blockname), null, navigation_node::TYPE_CUSTOM, null, 'myhistory');
3406acde
SH
302 foreach (array_reverse($history) as $node) {
303 $myhistorybranch->children->add($node);
304 }
7d2a0492 305 }
306
307 // Cache the history (or update the cached history as it is)
308 $cache->history = $history;
309
310 return true;
311 }
a9535f79
SH
312
313 /**
314 * Trims the text and shorttext properties of this node and optionally
315 * all of its children.
316 *
317 * @param int $mode One of navigation_node::TRIM_*
318 * @param int $long The length to trim text to
319 * @param int $short The length to trim shorttext to
320 * @param bool $recurse Recurse all children
321 * @param textlib|null $textlib
322 */
323 public function trim(navigation_node $node, $mode=1, $long=50, $short=25, $recurse=true, $textlib=null) {
324 if ($textlib == null) {
325 $textlib = textlib_get_instance();
326 }
327 switch ($mode) {
328 case self::TRIM_RIGHT :
329 if ($textlib->strlen($node->text)>($long+3)) {
330 // Truncate the text to $long characters
331 $node->text = $this->trim_right($textlib, $node->text, $long);
332 }
333 if (is_string($node->shorttext) && $textlib->strlen($node->shorttext)>($short+3)) {
334 // Truncate the shorttext
335 $node->shorttext = $this->trim_right($textlib, $node->shorttext, $short);
336 }
337 break;
338 case self::TRIM_LEFT :
339 if ($textlib->strlen($node->text)>($long+3)) {
340 // Truncate the text to $long characters
341 $node->text = $this->trim_left($textlib, $node->text, $long);
342 }
343 if (is_string($node->shorttext) && strlen($node->shorttext)>($short+3)) {
344 // Truncate the shorttext
345 $node->shorttext = $this->trim_left($textlib, $node->shorttext, $short);
346 }
347 break;
348 case self::TRIM_CENTER :
349 if ($textlib->strlen($node->text)>($long+3)) {
350 // Truncate the text to $long characters
351 $node->text = $this->trim_center($textlib, $node->text, $long);
352 }
353 if (is_string($node->shorttext) && strlen($node->shorttext)>($short+3)) {
354 // Truncate the shorttext
355 $node->shorttext = $this->trim_center($textlib, $node->shorttext, $short);
356 }
357 break;
358 }
359 if ($recurse && $node->children->count()) {
360 foreach ($node->children as &$child) {
361 $this->trim($child, $mode, $long, $short, true, $textlib);
362 }
363 }
364 }
365 /**
366 * Truncate a string from the left
367 * @param textlib $textlib
368 * @param string $string The string to truncate
369 * @param int $length The length to truncate to
370 * @return string The truncated string
371 */
372 protected function trim_left($textlib, $string, $length) {
373 return '...'.$textlib->substr($string, $textlib->strlen($string)-$length);
374 }
375 /**
376 * Truncate a string from the right
377 * @param textlib $textlib
378 * @param string $string The string to truncate
379 * @param int $length The length to truncate to
380 * @return string The truncated string
381 */
382 protected function trim_right($textlib, $string, $length) {
383 return $textlib->substr($string, 0, $length).'...';
384 }
385 /**
386 * Truncate a string in the center
387 * @param textlib $textlib
388 * @param string $string The string to truncate
389 * @param int $length The length to truncate to
390 * @return string The truncated string
391 */
392 protected function trim_center($textlib, $string, $length) {
393 $trimlength = ceil($length/2);
394 $start = $textlib->substr($string, 0, $trimlength);
395 $end = $textlib->substr($string, $textlib->strlen($string)-$trimlength);
396 $string = $start.'...'.$end;
397 return $string;
398 }
c1c0cecf 399}