1 |
efrain |
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.
|
|
|
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/>.
|
|
|
16 |
|
|
|
17 |
namespace theme_boost;
|
|
|
18 |
|
|
|
19 |
use core\navigation\views\view;
|
|
|
20 |
use navigation_node;
|
|
|
21 |
use moodle_url;
|
|
|
22 |
use action_link;
|
|
|
23 |
use lang_string;
|
|
|
24 |
|
|
|
25 |
/**
|
|
|
26 |
* Creates a navbar for boost that allows easy control of the navbar items.
|
|
|
27 |
*
|
|
|
28 |
* @package theme_boost
|
|
|
29 |
* @copyright 2021 Adrian Greeve <adrian@moodle.com>
|
|
|
30 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
31 |
*/
|
|
|
32 |
class boostnavbar implements \renderable {
|
|
|
33 |
|
|
|
34 |
/** @var array The individual items of the navbar. */
|
|
|
35 |
protected $items = [];
|
|
|
36 |
/** @var moodle_page The current moodle page. */
|
|
|
37 |
protected $page;
|
|
|
38 |
|
|
|
39 |
/**
|
|
|
40 |
* Takes a navbar object and picks the necessary parts for display.
|
|
|
41 |
*
|
|
|
42 |
* @param \moodle_page $page The current moodle page.
|
|
|
43 |
*/
|
|
|
44 |
public function __construct(\moodle_page $page) {
|
|
|
45 |
$this->page = $page;
|
|
|
46 |
foreach ($this->page->navbar->get_items() as $item) {
|
|
|
47 |
$this->items[] = $item;
|
|
|
48 |
}
|
|
|
49 |
$this->prepare_nodes_for_boost();
|
|
|
50 |
}
|
|
|
51 |
|
|
|
52 |
/**
|
|
|
53 |
* Prepares the navigation nodes for use with boost.
|
|
|
54 |
*/
|
|
|
55 |
protected function prepare_nodes_for_boost(): void {
|
|
|
56 |
global $PAGE;
|
|
|
57 |
|
|
|
58 |
// Remove the navbar nodes that already exist in the primary navigation menu.
|
|
|
59 |
$this->remove_items_that_exist_in_navigation($PAGE->primarynav);
|
|
|
60 |
|
|
|
61 |
// Defines whether section items with an action should be removed by default.
|
|
|
62 |
$removesections = true;
|
|
|
63 |
|
|
|
64 |
if ($this->page->context->contextlevel == CONTEXT_COURSECAT) {
|
|
|
65 |
// Remove the 'Permissions' navbar node in the Check permissions page.
|
|
|
66 |
if ($this->page->pagetype === 'admin-roles-check') {
|
|
|
67 |
$this->remove('permissions');
|
|
|
68 |
}
|
|
|
69 |
}
|
|
|
70 |
if ($this->page->context->contextlevel == CONTEXT_COURSE) {
|
|
|
71 |
// Remove any duplicate navbar nodes.
|
|
|
72 |
$this->remove_duplicate_items();
|
|
|
73 |
// Remove 'My courses' and 'Courses' if we are in the course context.
|
|
|
74 |
$this->remove('mycourses');
|
|
|
75 |
$this->remove('courses');
|
|
|
76 |
// Remove the course category breadcrumb nodes.
|
|
|
77 |
foreach ($this->items as $key => $item) {
|
|
|
78 |
// Remove if it is a course category breadcrumb node.
|
|
|
79 |
$this->remove($item->key, \breadcrumb_navigation_node::TYPE_CATEGORY);
|
|
|
80 |
}
|
|
|
81 |
// Remove the course breadcrumb node.
|
|
|
82 |
if (!str_starts_with($this->page->pagetype, 'course-view-section-')) {
|
|
|
83 |
$this->remove($this->page->course->id, \breadcrumb_navigation_node::TYPE_COURSE);
|
|
|
84 |
}
|
|
|
85 |
// Remove the navbar nodes that already exist in the secondary navigation menu.
|
|
|
86 |
$this->remove_items_that_exist_in_navigation($PAGE->secondarynav);
|
|
|
87 |
|
|
|
88 |
switch ($this->page->pagetype) {
|
|
|
89 |
case 'group-groupings':
|
|
|
90 |
case 'group-grouping':
|
|
|
91 |
case 'group-overview':
|
|
|
92 |
case 'group-assign':
|
|
|
93 |
// Remove the 'Groups' navbar node in the Groupings, Grouping, group Overview and Assign pages.
|
|
|
94 |
$this->remove('groups');
|
|
|
95 |
case 'backup-backup':
|
|
|
96 |
case 'backup-restorefile':
|
|
|
97 |
case 'backup-copy':
|
|
|
98 |
case 'course-reset':
|
|
|
99 |
// Remove the 'Import' navbar node in the Backup, Restore, Copy course and Reset pages.
|
|
|
100 |
$this->remove('import');
|
|
|
101 |
case 'course-user':
|
|
|
102 |
$this->remove('mygrades');
|
|
|
103 |
$this->remove('grades');
|
|
|
104 |
}
|
|
|
105 |
}
|
|
|
106 |
|
|
|
107 |
// Remove 'My courses' if we are in the module context.
|
|
|
108 |
if ($this->page->context->contextlevel == CONTEXT_MODULE) {
|
|
|
109 |
$this->remove('mycourses');
|
|
|
110 |
$this->remove('courses');
|
|
|
111 |
// Remove the course category breadcrumb nodes.
|
|
|
112 |
foreach ($this->items as $key => $item) {
|
|
|
113 |
// Remove if it is a course category breadcrumb node.
|
|
|
114 |
$this->remove($item->key, \breadcrumb_navigation_node::TYPE_CATEGORY);
|
|
|
115 |
}
|
|
|
116 |
$courseformat = course_get_format($this->page->course);
|
|
|
117 |
$removesections = $courseformat->can_sections_be_removed_from_navigation();
|
|
|
118 |
if ($removesections) {
|
|
|
119 |
// If the course sections are removed, we need to add the anchor of current section to the Course.
|
|
|
120 |
$coursenode = $this->get_item($this->page->course->id);
|
|
|
121 |
if (!is_null($coursenode) && $this->page->cm->sectionnum !== null) {
|
|
|
122 |
$coursenode->action = course_get_format($this->page->course)->get_view_url($this->page->cm->sectionnum);
|
|
|
123 |
}
|
|
|
124 |
}
|
|
|
125 |
}
|
|
|
126 |
|
|
|
127 |
if ($this->page->context->contextlevel == CONTEXT_SYSTEM) {
|
|
|
128 |
// Remove the navbar nodes that already exist in the secondary navigation menu.
|
|
|
129 |
$this->remove_items_that_exist_in_navigation($PAGE->secondarynav);
|
|
|
130 |
}
|
|
|
131 |
|
|
|
132 |
// Set the designated one path for courses.
|
|
|
133 |
$mycoursesnode = $this->get_item('mycourses');
|
|
|
134 |
if (!is_null($mycoursesnode)) {
|
|
|
135 |
$url = new \moodle_url('/my/courses.php');
|
|
|
136 |
$mycoursesnode->action = $url;
|
|
|
137 |
$mycoursesnode->text = get_string('mycourses');
|
|
|
138 |
}
|
|
|
139 |
|
|
|
140 |
$this->remove_no_link_items($removesections);
|
|
|
141 |
|
|
|
142 |
// Don't display the navbar if there is only one item. Apparently this is bad UX design.
|
|
|
143 |
if ($this->item_count() <= 1) {
|
|
|
144 |
$this->clear_items();
|
|
|
145 |
return;
|
|
|
146 |
}
|
|
|
147 |
|
|
|
148 |
// Make sure that the last item is not a link. Not sure if this is always a good idea.
|
|
|
149 |
$this->remove_last_item_action();
|
|
|
150 |
}
|
|
|
151 |
|
|
|
152 |
/**
|
|
|
153 |
* Get all the boostnavbaritem elements.
|
|
|
154 |
*
|
|
|
155 |
* @return boostnavbaritem[] Boost navbar items.
|
|
|
156 |
*/
|
|
|
157 |
public function get_items(): array {
|
|
|
158 |
return $this->items;
|
|
|
159 |
}
|
|
|
160 |
|
|
|
161 |
/**
|
|
|
162 |
* Removes all navigation items out of this boost navbar
|
|
|
163 |
*/
|
|
|
164 |
protected function clear_items(): void {
|
|
|
165 |
$this->items = [];
|
|
|
166 |
}
|
|
|
167 |
|
|
|
168 |
/**
|
|
|
169 |
* Retrieve a single navbar item.
|
|
|
170 |
*
|
|
|
171 |
* @param string|int $key The identifier of the navbar item to return.
|
|
|
172 |
* @return \breadcrumb_navigation_node|null The navbar item.
|
|
|
173 |
*/
|
|
|
174 |
protected function get_item($key): ?\breadcrumb_navigation_node {
|
|
|
175 |
foreach ($this->items as $item) {
|
|
|
176 |
if ($key === $item->key) {
|
|
|
177 |
return $item;
|
|
|
178 |
}
|
|
|
179 |
}
|
|
|
180 |
return null;
|
|
|
181 |
}
|
|
|
182 |
|
|
|
183 |
/**
|
|
|
184 |
* Counts all of the navbar items.
|
|
|
185 |
*
|
|
|
186 |
* @return int How many navbar items there are.
|
|
|
187 |
*/
|
|
|
188 |
protected function item_count(): int {
|
|
|
189 |
return count($this->items);
|
|
|
190 |
}
|
|
|
191 |
|
|
|
192 |
/**
|
|
|
193 |
* Remove a boostnavbaritem from the boost navbar.
|
|
|
194 |
*
|
|
|
195 |
* @param string|int $itemkey An identifier for the boostnavbaritem
|
|
|
196 |
* @param int|null $itemtype An additional type identifier for the boostnavbaritem (optional)
|
|
|
197 |
*/
|
|
|
198 |
protected function remove($itemkey, ?int $itemtype = null): void {
|
|
|
199 |
|
|
|
200 |
$itemfound = false;
|
|
|
201 |
foreach ($this->items as $key => $item) {
|
|
|
202 |
if ($item->key === $itemkey) {
|
|
|
203 |
// If a type identifier is also specified, check whether the type of the breadcrumb item matches the
|
|
|
204 |
// specified type. Skip if types to not match.
|
|
|
205 |
if (!is_null($itemtype) && $item->type !== $itemtype) {
|
|
|
206 |
continue;
|
|
|
207 |
}
|
|
|
208 |
unset($this->items[$key]);
|
|
|
209 |
$itemfound = true;
|
|
|
210 |
break;
|
|
|
211 |
}
|
|
|
212 |
}
|
|
|
213 |
if (!$itemfound) {
|
|
|
214 |
return;
|
|
|
215 |
}
|
|
|
216 |
|
|
|
217 |
$itemcount = $this->item_count();
|
|
|
218 |
if ($itemcount <= 0) {
|
|
|
219 |
return;
|
|
|
220 |
}
|
|
|
221 |
|
|
|
222 |
$this->items = array_values($this->items);
|
|
|
223 |
// Set the last item to last item if it is not.
|
|
|
224 |
$lastitem = $this->items[$itemcount - 1];
|
|
|
225 |
if (!$lastitem->is_last()) {
|
|
|
226 |
$lastitem->set_last(true);
|
|
|
227 |
}
|
|
|
228 |
}
|
|
|
229 |
|
|
|
230 |
/**
|
|
|
231 |
* Removes the action from the last item of the boostnavbaritem.
|
|
|
232 |
*/
|
|
|
233 |
protected function remove_last_item_action(): void {
|
|
|
234 |
$item = end($this->items);
|
|
|
235 |
$item->action = null;
|
|
|
236 |
reset($this->items);
|
|
|
237 |
}
|
|
|
238 |
|
|
|
239 |
/**
|
|
|
240 |
* Returns the second last navbar item. This is for use in the mobile view where we are showing just the second
|
|
|
241 |
* last item in the breadcrumb navbar.
|
|
|
242 |
*
|
|
|
243 |
* @return breakcrumb_navigation_node|null The second last navigation node.
|
|
|
244 |
*/
|
|
|
245 |
public function get_penultimate_item(): ?\breadcrumb_navigation_node {
|
|
|
246 |
$number = $this->item_count() - 2;
|
|
|
247 |
return ($number >= 0) ? $this->items[$number] : null;
|
|
|
248 |
}
|
|
|
249 |
|
|
|
250 |
/**
|
|
|
251 |
* Remove items that have no actions associated with them and optionally remove items that are sections.
|
|
|
252 |
*
|
|
|
253 |
* The only exception is the last item in the list which may not have a link but needs to be displayed.
|
|
|
254 |
*
|
|
|
255 |
* @param bool $removesections Whether section items should be also removed (only applies when they have an action)
|
|
|
256 |
*/
|
|
|
257 |
protected function remove_no_link_items(bool $removesections = true): void {
|
|
|
258 |
foreach ($this->items as $key => $value) {
|
|
|
259 |
if (!$value->is_last() &&
|
|
|
260 |
(!$value->has_action() || ($value->type == \navigation_node::TYPE_SECTION && $removesections))) {
|
|
|
261 |
unset($this->items[$key]);
|
|
|
262 |
}
|
|
|
263 |
}
|
|
|
264 |
$this->items = array_values($this->items);
|
|
|
265 |
}
|
|
|
266 |
|
|
|
267 |
/**
|
|
|
268 |
* Remove breadcrumb items that already exist in a given navigation view.
|
|
|
269 |
*
|
|
|
270 |
* This method removes the breadcrumb items that have a text => action match in a given navigation view
|
|
|
271 |
* (primary or secondary).
|
|
|
272 |
*
|
|
|
273 |
* @param view $navigationview The navigation view object.
|
|
|
274 |
*/
|
|
|
275 |
protected function remove_items_that_exist_in_navigation(view $navigationview): void {
|
|
|
276 |
// Loop through the navigation view items and create a 'text' => 'action' array which will be later used
|
|
|
277 |
// to compare whether any of the breadcrumb items matches these pairs.
|
|
|
278 |
$navigationviewitems = [];
|
|
|
279 |
foreach ($navigationview->children as $child) {
|
|
|
280 |
list($childtext, $childaction) = $this->get_node_text_and_action($child);
|
|
|
281 |
if ($childaction) {
|
|
|
282 |
$navigationviewitems[$childtext] = $childaction;
|
|
|
283 |
}
|
|
|
284 |
}
|
|
|
285 |
// Loop through the breadcrumb items and if the item's 'text' and 'action' values matches with any of the
|
|
|
286 |
// existing navigation view items, remove it from the breadcrumbs.
|
|
|
287 |
foreach ($this->items as $item) {
|
|
|
288 |
list($itemtext, $itemaction) = $this->get_node_text_and_action($item);
|
|
|
289 |
if ($itemaction) {
|
|
|
290 |
if (array_key_exists($itemtext, $navigationviewitems) &&
|
|
|
291 |
$navigationviewitems[$itemtext] === $itemaction) {
|
|
|
292 |
$this->remove($item->key);
|
|
|
293 |
}
|
|
|
294 |
}
|
|
|
295 |
}
|
|
|
296 |
}
|
|
|
297 |
|
|
|
298 |
/**
|
|
|
299 |
* Remove duplicate breadcrumb items.
|
|
|
300 |
*
|
|
|
301 |
* This method looks for breadcrumb items that have identical text and action values and removes the first item.
|
|
|
302 |
*/
|
|
|
303 |
protected function remove_duplicate_items(): void {
|
|
|
304 |
$taken = [];
|
|
|
305 |
// Reverse the order of the items before filtering so that the first occurrence is removed instead of the last.
|
|
|
306 |
$filtereditems = array_values(array_filter(array_reverse($this->items), function($item) use (&$taken) {
|
|
|
307 |
list($itemtext, $itemaction) = $this->get_node_text_and_action($item);
|
|
|
308 |
if ($itemaction) {
|
|
|
309 |
if (array_key_exists($itemtext, $taken) && $taken[$itemtext] === $itemaction) {
|
|
|
310 |
return false;
|
|
|
311 |
}
|
|
|
312 |
$taken[$itemtext] = $itemaction;
|
|
|
313 |
}
|
|
|
314 |
return true;
|
|
|
315 |
}));
|
|
|
316 |
// Reverse back the order.
|
|
|
317 |
$this->items = array_reverse($filtereditems);
|
|
|
318 |
}
|
|
|
319 |
|
|
|
320 |
/**
|
|
|
321 |
* Helper function that returns an array of the text and the outputted action url (if exists) for a given
|
|
|
322 |
* navigation node.
|
|
|
323 |
*
|
|
|
324 |
* @param navigation_node $node The navigation node object.
|
|
|
325 |
* @return array
|
|
|
326 |
*/
|
|
|
327 |
protected function get_node_text_and_action(navigation_node $node): array {
|
|
|
328 |
$text = $node->text instanceof lang_string ? $node->text->out() : $node->text;
|
|
|
329 |
$action = null;
|
|
|
330 |
if ($node->has_action()) {
|
|
|
331 |
if ($node->action instanceof moodle_url) {
|
|
|
332 |
$action = $node->action->out();
|
|
|
333 |
} else if ($node->action instanceof action_link) {
|
|
|
334 |
$action = $node->action->url->out();
|
|
|
335 |
} else {
|
|
|
336 |
$action = $node->action;
|
|
|
337 |
}
|
|
|
338 |
}
|
|
|
339 |
return [$text, $action];
|
|
|
340 |
}
|
|
|
341 |
}
|