Commit | Line | Data |
---|---|---|
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 | 34 | class block_navigation_renderer extends plugin_renderer_base { |
31fde54f AG |
35 | /** |
36 | * Returns the content of the navigation tree. | |
37 | * | |
38 | * @param global_navigation $navigation | |
39 | * @param int $expansionlimit | |
40 | * @param array $options | |
41 | * @return string $content | |
42 | */ | |
480f906e | 43 | public function navigation_tree(global_navigation $navigation, $expansionlimit, array $options = array()) { |
d2401694 | 44 | $navigation->add_class('navigation_node'); |
f8895446 JO |
45 | $navigationattrs = array( |
46 | 'class' => 'block_tree list', | |
47 | 'role' => 'tree', | |
48 | 'data-ajax-loader' => 'block_navigation/nav_loader'); | |
49 | $content = $this->navigation_node(array($navigation), $navigationattrs, $expansionlimit, $options); | |
3406acde SH |
50 | if (isset($navigation->id) && !is_numeric($navigation->id) && !empty($content)) { |
51 | $content = $this->output->box($content, 'block_tree_box', $navigation->id); | |
52 | } | |
53 | return $content; | |
54 | } | |
31fde54f AG |
55 | /** |
56 | * Produces a navigation node for the navigation tree | |
57 | * | |
892e21aa | 58 | * @param navigation_node[] $items |
31fde54f AG |
59 | * @param array $attrs |
60 | * @param int $expansionlimit | |
61 | * @param array $options | |
62 | * @param int $depth | |
63 | * @return string | |
64 | */ | |
480f906e | 65 | protected function navigation_node($items, $attrs=array(), $expansionlimit=null, array $options = array(), $depth=1) { |
892e21aa SH |
66 | // Exit if empty, we don't want an empty ul element. |
67 | if (count($items) === 0) { | |
3406acde SH |
68 | return ''; |
69 | } | |
70 | ||
892e21aa | 71 | // Turn our navigation items into list items. |
3406acde | 72 | $lis = array(); |
7e341eb4 RT |
73 | // Set the number to be static for unique id's. |
74 | static $number = 0; | |
3406acde | 75 | foreach ($items as $item) { |
f8895446 | 76 | $number++; |
8e5c23e0 | 77 | if (!$item->display && !$item->contains_active_node()) { |
3406acde SH |
78 | continue; |
79 | } | |
80 | $content = $item->get_content(); | |
81 | $title = $item->get_title(); | |
8e5c23e0 SH |
82 | |
83 | $isexpandable = (empty($expansionlimit) || ($item->type > navigation_node::TYPE_ACTIVITY || $item->type < $expansionlimit) || ($item->contains_active_node() && $item->children->count() > 0)); | |
84 | $isbranch = $isexpandable && ($item->children->count() > 0 || ($item->has_children() && (isloggedin() || $item->type <= navigation_node::TYPE_CATEGORY))); | |
85 | ||
65dcd906 ARN |
86 | // Skip elements which have no content and no action - no point in showing them |
87 | if (!$isexpandable && empty($item->action)) { | |
88 | continue; | |
89 | } | |
90 | ||
13915f89 | 91 | $hasicon = ((!$isbranch || $item->type == navigation_node::TYPE_ACTIVITY || $item->type == navigation_node::TYPE_RESOURCE) && $item->icon instanceof renderable); |
7081714d SH |
92 | |
93 | if ($hasicon) { | |
3406acde | 94 | $icon = $this->output->render($item->icon); |
892e21aa SH |
95 | // Because an icon is being used we're going to wrap the actual content in a span. |
96 | // This will allow designers to create columns for the content, as we've done in styles.css. | |
97 | $content = $icon . html_writer::span($content, 'item-content-wrap'); | |
13915f89 SH |
98 | } else { |
99 | $icon = ''; | |
3406acde | 100 | } |
892e21aa | 101 | |
3406acde SH |
102 | if ($item->helpbutton !== null) { |
103 | $content = trim($item->helpbutton).html_writer::tag('span', $content, array('class'=>'clearhelpbutton')); | |
104 | } | |
105 | ||
106 | if ($content === '') { | |
107 | continue; | |
108 | } | |
109 | ||
f8895446 JO |
110 | $nodetextid = 'label_' . $depth . '_' . $number; |
111 | $attributes = array('tabindex' => '-1', 'id' => $nodetextid); | |
480f906e SH |
112 | if ($title !== '') { |
113 | $attributes['title'] = $title; | |
114 | } | |
115 | if ($item->hidden) { | |
116 | $attributes['class'] = 'dimmed_text'; | |
117 | } | |
3b42c973 MG |
118 | if (is_string($item->action) || empty($item->action) || |
119 | (($item->type === navigation_node::TYPE_CATEGORY || $item->type === navigation_node::TYPE_MY_CATEGORY) && | |
120 | empty($options['linkcategories']))) { | |
480f906e SH |
121 | $content = html_writer::tag('span', $content, $attributes); |
122 | } else if ($item->action instanceof action_link) { | |
3406acde SH |
123 | //TODO: to be replaced with something else |
124 | $link = $item->action; | |
483fb4ec | 125 | $link->text = $icon.html_writer::span($link->text, 'item-content-wrap'); |
480f906e | 126 | $link->attributes = array_merge($link->attributes, $attributes); |
3406acde SH |
127 | $content = $this->output->render($link); |
128 | } else if ($item->action instanceof moodle_url) { | |
3406acde | 129 | $content = html_writer::link($item->action, $content, $attributes); |
3406acde SH |
130 | } |
131 | ||
892e21aa | 132 | // This applies to the li item which contains all child lists too. |
d2401694 | 133 | $liclasses = array($item->get_css_type(), 'depth_'.$depth); |
892e21aa SH |
134 | |
135 | // Class attribute on the div item which only contains the item content. | |
136 | $divclasses = array('tree_item'); | |
137 | ||
e5c46b54 | 138 | $liexpandable = array(); |
f8895446 | 139 | $lirole = array('role' => 'treeitem'); |
7081714d SH |
140 | if ($isbranch) { |
141 | $liclasses[] = 'contains_branch'; | |
f8895446 JO |
142 | if ($depth == 1) { |
143 | $liexpandable = array( | |
5697de18 RW |
144 | 'data-expandable' => 'false', |
145 | 'data-collapsible' => 'false' | |
f8895446 JO |
146 | ); |
147 | } else { | |
148 | $liexpandable = array( | |
149 | 'aria-expanded' => ($item->has_children() && | |
150 | (!$item->forceopen || $item->collapse)) ? "false" : "true"); | |
151 | } | |
152 | ||
153 | if ($item->requiresajaxloading) { | |
154 | $liexpandable['data-requires-ajax'] = 'true'; | |
155 | $liexpandable['data-loaded'] = 'false'; | |
156 | $liexpandable['data-node-id'] = $item->id; | |
157 | $liexpandable['data-node-key'] = $item->key; | |
158 | $liexpandable['data-node-type'] = $item->type; | |
159 | } | |
160 | ||
3406acde SH |
161 | $divclasses[] = 'branch'; |
162 | } else { | |
163 | $divclasses[] = 'leaf'; | |
164 | } | |
7081714d | 165 | if ($hasicon) { |
892e21aa SH |
166 | // Add this class if the item has an icon, whether it is a branch or not. |
167 | $liclasses[] = 'item_with_icon'; | |
7081714d SH |
168 | $divclasses[] = 'hasicon'; |
169 | } | |
892e21aa SH |
170 | if ($item->isactive === true) { |
171 | $liclasses[] = 'current_branch'; | |
172 | } | |
3406acde SH |
173 | if (!empty($item->classes) && count($item->classes)>0) { |
174 | $divclasses[] = join(' ', $item->classes); | |
175 | } | |
892e21aa SH |
176 | |
177 | // Now build attribute arrays. | |
f8895446 | 178 | $liattr = array('class' => join(' ', $liclasses)) + $liexpandable + $lirole; |
3406acde SH |
179 | $divattr = array('class'=>join(' ', $divclasses)); |
180 | if (!empty($item->id)) { | |
181 | $divattr['id'] = $item->id; | |
182 | } | |
892e21aa SH |
183 | |
184 | // Create the structure. | |
8e5c23e0 SH |
185 | $content = html_writer::tag('p', $content, $divattr); |
186 | if ($isexpandable) { | |
6759dc35 SL |
187 | $content .= $this->navigation_node($item->children, array('role' => 'group'), $expansionlimit, |
188 | $options, $depth + 1); | |
8e5c23e0 | 189 | } |
3406acde SH |
190 | if (!empty($item->preceedwithhr) && $item->preceedwithhr===true) { |
191 | $content = html_writer::empty_tag('hr') . $content; | |
192 | } | |
f8895446 JO |
193 | if ($depth == 1) { |
194 | $liattr['tabindex'] = '0'; | |
195 | } | |
196 | $liattr['aria-labelledby'] = $nodetextid; | |
3406acde SH |
197 | $content = html_writer::tag('li', $content, $liattr); |
198 | $lis[] = $content; | |
199 | } | |
6c721bbf | 200 | |
892e21aa SH |
201 | if (count($lis) === 0) { |
202 | // There is still a chance, despite having items, that nothing had content and no list items were created. | |
6c721bbf SH |
203 | return ''; |
204 | } | |
892e21aa SH |
205 | |
206 | // We used to separate using new lines, however we don't do that now, instead we'll save a few chars. | |
207 | // The source is complex already anyway. | |
208 | return html_writer::tag('ul', implode('', $lis), $attrs); | |
3406acde SH |
209 | } |
210 | ||
5dbfbacc | 211 | } |