MDL-68169 user: Add roles 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
46 /**
47 * Participants filter constructor.
48 *
49 * @param context_course $context The context where the filters are being rendered.
50 * @param string $tableregionid The table to be updated by this filter
51 */
52 public function __construct(context_course $context, string $tableregionid) {
53 $this->context = $context;
54 $this->tableregionid = $tableregionid;
55 }
56
57 /**
58 * Get data for all filter types.
59 *
60 * @return array
61 */
62 protected function get_filtertypes(): array {
63 $filtertypes = [];
64
65 if ($filtertype = $this->get_enrolmentstatus_filter()) {
66 $filtertypes[] = $filtertype;
67 }
68
ffc933ad
AN
69 if ($filtertype = $this->get_roles_filter()) {
70 $filtertypes[] = $filtertype;
71 }
72
77ba77f1
AN
73 return $filtertypes;
74 }
75
76 /**
77 * Get data for the enrolment status filter.
78 *
79 * @return stdClass|null
80 */
81 protected function get_enrolmentstatus_filter(): ?stdClass {
82 if (!has_capability('moodle/course:enrolreview', $this->context)) {
83 return null;
84 }
85
86 return $this->get_filter_object(
87 'status',
88 get_string('participationstatus', 'core_enrol'),
89 false,
90 true,
91 null,
92 [
93 (object) [
94 'value' => ENROL_USER_ACTIVE,
95 'title' => get_string('active'),
96 ],
97 (object) [
98 'value' => ENROL_USER_SUSPENDED,
99 'title' => get_string('inactive'),
100 ],
101 ]
102 );
103 }
104
ffc933ad
AN
105 /**
106 * Get data for the roles filter.
107 *
108 * @return stdClass|null
109 */
110 protected function get_roles_filter(): ?stdClass {
111 $roles = [];
112 $roles += [-1 => get_string('noroles', 'role')];
113 $roles += get_viewable_roles($this->context);
114
115 if (has_capability('moodle/role:assign', $this->context)) {
116 $roles += get_assignable_roles($this->context, ROLENAME_ALIAS);
117 }
118
119 return $this->get_filter_object(
120 'roles',
121 get_string('roles', 'core_role'),
122 false,
123 true,
124 null,
125 array_map(function($id, $title) {
126 return (object) [
127 'value' => $id,
128 'title' => $title,
129 ];
130 }, array_keys($roles), array_values($roles))
131 );
132 }
133
77ba77f1
AN
134 /**
135 * Export the renderer data in a mustache template friendly format.
136 *
137 * @param renderer_base $output Unused.
138 * @return stdClass Data in a format compatible with a mustache template.
139 */
140 public function export_for_template(renderer_base $output): stdClass {
141 return (object) [
142 'tableregionid' => $this->tableregionid,
143 'courseid' => $this->context->instanceid,
144 'filtertypes' => $this->get_filtertypes(),
145 ];
146
147 return $data;
148 }
149
150 /**
151 * Get a standardised filter object.
152 *
153 * @param string $name
154 * @param string $title
155 * @param bool $custom
156 * @param bool $multiple
157 * @param string|null $filterclass
158 * @param array $values
159 * @return stdClass|null
160 */
161 protected function get_filter_object(
162 string $name,
163 string $title,
164 bool $custom,
165 bool $multiple,
166 ?string $filterclass,
167 array $values
168 ): ?stdClass {
169 if (empty($values)) {
170 // Do not show empty filters.
171 return null;
172 }
173
174 return (object) [
175 'name' => $name,
176 'title' => $title,
177 'allowcustom' => $custom,
178 'allowmultiple' => $multiple,
179 'filtertypeclass' => $filterclass,
180 'values' => $values,
181 ];
182 }
183}