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\output;
18
 
1441 ariadna 19
use core\context\course as context_course;
1 efrain 20
use moodle_page;
21
use navigation_node;
22
use moodle_url;
23
 
24
/**
25
 * Class responsible for generating the action bar (tertiary nav) elements in the participants page and related pages.
26
 *
27
 * @package    core
28
 * @copyright  2021 Peter Dias
29
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
30
 */
1441 ariadna 31
class participants_action_bar implements renderable {
1 efrain 32
    /** @var object $course The course we are dealing with. */
33
    private $course;
34
    /** @var moodle_page $page The current page. */
35
    private $page;
36
    /** @var navigation_node $node The settings node for the participants page. */
37
    private $node;
38
    /** @var string|null $renderedcontent Rendered buttons to be displayed in-line with the select box */
39
    private $renderedcontent;
40
 
41
    /**
42
     * Constructor participants_action_bar
43
     * @param object $course The course that we are generating the nav for
44
     * @param moodle_page $page The page object
45
     * @param string|null $renderedcontent Any additional rendered content/actions to be displayed in line with the nav
46
     */
47
    public function __construct(object $course, moodle_page $page, ?string $renderedcontent) {
48
        $this->course = $course;
49
        $this->page = $page;
50
        $node = 'users';
51
        if ($this->page->context->contextlevel == CONTEXT_MODULE) {
52
            $node = 'modulesettings';
53
        } else if ($this->page->context->contextlevel == CONTEXT_COURSECAT) {
54
            $node = 'categorysettings';
55
        }
56
 
57
        $this->node = $this->page->settingsnav->find($node, null);
58
        $this->renderedcontent = $renderedcontent;
59
    }
60
 
61
    /**
62
     * Return the nodes required to be displayed in the url_select box.
63
     * The nodes are divided into 3 sections each with the heading as the key.
64
     *
65
     * @return array
66
     */
67
    protected function get_ordered_nodes(): array {
68
        return [
69
            'enrolments:enrol' => [
70
                'review',
71
                'manageinstances',
72
                'renameroles',
73
            ],
74
            'groups:group' => [
1441 ariadna 75
                'groups',
1 efrain 76
            ],
77
            'permissions:role' => [
78
                'override',
79
                'roles',
80
                'otherusers',
81
                'permissions',
82
                'roleoverride',
83
                'rolecheck',
84
                'roleassign',
85
            ],
86
        ];
87
    }
88
 
89
    /**
90
     * Get the content for the url_select select box.
91
     *
92
     * @return array
93
     */
94
    protected function get_content_for_select(): array {
95
        if (!$this->node) {
96
            return [];
97
        }
98
 
99
        $formattedcontent = [];
100
        $enrolmentsheading = get_string('enrolments', 'enrol');
1441 ariadna 101
        if (
102
            $this->page->context->contextlevel != CONTEXT_MODULE &&
103
                $this->page->context->contextlevel != CONTEXT_COURSECAT
104
        ) {
1 efrain 105
            // Pre-populate the formatted tertiary nav items with the "Enrolled users" node if user can view the participants page.
106
            $coursecontext = context_course::instance($this->course->id);
107
            $canviewparticipants = course_can_view_participants($coursecontext);
108
            if ($canviewparticipants) {
109
                $participantsurl = (new moodle_url('/user/index.php', ['id' => $this->course->id]))->out();
110
                $formattedcontent[] = [
111
                    $enrolmentsheading => [
112
                        $participantsurl => get_string('enrolledusers', 'enrol'),
1441 ariadna 113
                    ],
1 efrain 114
                ];
115
            }
116
        }
117
 
118
        $nodes = $this->get_ordered_nodes();
119
        foreach ($nodes as $description => $content) {
1441 ariadna 120
            [$stringid, $location] = explode(':', $description);
1 efrain 121
            $heading = get_string($stringid, $location);
122
            $items = [];
123
            foreach ($content as $key) {
124
                if ($node = $this->node->find($key, null)) {
125
                    if ($node->has_action()) {
126
                        $items[$node->action()->out()] = $node->text;
127
                    }
128
 
129
                    // Additional items to be added.
130
                    if ($key === 'groups') {
131
                        $params = ['id' => $this->course->id];
132
                        $items += [
133
                            (new moodle_url('/group/groupings.php', $params))->out() => get_string('groupings', 'group'),
1441 ariadna 134
                            (new moodle_url('/group/overview.php', $params))->out() => get_string('overview', 'group'),
1 efrain 135
                        ];
136
                    }
137
                }
138
            }
139
            if ($items) {
140
                if ($heading === $enrolmentsheading) {
141
                    // Merge the contents of the "Enrolments" group with the ones from the course settings nav.
142
                    $formattedcontent[0][$heading] = array_merge($formattedcontent[0][$heading], $items);
143
                } else {
144
                    $formattedcontent[][$heading] = $items;
145
                }
146
            }
147
        }
148
 
149
        // If we are accessing a page from a module/category context additional nodes will not be visible.
1441 ariadna 150
        if (
151
            $this->page->context->contextlevel != CONTEXT_MODULE &&
152
                $this->page->context->contextlevel != CONTEXT_COURSECAT
153
        ) {
1 efrain 154
            // Need to do some funky code here to find out if we have added third party navigation nodes.
155
            $thirdpartynodearray = $this->get_thirdparty_node_array() ?: [];
156
            $formattedcontent = array_merge($formattedcontent, $thirdpartynodearray);
157
        }
158
        return $formattedcontent;
159
    }
160
 
161
    /**
162
     * Gets an array of third party navigation nodes in an array formatted for a url_select element.
163
     *
164
     * @return array|null The thirdparty node array.
165
     */
166
    protected function get_thirdparty_node_array(): ?array {
167
        $results = [];
168
 
169
        $flatnodes = array_merge(...(array_values($this->get_ordered_nodes())));
170
 
171
        foreach ($this->node->children as $child) {
172
            if (array_search($child->key, $flatnodes) === false) {
173
                $results[] = $child;
174
            }
175
        }
176
 
177
        return \core\navigation\views\secondary::create_menu_element($results, true);
178
    }
179
 
180
    /**
181
     * Recursively tries to find a matching url
182
     * @param array $urlcontent The content for the url_select
183
     * @param int $strictness Strictness for the compare criteria
184
     * @return string The matching active url
185
     */
186
    protected function find_active_page(array $urlcontent, int $strictness = URL_MATCH_EXACT): string {
187
        foreach ($urlcontent as $key => $value) {
188
            if (is_array($value) && $activeitem = $this->find_active_page($value, $strictness)) {
189
                return $activeitem;
190
            } else if ($this->page->url->compare(new moodle_url($key), $strictness)) {
191
                return $key;
192
            }
193
        }
194
 
195
        return "";
196
    }
197
 
198
    /**
199
     * Gets the url_select to be displayed in the participants page if available.
200
     *
201
     * @param \renderer_base $output
202
     * @return object|null The content required to render the tertiary navigation
203
     */
204
    public function get_dropdown(\renderer_base $output): ?object {
205
        if ($urlselectcontent = $this->get_content_for_select()) {
206
            $activeurl = $this->find_active_page($urlselectcontent);
207
            $activeurl = $activeurl ?: $this->find_active_page($urlselectcontent, URL_MATCH_BASE);
208
 
209
            $selectmenu = new select_menu('participantsnavigation', $urlselectcontent, $activeurl);
1441 ariadna 210
            $selectmenu->set_label(get_string('participantsnavigation', 'course'), ['class' => 'visually-hidden']);
1 efrain 211
 
212
            return $selectmenu->export_for_template($output);
213
        }
214
 
215
        return null;
216
    }
217
 
218
    /**
219
     * Export the content to be displayed on the participants page.
220
     *
221
     * @param \renderer_base $output
222
     * @return array Consists of the following:
223
     *              - navigation A stdclass representing the standard navigation options to be fed into a urlselect
224
     *              - renderedcontent Rendered content to be displayed in line with the tertiary nav
225
     */
226
    public function export_for_template(\renderer_base $output) {
227
        return [
228
            'navigation' => $this->get_dropdown($output),
229
            'renderedcontent' => $this->renderedcontent,
230
        ];
231
    }
232
}