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