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_reportbuilder;
|
|
|
20 |
|
|
|
21 |
use coding_exception;
|
|
|
22 |
use core_reportbuilder\local\helpers\report;
|
|
|
23 |
use core_reportbuilder\local\models\column as column_model;
|
|
|
24 |
use core_reportbuilder\local\models\filter as filter_model;
|
|
|
25 |
use core_reportbuilder\local\report\base;
|
|
|
26 |
use core_reportbuilder\local\report\column;
|
|
|
27 |
use core_reportbuilder\local\report\filter;
|
|
|
28 |
|
|
|
29 |
/**
|
|
|
30 |
* Class datasource
|
|
|
31 |
*
|
|
|
32 |
* @package core_reportbuilder
|
|
|
33 |
* @copyright 2021 David Matamoros <davidmc@moodle.com>
|
|
|
34 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
35 |
*/
|
|
|
36 |
abstract class datasource extends base {
|
|
|
37 |
|
|
|
38 |
/** @var float[] $elementsmodified Track the time elements of specific reports have been added, updated, removed */
|
|
|
39 |
private static $elementsmodified = [];
|
|
|
40 |
|
|
|
41 |
/** @var array $activecolumns */
|
|
|
42 |
private $activecolumns;
|
|
|
43 |
|
|
|
44 |
/** @var array $activefilters */
|
|
|
45 |
private $activefilters;
|
|
|
46 |
|
|
|
47 |
/** @var array $activeconditions */
|
|
|
48 |
private $activeconditions;
|
|
|
49 |
|
|
|
50 |
/**
|
|
|
51 |
* Add columns from the given entity name to be available to use in a custom report
|
|
|
52 |
*
|
|
|
53 |
* Wildcard matching is supported with '*' in both $include and $exclude, e.g. ['customfield*']
|
|
|
54 |
*
|
|
|
55 |
* @param string $entityname
|
|
|
56 |
* @param string[] $include Include only these columns, if omitted then include all
|
|
|
57 |
* @param string[] $exclude Exclude these columns, if omitted then exclude none
|
|
|
58 |
* @throws coding_exception If both $include and $exclude are non-empty
|
|
|
59 |
*/
|
|
|
60 |
final protected function add_columns_from_entity(string $entityname, array $include = [], array $exclude = []): void {
|
|
|
61 |
if (!empty($include) && !empty($exclude)) {
|
|
|
62 |
throw new coding_exception('Cannot specify columns to include and exclude simultaneously');
|
|
|
63 |
}
|
|
|
64 |
|
|
|
65 |
$entity = $this->get_entity($entityname);
|
|
|
66 |
|
|
|
67 |
// Retrieve filtered columns from entity, respecting given $include/$exclude parameters.
|
|
|
68 |
$columns = array_filter($entity->get_columns(), function(column $column) use ($include, $exclude): bool {
|
|
|
69 |
if (!empty($include)) {
|
|
|
70 |
return $this->report_element_search($column->get_name(), $include);
|
|
|
71 |
}
|
|
|
72 |
|
|
|
73 |
if (!empty($exclude)) {
|
|
|
74 |
return !$this->report_element_search($column->get_name(), $exclude);
|
|
|
75 |
}
|
|
|
76 |
|
|
|
77 |
return true;
|
|
|
78 |
});
|
|
|
79 |
|
|
|
80 |
foreach ($columns as $column) {
|
|
|
81 |
$this->add_column($column);
|
|
|
82 |
}
|
|
|
83 |
}
|
|
|
84 |
|
|
|
85 |
/**
|
|
|
86 |
* Add default datasource columns to the report
|
|
|
87 |
*
|
|
|
88 |
* Uses column data returned by the source {@see get_default_columns} and {@see get_default_column_sorting} methods
|
|
|
89 |
*
|
|
|
90 |
* @throws coding_exception If default column sorting refers to an invalid column
|
|
|
91 |
*/
|
|
|
92 |
public function add_default_columns(): void {
|
|
|
93 |
$reportid = $this->get_report_persistent()->get('id');
|
|
|
94 |
|
|
|
95 |
// Retrieve default column sorting, and track index of both sorted/non-sorted columns.
|
|
|
96 |
$columnidentifiers = $this->get_default_columns();
|
|
|
97 |
|
|
|
98 |
$defaultcolumnsorting = $this->get_default_column_sorting();
|
|
|
99 |
$defaultcolumnsortinginvalid = array_diff_key($defaultcolumnsorting,
|
|
|
100 |
array_fill_keys($columnidentifiers, 1));
|
|
|
101 |
|
|
|
102 |
if (count($defaultcolumnsortinginvalid) > 0) {
|
|
|
103 |
throw new coding_exception('Invalid column name', array_key_first($defaultcolumnsortinginvalid));
|
|
|
104 |
}
|
|
|
105 |
|
|
|
106 |
$columnnonsortingindex = count($defaultcolumnsorting) + 1;
|
|
|
107 |
|
|
|
108 |
foreach ($columnidentifiers as $uniqueidentifier) {
|
|
|
109 |
$column = report::add_report_column($reportid, $uniqueidentifier);
|
|
|
110 |
|
|
|
111 |
// After adding the column, toggle sorting according to defaults provided by the datasource.
|
|
|
112 |
$sortorder = array_search($uniqueidentifier, array_keys($defaultcolumnsorting));
|
|
|
113 |
if ($sortorder !== false) {
|
|
|
114 |
$column->set_many([
|
|
|
115 |
'sortenabled' => true,
|
|
|
116 |
'sortdirection' => $defaultcolumnsorting[$uniqueidentifier],
|
|
|
117 |
'sortorder' => $sortorder + 1,
|
|
|
118 |
])->update();
|
|
|
119 |
} else if (!empty($defaultcolumnsorting)) {
|
|
|
120 |
$column->set('sortorder', $columnnonsortingindex++)->update();
|
|
|
121 |
}
|
|
|
122 |
}
|
|
|
123 |
}
|
|
|
124 |
|
|
|
125 |
/**
|
|
|
126 |
* Return the default columns that will be added to the report upon creation, by {@see add_default_columns}
|
|
|
127 |
*
|
|
|
128 |
* @return string[]
|
|
|
129 |
*/
|
|
|
130 |
abstract public function get_default_columns(): array;
|
|
|
131 |
|
|
|
132 |
/**
|
|
|
133 |
* Return the default column sorting that will be set for the report upon creation, by {@see add_default_columns}
|
|
|
134 |
*
|
|
|
135 |
* When overriding this method in child classes, column identifiers specified must refer to default columns returned from
|
|
|
136 |
* the {@see get_default_columns} method
|
|
|
137 |
*
|
|
|
138 |
* @return int[] array [column identifier => SORT_ASC/SORT_DESC]
|
|
|
139 |
*/
|
|
|
140 |
public function get_default_column_sorting(): array {
|
|
|
141 |
return [];
|
|
|
142 |
}
|
|
|
143 |
|
|
|
144 |
/**
|
|
|
145 |
* Override parent method, returning only those columns specifically added to the custom report (rather than all that are
|
|
|
146 |
* available)
|
|
|
147 |
*
|
|
|
148 |
* @return column[]
|
|
|
149 |
*/
|
|
|
150 |
public function get_active_columns(): array {
|
|
|
151 |
$reportid = $this->get_report_persistent()->get('id');
|
|
|
152 |
|
|
|
153 |
// Determine whether we already retrieved the columns since the report was last modified.
|
|
|
154 |
self::$elementsmodified += [$reportid => -1];
|
|
|
155 |
if ($this->activecolumns !== null && $this->activecolumns['builttime'] > self::$elementsmodified[$reportid]) {
|
|
|
156 |
return $this->activecolumns['values'];
|
|
|
157 |
}
|
|
|
158 |
|
|
|
159 |
$this->activecolumns = ['builttime' => microtime(true), 'values' => []];
|
|
|
160 |
|
|
|
161 |
$activecolumns = column_model::get_records(['reportid' => $reportid], 'columnorder');
|
|
|
162 |
foreach ($activecolumns as $index => $column) {
|
|
|
163 |
$instance = $this->get_column($column->get('uniqueidentifier'));
|
|
|
164 |
|
|
|
165 |
// Ensure the column is still present and available.
|
|
|
166 |
if ($instance !== null && $instance->get_is_available()) {
|
|
|
167 |
if ($instance->get_is_deprecated()) {
|
|
|
168 |
debugging("The column '{$instance->get_unique_identifier()}' is deprecated, please do not use it any more." .
|
|
|
169 |
" {$instance->get_is_deprecated_message()}", DEBUG_DEVELOPER);
|
|
|
170 |
}
|
|
|
171 |
|
|
|
172 |
// We should clone the report column to ensure if it's added twice to a report, each operates independently.
|
|
|
173 |
$this->activecolumns['values'][] = clone $instance
|
|
|
174 |
->set_index($index)
|
|
|
175 |
->set_persistent($column)
|
|
|
176 |
->set_aggregation($column->get('aggregation'));
|
|
|
177 |
}
|
|
|
178 |
}
|
|
|
179 |
|
|
|
180 |
return $this->activecolumns['values'];
|
|
|
181 |
}
|
|
|
182 |
|
|
|
183 |
/**
|
|
|
184 |
* Add filters from the given entity name to be available to use in a custom report
|
|
|
185 |
*
|
|
|
186 |
* Wildcard matching is supported with '*' in both $include and $exclude, e.g. ['customfield*']
|
|
|
187 |
*
|
|
|
188 |
* @param string $entityname
|
|
|
189 |
* @param string[] $include Include only these filters, if omitted then include all
|
|
|
190 |
* @param string[] $exclude Exclude these filters, if omitted then exclude none
|
|
|
191 |
* @throws coding_exception If both $include and $exclude are non-empty
|
|
|
192 |
*/
|
|
|
193 |
final protected function add_filters_from_entity(string $entityname, array $include = [], array $exclude = []): void {
|
|
|
194 |
if (!empty($include) && !empty($exclude)) {
|
|
|
195 |
throw new coding_exception('Cannot specify filters to include and exclude simultaneously');
|
|
|
196 |
}
|
|
|
197 |
|
|
|
198 |
$entity = $this->get_entity($entityname);
|
|
|
199 |
|
|
|
200 |
// Retrieve filtered filters from entity, respecting given $include/$exclude parameters.
|
|
|
201 |
$filters = array_filter($entity->get_filters(), function(filter $filter) use ($include, $exclude): bool {
|
|
|
202 |
if (!empty($include)) {
|
|
|
203 |
return $this->report_element_search($filter->get_name(), $include);
|
|
|
204 |
}
|
|
|
205 |
|
|
|
206 |
if (!empty($exclude)) {
|
|
|
207 |
return !$this->report_element_search($filter->get_name(), $exclude);
|
|
|
208 |
}
|
|
|
209 |
|
|
|
210 |
return true;
|
|
|
211 |
});
|
|
|
212 |
|
|
|
213 |
foreach ($filters as $filter) {
|
|
|
214 |
$this->add_filter($filter);
|
|
|
215 |
}
|
|
|
216 |
}
|
|
|
217 |
|
|
|
218 |
/**
|
|
|
219 |
* Add default datasource filters to the report
|
|
|
220 |
*
|
|
|
221 |
* This method is optional and can be called when the report is created to add the default filters defined in the
|
|
|
222 |
* selected datasource.
|
|
|
223 |
*/
|
|
|
224 |
public function add_default_filters(): void {
|
|
|
225 |
$reportid = $this->get_report_persistent()->get('id');
|
|
|
226 |
$filteridentifiers = $this->get_default_filters();
|
|
|
227 |
foreach ($filteridentifiers as $uniqueidentifier) {
|
|
|
228 |
report::add_report_filter($reportid, $uniqueidentifier);
|
|
|
229 |
}
|
|
|
230 |
}
|
|
|
231 |
|
|
|
232 |
/**
|
|
|
233 |
* Return the filters that will be added to the report once is created
|
|
|
234 |
*
|
|
|
235 |
* @return string[]
|
|
|
236 |
*/
|
|
|
237 |
abstract public function get_default_filters(): array;
|
|
|
238 |
|
|
|
239 |
/**
|
|
|
240 |
* Override parent method, returning only those filters specifically added to the custom report (rather than all that are
|
|
|
241 |
* available)
|
|
|
242 |
*
|
|
|
243 |
* @return filter[]
|
|
|
244 |
*/
|
|
|
245 |
public function get_active_filters(): array {
|
|
|
246 |
$reportid = $this->get_report_persistent()->get('id');
|
|
|
247 |
|
|
|
248 |
// Determine whether we already retrieved the filters since the report was last modified.
|
|
|
249 |
self::$elementsmodified += [$reportid => -1];
|
|
|
250 |
if ($this->activefilters !== null && $this->activefilters['builttime'] > self::$elementsmodified[$reportid]) {
|
|
|
251 |
return $this->activefilters['values'];
|
|
|
252 |
}
|
|
|
253 |
|
|
|
254 |
$this->activefilters = ['builttime' => microtime(true), 'values' => []];
|
|
|
255 |
|
|
|
256 |
$activefilters = filter_model::get_filter_records($reportid, 'filterorder');
|
|
|
257 |
foreach ($activefilters as $filter) {
|
|
|
258 |
$instance = $this->get_filter($filter->get('uniqueidentifier'));
|
|
|
259 |
|
|
|
260 |
// Ensure the filter is still present and available.
|
|
|
261 |
if ($instance !== null && $instance->get_is_available()) {
|
|
|
262 |
if ($instance->get_is_deprecated()) {
|
|
|
263 |
debugging("The filter '{$instance->get_unique_identifier()}' is deprecated, please do not use it any more." .
|
|
|
264 |
" {$instance->get_is_deprecated_message()}", DEBUG_DEVELOPER);
|
|
|
265 |
}
|
|
|
266 |
|
|
|
267 |
$this->activefilters['values'][$instance->get_unique_identifier()] =
|
|
|
268 |
$instance->set_persistent($filter);
|
|
|
269 |
}
|
|
|
270 |
}
|
|
|
271 |
|
|
|
272 |
return $this->activefilters['values'];
|
|
|
273 |
}
|
|
|
274 |
|
|
|
275 |
/**
|
|
|
276 |
* Add conditions from the given entity name to be available to use in a custom report
|
|
|
277 |
*
|
|
|
278 |
* Wildcard matching is supported with '*' in both $include and $exclude, e.g. ['customfield*']
|
|
|
279 |
*
|
|
|
280 |
* @param string $entityname
|
|
|
281 |
* @param string[] $include Include only these conditions, if omitted then include all
|
|
|
282 |
* @param string[] $exclude Exclude these conditions, if omitted then exclude none
|
|
|
283 |
* @throws coding_exception If both $include and $exclude are non-empty
|
|
|
284 |
*/
|
|
|
285 |
final protected function add_conditions_from_entity(string $entityname, array $include = [], array $exclude = []): void {
|
|
|
286 |
if (!empty($include) && !empty($exclude)) {
|
|
|
287 |
throw new coding_exception('Cannot specify conditions to include and exclude simultaneously');
|
|
|
288 |
}
|
|
|
289 |
|
|
|
290 |
$entity = $this->get_entity($entityname);
|
|
|
291 |
|
|
|
292 |
// Retrieve filtered conditions from entity, respecting given $include/$exclude parameters.
|
|
|
293 |
$conditions = array_filter($entity->get_conditions(), function(filter $condition) use ($include, $exclude): bool {
|
|
|
294 |
if (!empty($include)) {
|
|
|
295 |
return $this->report_element_search($condition->get_name(), $include);
|
|
|
296 |
}
|
|
|
297 |
|
|
|
298 |
if (!empty($exclude)) {
|
|
|
299 |
return !$this->report_element_search($condition->get_name(), $exclude);
|
|
|
300 |
}
|
|
|
301 |
|
|
|
302 |
return true;
|
|
|
303 |
});
|
|
|
304 |
|
|
|
305 |
foreach ($conditions as $condition) {
|
|
|
306 |
$this->add_condition($condition);
|
|
|
307 |
}
|
|
|
308 |
}
|
|
|
309 |
|
|
|
310 |
/**
|
|
|
311 |
* Add default datasource conditions to the report
|
|
|
312 |
*
|
|
|
313 |
* This method is optional and can be called when the report is created to add the default conditions defined in the
|
|
|
314 |
* selected datasource.
|
|
|
315 |
*/
|
|
|
316 |
public function add_default_conditions(): void {
|
|
|
317 |
$reportid = $this->get_report_persistent()->get('id');
|
|
|
318 |
$conditionidentifiers = $this->get_default_conditions();
|
|
|
319 |
foreach ($conditionidentifiers as $uniqueidentifier) {
|
|
|
320 |
report::add_report_condition($reportid, $uniqueidentifier);
|
|
|
321 |
}
|
|
|
322 |
|
|
|
323 |
// Set the default condition values if they have been set in the datasource.
|
|
|
324 |
$this->set_condition_values($this->get_default_condition_values());
|
|
|
325 |
}
|
|
|
326 |
|
|
|
327 |
/**
|
|
|
328 |
* Return the conditions that will be added to the report once is created
|
|
|
329 |
*
|
|
|
330 |
* @return string[]
|
|
|
331 |
*/
|
|
|
332 |
abstract public function get_default_conditions(): array;
|
|
|
333 |
|
|
|
334 |
/**
|
|
|
335 |
* Return the default condition values that will be added to the report once is created
|
|
|
336 |
*
|
|
|
337 |
* For any of the default conditions returned by the method {@see get_default_conditions} is
|
|
|
338 |
* possible to set the initial values.
|
|
|
339 |
*
|
|
|
340 |
* @return array
|
|
|
341 |
*/
|
|
|
342 |
public function get_default_condition_values(): array {
|
|
|
343 |
return [];
|
|
|
344 |
}
|
|
|
345 |
|
|
|
346 |
/**
|
|
|
347 |
* Override parent method, returning only those conditions specifically added to the custom report (rather than all that are
|
|
|
348 |
* available)
|
|
|
349 |
*
|
|
|
350 |
* @return filter[]
|
|
|
351 |
*/
|
|
|
352 |
public function get_active_conditions(): array {
|
|
|
353 |
$reportid = $this->get_report_persistent()->get('id');
|
|
|
354 |
|
|
|
355 |
// Determine whether we already retrieved the conditions since the report was last modified.
|
|
|
356 |
self::$elementsmodified += [$reportid => -1];
|
|
|
357 |
if ($this->activeconditions !== null && $this->activeconditions['builttime'] > self::$elementsmodified[$reportid]) {
|
|
|
358 |
return $this->activeconditions['values'];
|
|
|
359 |
}
|
|
|
360 |
|
|
|
361 |
$this->activeconditions = ['builttime' => microtime(true), 'values' => []];
|
|
|
362 |
|
|
|
363 |
$activeconditions = filter_model::get_condition_records($reportid, 'filterorder');
|
|
|
364 |
foreach ($activeconditions as $condition) {
|
|
|
365 |
$instance = $this->get_condition($condition->get('uniqueidentifier'));
|
|
|
366 |
|
|
|
367 |
// Ensure the condition is still present and available.
|
|
|
368 |
if ($instance !== null && $instance->get_is_available()) {
|
|
|
369 |
if ($instance->get_is_deprecated()) {
|
|
|
370 |
debugging("The condition '{$instance->get_unique_identifier()}' is deprecated, please do not use it any more." .
|
|
|
371 |
" {$instance->get_is_deprecated_message()}", DEBUG_DEVELOPER);
|
|
|
372 |
}
|
|
|
373 |
|
|
|
374 |
$this->activeconditions['values'][$instance->get_unique_identifier()] =
|
|
|
375 |
$instance->set_persistent($condition);
|
|
|
376 |
}
|
|
|
377 |
}
|
|
|
378 |
|
|
|
379 |
return $this->activeconditions['values'];
|
|
|
380 |
}
|
|
|
381 |
|
|
|
382 |
/**
|
|
|
383 |
* Adds all columns/filters/conditions from the given entity to the report at once
|
|
|
384 |
*
|
|
|
385 |
* @param string $entityname
|
|
|
386 |
* @param string[] $limitcolumns Include only these columns
|
|
|
387 |
* @param string[] $limitfilters Include only these filters
|
|
|
388 |
* @param string[] $limitconditions Include only these conditions
|
|
|
389 |
*/
|
|
|
390 |
final protected function add_all_from_entity(
|
|
|
391 |
string $entityname,
|
|
|
392 |
array $limitcolumns = [],
|
|
|
393 |
array $limitfilters = [],
|
|
|
394 |
array $limitconditions = [],
|
|
|
395 |
): void {
|
|
|
396 |
$this->add_columns_from_entity($entityname, $limitcolumns);
|
|
|
397 |
$this->add_filters_from_entity($entityname, $limitfilters);
|
|
|
398 |
$this->add_conditions_from_entity($entityname, $limitconditions);
|
|
|
399 |
}
|
|
|
400 |
|
|
|
401 |
/**
|
|
|
402 |
* Adds all columns/filters/conditions from all the entities added to the report at once
|
|
|
403 |
*/
|
|
|
404 |
final protected function add_all_from_entities(): void {
|
|
|
405 |
foreach ($this->get_entities() as $entity) {
|
|
|
406 |
$this->add_all_from_entity($entity->get_entity_name());
|
|
|
407 |
}
|
|
|
408 |
}
|
|
|
409 |
|
|
|
410 |
/**
|
|
|
411 |
* Indicate that report elements have been modified, e.g. columns/filters/conditions have been added, removed or updated
|
|
|
412 |
*
|
|
|
413 |
* @param int $reportid
|
|
|
414 |
*/
|
|
|
415 |
final public static function report_elements_modified(int $reportid): void {
|
|
|
416 |
self::$elementsmodified[$reportid] = microtime(true);
|
|
|
417 |
}
|
|
|
418 |
|
|
|
419 |
/**
|
|
|
420 |
* Search for given element within list of search items, supporting '*' wildcards
|
|
|
421 |
*
|
|
|
422 |
* @param string $element
|
|
|
423 |
* @param string[] $search
|
|
|
424 |
* @return bool
|
|
|
425 |
*/
|
|
|
426 |
private function report_element_search(string $element, array $search): bool {
|
|
|
427 |
foreach ($search as $item) {
|
|
|
428 |
// Simple matching.
|
|
|
429 |
if ($element === $item) {
|
|
|
430 |
return true;
|
|
|
431 |
}
|
|
|
432 |
|
|
|
433 |
// Wildcard matching.
|
|
|
434 |
if (strpos($item, '*') !== false) {
|
|
|
435 |
$pattern = '/^' . str_replace('\*', '.*', preg_quote($item)) . '$/';
|
|
|
436 |
return (bool) preg_match($pattern, $element);
|
|
|
437 |
}
|
|
|
438 |
}
|
|
|
439 |
|
|
|
440 |
return false;
|
|
|
441 |
}
|
|
|
442 |
}
|