mirrored from git://git.moodle.org/moodle.git
/
report.php
446 lines (382 loc) · 14.9 KB
/
report.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
declare(strict_types=1);
namespace core_reportbuilder\local\helpers;
use stdClass;
use invalid_parameter_exception;
use core\persistent;
use core_reportbuilder\datasource;
use core_reportbuilder\manager;
use core_reportbuilder\local\models\column;
use core_reportbuilder\local\models\filter;
use core_reportbuilder\local\models\report as report_model;
/**
* Helper class for manipulating custom reports and their elements (columns, filters, conditions, etc)
*
* @package core_reportbuilder
* @copyright 2021 Paul Holden <paulh@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class report {
/**
* Create custom report
*
* @param stdClass $data
* @param bool $default If $default is set to true it will populate report with default layout as defined by the selected
* source. These include pre-defined columns, filters and conditions.
* @return report_model
*/
public static function create_report(stdClass $data, bool $default = true): report_model {
$data->name = trim($data->name);
$data->type = datasource::TYPE_CUSTOM_REPORT;
$reportpersistent = manager::create_report_persistent($data);
// Add datasource default columns, filters and conditions to the report.
if ($default) {
$source = $reportpersistent->get('source');
/** @var datasource $datasource */
$datasource = new $source($reportpersistent, []);
$datasource->add_default_columns();
$datasource->add_default_filters();
$datasource->add_default_conditions();
}
return $reportpersistent;
}
/**
* Update custom report
*
* @param stdClass $data
* @return report_model
*/
public static function update_report(stdClass $data): report_model {
$report = report_model::get_record(['id' => $data->id, 'type' => datasource::TYPE_CUSTOM_REPORT]);
if ($report === false) {
throw new invalid_parameter_exception('Invalid report');
}
$report->set_many([
'name' => trim($data->name),
'uniquerows' => $data->uniquerows,
])->update();
return $report;
}
/**
* Delete custom report
*
* @param int $reportid
* @return bool
* @throws invalid_parameter_exception
*/
public static function delete_report(int $reportid): bool {
$report = report_model::get_record(['id' => $reportid, 'type' => datasource::TYPE_CUSTOM_REPORT]);
if ($report === false) {
throw new invalid_parameter_exception('Invalid report');
}
return $report->delete();
}
/**
* Add given column to report
*
* @param int $reportid
* @param string $uniqueidentifier
* @return column
* @throws invalid_parameter_exception
*/
public static function add_report_column(int $reportid, string $uniqueidentifier): column {
$report = manager::get_report_from_id($reportid);
if (!array_key_exists($uniqueidentifier, $report->get_columns())) {
throw new invalid_parameter_exception('Invalid column');
}
$column = new column(0, (object) [
'reportid' => $reportid,
'uniqueidentifier' => $uniqueidentifier,
'columnorder' => column::get_max_columnorder($reportid, 'columnorder') + 1,
'sortorder' => column::get_max_columnorder($reportid, 'sortorder') + 1,
]);
return $column->create();
}
/**
* Delete given column from report
*
* @param int $reportid
* @param int $columnid
* @return bool
* @throws invalid_parameter_exception
*/
public static function delete_report_column(int $reportid, int $columnid): bool {
global $DB;
$column = column::get_record(['id' => $columnid, 'reportid' => $reportid]);
if ($column === false) {
throw new invalid_parameter_exception('Invalid column');
}
// After deletion, re-index remaining report columns.
if ($result = $column->delete()) {
$sqlupdateorder = '
UPDATE {' . column::TABLE . '}
SET columnorder = columnorder - 1
WHERE reportid = :reportid
AND columnorder > :columnorder';
$DB->execute($sqlupdateorder, ['reportid' => $reportid, 'columnorder' => $column->get('columnorder')]);
}
return $result;
}
/**
* Re-order given column within a report
*
* @param int $reportid
* @param int $columnid
* @param int $position
* @return bool
* @throws invalid_parameter_exception
*/
public static function reorder_report_column(int $reportid, int $columnid, int $position): bool {
$column = column::get_record(['id' => $columnid, 'reportid' => $reportid]);
if ($column === false) {
throw new invalid_parameter_exception('Invalid column');
}
// Get the rest of the report columns, excluding the one we are moving.
$columns = column::get_records_select('reportid = :reportid AND id <> :id', [
'reportid' => $reportid,
'id' => $columnid,
], 'columnorder');
return static::reorder_persistents_by_field($column, $columns, $position, 'columnorder');
}
/**
* Re-order given column sorting within a report
*
* @param int $reportid
* @param int $columnid
* @param int $position
* @return bool
* @throws invalid_parameter_exception
*/
public static function reorder_report_column_sorting(int $reportid, int $columnid, int $position): bool {
$column = column::get_record(['id' => $columnid, 'reportid' => $reportid]);
if ($column === false) {
throw new invalid_parameter_exception('Invalid column');
}
// Get the rest of the report columns, excluding the one we are moving.
$columns = column::get_records_select('reportid = :reportid AND id <> :id', [
'reportid' => $reportid,
'id' => $columnid,
], 'sortorder');
return static::reorder_persistents_by_field($column, $columns, $position, 'sortorder');
}
/**
* Toggle sorting options for given column within a report
*
* @param int $reportid
* @param int $columnid
* @param bool $enabled
* @param int $direction
* @return bool
* @throws invalid_parameter_exception
*/
public static function toggle_report_column_sorting(int $reportid, int $columnid, bool $enabled,
int $direction = SORT_ASC): bool {
$column = column::get_record(['id' => $columnid, 'reportid' => $reportid]);
if ($column === false) {
throw new invalid_parameter_exception('Invalid column');
}
return $column
->set('sortenabled', $enabled)
->set('sortdirection', $direction)
->update();
}
/**
* Add given condition to report
*
* @param int $reportid
* @param string $uniqueidentifier
* @return filter
* @throws invalid_parameter_exception
*/
public static function add_report_condition(int $reportid, string $uniqueidentifier): filter {
$report = manager::get_report_from_id($reportid);
if (!array_key_exists($uniqueidentifier, $report->get_conditions())) {
throw new invalid_parameter_exception('Invalid condition');
}
$condition = new filter(0, (object) [
'reportid' => $reportid,
'uniqueidentifier' => $uniqueidentifier,
'iscondition' => true,
'filterorder' => filter::get_max_filterorder($reportid, true) + 1,
]);
return $condition->create();
}
/**
* Delete given condition from report
*
* @param int $reportid
* @param int $conditionid
* @return bool
* @throws invalid_parameter_exception
*/
public static function delete_report_condition(int $reportid, int $conditionid): bool {
global $DB;
$condition = filter::get_condition_record($reportid, $conditionid);
if ($condition === false) {
throw new invalid_parameter_exception('Invalid condition');
}
// After deletion, re-index remaining report conditions.
if ($result = $condition->delete()) {
$sqlupdateorder = '
UPDATE {' . filter::TABLE . '}
SET filterorder = filterorder - 1
WHERE reportid = :reportid
AND filterorder > :filterorder
AND iscondition = 1';
$DB->execute($sqlupdateorder, ['reportid' => $reportid, 'filterorder' => $condition->get('filterorder')]);
}
return $result;
}
/**
* Re-order given condition within a report
*
* @param int $reportid
* @param int $conditionid
* @param int $position
* @return bool
* @throws invalid_parameter_exception
*/
public static function reorder_report_condition(int $reportid, int $conditionid, int $position): bool {
$condition = filter::get_condition_record($reportid, $conditionid);
if ($condition === false) {
throw new invalid_parameter_exception('Invalid condition');
}
// Get the rest of the report conditions, excluding the one we are moving.
$conditions = filter::get_records_select('reportid = :reportid AND iscondition = 1 AND id <> :id', [
'reportid' => $reportid,
'id' => $conditionid,
], 'filterorder');
return static::reorder_persistents_by_field($condition, $conditions, $position, 'filterorder');
}
/**
* Add given filter to report
*
* @param int $reportid
* @param string $uniqueidentifier
* @return filter
* @throws invalid_parameter_exception
*/
public static function add_report_filter(int $reportid, string $uniqueidentifier): filter {
$report = manager::get_report_from_id($reportid);
$reportfilters = $report->get_filters();
if (!array_key_exists($uniqueidentifier, $reportfilters)) {
throw new invalid_parameter_exception('Invalid filter');
}
$filter = new filter(0, (object) [
'reportid' => $reportid,
'uniqueidentifier' => $uniqueidentifier,
'filterorder' => filter::get_max_filterorder($reportid) + 1,
]);
return $filter->create();
}
/**
* Delete given filter from report
*
* @param int $reportid
* @param int $filterid
* @return bool
* @throws invalid_parameter_exception
*/
public static function delete_report_filter(int $reportid, int $filterid): bool {
global $DB;
$filter = filter::get_filter_record($reportid, $filterid);
if ($filter === false) {
throw new invalid_parameter_exception('Invalid filter');
}
// After deletion, re-index remaining report filters.
if ($result = $filter->delete()) {
$sqlupdateorder = '
UPDATE {' . filter::TABLE . '}
SET filterorder = filterorder - 1
WHERE reportid = :reportid
AND filterorder > :filterorder
AND iscondition = 0';
$DB->execute($sqlupdateorder, ['reportid' => $reportid, 'filterorder' => $filter->get('filterorder')]);
}
return $result;
}
/**
* Re-order given filter within a report
*
* @param int $reportid
* @param int $filterid
* @param int $position
* @return bool
* @throws invalid_parameter_exception
*/
public static function reorder_report_filter(int $reportid, int $filterid, int $position): bool {
$filter = filter::get_filter_record($reportid, $filterid);
if ($filter === false) {
throw new invalid_parameter_exception('Invalid filter');
}
// Get the rest of the report filters, excluding the one we are moving.
$filters = filter::get_records_select('reportid = :reportid AND iscondition = 0 AND id <> :id', [
'reportid' => $reportid,
'id' => $filterid,
], 'filterorder');
return static::reorder_persistents_by_field($filter, $filters, $position, 'filterorder');
}
/**
* Get available columns for a given report
*
* @param report_model $persistent
* @return array
*/
public static function get_available_columns(report_model $persistent) : array {
$available = [];
$report = manager::get_report_from_persistent($persistent);
// Get current report columns.
foreach ($report->get_columns() as $column) {
$entityname = $column->get_entity_name();
$entitytitle = $column->get_title();
if (!array_key_exists($entityname, $available)) {
$available[$entityname] = [
'name' => (string) $report->get_entity_title($entityname),
'key' => $entityname,
'items' => [],
];
}
$available[$entityname]['items'][] = [
'name' => $entitytitle,
'identifier' => $column->get_unique_identifier(),
'title' => get_string('addcolumn', 'core_reportbuilder', $entitytitle),
'action' => 'report-add-column'
];
}
return array_values($available);
}
/**
* Helper method for re-ordering given persistents (columns, filters, etc)
*
* @param persistent $persistent The persistent we are moving
* @param persistent[] $persistents The rest of the persistents
* @param int $position
* @param string $field The field we need to update
* @return bool
*/
private static function reorder_persistents_by_field(persistent $persistent, array $persistents, int $position,
string $field): bool {
// Splice into new position.
array_splice($persistents, $position - 1, 0, [$persistent]);
$fieldorder = 1;
foreach ($persistents as $persistent) {
$persistent->set($field, $fieldorder++)
->update();
}
return true;
}
}