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_cohort\reportbuilder\local\entities;
|
|
|
20 |
|
|
|
21 |
use context;
|
|
|
22 |
use context_helper;
|
|
|
23 |
use lang_string;
|
|
|
24 |
use stdClass;
|
|
|
25 |
use theme_config;
|
|
|
26 |
use core_reportbuilder\local\entities\base;
|
|
|
27 |
use core_reportbuilder\local\filters\boolean_select;
|
|
|
28 |
use core_reportbuilder\local\filters\cohort as cohort_filter;
|
|
|
29 |
use core_reportbuilder\local\filters\date;
|
|
|
30 |
use core_reportbuilder\local\filters\select;
|
|
|
31 |
use core_reportbuilder\local\filters\text;
|
|
|
32 |
use core_reportbuilder\local\helpers\custom_fields;
|
|
|
33 |
use core_reportbuilder\local\helpers\format;
|
|
|
34 |
use core_reportbuilder\local\report\column;
|
|
|
35 |
use core_reportbuilder\local\report\filter;
|
|
|
36 |
|
|
|
37 |
/**
|
|
|
38 |
* Cohort entity
|
|
|
39 |
*
|
|
|
40 |
* @package core_cohort
|
|
|
41 |
* @copyright 2021 Paul Holden <paulh@moodle.com>
|
|
|
42 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
43 |
*/
|
|
|
44 |
class cohort extends base {
|
|
|
45 |
|
|
|
46 |
/**
|
|
|
47 |
* Database tables that this entity uses
|
|
|
48 |
*
|
|
|
49 |
* @return string[]
|
|
|
50 |
*/
|
|
|
51 |
protected function get_default_tables(): array {
|
|
|
52 |
return [
|
|
|
53 |
'cohort',
|
|
|
54 |
'context',
|
|
|
55 |
];
|
|
|
56 |
}
|
|
|
57 |
|
|
|
58 |
/**
|
|
|
59 |
* The default title for this entity
|
|
|
60 |
*
|
|
|
61 |
* @return lang_string
|
|
|
62 |
*/
|
|
|
63 |
protected function get_default_entity_title(): lang_string {
|
|
|
64 |
return new lang_string('cohort', 'core_cohort');
|
|
|
65 |
}
|
|
|
66 |
|
|
|
67 |
/**
|
|
|
68 |
* Initialise the entity
|
|
|
69 |
*
|
|
|
70 |
* @return base
|
|
|
71 |
*/
|
|
|
72 |
public function initialise(): base {
|
|
|
73 |
$tablealias = $this->get_table_alias('cohort');
|
|
|
74 |
|
|
|
75 |
$customfields = (new custom_fields(
|
|
|
76 |
"{$tablealias}.id",
|
|
|
77 |
$this->get_entity_name(),
|
|
|
78 |
'core_cohort',
|
|
|
79 |
'cohort',
|
|
|
80 |
))
|
|
|
81 |
->add_joins($this->get_joins());
|
|
|
82 |
|
|
|
83 |
$columns = array_merge($this->get_all_columns(), $customfields->get_columns());
|
|
|
84 |
foreach ($columns as $column) {
|
|
|
85 |
$this->add_column($column);
|
|
|
86 |
}
|
|
|
87 |
|
|
|
88 |
// All the filters defined by the entity can also be used as conditions.
|
|
|
89 |
$filters = array_merge($this->get_all_filters(), $customfields->get_filters());
|
|
|
90 |
foreach ($filters as $filter) {
|
|
|
91 |
$this
|
|
|
92 |
->add_filter($filter)
|
|
|
93 |
->add_condition($filter);
|
|
|
94 |
}
|
|
|
95 |
|
|
|
96 |
return $this;
|
|
|
97 |
}
|
|
|
98 |
|
|
|
99 |
/**
|
|
|
100 |
* Returns list of all available columns
|
|
|
101 |
*
|
|
|
102 |
* @return column[]
|
|
|
103 |
*/
|
|
|
104 |
protected function get_all_columns(): array {
|
|
|
105 |
$tablealias = $this->get_table_alias('cohort');
|
|
|
106 |
$contextalias = $this->get_table_alias('context');
|
|
|
107 |
|
|
|
108 |
// Category/context column.
|
|
|
109 |
$columns[] = (new column(
|
|
|
110 |
'context',
|
|
|
111 |
new lang_string('category'),
|
|
|
112 |
$this->get_entity_name()
|
|
|
113 |
))
|
|
|
114 |
->add_joins($this->get_joins())
|
|
|
115 |
->add_join($this->get_context_join())
|
|
|
116 |
->set_type(column::TYPE_TEXT)
|
|
|
117 |
->add_fields("{$tablealias}.contextid, " . context_helper::get_preload_record_columns_sql($contextalias))
|
|
|
118 |
->set_is_sortable(true)
|
|
|
119 |
->add_callback(static function($contextid, stdClass $cohort): string {
|
|
|
120 |
if ($contextid === null) {
|
|
|
121 |
return '';
|
|
|
122 |
}
|
|
|
123 |
|
|
|
124 |
context_helper::preload_from_record($cohort);
|
|
|
125 |
return context::instance_by_id($cohort->contextid)->get_context_name(false);
|
|
|
126 |
});
|
|
|
127 |
|
|
|
128 |
// Name column.
|
|
|
129 |
$columns[] = (new column(
|
|
|
130 |
'name',
|
|
|
131 |
new lang_string('name', 'core_cohort'),
|
|
|
132 |
$this->get_entity_name()
|
|
|
133 |
))
|
|
|
134 |
->add_joins($this->get_joins())
|
|
|
135 |
->set_type(column::TYPE_TEXT)
|
|
|
136 |
->add_fields("{$tablealias}.name")
|
|
|
137 |
->set_is_sortable(true);
|
|
|
138 |
|
|
|
139 |
// ID number column.
|
|
|
140 |
$columns[] = (new column(
|
|
|
141 |
'idnumber',
|
|
|
142 |
new lang_string('idnumber', 'core_cohort'),
|
|
|
143 |
$this->get_entity_name()
|
|
|
144 |
))
|
|
|
145 |
->add_joins($this->get_joins())
|
|
|
146 |
->set_type(column::TYPE_TEXT)
|
|
|
147 |
->add_fields("{$tablealias}.idnumber")
|
|
|
148 |
->set_is_sortable(true);
|
|
|
149 |
|
|
|
150 |
// Description column.
|
|
|
151 |
$columns[] = (new column(
|
|
|
152 |
'description',
|
|
|
153 |
new lang_string('description'),
|
|
|
154 |
$this->get_entity_name()
|
|
|
155 |
))
|
|
|
156 |
->add_joins($this->get_joins())
|
|
|
157 |
->add_join($this->get_context_join())
|
|
|
158 |
->set_type(column::TYPE_LONGTEXT)
|
1441 |
ariadna |
159 |
->add_fields("{$tablealias}.description, {$tablealias}.descriptionformat, {$tablealias}.id, {$tablealias}.contextid")
|
1 |
efrain |
160 |
->add_fields(context_helper::get_preload_record_columns_sql($contextalias))
|
1441 |
ariadna |
161 |
->set_is_sortable(true)
|
1 |
efrain |
162 |
->add_callback(static function(?string $description, stdClass $cohort): string {
|
|
|
163 |
global $CFG;
|
|
|
164 |
require_once("{$CFG->libdir}/filelib.php");
|
|
|
165 |
|
|
|
166 |
if ($description === null) {
|
|
|
167 |
return '';
|
|
|
168 |
}
|
|
|
169 |
|
|
|
170 |
context_helper::preload_from_record($cohort);
|
|
|
171 |
$context = context::instance_by_id($cohort->contextid);
|
|
|
172 |
|
|
|
173 |
$description = file_rewrite_pluginfile_urls($description, 'pluginfile.php', $context->id, 'cohort',
|
|
|
174 |
'description', $cohort->id);
|
|
|
175 |
|
|
|
176 |
return format_text($description, $cohort->descriptionformat, ['context' => $context->id]);
|
|
|
177 |
});
|
|
|
178 |
|
|
|
179 |
// Visible column.
|
|
|
180 |
$columns[] = (new column(
|
|
|
181 |
'visible',
|
|
|
182 |
new lang_string('visible', 'core_cohort'),
|
|
|
183 |
$this->get_entity_name()
|
|
|
184 |
))
|
|
|
185 |
->add_joins($this->get_joins())
|
|
|
186 |
->set_type(column::TYPE_BOOLEAN)
|
|
|
187 |
->add_fields("{$tablealias}.visible")
|
|
|
188 |
->set_is_sortable(true)
|
|
|
189 |
->set_callback([format::class, 'boolean_as_text']);
|
|
|
190 |
|
|
|
191 |
// Time created column.
|
|
|
192 |
$columns[] = (new column(
|
|
|
193 |
'timecreated',
|
|
|
194 |
new lang_string('timecreated', 'core_reportbuilder'),
|
|
|
195 |
$this->get_entity_name()
|
|
|
196 |
))
|
|
|
197 |
->add_joins($this->get_joins())
|
|
|
198 |
->set_type(column::TYPE_TIMESTAMP)
|
|
|
199 |
->add_fields("{$tablealias}.timecreated")
|
|
|
200 |
->set_is_sortable(true)
|
|
|
201 |
->set_callback([format::class, 'userdate']);
|
|
|
202 |
|
|
|
203 |
// Time modified column.
|
|
|
204 |
$columns[] = (new column(
|
|
|
205 |
'timemodified',
|
|
|
206 |
new lang_string('timemodified', 'core_reportbuilder'),
|
|
|
207 |
$this->get_entity_name()
|
|
|
208 |
))
|
|
|
209 |
->add_joins($this->get_joins())
|
|
|
210 |
->set_type(column::TYPE_TIMESTAMP)
|
|
|
211 |
->add_fields("{$tablealias}.timemodified")
|
|
|
212 |
->set_is_sortable(true)
|
|
|
213 |
->set_callback([format::class, 'userdate']);
|
|
|
214 |
|
|
|
215 |
// Component column.
|
|
|
216 |
$columns[] = (new column(
|
|
|
217 |
'component',
|
|
|
218 |
new lang_string('component', 'core_cohort'),
|
|
|
219 |
$this->get_entity_name()
|
|
|
220 |
))
|
|
|
221 |
->add_joins($this->get_joins())
|
|
|
222 |
->set_type(column::TYPE_TEXT)
|
|
|
223 |
->add_fields("{$tablealias}.component")
|
|
|
224 |
->set_is_sortable(true)
|
|
|
225 |
->add_callback(static function(?string $component): string {
|
|
|
226 |
if ($component === null) {
|
|
|
227 |
return '';
|
|
|
228 |
}
|
|
|
229 |
|
|
|
230 |
return $component === ''
|
|
|
231 |
? get_string('nocomponent', 'cohort')
|
|
|
232 |
: get_string('pluginname', $component);
|
|
|
233 |
});
|
|
|
234 |
|
|
|
235 |
// Theme column.
|
|
|
236 |
$columns[] = (new column(
|
|
|
237 |
'theme',
|
|
|
238 |
new lang_string('theme'),
|
|
|
239 |
$this->get_entity_name()
|
|
|
240 |
))
|
|
|
241 |
->add_joins($this->get_joins())
|
|
|
242 |
->set_type(column::TYPE_TEXT)
|
|
|
243 |
->add_fields("{$tablealias}.theme")
|
|
|
244 |
->set_is_sortable(true)
|
|
|
245 |
->add_callback(static function (?string $theme): string {
|
|
|
246 |
if ((string) $theme === '') {
|
|
|
247 |
return '';
|
|
|
248 |
}
|
|
|
249 |
|
|
|
250 |
return get_string('pluginname', "theme_{$theme}");
|
|
|
251 |
});
|
|
|
252 |
|
|
|
253 |
return $columns;
|
|
|
254 |
}
|
|
|
255 |
|
|
|
256 |
/**
|
|
|
257 |
* Return list of all available filters
|
|
|
258 |
*
|
|
|
259 |
* @return filter[]
|
|
|
260 |
*/
|
|
|
261 |
protected function get_all_filters(): array {
|
|
|
262 |
$tablealias = $this->get_table_alias('cohort');
|
|
|
263 |
|
|
|
264 |
// Cohort select filter.
|
|
|
265 |
$filters[] = (new filter(
|
|
|
266 |
cohort_filter::class,
|
|
|
267 |
'cohortselect',
|
|
|
268 |
new lang_string('selectcohort', 'core_cohort'),
|
|
|
269 |
$this->get_entity_name(),
|
|
|
270 |
"{$tablealias}.id"
|
|
|
271 |
))
|
|
|
272 |
->add_joins($this->get_joins());
|
|
|
273 |
|
|
|
274 |
// Context filter.
|
|
|
275 |
$filters[] = (new filter(
|
|
|
276 |
select::class,
|
|
|
277 |
'context',
|
|
|
278 |
new lang_string('category'),
|
|
|
279 |
$this->get_entity_name(),
|
|
|
280 |
"{$tablealias}.contextid"
|
|
|
281 |
))
|
|
|
282 |
->add_joins($this->get_joins())
|
|
|
283 |
->set_options_callback(static function(): array {
|
|
|
284 |
global $DB;
|
|
|
285 |
|
|
|
286 |
// Load all contexts in which there are cohorts.
|
|
|
287 |
$ctxfields = context_helper::get_preload_record_columns_sql('ctx');
|
|
|
288 |
$contexts = $DB->get_records_sql("
|
|
|
289 |
SELECT DISTINCT {$ctxfields}, c.contextid
|
|
|
290 |
FROM {context} ctx
|
|
|
291 |
JOIN {cohort} c ON c.contextid = ctx.id");
|
|
|
292 |
|
|
|
293 |
// Transform context record into it's name (used as the filter options).
|
|
|
294 |
return array_map(static function(stdClass $contextrecord): string {
|
|
|
295 |
context_helper::preload_from_record($contextrecord);
|
|
|
296 |
|
|
|
297 |
return context::instance_by_id($contextrecord->contextid)
|
|
|
298 |
->get_context_name(false);
|
|
|
299 |
}, $contexts);
|
|
|
300 |
});
|
|
|
301 |
|
|
|
302 |
// Name filter.
|
|
|
303 |
$filters[] = (new filter(
|
|
|
304 |
text::class,
|
|
|
305 |
'name',
|
|
|
306 |
new lang_string('name', 'core_cohort'),
|
|
|
307 |
$this->get_entity_name(),
|
|
|
308 |
"{$tablealias}.name"
|
|
|
309 |
))
|
|
|
310 |
->add_joins($this->get_joins());
|
|
|
311 |
|
|
|
312 |
// ID number filter.
|
|
|
313 |
$filters[] = (new filter(
|
|
|
314 |
text::class,
|
|
|
315 |
'idnumber',
|
|
|
316 |
new lang_string('idnumber', 'core_cohort'),
|
|
|
317 |
$this->get_entity_name(),
|
|
|
318 |
"{$tablealias}.idnumber"
|
|
|
319 |
))
|
|
|
320 |
->add_joins($this->get_joins());
|
|
|
321 |
|
|
|
322 |
// Time created filter.
|
|
|
323 |
$filters[] = (new filter(
|
|
|
324 |
date::class,
|
|
|
325 |
'timecreated',
|
|
|
326 |
new lang_string('timecreated', 'core_reportbuilder'),
|
|
|
327 |
$this->get_entity_name(),
|
|
|
328 |
"{$tablealias}.timecreated"
|
|
|
329 |
))
|
|
|
330 |
->add_joins($this->get_joins());
|
|
|
331 |
|
|
|
332 |
// Description filter.
|
|
|
333 |
$filters[] = (new filter(
|
|
|
334 |
text::class,
|
|
|
335 |
'description',
|
|
|
336 |
new lang_string('description'),
|
|
|
337 |
$this->get_entity_name(),
|
1441 |
ariadna |
338 |
"{$tablealias}.description"
|
1 |
efrain |
339 |
))
|
|
|
340 |
->add_joins($this->get_joins());
|
|
|
341 |
|
|
|
342 |
// Theme filter.
|
|
|
343 |
$filters[] = (new filter(
|
|
|
344 |
select::class,
|
|
|
345 |
'theme',
|
|
|
346 |
new lang_string('theme'),
|
|
|
347 |
$this->get_entity_name(),
|
|
|
348 |
"{$tablealias}.theme",
|
|
|
349 |
))
|
|
|
350 |
->set_options_callback(static function(): array {
|
|
|
351 |
return array_map(
|
|
|
352 |
fn(theme_config $theme) => $theme->get_theme_name(),
|
|
|
353 |
get_list_of_themes(),
|
|
|
354 |
);
|
|
|
355 |
})
|
|
|
356 |
->add_joins($this->get_joins());
|
|
|
357 |
|
|
|
358 |
// Visible filter.
|
|
|
359 |
$filters[] = (new filter(
|
|
|
360 |
boolean_select::class,
|
|
|
361 |
'visible',
|
|
|
362 |
new lang_string('visible', 'core_cohort'),
|
|
|
363 |
$this->get_entity_name(),
|
|
|
364 |
"{$tablealias}.visible"
|
|
|
365 |
))
|
|
|
366 |
->add_joins($this->get_joins());
|
|
|
367 |
|
|
|
368 |
return $filters;
|
|
|
369 |
}
|
|
|
370 |
|
|
|
371 |
/**
|
|
|
372 |
* Return context join used by columns
|
|
|
373 |
*
|
|
|
374 |
* @return string
|
|
|
375 |
*/
|
|
|
376 |
private function get_context_join(): string {
|
|
|
377 |
|
|
|
378 |
// If the context table is already joined, we don't need to do that again.
|
|
|
379 |
if ($this->has_table_join_alias('context')) {
|
|
|
380 |
return '';
|
|
|
381 |
}
|
|
|
382 |
|
|
|
383 |
$tablealias = $this->get_table_alias('cohort');
|
|
|
384 |
$contextalias = $this->get_table_alias('context');
|
|
|
385 |
|
|
|
386 |
return "LEFT JOIN {context} {$contextalias} ON {$contextalias}.id = {$tablealias}.contextid";
|
|
|
387 |
}
|
|
|
388 |
}
|