PageRenderTime 43ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/user/filters/text.php

https://gitlab.com/unofficial-mirrors/moodle
PHP | 194 lines | 110 code | 19 blank | 65 comment | 9 complexity | 727ff63a4f1e6ebbe23fb6408d597014 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. /**
  17. * Text field filter
  18. *
  19. * @package core_user
  20. * @category user
  21. * @copyright 1999 Martin Dougiamas http://dougiamas.com
  22. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23. */
  24. require_once($CFG->dirroot.'/user/filters/lib.php');
  25. /**
  26. * Generic filter for text fields.
  27. * @copyright 1999 Martin Dougiamas http://dougiamas.com
  28. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  29. */
  30. class user_filter_text extends user_filter_type {
  31. /** @var string */
  32. public $_field;
  33. /**
  34. * Constructor
  35. * @param string $name the name of the filter instance
  36. * @param string $label the label of the filter instance
  37. * @param boolean $advanced advanced form element flag
  38. * @param string $field user table filed name
  39. */
  40. public function __construct($name, $label, $advanced, $field) {
  41. parent::__construct($name, $label, $advanced);
  42. $this->_field = $field;
  43. }
  44. /**
  45. * Old syntax of class constructor. Deprecated in PHP7.
  46. *
  47. * @deprecated since Moodle 3.1
  48. */
  49. public function user_filter_text($name, $label, $advanced, $field) {
  50. debugging('Use of class name as constructor is deprecated', DEBUG_DEVELOPER);
  51. self::__construct($name, $label, $advanced, $field);
  52. }
  53. /**
  54. * Returns an array of comparison operators
  55. * @return array of comparison operators
  56. */
  57. public function getOperators() {
  58. return array(0 => get_string('contains', 'filters'),
  59. 1 => get_string('doesnotcontain', 'filters'),
  60. 2 => get_string('isequalto', 'filters'),
  61. 3 => get_string('startswith', 'filters'),
  62. 4 => get_string('endswith', 'filters'),
  63. 5 => get_string('isempty', 'filters'));
  64. }
  65. /**
  66. * Adds controls specific to this filter in the form.
  67. * @param object $mform a MoodleForm object to setup
  68. */
  69. public function setupForm(&$mform) {
  70. $objs = array();
  71. $objs['select'] = $mform->createElement('select', $this->_name.'_op', null, $this->getOperators());
  72. $objs['text'] = $mform->createElement('text', $this->_name, null);
  73. $objs['select']->setLabel(get_string('limiterfor', 'filters', $this->_label));
  74. $objs['text']->setLabel(get_string('valuefor', 'filters', $this->_label));
  75. $grp =& $mform->addElement('group', $this->_name.'_grp', $this->_label, $objs, '', false);
  76. $mform->setType($this->_name, PARAM_RAW);
  77. $mform->disabledIf($this->_name, $this->_name.'_op', 'eq', 5);
  78. if ($this->_advanced) {
  79. $mform->setAdvanced($this->_name.'_grp');
  80. }
  81. }
  82. /**
  83. * Retrieves data from the form data
  84. * @param object $formdata data submited with the form
  85. * @return mixed array filter data or false when filter not set
  86. */
  87. public function check_data($formdata) {
  88. $field = $this->_name;
  89. $operator = $field.'_op';
  90. if (array_key_exists($operator, $formdata)) {
  91. if ($formdata->$operator != 5 and $formdata->$field == '') {
  92. // No data - no change except for empty filter.
  93. return false;
  94. }
  95. // If field value is set then use it, else it's null.
  96. $fieldvalue = null;
  97. if (isset($formdata->$field)) {
  98. $fieldvalue = $formdata->$field;
  99. }
  100. return array('operator' => (int)$formdata->$operator, 'value' => $fieldvalue);
  101. }
  102. return false;
  103. }
  104. /**
  105. * Returns the condition to be used with SQL where
  106. * @param array $data filter settings
  107. * @return array sql string and $params
  108. */
  109. public function get_sql_filter($data) {
  110. global $DB;
  111. static $counter = 0;
  112. $name = 'ex_text'.$counter++;
  113. $operator = $data['operator'];
  114. $value = $data['value'];
  115. $field = $this->_field;
  116. $params = array();
  117. if ($operator != 5 and $value === '') {
  118. return '';
  119. }
  120. switch($operator) {
  121. case 0: // Contains.
  122. $res = $DB->sql_like($field, ":$name", false, false);
  123. $params[$name] = "%$value%";
  124. break;
  125. case 1: // Does not contain.
  126. $res = $DB->sql_like($field, ":$name", false, false, true);
  127. $params[$name] = "%$value%";
  128. break;
  129. case 2: // Equal to.
  130. $res = $DB->sql_like($field, ":$name", false, false);
  131. $params[$name] = "$value";
  132. break;
  133. case 3: // Starts with.
  134. $res = $DB->sql_like($field, ":$name", false, false);
  135. $params[$name] = "$value%";
  136. break;
  137. case 4: // Ends with.
  138. $res = $DB->sql_like($field, ":$name", false, false);
  139. $params[$name] = "%$value";
  140. break;
  141. case 5: // Empty.
  142. $res = "$field = :$name";
  143. $params[$name] = '';
  144. break;
  145. default:
  146. return '';
  147. }
  148. return array($res, $params);
  149. }
  150. /**
  151. * Returns a human friendly description of the filter used as label.
  152. * @param array $data filter settings
  153. * @return string active filter label
  154. */
  155. public function get_label($data) {
  156. $operator = $data['operator'];
  157. $value = $data['value'];
  158. $operators = $this->getOperators();
  159. $a = new stdClass();
  160. $a->label = $this->_label;
  161. $a->value = '"'.s($value).'"';
  162. $a->operator = $operators[$operator];
  163. switch ($operator) {
  164. case 0: // Contains.
  165. case 1: // Doesn't contain.
  166. case 2: // Equal to.
  167. case 3: // Starts with.
  168. case 4: // Ends with.
  169. return get_string('textlabel', 'filters', $a);
  170. case 5: // Empty.
  171. return get_string('textlabelnovalue', 'filters', $a);
  172. }
  173. return '';
  174. }
  175. }