| 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 |
declare(strict_types=1);
|
|
|
18 |
|
|
|
19 |
namespace core_role\reportbuilder\local\entities;
|
|
|
20 |
|
| 1441 |
ariadna |
21 |
use core\{context, context_helper};
|
|
|
22 |
use core\lang_string;
|
| 1 |
efrain |
23 |
use core_reportbuilder\local\entities\base;
|
|
|
24 |
use core_reportbuilder\local\filters\select;
|
|
|
25 |
use core_reportbuilder\local\report\{column, filter};
|
| 1441 |
ariadna |
26 |
use stdClass;
|
| 1 |
efrain |
27 |
|
|
|
28 |
/**
|
|
|
29 |
* Role entity
|
|
|
30 |
*
|
|
|
31 |
* @package core_role
|
|
|
32 |
* @copyright 2023 Paul Holden <paulh@moodle.com>
|
|
|
33 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
34 |
*/
|
|
|
35 |
class role extends base {
|
|
|
36 |
|
|
|
37 |
/**
|
|
|
38 |
* Database tables that this entity uses
|
|
|
39 |
*
|
|
|
40 |
* @return string[]
|
|
|
41 |
*/
|
|
|
42 |
protected function get_default_tables(): array {
|
|
|
43 |
return [
|
|
|
44 |
'context',
|
|
|
45 |
'role',
|
|
|
46 |
];
|
|
|
47 |
}
|
|
|
48 |
|
|
|
49 |
/**
|
|
|
50 |
* The default title for this entity
|
|
|
51 |
*
|
|
|
52 |
* @return lang_string
|
|
|
53 |
*/
|
|
|
54 |
protected function get_default_entity_title(): lang_string {
|
|
|
55 |
return new lang_string('role');
|
|
|
56 |
}
|
|
|
57 |
|
|
|
58 |
/**
|
|
|
59 |
* Initialise the entity
|
|
|
60 |
*
|
|
|
61 |
* @return base
|
|
|
62 |
*/
|
|
|
63 |
public function initialise(): base {
|
|
|
64 |
$columns = $this->get_all_columns();
|
|
|
65 |
foreach ($columns as $column) {
|
|
|
66 |
$this->add_column($column);
|
|
|
67 |
}
|
|
|
68 |
|
|
|
69 |
// All the filters defined by the entity can also be used as conditions.
|
|
|
70 |
$filters = $this->get_all_filters();
|
|
|
71 |
foreach ($filters as $filter) {
|
|
|
72 |
$this
|
|
|
73 |
->add_filter($filter)
|
|
|
74 |
->add_condition($filter);
|
|
|
75 |
}
|
|
|
76 |
|
|
|
77 |
return $this;
|
|
|
78 |
}
|
|
|
79 |
|
|
|
80 |
/**
|
|
|
81 |
* Returns list of all available columns
|
|
|
82 |
*
|
|
|
83 |
* @return column[]
|
|
|
84 |
*/
|
|
|
85 |
protected function get_all_columns(): array {
|
|
|
86 |
$contextalias = $this->get_table_alias('context');
|
|
|
87 |
$rolealias = $this->get_table_alias('role');
|
|
|
88 |
|
|
|
89 |
// Name column.
|
|
|
90 |
$columns[] = (new column(
|
|
|
91 |
'name',
|
|
|
92 |
new lang_string('rolefullname', 'core_role'),
|
|
|
93 |
$this->get_entity_name()
|
|
|
94 |
))
|
|
|
95 |
->add_joins($this->get_joins())
|
|
|
96 |
->add_fields("{$rolealias}.name, {$rolealias}.shortname, {$rolealias}.id, {$contextalias}.id AS contextid")
|
|
|
97 |
->add_fields(context_helper::get_preload_record_columns_sql($contextalias))
|
| 1441 |
ariadna |
98 |
// The sorting is on name, unless empty then we use shortname.
|
| 1 |
efrain |
99 |
->set_is_sortable(true, [
|
| 1441 |
ariadna |
100 |
"CASE WHEN COALESCE({$rolealias}.name, '') = ''
|
| 1 |
efrain |
101 |
THEN {$rolealias}.shortname
|
|
|
102 |
ELSE {$rolealias}.name
|
|
|
103 |
END",
|
|
|
104 |
])
|
| 1441 |
ariadna |
105 |
->add_callback(static function(?string $name, stdClass $role): string {
|
| 1 |
efrain |
106 |
if ($name === null) {
|
|
|
107 |
return '';
|
|
|
108 |
}
|
|
|
109 |
|
|
|
110 |
context_helper::preload_from_record($role);
|
|
|
111 |
$context = context::instance_by_id($role->contextid);
|
|
|
112 |
|
|
|
113 |
return role_get_name($role, $context, ROLENAME_BOTH);
|
|
|
114 |
});
|
|
|
115 |
|
|
|
116 |
// Original name column.
|
|
|
117 |
$columns[] = (new column(
|
|
|
118 |
'originalname',
|
|
|
119 |
new lang_string('roleoriginalname', 'core_role'),
|
|
|
120 |
$this->get_entity_name()
|
|
|
121 |
))
|
|
|
122 |
->add_joins($this->get_joins())
|
|
|
123 |
->add_fields("{$rolealias}.name, {$rolealias}.shortname")
|
| 1441 |
ariadna |
124 |
// The sorting is on name, unless empty then we use shortname.
|
| 1 |
efrain |
125 |
->set_is_sortable(true, [
|
| 1441 |
ariadna |
126 |
"CASE WHEN COALESCE({$rolealias}.name, '') = ''
|
| 1 |
efrain |
127 |
THEN {$rolealias}.shortname
|
|
|
128 |
ELSE {$rolealias}.name
|
|
|
129 |
END",
|
|
|
130 |
])
|
| 1441 |
ariadna |
131 |
->add_callback(fn(?string $name, stdClass $role) => match ($name) {
|
|
|
132 |
null => '',
|
|
|
133 |
default => role_get_name($role, null, ROLENAME_ORIGINAL),
|
| 1 |
efrain |
134 |
});
|
|
|
135 |
|
|
|
136 |
// Short name column.
|
|
|
137 |
$columns[] = (new column(
|
|
|
138 |
'shortname',
|
|
|
139 |
new lang_string('roleshortname', 'core_role'),
|
|
|
140 |
$this->get_entity_name()
|
|
|
141 |
))
|
|
|
142 |
->add_joins($this->get_joins())
|
| 1441 |
ariadna |
143 |
->add_field("{$rolealias}.shortname")
|
| 1 |
efrain |
144 |
->set_is_sortable(true);
|
|
|
145 |
|
| 1441 |
ariadna |
146 |
// Archetype column.
|
|
|
147 |
$columns[] = (new column(
|
|
|
148 |
'archetype',
|
|
|
149 |
new lang_string('archetype', 'core_role'),
|
|
|
150 |
$this->get_entity_name(),
|
|
|
151 |
))
|
|
|
152 |
->add_joins($this->get_joins())
|
|
|
153 |
->add_field("{$rolealias}.archetype")
|
|
|
154 |
->add_callback(fn(?string $archetype) => match ($archetype) {
|
|
|
155 |
null => '',
|
|
|
156 |
'' => get_string('none'),
|
|
|
157 |
default => get_string("archetype{$archetype}", 'core_role'),
|
|
|
158 |
})
|
|
|
159 |
->set_is_sortable(true);
|
|
|
160 |
|
| 1 |
efrain |
161 |
// Description column.
|
|
|
162 |
$columns[] = (new column(
|
|
|
163 |
'description',
|
|
|
164 |
new lang_string('description'),
|
|
|
165 |
$this->get_entity_name()
|
|
|
166 |
))
|
|
|
167 |
->add_joins($this->get_joins())
|
|
|
168 |
->set_type(column::TYPE_LONGTEXT)
|
| 1441 |
ariadna |
169 |
->add_fields("{$rolealias}.description, {$rolealias}.shortname")
|
|
|
170 |
->set_is_sortable(true)
|
|
|
171 |
->add_callback(fn(?string $description, stdClass $role) => match ($description) {
|
|
|
172 |
null => '',
|
|
|
173 |
default => role_get_description($role),
|
| 1 |
efrain |
174 |
});
|
|
|
175 |
|
|
|
176 |
return $columns;
|
|
|
177 |
}
|
|
|
178 |
|
|
|
179 |
/**
|
|
|
180 |
* Return list of all available filters
|
|
|
181 |
*
|
|
|
182 |
* @return filter[]
|
|
|
183 |
*/
|
|
|
184 |
protected function get_all_filters(): array {
|
|
|
185 |
$rolealias = $this->get_table_alias('role');
|
|
|
186 |
|
|
|
187 |
// Name filter.
|
|
|
188 |
$filters[] = (new filter(
|
|
|
189 |
select::class,
|
|
|
190 |
'name',
|
|
|
191 |
new lang_string('rolefullname', 'core_role'),
|
|
|
192 |
$this->get_entity_name(),
|
|
|
193 |
"{$rolealias}.id"
|
|
|
194 |
))
|
|
|
195 |
->add_joins($this->get_joins())
|
|
|
196 |
->set_options_callback(static function(): array {
|
|
|
197 |
return role_get_names(null, ROLENAME_ORIGINAL, true);
|
|
|
198 |
});
|
|
|
199 |
|
| 1441 |
ariadna |
200 |
// Archetype filter.
|
|
|
201 |
$filters[] = (new filter(
|
|
|
202 |
select::class,
|
|
|
203 |
'archetype',
|
|
|
204 |
new lang_string('archetype', 'core_role'),
|
|
|
205 |
$this->get_entity_name(),
|
|
|
206 |
"{$rolealias}.archetype",
|
|
|
207 |
))
|
|
|
208 |
->add_joins($this->get_joins())
|
|
|
209 |
->set_options_callback(static function(): array {
|
|
|
210 |
return array_map(
|
|
|
211 |
fn(string $archetype) => get_string("archetype{$archetype}", 'core_role'),
|
|
|
212 |
get_role_archetypes(),
|
|
|
213 |
);
|
|
|
214 |
});
|
|
|
215 |
|
| 1 |
efrain |
216 |
return $filters;
|
|
|
217 |
}
|
|
|
218 |
}
|