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