MDL-65809 environment: mariadb requirement up to 10.2
[moodle.git] / blocks / navigation / block_navigation.php
blobe7a5c11518c64ddfec120725348ee673fc00c66d
1 <?php
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.
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/>.
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
21 * @since Moodle 2.0
22 * @package block_navigation
23 * @copyright 2009 Sam Hemelryk
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
27 /**
28 * The global navigation tree block class
30 * Used to produce the global navigation block new to Moodle 2.0
32 * @package block_navigation
33 * @category navigation
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 {
39 /** @var int This allows for multiple navigation trees */
40 public static $navcount;
41 /** @var string The name of the block */
42 public $blockname = null;
43 /** @var bool A switch to indicate whether content has been generated or not. */
44 protected $contentgenerated = false;
45 /** @var bool|null variable for checking if the block is docked*/
46 protected $docked = null;
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;
55 /**
56 * Set the initial properties for the block
58 function init() {
59 $this->blockname = get_class($this);
60 $this->title = get_string('pluginname', $this->blockname);
63 /**
64 * All multiple instances of this block
65 * @return bool Returns false
67 function instance_allow_multiple() {
68 return false;
71 /**
72 * Set the applicable formats for this block to all
73 * @return array
75 function applicable_formats() {
76 return array('all' => true);
79 /**
80 * Allow the user to configure a block instance
81 * @return bool Returns true
83 function instance_allow_config() {
84 return true;
87 /**
88 * The navigation block cannot be hidden by default as it is integral to
89 * the navigation of Moodle.
91 * @return false
93 function instance_can_be_hidden() {
94 return false;
97 /**
98 * Find out if an instance can be docked.
100 * @return bool true or false depending on whether the instance can be docked or not.
102 function instance_can_be_docked() {
103 return (parent::instance_can_be_docked() && (empty($this->config->enabledock) || $this->config->enabledock=='yes'));
107 * Gets Javascript that may be required for navigation
109 function get_required_javascript() {
110 parent::get_required_javascript();
111 $arguments = array(
112 'instanceid' => $this->instance->id
114 $this->page->requires->string_for_js('viewallcourses', 'moodle');
115 $this->page->requires->js_call_amd('block_navigation/navblock', 'init', $arguments);
119 * Gets the content for this block by grabbing it from $this->page
121 * @return object $this->content
123 function get_content() {
124 global $CFG;
125 // First check if we have already generated, don't waste cycles
126 if ($this->contentgenerated === true) {
127 return $this->content;
129 // JS for navigation moved to the standard theme, the code will probably have to depend on the actual page structure
130 // $this->page->requires->js('/lib/javascript-navigation.js');
131 // Navcount is used to allow us to have multiple trees although I dont' know why
132 // you would want two trees the same
134 block_navigation::$navcount++;
136 // Check if this block has been docked
137 if ($this->docked === null) {
138 $this->docked = get_user_preferences('nav_in_tab_panel_globalnav'.block_navigation::$navcount, 0);
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) {
143 unset_user_preference('nav_in_tab_panel_globalnav'.block_navigation::$navcount);
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) {
148 set_user_preferences(array('nav_in_tab_panel_globalnav'.block_navigation::$navcount=>1));
149 $url = $this->page->url;
150 $url->remove_params(array('dock'));
151 redirect($url);
154 $trimmode = self::TRIM_RIGHT;
155 $trimlength = 50;
157 if (!empty($this->config->trimmode)) {
158 $trimmode = (int)$this->config->trimmode;
161 if (!empty($this->config->trimlength)) {
162 $trimlength = (int)$this->config->trimlength;
165 // Get the navigation object or don't display the block if none provided.
166 if (!$navigation = $this->get_navigation()) {
167 return null;
169 $expansionlimit = null;
170 if (!empty($this->config->expansionlimit)) {
171 $expansionlimit = $this->config->expansionlimit;
172 $navigation->set_expansion_limit($this->config->expansionlimit);
174 $this->trim($navigation, $trimmode, $trimlength, ceil($trimlength/2));
176 // Get the expandable items so we can pass them to JS
177 $expandable = array();
178 $navigation->find_expandable($expandable);
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]);
187 $limit = 20;
188 if (!empty($CFG->navcourselimit)) {
189 $limit = $CFG->navcourselimit;
191 $expansionlimit = 0;
192 if (!empty($this->config->expansionlimit)) {
193 $expansionlimit = $this->config->expansionlimit;
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
203 $options = array();
204 $options['linkcategories'] = (!empty($this->config->linkcategories) && $this->config->linkcategories == 'yes');
206 // Grab the items to display
207 $renderer = $this->page->get_renderer($this->blockname);
208 $this->content = new stdClass();
209 $this->content->text = $renderer->navigation_tree($navigation, $expansionlimit, $options);
211 // Set content generated to true so that we know it has been done
212 $this->contentgenerated = true;
214 return $this->content;
218 * Returns the navigation
220 * @return navigation_node The navigation object to display
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);
229 * Returns the attributes to set for this block
231 * This function returns an array of HTML attributes for this block including
232 * the defaults.
233 * {@link block_tree::html_attributes()} is used to get the default arguments
234 * and then we check whether the user has enabled hover expansion and add the
235 * appropriate hover class if it has.
237 * @return array An array of HTML attributes
239 public function html_attributes() {
240 $attributes = parent::html_attributes();
241 if (!empty($this->config->enablehoverexpansion) && $this->config->enablehoverexpansion == 'yes') {
242 $attributes['class'] .= ' block_js_expansion';
244 return $attributes;
248 * Trims the text and shorttext properties of this node and optionally
249 * all of its children.
251 * @param navigation_node $node
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
257 public function trim(navigation_node $node, $mode=1, $long=50, $short=25, $recurse=true) {
258 switch ($mode) {
259 case self::TRIM_RIGHT :
260 if (core_text::strlen($node->text)>($long+3)) {
261 // Truncate the text to $long characters
262 $node->text = $this->trim_right($node->text, $long);
264 if (is_string($node->shorttext) && core_text::strlen($node->shorttext)>($short+3)) {
265 // Truncate the shorttext
266 $node->shorttext = $this->trim_right($node->shorttext, $short);
268 break;
269 case self::TRIM_LEFT :
270 if (core_text::strlen($node->text)>($long+3)) {
271 // Truncate the text to $long characters
272 $node->text = $this->trim_left($node->text, $long);
274 if (is_string($node->shorttext) && core_text::strlen($node->shorttext)>($short+3)) {
275 // Truncate the shorttext
276 $node->shorttext = $this->trim_left($node->shorttext, $short);
278 break;
279 case self::TRIM_CENTER :
280 if (core_text::strlen($node->text)>($long+3)) {
281 // Truncate the text to $long characters
282 $node->text = $this->trim_center($node->text, $long);
284 if (is_string($node->shorttext) && core_text::strlen($node->shorttext)>($short+3)) {
285 // Truncate the shorttext
286 $node->shorttext = $this->trim_center($node->shorttext, $short);
288 break;
290 if ($recurse && $node->children->count()) {
291 foreach ($node->children as &$child) {
292 $this->trim($child, $mode, $long, $short, true);
297 * Truncate a string from the left
298 * @param string $string The string to truncate
299 * @param int $length The length to truncate to
300 * @return string The truncated string
302 protected function trim_left($string, $length) {
303 return '...'.core_text::substr($string, core_text::strlen($string)-$length, $length);
306 * Truncate a string from the right
307 * @param string $string The string to truncate
308 * @param int $length The length to truncate to
309 * @return string The truncated string
311 protected function trim_right($string, $length) {
312 return core_text::substr($string, 0, $length).'...';
315 * Truncate a string in the center
316 * @param string $string The string to truncate
317 * @param int $length The length to truncate to
318 * @return string The truncated string
320 protected function trim_center($string, $length) {
321 $trimlength = ceil($length/2);
322 $start = core_text::substr($string, 0, $trimlength);
323 $end = core_text::substr($string, core_text::strlen($string)-$trimlength);
324 $string = $start.'...'.$end;
325 return $string;
329 * Returns the role that best describes the navigation block... 'navigation'
331 * @return string 'navigation'
333 public function get_aria_role() {
334 return 'navigation';