MDL-64285 core: Fix environment check test
[moodle.git] / user / filters / text.php
blobfabea89c5a1445cf3fa10f322c975443c0fa866e
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 (array_key_exists($operator, $formdata)) {
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 return array('operator' => (int)$formdata->$operator, 'value' => $fieldvalue);
112 return false;
116 * Returns the condition to be used with SQL where
117 * @param array $data filter settings
118 * @return array sql string and $params
120 public function get_sql_filter($data) {
121 global $DB;
122 static $counter = 0;
123 $name = 'ex_text'.$counter++;
125 $operator = $data['operator'];
126 $value = $data['value'];
127 $field = $this->_field;
129 $params = array();
131 if ($operator != 5 and $value === '') {
132 return '';
135 switch($operator) {
136 case 0: // Contains.
137 $res = $DB->sql_like($field, ":$name", false, false);
138 $params[$name] = "%$value%";
139 break;
140 case 1: // Does not contain.
141 $res = $DB->sql_like($field, ":$name", false, false, true);
142 $params[$name] = "%$value%";
143 break;
144 case 2: // Equal to.
145 $res = $DB->sql_like($field, ":$name", false, false);
146 $params[$name] = "$value";
147 break;
148 case 3: // Starts with.
149 $res = $DB->sql_like($field, ":$name", false, false);
150 $params[$name] = "$value%";
151 break;
152 case 4: // Ends with.
153 $res = $DB->sql_like($field, ":$name", false, false);
154 $params[$name] = "%$value";
155 break;
156 case 5: // Empty.
157 $res = "$field = :$name";
158 $params[$name] = '';
159 break;
160 default:
161 return '';
163 return array($res, $params);
167 * Returns a human friendly description of the filter used as label.
168 * @param array $data filter settings
169 * @return string active filter label
171 public function get_label($data) {
172 $operator = $data['operator'];
173 $value = $data['value'];
174 $operators = $this->getOperators();
176 $a = new stdClass();
177 $a->label = $this->_label;
178 $a->value = '"'.s($value).'"';
179 $a->operator = $operators[$operator];
181 switch ($operator) {
182 case 0: // Contains.
183 case 1: // Doesn't contain.
184 case 2: // Equal to.
185 case 3: // Starts with.
186 case 4: // Ends with.
187 return get_string('textlabel', 'filters', $a);
188 case 5: // Empty.
189 return get_string('textlabelnovalue', 'filters', $a);
192 return '';