Merge branch 'MDL-58490-master' of git://github.com/lameze/moodle
[moodle.git] / blocks / navigation / block_navigation.php
CommitLineData
7d2a0492 1<?php
7d2a0492 2// This file is part of Moodle - http://moodle.org/
3//
4// Moodle is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// Moodle is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
16
17/**
18 * This file contains classes used to manage the navigation structures in Moodle
19 * and was introduced as part of the changes occuring in Moodle 2.0
20 *
5bcfd504 21 * @since Moodle 2.0
31fde54f 22 * @package block_navigation
7d2a0492 23 * @copyright 2009 Sam Hemelryk
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 */
26
27/**
28 * The global navigation tree block class
29 *
30 * Used to produce the global navigation block new to Moodle 2.0
31 *
31fde54f
AG
32 * @package block_navigation
33 * @category navigation
7d2a0492 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
31fde54f 39 /** @var int This allows for multiple navigation trees */
7d2a0492 40 public static $navcount;
31fde54f 41 /** @var string The name of the block */
7d2a0492 42 public $blockname = null;
31fde54f 43 /** @var bool A switch to indicate whether content has been generated or not. */
7d2a0492 44 protected $contentgenerated = false;
31fde54f 45 /** @var bool|null variable for checking if the block is docked*/
d7319652 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() {
7d2a0492 59 $this->blockname = get_class($this);
621f2f28 60 $this->title = get_string('pluginname', $this->blockname);
7d2a0492 61 }
62
63 /**
64 * All multiple instances of this block
31fde54f 65 * @return bool Returns false
7d2a0492 66 */
67 function instance_allow_multiple() {
68 return false;
69 }
70
71 /**
72 * Set the applicable formats for this block to all
73 * @return array
74 */
75 function applicable_formats() {
76 return array('all' => true);
77 }
78
79 /**
80 * Allow the user to configure a block instance
81 * @return bool Returns true
82 */
83 function instance_allow_config() {
84 return true;
85 }
86
d8ef60bd
SH
87 /**
88 * The navigation block cannot be hidden by default as it is integral to
89 * the navigation of Moodle.
90 *
91 * @return false
92 */
93 function instance_can_be_hidden() {
94 return false;
95 }
96
31fde54f
AG
97 /**
98 * Find out if an instance can be docked.
99 *
100 * @return bool true or false depending on whether the instance can be docked or not.
101 */
3406acde
SH
102 function instance_can_be_docked() {
103 return (parent::instance_can_be_docked() && (empty($this->config->enabledock) || $this->config->enabledock=='yes'));
104 }
105
31fde54f
AG
106 /**
107 * Gets Javascript that may be required for navigation
108 */
9d897331 109 function get_required_javascript() {
84192d78 110 parent::get_required_javascript();
fd5466af
CB
111 $arguments = array(
112 'instanceid' => $this->instance->id
113 );
4c2820fb 114 $this->page->requires->string_for_js('viewallcourses', 'moodle');
fd5466af 115 $this->page->requires->js_call_amd('block_navigation/navblock', 'init', $arguments);
9d897331
SH
116 }
117
7d2a0492 118 /**
119 * Gets the content for this block by grabbing it from $this->page
31fde54f
AG
120 *
121 * @return object $this->content
7d2a0492 122 */
123 function get_content() {
6759dc35 124 global $CFG;
7d2a0492 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
31fde54f 132 // you would want two 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
b359900c 154 $trimmode = self::TRIM_RIGHT;
a9535f79
SH
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
c8ed2817
IT
165 // Get the navigation object or don't display the block if none provided.
166 if (!$navigation = $this->get_navigation()) {
167 return null;
168 }
88f77c3c
SH
169 $expansionlimit = null;
170 if (!empty($this->config->expansionlimit)) {
171 $expansionlimit = $this->config->expansionlimit;
172 $navigation->set_expansion_limit($this->config->expansionlimit);
173 }
a9535f79 174 $this->trim($navigation, $trimmode, $trimlength, ceil($trimlength/2));
7d2a0492 175
7d2a0492 176 // Get the expandable items so we can pass them to JS
177 $expandable = array();
88f77c3c 178 $navigation->find_expandable($expandable);
1af67ecb
SH
179 if ($expansionlimit) {
180 foreach ($expandable as $key=>$node) {
181 if ($node['type'] > $expansionlimit && !($expansionlimit == navigation_node::TYPE_COURSE && $node['type'] == $expansionlimit && $node['branchid'] == SITEID)) {
182 unset($expandable[$key]);
183 }
184 }
185 }
d2c394f3 186
f8895446
JO
187 $limit = 20;
188 if (!empty($CFG->navcourselimit)) {
189 $limit = $CFG->navcourselimit;
190 }
191 $expansionlimit = 0;
192 if (!empty($this->config->expansionlimit)) {
193 $expansionlimit = $this->config->expansionlimit;
194 }
195 $arguments = array(
196 'id' => $this->instance->id,
197 'instance' => $this->instance->id,
198 'candock' => $this->instance_can_be_docked(),
199 'courselimit' => $limit,
200 'expansionlimit' => $expansionlimit
201 );
4f0c2d00 202
480f906e
SH
203 $options = array();
204 $options['linkcategories'] = (!empty($this->config->linkcategories) && $this->config->linkcategories == 'yes');
5dbfbacc 205
7d2a0492 206 // Grab the items to display
c8ed2817 207 $renderer = $this->page->get_renderer($this->blockname);
b85b25eb 208 $this->content = new stdClass();
480f906e 209 $this->content->text = $renderer->navigation_tree($navigation, $expansionlimit, $options);
4ca6cfbf 210
7d2a0492 211 // Set content generated to true so that we know it has been done
212 $this->contentgenerated = true;
4f0c2d00
PS
213
214 return $this->content;
7d2a0492 215 }
216
c8ed2817
IT
217 /**
218 * Returns the navigation
219 *
220 * @return navigation_node The navigation object to display
221 */
222 protected function get_navigation() {
223 // Initialise (only actually happens if it hasn't already been done yet)
224 $this->page->navigation->initialise();
225 return clone($this->page->navigation);
226 }
227
7d2a0492 228 /**
229 * Returns the attributes to set for this block
230 *
231 * This function returns an array of HTML attributes for this block including
93123b17
EL
232 * the defaults.
233 * {@link block_tree::html_attributes()} is used to get the default arguments
7d2a0492 234 * and then we check whether the user has enabled hover expansion and add the
31fde54f 235 * appropriate hover class if it has.
7d2a0492 236 *
237 * @return array An array of HTML attributes
238 */
239 public function html_attributes() {
240 $attributes = parent::html_attributes();
241 if (!empty($this->config->enablehoverexpansion) && $this->config->enablehoverexpansion == 'yes') {
9d897331 242 $attributes['class'] .= ' block_js_expansion';
7d2a0492 243 }
7d2a0492 244 return $attributes;
245 }
246
a9535f79
SH
247 /**
248 * Trims the text and shorttext properties of this node and optionally
249 * all of its children.
250 *
31fde54f 251 * @param navigation_node $node
a9535f79
SH
252 * @param int $mode One of navigation_node::TRIM_*
253 * @param int $long The length to trim text to
254 * @param int $short The length to trim shorttext to
255 * @param bool $recurse Recurse all children
a9535f79 256 */
f8311def 257 public function trim(navigation_node $node, $mode=1, $long=50, $short=25, $recurse=true) {
a9535f79
SH
258 switch ($mode) {
259 case self::TRIM_RIGHT :
2f1e464a 260 if (core_text::strlen($node->text)>($long+3)) {
a9535f79 261 // Truncate the text to $long characters
f8311def 262 $node->text = $this->trim_right($node->text, $long);
a9535f79 263 }
2f1e464a 264 if (is_string($node->shorttext) && core_text::strlen($node->shorttext)>($short+3)) {
a9535f79 265 // Truncate the shorttext
f8311def 266 $node->shorttext = $this->trim_right($node->shorttext, $short);
a9535f79
SH
267 }
268 break;
269 case self::TRIM_LEFT :
2f1e464a 270 if (core_text::strlen($node->text)>($long+3)) {
a9535f79 271 // Truncate the text to $long characters
f8311def 272 $node->text = $this->trim_left($node->text, $long);
a9535f79 273 }
2f1e464a 274 if (is_string($node->shorttext) && core_text::strlen($node->shorttext)>($short+3)) {
a9535f79 275 // Truncate the shorttext
f8311def 276 $node->shorttext = $this->trim_left($node->shorttext, $short);
a9535f79
SH
277 }
278 break;
279 case self::TRIM_CENTER :
2f1e464a 280 if (core_text::strlen($node->text)>($long+3)) {
a9535f79 281 // Truncate the text to $long characters
f8311def 282 $node->text = $this->trim_center($node->text, $long);
a9535f79 283 }
2f1e464a 284 if (is_string($node->shorttext) && core_text::strlen($node->shorttext)>($short+3)) {
a9535f79 285 // Truncate the shorttext
f8311def 286 $node->shorttext = $this->trim_center($node->shorttext, $short);
a9535f79
SH
287 }
288 break;
289 }
290 if ($recurse && $node->children->count()) {
291 foreach ($node->children as &$child) {
f8311def 292 $this->trim($child, $mode, $long, $short, true);
a9535f79
SH
293 }
294 }
295 }
296 /**
297 * Truncate a string from the left
a9535f79
SH
298 * @param string $string The string to truncate
299 * @param int $length The length to truncate to
300 * @return string The truncated string
301 */
f8311def 302 protected function trim_left($string, $length) {
2f1e464a 303 return '...'.core_text::substr($string, core_text::strlen($string)-$length, $length);
a9535f79
SH
304 }
305 /**
306 * Truncate a string from the right
a9535f79
SH
307 * @param string $string The string to truncate
308 * @param int $length The length to truncate to
309 * @return string The truncated string
310 */
f8311def 311 protected function trim_right($string, $length) {
2f1e464a 312 return core_text::substr($string, 0, $length).'...';
a9535f79
SH
313 }
314 /**
315 * Truncate a string in the center
a9535f79
SH
316 * @param string $string The string to truncate
317 * @param int $length The length to truncate to
318 * @return string The truncated string
319 */
f8311def 320 protected function trim_center($string, $length) {
a9535f79 321 $trimlength = ceil($length/2);
2f1e464a
PS
322 $start = core_text::substr($string, 0, $trimlength);
323 $end = core_text::substr($string, core_text::strlen($string)-$trimlength);
a9535f79
SH
324 $string = $start.'...'.$end;
325 return $string;
326 }
697f7bd7
SH
327
328 /**
329 * Returns the role that best describes the navigation block... 'navigation'
330 *
331 * @return string 'navigation'
332 */
333 public function get_aria_role() {
334 return 'navigation';
335 }
c1c0cecf 336}