Merge branch 'MDL-78684-402' of https://github.com/andelacruz/moodle into MOODLE_402_...
[moodle.git] / blocks / navigation / renderer.php
blob4583a12722aa56f274a3482637c5f4077303abc2
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 * Outputs the navigation tree.
20 * @since Moodle 2.0
21 * @package block_navigation
22 * @copyright 2009 Sam Hemelryk
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 /**
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 {
36 /**
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',
48 'role' => 'tree',
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);
54 return $content;
56 /**
57 * Produces a navigation node for the navigation tree
59 * @param navigation_node[] $items
60 * @param array $attrs
61 * @param int $expansionlimit
62 * @param array $options
63 * @param int $depth
64 * @return string
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) {
69 return '';
72 // Turn our navigation items into list items.
73 $lis = array();
74 // Set the number to be static for unique id's.
75 static $number = 0;
76 $htmlidprefix = html_writer::random_id();
77 foreach ($items as $item) {
78 $number++;
79 if (!$item->display && !$item->contains_active_node()) {
80 continue;
83 $isexpandable = (empty($expansionlimit) || ($item->type > navigation_node::TYPE_ACTIVITY || $item->type < $expansionlimit) || ($item->contains_active_node() && $item->children->count() > 0));
85 // Skip elements which have no content and no action - no point in showing them
86 if (!$isexpandable && empty($item->action)) {
87 continue;
90 $id = $item->id ? $item->id : html_writer::random_id();
91 $content = $item->get_content();
92 $title = $item->get_title();
93 $ulattr = ['id' => $id . '_group', 'role' => 'group'];
94 $liattr = ['class' => [$item->get_css_type(), 'depth_'.$depth], 'role' => 'treeitem'];
95 $pattr = ['class' => ['tree_item']];
96 $pattr += !empty($item->id) ? ['id' => $item->id] : [];
97 $isbranch = $isexpandable && ($item->children->count() > 0 || ($item->has_children() && (isloggedin() || $item->type <= navigation_node::TYPE_CATEGORY)));
98 $hasicon = ((!$isbranch || $item->type == navigation_node::TYPE_ACTIVITY || $item->type == navigation_node::TYPE_RESOURCE) && $item->icon instanceof renderable);
99 $icon = '';
101 if ($hasicon) {
102 $liattr['class'][] = 'item_with_icon';
103 $pattr['class'][] = 'hasicon';
104 $icon = $this->output->render($item->icon);
105 // Because an icon is being used we're going to wrap the actual content in a span.
106 // This will allow designers to create columns for the content, as we've done in styles.css.
107 $content = $icon . html_writer::span($content, 'item-content-wrap');
109 if ($item->helpbutton !== null) {
110 $content = trim($item->helpbutton).html_writer::tag('span', $content, array('class'=>'clearhelpbutton'));
112 if (empty($content)) {
113 continue;
116 $nodetextid = $htmlidprefix . '_label_' . $depth . '_' . $number;
117 $attributes = array('tabindex' => '-1', 'id' => $nodetextid);
118 if ($title !== '') {
119 $attributes['title'] = $title;
121 if ($item->hidden) {
122 $attributes['class'] = 'dimmed_text';
124 if (is_string($item->action) || empty($item->action) ||
125 (($item->type === navigation_node::TYPE_CATEGORY || $item->type === navigation_node::TYPE_MY_CATEGORY) &&
126 empty($options['linkcategories']))) {
127 $content = html_writer::tag('span', $content, $attributes);
128 } else if ($item->action instanceof action_link) {
129 //TODO: to be replaced with something else
130 $link = $item->action;
131 $link->text = $icon.html_writer::span($link->text, 'item-content-wrap');
132 $link->attributes = array_merge($link->attributes, $attributes);
133 $content = $this->output->render($link);
134 } else if ($item->action instanceof moodle_url) {
135 $content = html_writer::link($item->action, $content, $attributes);
138 if ($isbranch) {
139 $ariaexpanded = $item->has_children() && (!$item->forceopen || $item->collapse);
140 $pattr['class'][] = 'branch';
141 $liattr['class'][] = 'contains_branch';
142 $liattr += ['aria-expanded' => $ariaexpanded ? "false" : "true"];
143 if ($item->requiresajaxloading) {
144 $liattr += [
145 'data-requires-ajax' => 'true',
146 'data-loaded' => 'false',
147 'data-node-id' => $item->id,
148 'data-node-key' => $item->key,
149 'data-node-type' => $item->type
151 } else {
152 $liattr += ['aria-owns' => $id . '_group'];
156 if ($item->isactive === true) {
157 $liattr['class'][] = 'current_branch';
159 if (!empty($item->classes) && count($item->classes)>0) {
160 $pattr['class'] = array_merge($pattr['class'], $item->classes);
163 $liattr['class'] = join(' ', $liattr['class']);
164 $pattr['class'] = join(' ', $pattr['class']);
166 $liattr += $depth == 1 ? ['data-collapsible' => 'false'] : [];
167 if (isset($liattr['aria-expanded']) && $liattr['aria-expanded'] === 'false') {
168 $ulattr += ['aria-hidden' => 'true'];
171 // Create the structure.
172 $content = html_writer::tag('p', $content, $pattr);
173 if ($isexpandable) {
174 $content .= $this->navigation_node($item->children, $ulattr, $expansionlimit, $options, $depth + 1);
176 if (!empty($item->preceedwithhr) && $item->preceedwithhr===true) {
177 $content = html_writer::empty_tag('hr') . $content;
180 $liattr['aria-labelledby'] = $nodetextid;
181 $content = html_writer::tag('li', $content, $liattr);
182 $lis[] = $content;
185 if (count($lis) === 0) {
186 // There is still a chance, despite having items, that nothing had content and no list items were created.
187 return '';
190 // We used to separate using new lines, however we don't do that now, instead we'll save a few chars.
191 // The source is complex already anyway.
192 return html_writer::tag('ul', implode('', $lis), $attrs);