MDL-54977 blocks_navigation: Use html_writer::random_id for id
[moodle.git] / blocks / navigation / renderer.php
CommitLineData
3406acde 1<?php
31fde54f
AG
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/>.
3406acde 16
31fde54f
AG
17/**
18 * Outputs the navigation tree.
19 *
5bcfd504 20 * @since Moodle 2.0
31fde54f
AG
21 * @package block_navigation
22 * @copyright 2009 Sam Hemelryk
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 */
25
26/**
27 * Renderer for block navigation
28 *
29 * @package block_navigation
30 * @category navigation
31 * @copyright 2009 Sam Hemelryk
32 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
33 */
3406acde 34class block_navigation_renderer extends plugin_renderer_base {
0043daf4 35
31fde54f
AG
36 /**
37 * Returns the content of the navigation tree.
38 *
39 * @param global_navigation $navigation
40 * @param int $expansionlimit
41 * @param array $options
42 * @return string $content
43 */
480f906e 44 public function navigation_tree(global_navigation $navigation, $expansionlimit, array $options = array()) {
d2401694 45 $navigation->add_class('navigation_node');
f8895446
JO
46 $navigationattrs = array(
47 'class' => 'block_tree list',
48 'role' => 'tree',
49 'data-ajax-loader' => 'block_navigation/nav_loader');
50 $content = $this->navigation_node(array($navigation), $navigationattrs, $expansionlimit, $options);
3406acde
SH
51 if (isset($navigation->id) && !is_numeric($navigation->id) && !empty($content)) {
52 $content = $this->output->box($content, 'block_tree_box', $navigation->id);
53 }
54 return $content;
55 }
31fde54f
AG
56 /**
57 * Produces a navigation node for the navigation tree
58 *
892e21aa 59 * @param navigation_node[] $items
31fde54f
AG
60 * @param array $attrs
61 * @param int $expansionlimit
62 * @param array $options
63 * @param int $depth
64 * @return string
65 */
480f906e 66 protected function navigation_node($items, $attrs=array(), $expansionlimit=null, array $options = array(), $depth=1) {
892e21aa
SH
67 // Exit if empty, we don't want an empty ul element.
68 if (count($items) === 0) {
3406acde
SH
69 return '';
70 }
71
892e21aa 72 // Turn our navigation items into list items.
3406acde 73 $lis = array();
7e341eb4
RT
74 // Set the number to be static for unique id's.
75 static $number = 0;
3406acde 76 foreach ($items as $item) {
f8895446 77 $number++;
8e5c23e0 78 if (!$item->display && !$item->contains_active_node()) {
3406acde
SH
79 continue;
80 }
8e5c23e0
SH
81
82 $isexpandable = (empty($expansionlimit) || ($item->type > navigation_node::TYPE_ACTIVITY || $item->type < $expansionlimit) || ($item->contains_active_node() && $item->children->count() > 0));
8e5c23e0 83
65dcd906
ARN
84 // Skip elements which have no content and no action - no point in showing them
85 if (!$isexpandable && empty($item->action)) {
86 continue;
87 }
88
560cea83 89 $id = $item->id ? $item->id : html_writer::random_id();
0043daf4
CB
90 $content = $item->get_content();
91 $title = $item->get_title();
92 $ulattr = ['id' => $id . '_group', 'role' => 'group'];
93 $liattr = ['class' => [$item->get_css_type(), 'depth_'.$depth]];
94 $pattr = ['class' => ['tree_item'], 'role' => 'treeitem'];
95 $pattr += !empty($item->id) ? ['id' => $item->id] : [];
96 $isbranch = $isexpandable && ($item->children->count() > 0 || ($item->has_children() && (isloggedin() || $item->type <= navigation_node::TYPE_CATEGORY)));
13915f89 97 $hasicon = ((!$isbranch || $item->type == navigation_node::TYPE_ACTIVITY || $item->type == navigation_node::TYPE_RESOURCE) && $item->icon instanceof renderable);
0043daf4 98 $icon = '';
7081714d
SH
99
100 if ($hasicon) {
0043daf4
CB
101 $liattr['class'][] = 'item_with_icon';
102 $pattr['class'][] = 'hasicon';
3406acde 103 $icon = $this->output->render($item->icon);
892e21aa
SH
104 // Because an icon is being used we're going to wrap the actual content in a span.
105 // This will allow designers to create columns for the content, as we've done in styles.css.
106 $content = $icon . html_writer::span($content, 'item-content-wrap');
3406acde
SH
107 }
108 if ($item->helpbutton !== null) {
109 $content = trim($item->helpbutton).html_writer::tag('span', $content, array('class'=>'clearhelpbutton'));
110 }
0043daf4 111 if (empty($content)) {
3406acde
SH
112 continue;
113 }
114
f8895446
JO
115 $nodetextid = 'label_' . $depth . '_' . $number;
116 $attributes = array('tabindex' => '-1', 'id' => $nodetextid);
480f906e
SH
117 if ($title !== '') {
118 $attributes['title'] = $title;
119 }
120 if ($item->hidden) {
121 $attributes['class'] = 'dimmed_text';
122 }
3b42c973
MG
123 if (is_string($item->action) || empty($item->action) ||
124 (($item->type === navigation_node::TYPE_CATEGORY || $item->type === navigation_node::TYPE_MY_CATEGORY) &&
125 empty($options['linkcategories']))) {
480f906e
SH
126 $content = html_writer::tag('span', $content, $attributes);
127 } else if ($item->action instanceof action_link) {
3406acde
SH
128 //TODO: to be replaced with something else
129 $link = $item->action;
483fb4ec 130 $link->text = $icon.html_writer::span($link->text, 'item-content-wrap');
480f906e 131 $link->attributes = array_merge($link->attributes, $attributes);
3406acde
SH
132 $content = $this->output->render($link);
133 } else if ($item->action instanceof moodle_url) {
3406acde 134 $content = html_writer::link($item->action, $content, $attributes);
3406acde
SH
135 }
136
7081714d 137 if ($isbranch) {
0043daf4
CB
138 $pattr['class'][] = 'branch';
139 $liattr['class'][] = 'contains_branch';
140 $pattr += ['aria-expanded' => ($item->has_children() && (!$item->forceopen || $item->collapse)) ? "false" : "true"];
f8895446 141 if ($item->requiresajaxloading) {
0043daf4
CB
142 $pattr += [
143 'data-requires-ajax' => 'true',
144 'data-loaded' => 'false',
145 'data-node-id' => $item->id,
146 'data-node-key' => $item->key,
147 'data-node-type' => $item->type
148 ];
149 } else {
150 $pattr += ['aria-owns' => $id . '_group'];
f8895446 151 }
7081714d 152 }
0043daf4 153
892e21aa 154 if ($item->isactive === true) {
0043daf4 155 $liattr['class'][] = 'current_branch';
892e21aa 156 }
3406acde 157 if (!empty($item->classes) && count($item->classes)>0) {
0043daf4 158 $pattr['class'] = array_merge($pattr['class'], $item->classes);
3406acde 159 }
892e21aa 160
0043daf4
CB
161 $liattr['class'] = join(' ', $liattr['class']);
162 $pattr['class'] = join(' ', $pattr['class']);
163
164 $pattr += $depth == 1 ? ['data-collapsible' => 'false'] : [];
165 if (isset($pattr['aria-expanded']) && $pattr['aria-expanded'] === 'false') {
166 $ulattr += ['aria-hidden' => 'true'];
3406acde 167 }
892e21aa
SH
168
169 // Create the structure.
0043daf4 170 $content = html_writer::tag('p', $content, $pattr);
8e5c23e0 171 if ($isexpandable) {
0043daf4 172 $content .= $this->navigation_node($item->children, $ulattr, $expansionlimit, $options, $depth + 1);
8e5c23e0 173 }
3406acde
SH
174 if (!empty($item->preceedwithhr) && $item->preceedwithhr===true) {
175 $content = html_writer::empty_tag('hr') . $content;
176 }
0043daf4 177
f8895446 178 $liattr['aria-labelledby'] = $nodetextid;
3406acde
SH
179 $content = html_writer::tag('li', $content, $liattr);
180 $lis[] = $content;
181 }
6c721bbf 182
892e21aa
SH
183 if (count($lis) === 0) {
184 // There is still a chance, despite having items, that nothing had content and no list items were created.
6c721bbf
SH
185 return '';
186 }
892e21aa
SH
187
188 // We used to separate using new lines, however we don't do that now, instead we'll save a few chars.
189 // The source is complex already anyway.
190 return html_writer::tag('ul', implode('', $lis), $attrs);
3406acde 191 }
5dbfbacc 192}