Proyectos de Subversion Moodle

Rev

Rev 1 | | Comparar con el anterior | Ultima modificación | Ver Log |

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