2 // This file is part of Moodle - http://moodle.org/
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.
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/>.
18 * Outputs the navigation tree.
21 * @package block_navigation
22 * @copyright 2009 Sam Hemelryk
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
27 * Renderer for block navigation
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
34 class block_navigation_renderer
extends plugin_renderer_base
{
37 * Returns the content of the navigation tree.
39 * @param global_navigation $navigation
40 * @param int $expansionlimit
41 * @param array $options
42 * @return string $content
44 public function navigation_tree(global_navigation
$navigation, $expansionlimit, array $options = array()) {
45 $navigation->add_class('navigation_node');
46 $navigationattrs = array(
47 'class' => 'block_tree list',
49 'data-ajax-loader' => 'block_navigation/nav_loader');
50 $content = $this->navigation_node(array($navigation), $navigationattrs, $expansionlimit, $options);
51 if (isset($navigation->id
) && !is_numeric($navigation->id
) && !empty($content)) {
52 $content = $this->output
->box($content, 'block_tree_box', $navigation->id
);
57 * Produces a navigation node for the navigation tree
59 * @param navigation_node[] $items
61 * @param int $expansionlimit
62 * @param array $options
66 protected function navigation_node($items, $attrs=array(), $expansionlimit=null, array $options = array(), $depth=1) {
67 // Exit if empty, we don't want an empty ul element.
68 if (count($items) === 0) {
72 // Turn our navigation items into list items.
74 // Set the number to be static for unique id's.
76 foreach ($items as $item) {
78 if (!$item->display
&& !$item->contains_active_node()) {
82 $isexpandable = (empty($expansionlimit) ||
($item->type
> navigation_node
::TYPE_ACTIVITY ||
$item->type
< $expansionlimit) ||
($item->contains_active_node() && $item->children
->count() > 0));
84 // Skip elements which have no content and no action - no point in showing them
85 if (!$isexpandable && empty($item->action
)) {
89 $id = $item->id ?
$item->id
: html_writer
::random_id();
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
)));
97 $hasicon = ((!$isbranch ||
$item->type
== navigation_node
::TYPE_ACTIVITY ||
$item->type
== navigation_node
::TYPE_RESOURCE
) && $item->icon
instanceof renderable
);
101 $liattr['class'][] = 'item_with_icon';
102 $pattr['class'][] = 'hasicon';
103 $icon = $this->output
->render($item->icon
);
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');
108 if ($item->helpbutton
!== null) {
109 $content = trim($item->helpbutton
).html_writer
::tag('span', $content, array('class'=>'clearhelpbutton'));
111 if (empty($content)) {
115 $nodetextid = 'label_' . $depth . '_' . $number;
116 $attributes = array('tabindex' => '-1', 'id' => $nodetextid);
118 $attributes['title'] = $title;
121 $attributes['class'] = 'dimmed_text';
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']))) {
126 $content = html_writer
::tag('span', $content, $attributes);
127 } else if ($item->action
instanceof action_link
) {
128 //TODO: to be replaced with something else
129 $link = $item->action
;
130 $link->text
= $icon.html_writer
::span($link->text
, 'item-content-wrap');
131 $link->attributes
= array_merge($link->attributes
, $attributes);
132 $content = $this->output
->render($link);
133 } else if ($item->action
instanceof moodle_url
) {
134 $content = html_writer
::link($item->action
, $content, $attributes);
138 $pattr['class'][] = 'branch';
139 $liattr['class'][] = 'contains_branch';
140 $pattr +
= ['aria-expanded' => ($item->has_children() && (!$item->forceopen ||
$item->collapse
)) ?
"false" : "true"];
141 if ($item->requiresajaxloading
) {
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
150 $pattr +
= ['aria-owns' => $id . '_group'];
154 if ($item->isactive
=== true) {
155 $liattr['class'][] = 'current_branch';
157 if (!empty($item->classes
) && count($item->classes
)>0) {
158 $pattr['class'] = array_merge($pattr['class'], $item->classes
);
161 $liattr['class'] = join(' ', $liattr['class']);
162 $pattr['class'] = join(' ', $pattr['class']);
164 $pattr +
= $depth == 1 ?
['data-collapsible' => 'false'] : [];
165 if (isset($pattr['aria-expanded']) && $pattr['aria-expanded'] === 'false') {
166 $ulattr +
= ['aria-hidden' => 'true'];
169 // Create the structure.
170 $content = html_writer
::tag('p', $content, $pattr);
172 $content .= $this->navigation_node($item->children
, $ulattr, $expansionlimit, $options, $depth +
1);
174 if (!empty($item->preceedwithhr
) && $item->preceedwithhr
===true) {
175 $content = html_writer
::empty_tag('hr') . $content;
178 $liattr['aria-labelledby'] = $nodetextid;
179 $content = html_writer
::tag('li', $content, $liattr);
183 if (count($lis) === 0) {
184 // There is still a chance, despite having items, that nothing had content and no list items were created.
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);