1441 |
ariadna |
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_course\output\actionbar;
|
|
|
18 |
|
|
|
19 |
use core\output\comboboxsearch;
|
|
|
20 |
use stdClass;
|
|
|
21 |
|
|
|
22 |
/**
|
|
|
23 |
* Renderable class for the group selector element in the action bar.
|
|
|
24 |
*
|
|
|
25 |
* @package core_course
|
|
|
26 |
* @copyright 2024 Shamim Rezaie <shamim@moodle.com>
|
|
|
27 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
28 |
*/
|
|
|
29 |
class group_selector extends comboboxsearch {
|
|
|
30 |
|
|
|
31 |
/** @var int|bool the active group, false if groups not used. */
|
|
|
32 |
private int|bool $activegroup;
|
|
|
33 |
|
|
|
34 |
/**
|
|
|
35 |
* The class constructor.
|
|
|
36 |
*
|
|
|
37 |
* @param stdClass $context The context object.
|
|
|
38 |
*/
|
|
|
39 |
public function __construct(private stdClass $context) {
|
|
|
40 |
$this->activegroup = $this->get_active_group();
|
|
|
41 |
$this->label = $this->get_label();
|
|
|
42 |
|
|
|
43 |
// The second and third arguments (buttoncontent and dropdowncontent) need to be rendered here, since the comboboxsearch
|
|
|
44 |
// template expects HTML in its respective context properties. Ideally, children of comboboxsearch would leverage Mustache's
|
|
|
45 |
// blocks pragma, meaning a child template could extend the comboboxsearch, allowing rendering of the child component,
|
|
|
46 |
// instead of needing to inject the child's content HTML as part of rendering the comboboxsearch parent, as is the case
|
|
|
47 |
// here. Achieving this, however, requires a refactor of comboboxsearch. For now, this must be pre-rendered and injected.
|
|
|
48 |
parent::__construct(false, $this->get_button_content(), $this->get_dropdown_content(), 'group-search',
|
|
|
49 |
'groupsearchwidget', 'groupsearchdropdown overflow-auto', null, true, $this->label, 'group',
|
|
|
50 |
$this->activegroup);
|
|
|
51 |
}
|
|
|
52 |
|
|
|
53 |
/**
|
|
|
54 |
* Returns the output for the button (trigger) element of the group selector.
|
|
|
55 |
*
|
|
|
56 |
* @return string HTML fragment
|
|
|
57 |
*/
|
|
|
58 |
private function get_button_content(): string {
|
|
|
59 |
global $PAGE;
|
|
|
60 |
$groupsselectorbutton = new group_selector_button($this->context, $this->activegroup, $this->label);
|
|
|
61 |
|
|
|
62 |
return $PAGE->get_renderer('core', 'course')->render($groupsselectorbutton);
|
|
|
63 |
}
|
|
|
64 |
|
|
|
65 |
/**
|
|
|
66 |
* Returns the output of the content rendered within the dropdown (search body area) of the group selector.
|
|
|
67 |
*
|
|
|
68 |
* @return string HTML fragment
|
|
|
69 |
*/
|
|
|
70 |
private function get_dropdown_content(): string {
|
|
|
71 |
global $PAGE;
|
|
|
72 |
$groupsdropdownform = new group_selector_dropdown_form($this->context);
|
|
|
73 |
|
|
|
74 |
return $PAGE->get_renderer('core', 'course')->render($groupsdropdownform);
|
|
|
75 |
}
|
|
|
76 |
|
|
|
77 |
/**
|
|
|
78 |
* Returns the label text for the group selector based on specified group mode.
|
|
|
79 |
*
|
|
|
80 |
* @return string
|
|
|
81 |
*/
|
|
|
82 |
private function get_label(): string {
|
|
|
83 |
return $this->get_group_mode() === VISIBLEGROUPS ? get_string('selectgroupsvisible') :
|
|
|
84 |
get_string('selectgroupsseparate');
|
|
|
85 |
}
|
|
|
86 |
|
|
|
87 |
/**
|
|
|
88 |
* Returns the active group based on the context level.
|
|
|
89 |
*
|
|
|
90 |
* @return int|bool The active group (false if groups not used, int if groups used)
|
|
|
91 |
*/
|
|
|
92 |
private function get_active_group(): int|bool {
|
|
|
93 |
global $USER;
|
|
|
94 |
|
|
|
95 |
$canaccessallgroups = has_capability('moodle/site:accessallgroups', $this->context);
|
|
|
96 |
$userid = $this->get_group_mode() == VISIBLEGROUPS || $canaccessallgroups ? 0 : $USER->id;
|
|
|
97 |
$course = get_course($this->context->get_course_context()->instanceid);
|
|
|
98 |
// Based on the current context level, retrieve the correct grouping ID and specify whether only groups with the
|
|
|
99 |
// participation field set to true should be returned.
|
|
|
100 |
if ($this->context->contextlevel === CONTEXT_MODULE) {
|
|
|
101 |
$cm = get_coursemodule_from_id(false, $this->context->instanceid);
|
|
|
102 |
$groupingid = $cm->groupingid;
|
|
|
103 |
$participationonly = true;
|
|
|
104 |
} else {
|
|
|
105 |
$cm = null;
|
|
|
106 |
$groupingid = $course->defaultgroupingid;
|
|
|
107 |
$participationonly = false;
|
|
|
108 |
}
|
|
|
109 |
|
|
|
110 |
$allowedgroups = groups_get_all_groups(
|
|
|
111 |
courseid: $course->id,
|
|
|
112 |
userid: $userid,
|
|
|
113 |
groupingid: $groupingid,
|
|
|
114 |
participationonly: $participationonly
|
|
|
115 |
);
|
|
|
116 |
|
|
|
117 |
if ($cm) {
|
|
|
118 |
return groups_get_activity_group($cm, true, $allowedgroups);
|
|
|
119 |
}
|
|
|
120 |
return groups_get_course_group($course, true, $allowedgroups);
|
|
|
121 |
}
|
|
|
122 |
|
|
|
123 |
/**
|
|
|
124 |
* Returns the group mode based on the context level.
|
|
|
125 |
*
|
|
|
126 |
* @return int The group mode
|
|
|
127 |
*/
|
|
|
128 |
private function get_group_mode(): int {
|
|
|
129 |
if ($this->context->contextlevel == CONTEXT_MODULE) {
|
|
|
130 |
$cm = get_coursemodule_from_id(false, $this->context->instanceid);
|
|
|
131 |
return groups_get_activity_groupmode($cm);
|
|
|
132 |
}
|
|
|
133 |
$course = get_course($this->context->instanceid);
|
|
|
134 |
return $course->groupmode;
|
|
|
135 |
}
|
|
|
136 |
}
|