Merge branch 'MDL-79669' of https://github.com/Chocolate-lightning/moodle
[moodle.git] / user / filters / text.php
blobe8d71acb523990ee49dce784f985fd760dfb3dd3
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.
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/>.
17 /**
18 * Text field filter
20 * @package core_user
21 * @category user
22 * @copyright 1999 Martin Dougiamas http://dougiamas.com
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 require_once($CFG->dirroot.'/user/filters/lib.php');
28 /**
29 * Generic filter for text fields.
30 * @copyright 1999 Martin Dougiamas http://dougiamas.com
31 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
33 class user_filter_text extends user_filter_type {
34 /** @var string */
35 public $_field;
37 /**
38 * Constructor
39 * @param string $name the name of the filter instance
40 * @param string $label the label of the filter instance
41 * @param boolean $advanced advanced form element flag
42 * @param string $field user table filed name
44 public function __construct($name, $label, $advanced, $field) {
45 parent::__construct($name, $label, $advanced);
46 $this->_field = $field;
49 /**
50 * Old syntax of class constructor. Deprecated in PHP7.
52 * @deprecated since Moodle 3.1
54 public function user_filter_text($name, $label, $advanced, $field) {
55 debugging('Use of class name as constructor is deprecated', DEBUG_DEVELOPER);
56 self::__construct($name, $label, $advanced, $field);
59 /**
60 * Returns an array of comparison operators
61 * @return array of comparison operators
63 public function getOperators() {
64 return array(0 => get_string('contains', 'filters'),
65 1 => get_string('doesnotcontain', 'filters'),
66 2 => get_string('isequalto', 'filters'),
67 3 => get_string('startswith', 'filters'),
68 4 => get_string('endswith', 'filters'),
69 5 => get_string('isempty', 'filters'));
72 /**
73 * Adds controls specific to this filter in the form.
74 * @param object $mform a MoodleForm object to setup
76 public function setupForm(&$mform) {
77 $objs = array();
78 $objs['select'] = $mform->createElement('select', $this->_name.'_op', null, $this->getOperators());
79 $objs['text'] = $mform->createElement('text', $this->_name, null);
80 $objs['select']->setLabel(get_string('limiterfor', 'filters', $this->_label));
81 $objs['text']->setLabel(get_string('valuefor', 'filters', $this->_label));
82 $grp =& $mform->addElement('group', $this->_name.'_grp', $this->_label, $objs, '', false);
83 $mform->setType($this->_name, PARAM_RAW);
84 $mform->disabledIf($this->_name, $this->_name.'_op', 'eq', 5);
85 if ($this->_advanced) {
86 $mform->setAdvanced($this->_name.'_grp');
90 /**
91 * Retrieves data from the form data
92 * @param object $formdata data submited with the form
93 * @return mixed array filter data or false when filter not set
95 public function check_data($formdata) {
96 $field = $this->_name;
97 $operator = $field.'_op';
99 if (property_exists($formdata, $operator)) {
100 if ($formdata->$operator != 5 and $formdata->$field == '') {
101 // No data - no change except for empty filter.
102 return false;
104 // If field value is set then use it, else it's null.
105 $fieldvalue = null;
106 if (isset($formdata->$field)) {
107 $fieldvalue = $formdata->$field;
109 // If we aren't doing a whitespace comparison, an exact match, trim will give us a better result set.
110 $trimmed = trim($fieldvalue);
111 if ($trimmed !== '' && $formdata->$operator != 2) {
112 $fieldvalue = $trimmed;
115 return array('operator' => (int)$formdata->$operator, 'value' => $fieldvalue);
118 return false;
122 * Returns the condition to be used with SQL where
123 * @param array $data filter settings
124 * @return array sql string and $params
126 public function get_sql_filter($data) {
127 global $DB;
128 static $counter = 0;
129 $name = 'ex_text'.$counter++;
131 $operator = $data['operator'];
132 $value = $data['value'];
133 $field = $this->_field;
135 $params = array();
137 if ($operator != 5 and $value === '') {
138 return '';
141 switch($operator) {
142 case 0: // Contains.
143 $res = $DB->sql_like($field, ":$name", false, false);
144 $params[$name] = "%$value%";
145 break;
146 case 1: // Does not contain.
147 $res = $DB->sql_like($field, ":$name", false, false, true);
148 $params[$name] = "%$value%";
149 break;
150 case 2: // Equal to.
151 $res = $DB->sql_like($field, ":$name", false, false);
152 $params[$name] = "$value";
153 break;
154 case 3: // Starts with.
155 $res = $DB->sql_like($field, ":$name", false, false);
156 $params[$name] = "$value%";
157 break;
158 case 4: // Ends with.
159 $res = $DB->sql_like($field, ":$name", false, false);
160 $params[$name] = "%$value";
161 break;
162 case 5: // Empty.
163 $res = "$field = :$name";
164 $params[$name] = '';
165 break;
166 default:
167 return '';
169 return array($res, $params);
173 * Returns a human friendly description of the filter used as label.
174 * @param array $data filter settings
175 * @return string active filter label
177 public function get_label($data) {
178 $operator = $data['operator'];
179 $value = $data['value'];
180 $operators = $this->getOperators();
182 $a = new stdClass();
183 $a->label = $this->_label;
184 $a->value = '"'.s($value).'"';
185 $a->operator = $operators[$operator];
187 switch ($operator) {
188 case 0: // Contains.
189 case 1: // Doesn't contain.
190 case 2: // Equal to.
191 case 3: // Starts with.
192 case 4: // Ends with.
193 return get_string('textlabel', 'filters', $a);
194 case 5: // Empty.
195 return get_string('textlabelnovalue', 'filters', $a);
198 return '';