blocks-navigation MDL-25596 Fixed up redundant AJAX calls when navigation was limited...
[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 }
63
64 /**
65 * All multiple instances of this block
66 * @return bool Returns true
67 */
68 function instance_allow_multiple() {
69 return false;
70 }
71
72 /**
73 * Set the applicable formats for this block to all
74 * @return array
75 */
76 function applicable_formats() {
77 return array('all' => true);
78 }
79
80 /**
81 * Allow the user to configure a block instance
82 * @return bool Returns true
83 */
84 function instance_allow_config() {
85 return true;
86 }
87
d8ef60bd
SH
88 /**
89 * The navigation block cannot be hidden by default as it is integral to
90 * the navigation of Moodle.
91 *
92 * @return false
93 */
94 function instance_can_be_hidden() {
95 return false;
96 }
97
3406acde
SH
98 function instance_can_be_docked() {
99 return (parent::instance_can_be_docked() && (empty($this->config->enabledock) || $this->config->enabledock=='yes'));
100 }
101
9d897331 102 function get_required_javascript() {
1ce15fda 103 global $CFG;
9d897331 104 user_preference_allow_ajax_update('docked_block_instance_'.$this->instance->id, PARAM_INT);
48d8d090
SH
105 $this->page->requires->js_module('core_dock');
106 $limit = 20;
107 if (!empty($CFG->navcourselimit)) {
108 $limit = $CFG->navcourselimit;
8ad24c1a 109 }
8e5c23e0
SH
110 $arguments = array(
111 'id' => $this->instance->id,
112 'instance' => $this->instance->id,
113 'candock' => $this->instance_can_be_docked(),
114 'courselimit' => $limit,
115 'expansionlimit' => $this->config->expansionlimit
116 );
48d8d090 117 $this->page->requires->yui_module(array('core_dock', 'moodle-block_navigation-navigation'), 'M.block_navigation.init_add_tree', array($arguments));
9d897331
SH
118 }
119
7d2a0492 120 /**
121 * Gets the content for this block by grabbing it from $this->page
122 */
123 function get_content() {
124 global $CFG, $OUTPUT;
125 // First check if we have already generated, don't waste cycles
126 if ($this->contentgenerated === true) {
4f0c2d00 127 return $this->content;
7d2a0492 128 }
2574fae6 129 // JS for navigation moved to the standard theme, the code will probably have to depend on the actual page structure
9dec75db 130 // $this->page->requires->js('/lib/javascript-navigation.js');
7d2a0492 131 // Navcount is used to allow us to have multiple trees although I dont' know why
132 // you would want to trees the same
c1c0cecf 133
3406acde 134 block_navigation::$navcount++;
c1c0cecf 135
d7319652 136 // Check if this block has been docked
137 if ($this->docked === null) {
3406acde 138 $this->docked = get_user_preferences('nav_in_tab_panel_globalnav'.block_navigation::$navcount, 0);
d7319652 139 }
140
141 // Check if there is a param to change the docked state
142 if ($this->docked && optional_param('undock', null, PARAM_INT)==$this->instance->id) {
3406acde 143 unset_user_preference('nav_in_tab_panel_globalnav'.block_navigation::$navcount);
d7319652 144 $url = $this->page->url;
145 $url->remove_params(array('undock'));
146 redirect($url);
147 } else if (!$this->docked && optional_param('dock', null, PARAM_INT)==$this->instance->id) {
3406acde 148 set_user_preferences(array('nav_in_tab_panel_globalnav'.block_navigation::$navcount=>1));
d7319652 149 $url = $this->page->url;
150 $url->remove_params(array('dock'));
151 redirect($url);
152 }
153
a9535f79
SH
154 $trimmode = self::TRIM_LEFT;
155 $trimlength = 50;
156
157 if (!empty($this->config->trimmode)) {
158 $trimmode = (int)$this->config->trimmode;
159 }
160
161 if (!empty($this->config->trimlength)) {
162 $trimlength = (int)$this->config->trimlength;
163 }
164
7d2a0492 165 // Initialise (only actually happens if it hasn't already been done yet
166 $this->page->navigation->initialise();
a9535f79 167 $navigation = clone($this->page->navigation);
88f77c3c
SH
168 $expansionlimit = null;
169 if (!empty($this->config->expansionlimit)) {
170 $expansionlimit = $this->config->expansionlimit;
171 $navigation->set_expansion_limit($this->config->expansionlimit);
172 }
a9535f79 173 $this->trim($navigation, $trimmode, $trimlength, ceil($trimlength/2));
7d2a0492 174
7d2a0492 175 // Get the expandable items so we can pass them to JS
176 $expandable = array();
88f77c3c 177 $navigation->find_expandable($expandable);
1af67ecb
SH
178 if ($expansionlimit) {
179 foreach ($expandable as $key=>$node) {
180 if ($node['type'] > $expansionlimit && !($expansionlimit == navigation_node::TYPE_COURSE && $node['type'] == $expansionlimit && $node['branchid'] == SITEID)) {
181 unset($expandable[$key]);
182 }
183 }
184 }
d2c394f3 185
48d8d090 186 $this->page->requires->data_for_js('navtreeexpansions'.$this->instance->id, $expandable);
4f0c2d00 187
480f906e
SH
188 $options = array();
189 $options['linkcategories'] = (!empty($this->config->linkcategories) && $this->config->linkcategories == 'yes');
190
7d2a0492 191 // Grab the items to display
3406acde 192 $renderer = $this->page->get_renderer('block_navigation');
480f906e 193 $this->content->text = $renderer->navigation_tree($navigation, $expansionlimit, $options);
4ca6cfbf 194
7d2a0492 195 // Set content generated to true so that we know it has been done
196 $this->contentgenerated = true;
4f0c2d00
PS
197
198 return $this->content;
7d2a0492 199 }
200
201 /**
202 * Returns the attributes to set for this block
203 *
204 * This function returns an array of HTML attributes for this block including
205 * the defaults
206 * {@link block_tree->html_attributes()} is used to get the default arguments
207 * and then we check whether the user has enabled hover expansion and add the
208 * appropriate hover class if it has
209 *
210 * @return array An array of HTML attributes
211 */
212 public function html_attributes() {
213 $attributes = parent::html_attributes();
214 if (!empty($this->config->enablehoverexpansion) && $this->config->enablehoverexpansion == 'yes') {
9d897331 215 $attributes['class'] .= ' block_js_expansion';
7d2a0492 216 }
7d2a0492 217 return $attributes;
218 }
219
a9535f79
SH
220 /**
221 * Trims the text and shorttext properties of this node and optionally
222 * all of its children.
223 *
224 * @param int $mode One of navigation_node::TRIM_*
225 * @param int $long The length to trim text to
226 * @param int $short The length to trim shorttext to
227 * @param bool $recurse Recurse all children
228 * @param textlib|null $textlib
229 */
230 public function trim(navigation_node $node, $mode=1, $long=50, $short=25, $recurse=true, $textlib=null) {
231 if ($textlib == null) {
232 $textlib = textlib_get_instance();
233 }
234 switch ($mode) {
235 case self::TRIM_RIGHT :
236 if ($textlib->strlen($node->text)>($long+3)) {
237 // Truncate the text to $long characters
238 $node->text = $this->trim_right($textlib, $node->text, $long);
239 }
240 if (is_string($node->shorttext) && $textlib->strlen($node->shorttext)>($short+3)) {
241 // Truncate the shorttext
242 $node->shorttext = $this->trim_right($textlib, $node->shorttext, $short);
243 }
244 break;
245 case self::TRIM_LEFT :
246 if ($textlib->strlen($node->text)>($long+3)) {
247 // Truncate the text to $long characters
248 $node->text = $this->trim_left($textlib, $node->text, $long);
249 }
250 if (is_string($node->shorttext) && strlen($node->shorttext)>($short+3)) {
251 // Truncate the shorttext
252 $node->shorttext = $this->trim_left($textlib, $node->shorttext, $short);
253 }
254 break;
255 case self::TRIM_CENTER :
256 if ($textlib->strlen($node->text)>($long+3)) {
257 // Truncate the text to $long characters
258 $node->text = $this->trim_center($textlib, $node->text, $long);
259 }
260 if (is_string($node->shorttext) && strlen($node->shorttext)>($short+3)) {
261 // Truncate the shorttext
262 $node->shorttext = $this->trim_center($textlib, $node->shorttext, $short);
263 }
264 break;
265 }
266 if ($recurse && $node->children->count()) {
267 foreach ($node->children as &$child) {
268 $this->trim($child, $mode, $long, $short, true, $textlib);
269 }
270 }
271 }
272 /**
273 * Truncate a string from the left
274 * @param textlib $textlib
275 * @param string $string The string to truncate
276 * @param int $length The length to truncate to
277 * @return string The truncated string
278 */
279 protected function trim_left($textlib, $string, $length) {
280 return '...'.$textlib->substr($string, $textlib->strlen($string)-$length);
281 }
282 /**
283 * Truncate a string from the right
284 * @param textlib $textlib
285 * @param string $string The string to truncate
286 * @param int $length The length to truncate to
287 * @return string The truncated string
288 */
289 protected function trim_right($textlib, $string, $length) {
290 return $textlib->substr($string, 0, $length).'...';
291 }
292 /**
293 * Truncate a string in the center
294 * @param textlib $textlib
295 * @param string $string The string to truncate
296 * @param int $length The length to truncate to
297 * @return string The truncated string
298 */
299 protected function trim_center($textlib, $string, $length) {
300 $trimlength = ceil($length/2);
301 $start = $textlib->substr($string, 0, $trimlength);
302 $end = $textlib->substr($string, $textlib->strlen($string)-$trimlength);
303 $string = $start.'...'.$end;
304 return $string;
305 }
c1c0cecf 306}