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