2 // This file is part of Moodle - http://moodle.org/
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.
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/>.
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');
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
{
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;
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);
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'));
73 * Adds controls specific to this filter in the form.
74 * @param object $mform a MoodleForm object to setup
76 public function setupForm(&$mform) {
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');
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.
104 // If field value is set then use it, else it's null.
106 if (isset($formdata->$field)) {
107 $fieldvalue = $formdata->$field;
109 return array('operator' => (int)$formdata->$operator, 'value' => $fieldvalue);
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) {
123 $name = 'ex_text'.$counter++
;
125 $operator = $data['operator'];
126 $value = $data['value'];
127 $field = $this->_field
;
131 if ($operator != 5 and $value === '') {
137 $res = $DB->sql_like($field, ":$name", false, false);
138 $params[$name] = "%$value%";
140 case 1: // Does not contain.
141 $res = $DB->sql_like($field, ":$name", false, false, true);
142 $params[$name] = "%$value%";
145 $res = $DB->sql_like($field, ":$name", false, false);
146 $params[$name] = "$value";
148 case 3: // Starts with.
149 $res = $DB->sql_like($field, ":$name", false, false);
150 $params[$name] = "$value%";
152 case 4: // Ends with.
153 $res = $DB->sql_like($field, ":$name", false, false);
154 $params[$name] = "%$value";
157 $res = "$field = :$name";
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();
177 $a->label
= $this->_label
;
178 $a->value
= '"'.s($value).'"';
179 $a->operator
= $operators[$operator];
183 case 1: // Doesn't contain.
185 case 3: // Starts with.
186 case 4: // Ends with.
187 return get_string('textlabel', 'filters', $a);
189 return get_string('textlabelnovalue', 'filters', $a);