| Línea 21... |
Línea 21... |
| 21 |
use core_reportbuilder\local\helpers\database;
|
21 |
use core_reportbuilder\local\helpers\database;
|
| Línea 22... |
Línea 22... |
| 22 |
|
22 |
|
| 23 |
/**
|
23 |
/**
|
| 24 |
* Number report filter
|
24 |
* Number report filter
|
| - |
|
25 |
*
|
| - |
|
26 |
* This filter accepts a number value to perform filtering on (note that the value will be cast to float prior to comparison)
|
| 25 |
*
|
27 |
*
|
| 26 |
* @package core_reportbuilder
|
28 |
* @package core_reportbuilder
|
| 27 |
* @copyright 2021 David Matamoros <davidmc@moodle.com>
|
29 |
* @copyright 2021 David Matamoros <davidmc@moodle.com>
|
| 28 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
30 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
| 29 |
*/
|
31 |
*/
|
| Línea 89... |
Línea 91... |
| 89 |
get_string('filterfieldoperator', 'core_reportbuilder', $this->get_header()), $this->get_operators());
|
91 |
get_string('filterfieldoperator', 'core_reportbuilder', $this->get_header()), $this->get_operators());
|
| 90 |
$mform->setType($this->name . '_operator', PARAM_INT);
|
92 |
$mform->setType($this->name . '_operator', PARAM_INT);
|
| Línea 91... |
Línea 93... |
| 91 |
|
93 |
|
| 92 |
$objs['text'] = $mform->createElement('text', $this->name . '_value1',
|
94 |
$objs['text'] = $mform->createElement('text', $this->name . '_value1',
|
| 93 |
get_string('filterfieldvalue', 'core_reportbuilder', $this->get_header()), ['size' => 3]);
|
95 |
get_string('filterfieldvalue', 'core_reportbuilder', $this->get_header()), ['size' => 3]);
|
| 94 |
$mform->setType($this->name . '_value1', PARAM_INT);
|
96 |
$mform->setType($this->name . '_value1', PARAM_LOCALISEDFLOAT);
|
| 95 |
$mform->setDefault($this->name . '_value1', 0);
|
97 |
$mform->setDefault($this->name . '_value1', 0);
|
| 96 |
$mform->hideIf($this->name . '_value1', $this->name . '_operator', 'in',
|
98 |
$mform->hideIf($this->name . '_value1', $this->name . '_operator', 'in',
|
| Línea 97... |
Línea 99... |
| 97 |
[self::ANY_VALUE, self::IS_NOT_EMPTY, self::IS_EMPTY]);
|
99 |
[self::ANY_VALUE, self::IS_NOT_EMPTY, self::IS_EMPTY]);
|
| 98 |
|
100 |
|
| 99 |
$objs['text2'] = $mform->createElement('text', $this->name . '_value2',
|
101 |
$objs['text2'] = $mform->createElement('text', $this->name . '_value2',
|
| 100 |
get_string('filterfieldto', 'reportbuilder', $this->get_header()), ['size' => 3]);
|
102 |
get_string('filterfieldto', 'core_reportbuilder', $this->get_header()), ['size' => 3]);
|
| 101 |
$mform->setType($this->name . '_value2', PARAM_INT);
|
103 |
$mform->setType($this->name . '_value2', PARAM_LOCALISEDFLOAT);
|
| Línea 102... |
Línea 104... |
| 102 |
$mform->setDefault($this->name . '_value2', 0);
|
104 |
$mform->setDefault($this->name . '_value2', 0);
|
| 103 |
$mform->hideIf($this->name . '_value2', $this->name . '_operator', 'noteq', self::RANGE);
|
105 |
$mform->hideIf($this->name . '_value2', $this->name . '_operator', 'noteq', self::RANGE);
|
| Línea 111... |
Línea 113... |
| 111 |
*
|
113 |
*
|
| 112 |
* @param array $values
|
114 |
* @param array $values
|
| 113 |
* @return array array of two elements - SQL query and named parameters
|
115 |
* @return array array of two elements - SQL query and named parameters
|
| 114 |
*/
|
116 |
*/
|
| 115 |
public function get_sql_filter(array $values): array {
|
117 |
public function get_sql_filter(array $values): array {
|
| - |
|
118 |
global $DB;
|
| - |
|
119 |
|
| 116 |
$operator = (int) ($values["{$this->name}_operator"] ?? self::ANY_VALUE);
|
120 |
$operator = (int) ($values["{$this->name}_operator"] ?? self::ANY_VALUE);
|
| Línea -... |
Línea 121... |
| - |
|
121 |
|
| - |
|
122 |
$value1 = $value2 = null;
|
| 117 |
|
123 |
if (array_key_exists("{$this->name}_value1", $values)) {
|
| - |
|
124 |
$value1 = unformat_float($values["{$this->name}_value1"]);
|
| - |
|
125 |
}
|
| 118 |
$value1 = $values["{$this->name}_value1"] ?? null;
|
126 |
if (array_key_exists("{$this->name}_value2", $values)) {
|
| - |
|
127 |
$value2 = unformat_float($values["{$this->name}_value2"]);
|
| Línea 119... |
Línea 128... |
| 119 |
$value2 = $values["{$this->name}_value2"] ?? null;
|
128 |
}
|
| 120 |
|
129 |
|
| 121 |
// Validate filter form values.
|
130 |
// Validate filter form values.
|
| 122 |
if (!$this->validate_filter_values($operator, $value1, $value2)) {
|
131 |
if (!$this->validate_filter_values($operator, $value1, $value2)) {
|
| Línea 137... |
Línea 146... |
| 137 |
break;
|
146 |
break;
|
| 138 |
case self::IS_EMPTY:
|
147 |
case self::IS_EMPTY:
|
| 139 |
$res = "COALESCE({$fieldsql}, 0) = 0";
|
148 |
$res = "COALESCE({$fieldsql}, 0) = 0";
|
| 140 |
break;
|
149 |
break;
|
| 141 |
case self::LESS_THAN:
|
150 |
case self::LESS_THAN:
|
| 142 |
$res = "{$fieldsql} < :{$param}";
|
151 |
$res = $DB->sql_cast_char2real("({$fieldsql})") . " < :{$param}";
|
| 143 |
$params[$param] = $value1;
|
152 |
$params[$param] = $value1;
|
| 144 |
break;
|
153 |
break;
|
| 145 |
case self::GREATER_THAN:
|
154 |
case self::GREATER_THAN:
|
| 146 |
$res = "{$fieldsql} > :{$param}";
|
155 |
$res = $DB->sql_cast_char2real("({$fieldsql})") . " > :{$param}";
|
| 147 |
$params[$param] = $value1;
|
156 |
$params[$param] = $value1;
|
| 148 |
break;
|
157 |
break;
|
| 149 |
case self::EQUAL_TO:
|
158 |
case self::EQUAL_TO:
|
| 150 |
$res = "{$fieldsql} = :{$param}";
|
159 |
$res = $DB->sql_cast_char2real("({$fieldsql})") . " = :{$param}";
|
| 151 |
$params[$param] = $value1;
|
160 |
$params[$param] = $value1;
|
| 152 |
break;
|
161 |
break;
|
| 153 |
case self::EQUAL_OR_LESS_THAN:
|
162 |
case self::EQUAL_OR_LESS_THAN:
|
| 154 |
$res = "{$fieldsql} <= :{$param}";
|
163 |
$res = $DB->sql_cast_char2real("({$fieldsql})") . " <= :{$param}";
|
| 155 |
$params[$param] = $value1;
|
164 |
$params[$param] = $value1;
|
| 156 |
break;
|
165 |
break;
|
| 157 |
case self::EQUAL_OR_GREATER_THAN:
|
166 |
case self::EQUAL_OR_GREATER_THAN:
|
| 158 |
$res = "{$fieldsql} >= :{$param}";
|
167 |
$res = $DB->sql_cast_char2real("({$fieldsql})") . " >= :{$param}";
|
| 159 |
$params[$param] = $value1;
|
168 |
$params[$param] = $value1;
|
| 160 |
break;
|
169 |
break;
|
| 161 |
case self::RANGE:
|
170 |
case self::RANGE:
|
| 162 |
$res = "{$fieldsql} BETWEEN :{$param} AND :{$param2}";
|
171 |
$res = $DB->sql_cast_char2real("({$fieldsql})") . " BETWEEN :{$param} AND :{$param2}";
|
| 163 |
$params[$param] = $value1;
|
172 |
$params[$param] = $value1;
|
| 164 |
$params[$param2] = $value2;
|
173 |
$params[$param2] = $value2;
|
| 165 |
break;
|
174 |
break;
|
| 166 |
default:
|
175 |
default:
|
| 167 |
// Filter configuration is invalid. Ignore the filter.
|
176 |
// Filter configuration is invalid. Ignore the filter.
|
| Línea 172... |
Línea 181... |
| 172 |
|
181 |
|
| 173 |
/**
|
182 |
/**
|
| 174 |
* Validate filter form values
|
183 |
* Validate filter form values
|
| 175 |
*
|
184 |
*
|
| 176 |
* @param int $operator
|
185 |
* @param int $operator
|
| 177 |
* @param int|null $value1
|
186 |
* @param float|null $value1
|
| 178 |
* @param int|null $value2
|
187 |
* @param float|null $value2
|
| 179 |
* @return bool
|
188 |
* @return bool
|
| 180 |
*/
|
189 |
*/
|
| 181 |
private function validate_filter_values(int $operator, ?int $value1, ?int $value2): bool {
|
190 |
private function validate_filter_values(int $operator, ?float $value1, ?float $value2): bool {
|
| 182 |
// Check that for any of these operators value1 can not be null.
|
191 |
// Check that for any of these operators value1 can not be null.
|
| 183 |
$requirescomparisonvalue = [
|
192 |
$requirescomparisonvalue = [
|
| 184 |
self::LESS_THAN,
|
193 |
self::LESS_THAN,
|
| 185 |
self::GREATER_THAN,
|
194 |
self::GREATER_THAN,
|