Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
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\local\filters;
20
 
21
use core_reportbuilder\local\helpers\database;
22
 
23
/**
24
 * Text report filter
25
 *
26
 * @package     core_reportbuilder
27
 * @copyright   2021 David Matamoros <davidmc@moodle.com>
28
 * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
29
 */
30
class text extends base {
31
 
32
    /** @var int */
33
    public const ANY_VALUE = 0;
34
 
35
    /** @var int */
36
    public const CONTAINS = 1;
37
 
38
    /** @var int */
39
    public const DOES_NOT_CONTAIN = 2;
40
 
41
    /** @var int */
42
    public const IS_EQUAL_TO = 3;
43
 
44
    /** @var int */
45
    public const IS_NOT_EQUAL_TO = 4;
46
 
47
    /** @var int */
48
    public const STARTS_WITH = 5;
49
 
50
    /** @var int */
51
    public const ENDS_WITH = 6;
52
 
53
    /** @var int */
54
    public const IS_EMPTY = 7;
55
 
56
    /** @var int */
57
    public const IS_NOT_EMPTY = 8;
58
 
59
    /**
60
     * Return an array of operators available for this filter
61
     *
62
     * @return array of comparison operators
63
     */
64
    private function get_operators(): array {
65
        $operators = [
66
            self::ANY_VALUE => get_string('filterisanyvalue', 'core_reportbuilder'),
67
            self::CONTAINS => get_string('filtercontains', 'core_reportbuilder'),
68
            self::DOES_NOT_CONTAIN => get_string('filterdoesnotcontain', 'core_reportbuilder'),
69
            self::IS_EQUAL_TO => get_string('filterisequalto', 'core_reportbuilder'),
70
            self::IS_NOT_EQUAL_TO => get_string('filterisnotequalto', 'core_reportbuilder'),
71
            self::STARTS_WITH => get_string('filterstartswith', 'core_reportbuilder'),
72
            self::ENDS_WITH => get_string('filterendswith', 'core_reportbuilder'),
73
            self::IS_EMPTY => get_string('filterisempty', 'core_reportbuilder'),
74
            self::IS_NOT_EMPTY => get_string('filterisnotempty', 'core_reportbuilder')
75
        ];
76
 
77
        return $this->filter->restrict_limited_operators($operators);
78
    }
79
 
80
    /**
81
     * Adds controls specific to this filter in the form.
82
     *
83
     * Operator selector use the "$this->name . '_operator'" naming convention and the fields to enter custom values should
84
     * use "$this->name . '_value'" or _value1/_value2/... in case there is more than one field for their naming.
85
     *
86
     * @param \MoodleQuickForm $mform
87
     */
88
    public function setup_form(\MoodleQuickForm $mform): void {
89
        $elements = [];
90
        $elements['operator'] = $mform->createElement('select', $this->name . '_operator',
91
            get_string('filterfieldoperator', 'core_reportbuilder', $this->get_header()), $this->get_operators());
92
        $elements['value'] = $mform->createElement('text', $this->name . '_value',
93
            get_string('filterfieldvalue', 'core_reportbuilder', $this->get_header()));
94
 
95
        $mform->addGroup($elements, $this->name . '_group', $this->get_header(), '', false)
96
            ->setHiddenLabel(true);
97
 
98
        $mform->setType($this->name . '_value', PARAM_RAW_TRIMMED);
99
        $mform->hideIf($this->name . '_value', $this->name . '_operator', 'eq', self::ANY_VALUE);
100
        $mform->hideIf($this->name . '_value', $this->name . '_operator', 'eq', self::IS_EMPTY);
101
        $mform->hideIf($this->name . '_value', $this->name . '_operator', 'eq', self::IS_NOT_EMPTY);
102
    }
103
 
104
    /**
105
     * Return filter SQL
106
     *
107
     * @param array $values
108
     * @return array array of two elements - SQL query and named parameters
109
     */
110
    public function get_sql_filter(array $values): array {
111
        global $DB;
112
        $name = database::generate_param_name();
113
 
114
        $operator = (int) ($values["{$this->name}_operator"] ?? self::ANY_VALUE);
115
        $value = trim($values["{$this->name}_value"] ?? '');
116
 
117
        $fieldsql = $this->filter->get_field_sql();
118
        $params = $this->filter->get_field_params();
119
 
120
        // Validate filter form values.
121
        if (!$this->validate_filter_values($operator, $value)) {
122
            // Filter configuration is invalid. Ignore the filter.
123
            return ['', []];
124
        }
125
 
126
        switch($operator) {
127
            case self::CONTAINS:
128
                $res = $DB->sql_like($fieldsql, ":$name", false, false);
129
                $value = $DB->sql_like_escape($value);
130
                $params[$name] = "%$value%";
131
                break;
132
            case self::DOES_NOT_CONTAIN:
133
                $res = $DB->sql_like($fieldsql, ":$name", false, false, true);
134
                $value = $DB->sql_like_escape($value);
135
                $params[$name] = "%$value%";
136
                break;
137
            case self::IS_EQUAL_TO:
138
                $res = $DB->sql_equal($fieldsql, ":$name", false, false);
139
                $params[$name] = $value;
140
                break;
141
            case self::IS_NOT_EQUAL_TO:
142
                $res = $DB->sql_equal($fieldsql, ":$name", false, false, true);
143
                $params[$name] = $value;
144
                break;
145
            case self::STARTS_WITH:
146
                $res = $DB->sql_like($fieldsql, ":$name", false, false);
147
                $value = $DB->sql_like_escape($value);
148
                $params[$name] = "$value%";
149
                break;
150
            case self::ENDS_WITH:
151
                $res = $DB->sql_like($fieldsql, ":$name", false, false);
152
                $value = $DB->sql_like_escape($value);
153
                $params[$name] = "%$value";
154
                break;
155
            case self::IS_EMPTY:
156
                $paramempty = database::generate_param_name();
157
                $res = "COALESCE({$fieldsql}, :{$paramempty}) = :{$name}";
158
                $params[$paramempty] = $params[$name] = '';
159
                break;
160
            case self::IS_NOT_EMPTY:
161
                $paramempty = database::generate_param_name();
162
                $res = "COALESCE({$fieldsql}, :{$paramempty}) != :{$name}";
163
                $params[$paramempty] = $params[$name] = '';
164
                break;
165
            default:
166
                // Filter configuration is invalid. Ignore the filter.
167
                return ['', []];
168
        }
169
        return array($res, $params);
170
    }
171
 
172
    /**
173
     * Validate filter form values
174
     *
175
     * @param int $operator
176
     * @param string|null $value
177
     * @return bool
178
     */
179
    private function validate_filter_values(int $operator, ?string $value): bool {
180
        $operatorsthatdontrequirevalue = [
181
            self::ANY_VALUE,
182
            self::IS_EMPTY,
183
            self::IS_NOT_EMPTY,
184
        ];
185
 
186
        if ($value === '' && !in_array($operator, $operatorsthatdontrequirevalue)) {
187
            return false;
188
        }
189
 
190
        return true;
191
    }
192
 
193
    /**
194
     * Return sample filter values
195
     *
196
     * @return array
197
     */
198
    public function get_sample_values(): array {
199
        return [
200
            "{$this->name}_operator" => self::IS_EQUAL_TO,
201
            "{$this->name}_value" => 'test',
202
        ];
203
    }
204
}