/reportbuilder/classes/local/filters/number.php

https://github.com/markn86/moodle · PHP · 199 lines · 111 code · 29 blank · 59 comment · 7 complexity · e19b92308df852e134ec53dd7e627d29 MD5 · raw file

  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. declare(strict_types=1);
  17. namespace core_reportbuilder\local\filters;
  18. use core_reportbuilder\local\helpers\database;
  19. /**
  20. * Number report filter
  21. *
  22. * @package core_reportbuilder
  23. * @copyright 2021 David Matamoros <davidmc@moodle.com>
  24. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  25. */
  26. class number extends base {
  27. /** @var int Any value */
  28. public const ANY_VALUE = 0;
  29. /** @var int Is not empty */
  30. public const IS_NOT_EMPTY = 1;
  31. /** @var int Is empty */
  32. public const IS_EMPTY = 2;
  33. /** @var int Less than */
  34. public const LESS_THAN = 3;
  35. /** @var int Greater than */
  36. public const GREATER_THAN = 4;
  37. /** @var int Equal to */
  38. public const EQUAL_TO = 5;
  39. /** @var int Equal or less than */
  40. public const EQUAL_OR_LESS_THAN = 6;
  41. /** @var int Equal or greater than */
  42. public const EQUAL_OR_GREATER_THAN = 7;
  43. /** @var int Range */
  44. public const RANGE = 8;
  45. /**
  46. * Returns an array of comparison operators
  47. *
  48. * @return array of comparison operators
  49. */
  50. private function get_operators(): array {
  51. $operators = [
  52. self::ANY_VALUE => get_string('filterisanyvalue', 'core_reportbuilder'),
  53. self::IS_NOT_EMPTY => get_string('filterisnotempty', 'core_reportbuilder'),
  54. self::IS_EMPTY => get_string('filterisempty', 'core_reportbuilder'),
  55. self::LESS_THAN => get_string('filterlessthan', 'core_reportbuilder'),
  56. self::GREATER_THAN => get_string('filtergreaterthan', 'core_reportbuilder'),
  57. self::EQUAL_TO => get_string('filterisequalto', 'core_reportbuilder'),
  58. self::EQUAL_OR_LESS_THAN => get_string('filterequalorlessthan', 'core_reportbuilder'),
  59. self::EQUAL_OR_GREATER_THAN => get_string('filterequalorgreaterthan', 'core_reportbuilder'),
  60. self::RANGE => get_string('filterrange', 'core_reportbuilder'),
  61. ];
  62. return $this->filter->restrict_limited_operators($operators);
  63. }
  64. /**
  65. * Adds controls specific to this filter in the form.
  66. *
  67. * @param \MoodleQuickForm $mform
  68. */
  69. public function setup_form(\MoodleQuickForm $mform): void {
  70. $objs = [];
  71. $objs['select'] = $mform->createElement('select', $this->name . '_operator', null, $this->get_operators());
  72. $mform->setType($this->name . '_operator', PARAM_INT);
  73. $objs['text'] = $mform->createElement('text', $this->name . '_value1', null, ['size' => 3]);
  74. $mform->setType($this->name . '_value1', PARAM_INT);
  75. $mform->setDefault($this->name . '_value1', 0);
  76. $objs['text2'] = $mform->createElement('text', $this->name . '_value2', null, ['size' => 3]);
  77. $mform->setType($this->name . '_value2', PARAM_INT);
  78. $mform->setDefault($this->name . '_value2', 0);
  79. $mform->addElement('group', $this->name . '_grp', '', $objs, '', false);
  80. $mform->hideIf($this->name . '_value1', $this->name . '_operator', 'eq', self::ANY_VALUE);
  81. $mform->hideIf($this->name . '_value1', $this->name . '_operator', 'eq', self::IS_NOT_EMPTY);
  82. $mform->hideIf($this->name . '_value1', $this->name . '_operator', 'eq', self::IS_EMPTY);
  83. $mform->hideIf($this->name . '_value2', $this->name . '_operator', 'noteq', self::RANGE);
  84. }
  85. /**
  86. * Return filter SQL
  87. *
  88. * @param array $values
  89. * @return array array of two elements - SQL query and named parameters
  90. */
  91. public function get_sql_filter(array $values) : array {
  92. $operator = $values["{$this->name}_operator"] ?? self::ANY_VALUE;
  93. $value1 = $values["{$this->name}_value1"] ?? null;
  94. $value2 = $values["{$this->name}_value2"] ?? null;
  95. // Validate filter form values.
  96. if (!$this->validate_filter_values($operator, $value1, $value2)) {
  97. // Filter configuration is invalid. Ignore the filter.
  98. return ['', []];
  99. }
  100. $param = database::generate_param_name();
  101. $param2 = database::generate_param_name();
  102. $fieldsql = $this->filter->get_field_sql();
  103. $params = $this->filter->get_field_params();
  104. switch ($operator) {
  105. case self::ANY_VALUE:
  106. return ['', []];
  107. case self::IS_NOT_EMPTY:
  108. $res = "{$fieldsql} IS NOT NULL AND {$fieldsql} <> 0";
  109. break;
  110. case self::IS_EMPTY:
  111. $res = "{$fieldsql} IS NULL OR {$fieldsql} = 0";
  112. break;
  113. case self::LESS_THAN:
  114. $res = "{$fieldsql} < :{$param}";
  115. $params[$param] = $value1;
  116. break;
  117. case self::GREATER_THAN:
  118. $res = "{$fieldsql} > :{$param}";
  119. $params[$param] = $value1;
  120. break;
  121. case self::EQUAL_TO:
  122. $res = "{$fieldsql} = :{$param}";
  123. $params[$param] = $value1;
  124. break;
  125. case self::EQUAL_OR_LESS_THAN:
  126. $res = "{$fieldsql} <= :{$param}";
  127. $params[$param] = $value1;
  128. break;
  129. case self::EQUAL_OR_GREATER_THAN:
  130. $res = "{$fieldsql} >= :{$param}";
  131. $params[$param] = $value1;
  132. break;
  133. case self::RANGE:
  134. $res = "({$fieldsql} >= :{$param} AND {$fieldsql} <= :{$param2})";
  135. $params[$param] = $value1;
  136. $params[$param2] = $value2;
  137. break;
  138. default:
  139. // Filter configuration is invalid. Ignore the filter.
  140. return ['', []];
  141. }
  142. return [$res, $params];
  143. }
  144. /**
  145. * Validate filter form values
  146. *
  147. * @param int $operator
  148. * @param int|null $value1
  149. * @param int|null $value2
  150. * @return bool
  151. */
  152. private function validate_filter_values(int $operator, ?int $value1, ?int $value2): bool {
  153. // Check that for any of these operators value1 can not be null.
  154. $requirescomparisonvalue = [
  155. self::LESS_THAN,
  156. self::GREATER_THAN,
  157. self::EQUAL_TO,
  158. self::EQUAL_OR_LESS_THAN,
  159. self::EQUAL_OR_GREATER_THAN
  160. ];
  161. if (in_array($operator, $requirescomparisonvalue) && $value1 === null) {
  162. return false;
  163. }
  164. // When operator is between $value1 and $value2, can not be null.
  165. if (($operator === self::RANGE) && ($value1 === null || $value2 === null)) {
  166. return false;
  167. }
  168. return true;
  169. }
  170. }