3 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
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
24 * @copyright 2009 Sam Hemelryk
25 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
29 * The global navigation tree block class
31 * Used to produce the global navigation block new to Moodle 2.0
34 * @copyright 2009 Sam Hemelryk
35 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
37 class block_navigation extends block_base {
40 public static $navcount;
42 public $blockname = null;
44 protected $contentgenerated = false;
46 protected $docked = null;
48 /** @var int Trim characters from the right */
50 /** @var int Trim characters from the left */
52 /** @var int Trim characters from the center */
53 const TRIM_CENTER = 3;
56 * Set the initial properties for the block
60 $this->blockname = get_class($this);
61 $this->title = get_string('pluginname', $this->blockname);
65 * All multiple instances of this block
66 * @return bool Returns true
68 function instance_allow_multiple() {
73 * Set the applicable formats for this block to all
76 function applicable_formats() {
77 return array('all' => true);
81 * Allow the user to configure a block instance
82 * @return bool Returns true
84 function instance_allow_config() {
89 * The navigation block cannot be hidden by default as it is integral to
90 * the navigation of Moodle.
94 function instance_can_be_hidden() {
98 function instance_can_be_docked() {
99 return (parent::instance_can_be_docked() && (empty($this->config->enabledock) || $this->config->enabledock=='yes'));
102 function get_required_javascript() {
104 user_preference_allow_ajax_update('docked_block_instance_'.$this->instance->id, PARAM_INT);
105 $this->page->requires->js_module('core_dock');
107 if (!empty($CFG->navcourselimit)) {
108 $limit = $CFG->navcourselimit;
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));
115 * Gets the content for this block by grabbing it from $this->page
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) {
121 return $this->content;
123 // JS for navigation moved to the standard theme, the code will probably have to depend on the actual page structure
124 // $this->page->requires->js('/lib/javascript-navigation.js');
125 // Navcount is used to allow us to have multiple trees although I dont' know why
126 // you would want to trees the same
128 block_navigation::$navcount++;
130 // Check if this block has been docked
131 if ($this->docked === null) {
132 $this->docked = get_user_preferences('nav_in_tab_panel_globalnav'.block_navigation::$navcount, 0);
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) {
137 unset_user_preference('nav_in_tab_panel_globalnav'.block_navigation::$navcount);
138 $url = $this->page->url;
139 $url->remove_params(array('undock'));
141 } else if (!$this->docked && optional_param('dock', null, PARAM_INT)==$this->instance->id) {
142 set_user_preferences(array('nav_in_tab_panel_globalnav'.block_navigation::$navcount=>1));
143 $url = $this->page->url;
144 $url->remove_params(array('dock'));
148 $trimmode = self::TRIM_LEFT;
151 if (!empty($this->config->trimmode)) {
152 $trimmode = (int)$this->config->trimmode;
155 if (!empty($this->config->trimlength)) {
156 $trimlength = (int)$this->config->trimlength;
159 // Initialise (only actually happens if it hasn't already been done yet
160 $this->page->navigation->initialise();
161 $navigation = clone($this->page->navigation);
162 $expansionlimit = null;
163 if (!empty($this->config->expansionlimit)) {
164 $expansionlimit = $this->config->expansionlimit;
165 $navigation->set_expansion_limit($this->config->expansionlimit);
167 $this->trim($navigation, $trimmode, $trimlength, ceil($trimlength/2));
169 // Get the expandable items so we can pass them to JS
170 $expandable = array();
171 $navigation->find_expandable($expandable);
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]);
180 $this->page->requires->data_for_js('navtreeexpansions'.$this->instance->id, $expandable);
183 $options['linkcategories'] = (!empty($this->config->linkcategories) && $this->config->linkcategories == 'yes');
185 // Grab the items to display
186 $renderer = $this->page->get_renderer('block_navigation');
187 $this->content->text = $renderer->navigation_tree($navigation, $expansionlimit, $options);
189 // Set content generated to true so that we know it has been done
190 $this->contentgenerated = true;
192 return $this->content;
196 * Returns the attributes to set for this block
198 * This function returns an array of HTML attributes for this block including
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
204 * @return array An array of HTML attributes
206 public function html_attributes() {
207 $attributes = parent::html_attributes();
208 if (!empty($this->config->enablehoverexpansion) && $this->config->enablehoverexpansion == 'yes') {
209 $attributes['class'] .= ' block_js_expansion';
215 * Trims the text and shorttext properties of this node and optionally
216 * all of its children.
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
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();
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);
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);
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);
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);
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);
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);
260 if ($recurse && $node->children->count()) {
261 foreach ($node->children as &$child) {
262 $this->trim($child, $mode, $long, $short, true, $textlib);
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
273 protected function trim_left($textlib, $string, $length) {
274 return '...'.$textlib->substr($string, $textlib->strlen($string)-$length);
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
283 protected function trim_right($textlib, $string, $length) {
284 return $textlib->substr($string, 0, $length).'...';
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
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;