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 core\navigation\views;
18
 
19
use navigation_node;
20
 
21
/**
22
 * Class primary.
23
 *
24
 * The primary navigation view is a combination of few components - navigation, output->navbar,
25
 *
26
 * @package     core
27
 * @category    navigation
28
 * @copyright   2021 onwards Peter Dias
29
 * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
30
 */
31
class primary extends view {
32
    /**
33
     * Initialise the primary navigation node
34
     */
35
    public function initialise(): void {
36
        global $CFG;
37
 
38
        if (during_initial_install() || $this->initialised) {
39
            return;
40
        }
41
        $this->id = 'primary_navigation';
42
 
43
        $showhomenode = empty($this->page->theme->removedprimarynavitems) ||
44
            !in_array('home', $this->page->theme->removedprimarynavitems);
45
        // We do not need to change the text for the home/dashboard depending on the set homepage.
46
        if ($showhomenode) {
47
            $sitehome = $this->add(get_string('home'), new \moodle_url('/'), self::TYPE_SYSTEM,
48
                null, 'home', new \pix_icon('i/home', ''));
49
        }
50
        if (isloggedin() && !isguestuser()) {
51
            $homepage = get_home_page();
52
            if ($homepage == HOMEPAGE_MY || $homepage == HOMEPAGE_MYCOURSES) {
53
                // We need to stop automatic redirection.
54
                if ($showhomenode) {
55
                    $sitehome->action->param('redirect', '0');
56
                }
57
            }
58
 
59
            // Add the dashboard link.
60
            $showmyhomenode = !empty($CFG->enabledashboard) && (empty($this->page->theme->removedprimarynavitems) ||
61
                !in_array('myhome', $this->page->theme->removedprimarynavitems));
62
            if ($showmyhomenode) {
63
                $this->add(get_string('myhome'), new \moodle_url('/my/'),
64
                    self::TYPE_SETTING, null, 'myhome', new \pix_icon('i/dashboard', ''));
65
            }
66
 
67
            // Add the mycourses link.
68
            $showcoursesnode = empty($this->page->theme->removedprimarynavitems) ||
69
                !in_array('courses', $this->page->theme->removedprimarynavitems);
70
            if ($showcoursesnode) {
71
                $this->add(get_string('mycourses'), new \moodle_url('/my/courses.php'), self::TYPE_ROOTNODE, null, 'mycourses');
72
            }
73
        }
74
 
1441 ariadna 75
        // Add the calendar link only for guest users.
76
        if (isguestuser()) {
77
            $this->add(get_string('calendar', 'calendar'), new \moodle_url('/calendar/view.php?view=month'), self::TYPE_ROOTNODE,
78
                null, 'calendar');
79
        }
80
 
1 efrain 81
        $showsiteadminnode = empty($this->page->theme->removedprimarynavitems) ||
82
            !in_array('siteadminnode', $this->page->theme->removedprimarynavitems);
83
 
84
        if ($showsiteadminnode && $node = $this->get_site_admin_node()) {
85
            // We don't need everything from the node just the initial link.
86
            $this->add($node->text, $node->action(), self::TYPE_SITE_ADMIN, null, 'siteadminnode', $node->icon);
87
        }
88
 
89
        // Allow plugins to add nodes to the primary navigation.
90
        $hook = new \core\hook\navigation\primary_extend($this);
91
        \core\di::get(\core\hook\manager::class)->dispatch($hook);
92
 
93
        // Search and set the active node.
94
        $this->set_active_node();
95
        $this->initialised = true;
96
    }
97
 
98
    /**
99
     * Get the site admin node if available.
100
     *
101
     * @return navigation_node|null
102
     */
103
    private function get_site_admin_node(): ?navigation_node {
104
        // Add the site admin node. We are using the settingsnav so as to avoid rechecking permissions again.
105
        $settingsnav = $this->page->settingsnav;
106
        $node = $settingsnav->find('siteadministration', self::TYPE_SITE_ADMIN);
107
        if (!$node) {
108
            // Try again. This node can exist with 2 different keys.
109
            $node = $settingsnav->find('root', self::TYPE_SITE_ADMIN);
110
        }
111
 
112
        return $node ?: null;
113
    }
114
 
115
    /**
116
     * Find and set the active node. Initially searches based on URL/explicitly set active node.
117
     * If nothing is found, it checks the following:
118
     *      - If the node is a site page, set 'Home' as active
119
     *      - If within a course context, set 'My courses' as active
120
     *      - If within a course category context, set 'Site Admin' (if available) else set 'Home'
121
     *      - Else if available set site admin as active
122
     *      - Fallback, set 'Home' as active
123
     */
124
    private function set_active_node(): void {
125
        global $SITE;
126
        $activenode = $this->search_and_set_active_node($this);
127
        // If we haven't found an active node based on the standard search. Follow the criteria above.
128
        if (!$activenode) {
129
            $children = $this->get_children_key_list();
130
            $navactivenode = $this->page->navigation->find_active_node();
131
            $activekey = 'home';
132
            if (isset($navactivenode->parent) && $navactivenode->parent->text == get_string('sitepages')) {
133
                $activekey = 'home';
134
            } else if (in_array($this->context->contextlevel, [CONTEXT_COURSE, CONTEXT_MODULE])) {
135
                if ($this->page->course->id != $SITE->id) {
136
                    $activekey = 'courses';
137
                }
138
            } else if (in_array('siteadminnode', $children) && $node = $this->get_site_admin_node()) {
139
                if ($this->context->contextlevel == CONTEXT_COURSECAT || $node->search_for_active_node(URL_MATCH_EXACT)) {
140
                    $activekey = 'siteadminnode';
141
                }
1441 ariadna 142
            } else if (in_array('calendar', $children) && $node = $this->find('calendar', self::TYPE_ROOTNODE)) {
143
                if ($node->search_for_active_node(URL_MATCH_BASE)) {
144
                    $activekey = 'calendar';
145
                }
1 efrain 146
            }
147
 
148
            if ($activekey && $activenode = $this->find($activekey, null)) {
149
                $activenode->make_active();
150
            }
151
        }
152
    }
153
 
154
    /**
155
     * Searches all children for the matching active node
156
     *
157
     * This method recursively traverse through the node tree to
158
     * find the node to activate/highlight:
159
     * 1. If the user had set primary node key to highlight, it
160
     *    tries to match this key with the node(s). Hence it would
161
     *    travel all the nodes.
162
     * 2. If no primary key is provided by the dev, then it would
163
     *    check for the active node set in the tree.
164
     *
165
     * @param navigation_node $node
166
     * @param array $actionnodes navigation nodes array to set active and inactive.
167
     * @return navigation_node|null
168
     */
169
    private function search_and_set_active_node(navigation_node $node,
170
        array &$actionnodes = []): ?navigation_node {
171
        global $PAGE;
172
 
173
        $activekey = $PAGE->get_primary_activate_tab();
174
        if ($activekey) {
175
            if ($node->key && ($activekey === $node->key)) {
176
                return $node;
177
            }
178
        } else if ($node->check_if_active()) {
179
            return $node;
180
        }
181
 
182
        foreach ($node->children as $child) {
183
            $outcome = $this->search_and_set_active_node($child, $actionnodes);
184
            if ($outcome !== null) {
185
                $outcome->make_active();
186
                $actionnodes['active'] = $outcome;
187
                if ($activekey === null) {
188
                    return $actionnodes['active'];
189
                }
190
            } else {
191
                // If the child is active then make it inactive.
192
                if ($child->isactive) {
193
                    $actionnodes['set_inactive'][] = $child;
194
                }
195
            }
196
        }
197
 
198
        // If we have successfully found an active node then reset any other nodes to inactive.
199
        if (isset($actionnodes['set_inactive']) && isset($actionnodes['active'])) {
200
            foreach ($actionnodes['set_inactive'] as $inactivenode) {
201
                $inactivenode->make_inactive();
202
            }
203
            $actionnodes['set_inactive'] = [];
204
        }
205
        return ($actionnodes['active'] ?? null);
206
    }
207
}