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 block_dedication\local\filters;
|
|
|
20 |
|
|
|
21 |
use core_reportbuilder\local\helpers\database;
|
|
|
22 |
use core_reportbuilder\local\filters\select as core_select;
|
|
|
23 |
|
|
|
24 |
/**
|
|
|
25 |
* Select report filter
|
|
|
26 |
*
|
|
|
27 |
* System level reports do not support aggregation (MDL-76392) so we have to manually aggregate the data,
|
|
|
28 |
* This filter has been adapted to match within a string, as opposed to core_select which matches the whole string.
|
|
|
29 |
* block_dedication needs this for its group filter. Students can belong to multiple groups in a course, and the groups column
|
|
|
30 |
* values look generally like ",groupid1,groupid2,groupid3,".
|
|
|
31 |
* When MDL-76392 is fixed, we can probably revisit and delete this.
|
|
|
32 |
*
|
|
|
33 |
* @package block_dedication
|
|
|
34 |
* @copyright 2022 Michael Kotlyar <michael.kotlyar@catalyst.net.nz>
|
|
|
35 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
36 |
*/
|
|
|
37 |
class select extends core_select {
|
|
|
38 |
|
|
|
39 |
/**
|
|
|
40 |
* Return filter SQL
|
|
|
41 |
*
|
|
|
42 |
* Modified to filter with SQL LIKE in string arrays.
|
|
|
43 |
*
|
|
|
44 |
* @param array $values Must be delimited with a comma and also must start and end with a comma. E.g. [",val1,val2,val3,", ...]
|
|
|
45 |
* @return array array of two elements - SQL query and named parameters
|
|
|
46 |
*/
|
|
|
47 |
public function get_sql_filter(array $values): array {
|
|
|
48 |
global $DB;
|
|
|
49 |
$name = database::generate_param_name();
|
|
|
50 |
|
|
|
51 |
$operator = $values["{$this->name}_operator"] ?? self::ANY_VALUE;
|
|
|
52 |
$value = $values["{$this->name}_value"] ?? 0;
|
|
|
53 |
|
|
|
54 |
$fieldsql = $this->filter->get_field_sql();
|
|
|
55 |
$params = $this->filter->get_field_params();
|
|
|
56 |
|
|
|
57 |
// Validate filter form values.
|
|
|
58 |
if (!$this->validate_filter_values((int) $operator, $value)) {
|
|
|
59 |
// Filter configuration is invalid. Ignore the filter.
|
|
|
60 |
return ['', []];
|
|
|
61 |
}
|
|
|
62 |
|
|
|
63 |
$value = "%,$value,%";
|
|
|
64 |
|
|
|
65 |
switch ($operator) {
|
|
|
66 |
case self::EQUAL_TO:
|
|
|
67 |
$fieldsql = $DB->sql_like($fieldsql, ":$name");
|
|
|
68 |
$params[$name] = $value;
|
|
|
69 |
break;
|
|
|
70 |
case self::NOT_EQUAL_TO:
|
|
|
71 |
$fieldsql = $DB->sql_like($fieldsql, ":$name", true, true, true);
|
|
|
72 |
$params[$name] = $value;
|
|
|
73 |
break;
|
|
|
74 |
default:
|
|
|
75 |
return ['', []];
|
|
|
76 |
}
|
|
|
77 |
return [$fieldsql, $params];
|
|
|
78 |
}
|
|
|
79 |
|
|
|
80 |
/**
|
|
|
81 |
* Validate filter form values
|
|
|
82 |
*
|
|
|
83 |
* @param int|null $operator
|
|
|
84 |
* @param mixed|null $value
|
|
|
85 |
* @return bool
|
|
|
86 |
*/
|
|
|
87 |
private function validate_filter_values(?int $operator, $value): bool {
|
|
|
88 |
return !($operator === null || $value === '');
|
|
|
89 |
}
|
|
|
90 |
}
|