MDL-68169 user: Add groups filter
[moodle.git] / user / classes / output / participants_filter.php
CommitLineData
77ba77f1
AN
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/**
18 * Class for rendering user filters on the course participants page.
19 *
20 * @package core_user
21 * @copyright 2020 Michael Hawkins <michaelh@moodle.com>
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 */
24namespace core_user\output;
25
26use context_course;
27use renderable;
28use renderer_base;
29use stdClass;
30use templatable;
31
32/**
33 * Class for rendering user filters on the course participants page.
34 *
35 * @copyright 2020 Michael Hawkins <michaelh@moodle.com>
36 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
37 */
38class participants_filter implements renderable, templatable {
39
40 /** @var context_course $context The context where the filters are being rendered. */
41 protected $context;
42
43 /** @var string $tableregionid The table to be updated by this filter */
44 protected $tableregionid;
45
de83d435
AN
46 /** @var stdClass $course The course shown */
47 protected $course;
48
77ba77f1
AN
49 /**
50 * Participants filter constructor.
51 *
52 * @param context_course $context The context where the filters are being rendered.
53 * @param string $tableregionid The table to be updated by this filter
54 */
55 public function __construct(context_course $context, string $tableregionid) {
56 $this->context = $context;
57 $this->tableregionid = $tableregionid;
de83d435
AN
58
59 $this->course = get_course($context->instanceid);
77ba77f1
AN
60 }
61
62 /**
63 * Get data for all filter types.
64 *
65 * @return array
66 */
67 protected function get_filtertypes(): array {
68 $filtertypes = [];
69
70 if ($filtertype = $this->get_enrolmentstatus_filter()) {
71 $filtertypes[] = $filtertype;
72 }
73
ffc933ad
AN
74 if ($filtertype = $this->get_roles_filter()) {
75 $filtertypes[] = $filtertype;
76 }
77
de83d435
AN
78 if ($filtertype = $this->get_groups_filter()) {
79 $filtertypes[] = $filtertype;
80 }
81
77ba77f1
AN
82 return $filtertypes;
83 }
84
85 /**
86 * Get data for the enrolment status filter.
87 *
88 * @return stdClass|null
89 */
90 protected function get_enrolmentstatus_filter(): ?stdClass {
91 if (!has_capability('moodle/course:enrolreview', $this->context)) {
92 return null;
93 }
94
95 return $this->get_filter_object(
96 'status',
97 get_string('participationstatus', 'core_enrol'),
98 false,
99 true,
100 null,
101 [
102 (object) [
103 'value' => ENROL_USER_ACTIVE,
104 'title' => get_string('active'),
105 ],
106 (object) [
107 'value' => ENROL_USER_SUSPENDED,
108 'title' => get_string('inactive'),
109 ],
110 ]
111 );
112 }
113
ffc933ad
AN
114 /**
115 * Get data for the roles filter.
116 *
117 * @return stdClass|null
118 */
119 protected function get_roles_filter(): ?stdClass {
120 $roles = [];
121 $roles += [-1 => get_string('noroles', 'role')];
122 $roles += get_viewable_roles($this->context);
123
124 if (has_capability('moodle/role:assign', $this->context)) {
125 $roles += get_assignable_roles($this->context, ROLENAME_ALIAS);
126 }
127
128 return $this->get_filter_object(
129 'roles',
130 get_string('roles', 'core_role'),
131 false,
132 true,
133 null,
134 array_map(function($id, $title) {
135 return (object) [
136 'value' => $id,
137 'title' => $title,
138 ];
139 }, array_keys($roles), array_values($roles))
140 );
141 }
142
de83d435
AN
143 /**
144 * Get data for the groups filter.
145 *
146 * @return stdClass|null
147 */
148 protected function get_groups_filter(): ?stdClass {
149 global $USER;
150
151 // Filter options for groups, if available.
152 $seeallgroups = has_capability('moodle/site:accessallgroups', $this->context);
153 $seeallgroups = $seeallgroups || ($this->course->groupmode != SEPARATEGROUPS);
154 if ($seeallgroups) {
155 $groups = [];
156 $groups += [USERSWITHOUTGROUP => (object) [
157 'id' => USERSWITHOUTGROUP,
158 'name' => get_string('nogroup', 'group'),
159 ]];
160 $groups += groups_get_all_groups($this->course->id);
161 } else {
162 // Otherwise, just list the groups the user belongs to.
163 $groups = groups_get_all_groups($this->course->id, $USER->id);
164 }
165
166 if (empty($groups)) {
167 return null;
168 }
169
170 return $this->get_filter_object(
171 'groups',
172 get_string('groups', 'core_group'),
173 false,
174 true,
175 null,
176 array_map(function($group) {
177 return (object) [
178 'value' => $group->id,
179 'title' => $group->name,
180 ];
181 }, array_values($groups))
182 );
183 }
184
77ba77f1
AN
185 /**
186 * Export the renderer data in a mustache template friendly format.
187 *
188 * @param renderer_base $output Unused.
189 * @return stdClass Data in a format compatible with a mustache template.
190 */
191 public function export_for_template(renderer_base $output): stdClass {
192 return (object) [
193 'tableregionid' => $this->tableregionid,
194 'courseid' => $this->context->instanceid,
195 'filtertypes' => $this->get_filtertypes(),
196 ];
197
198 return $data;
199 }
200
201 /**
202 * Get a standardised filter object.
203 *
204 * @param string $name
205 * @param string $title
206 * @param bool $custom
207 * @param bool $multiple
208 * @param string|null $filterclass
209 * @param array $values
210 * @return stdClass|null
211 */
212 protected function get_filter_object(
213 string $name,
214 string $title,
215 bool $custom,
216 bool $multiple,
217 ?string $filterclass,
218 array $values
219 ): ?stdClass {
220 if (empty($values)) {
221 // Do not show empty filters.
222 return null;
223 }
224
225 return (object) [
226 'name' => $name,
227 'title' => $title,
228 'allowcustom' => $custom,
229 'allowmultiple' => $multiple,
230 'filtertypeclass' => $filterclass,
231 'values' => $values,
232 ];
233 }
234}