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