1 |
efrain |
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 |
/**
|
|
|
18 |
* Text field filter
|
|
|
19 |
*
|
|
|
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
|
|
|
24 |
*/
|
|
|
25 |
|
|
|
26 |
require_once($CFG->dirroot.'/user/filters/lib.php');
|
|
|
27 |
|
|
|
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
|
|
|
32 |
*/
|
|
|
33 |
class user_filter_text extends user_filter_type {
|
|
|
34 |
/** @var string */
|
|
|
35 |
public $_field;
|
|
|
36 |
|
|
|
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
|
|
|
43 |
*/
|
|
|
44 |
public function __construct($name, $label, $advanced, $field) {
|
|
|
45 |
parent::__construct($name, $label, $advanced);
|
|
|
46 |
$this->_field = $field;
|
|
|
47 |
}
|
|
|
48 |
|
|
|
49 |
/**
|
|
|
50 |
* Old syntax of class constructor. Deprecated in PHP7.
|
|
|
51 |
*
|
|
|
52 |
* @deprecated since Moodle 3.1
|
|
|
53 |
*/
|
|
|
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);
|
|
|
57 |
}
|
|
|
58 |
|
|
|
59 |
/**
|
|
|
60 |
* Returns an array of comparison operators
|
|
|
61 |
* @return array of comparison operators
|
|
|
62 |
*/
|
|
|
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'));
|
|
|
70 |
}
|
|
|
71 |
|
|
|
72 |
/**
|
|
|
73 |
* Adds controls specific to this filter in the form.
|
|
|
74 |
* @param object $mform a MoodleForm object to setup
|
|
|
75 |
*/
|
|
|
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');
|
|
|
87 |
}
|
|
|
88 |
}
|
|
|
89 |
|
|
|
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
|
|
|
94 |
*/
|
|
|
95 |
public function check_data($formdata) {
|
|
|
96 |
$field = $this->_name;
|
|
|
97 |
$operator = $field.'_op';
|
|
|
98 |
|
|
|
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;
|
|
|
103 |
}
|
|
|
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;
|
|
|
108 |
|
|
|
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;
|
|
|
113 |
}
|
|
|
114 |
}
|
|
|
115 |
return array('operator' => (int)$formdata->$operator, 'value' => $fieldvalue);
|
|
|
116 |
}
|
|
|
117 |
|
|
|
118 |
return false;
|
|
|
119 |
}
|
|
|
120 |
|
|
|
121 |
/**
|
|
|
122 |
* Returns the condition to be used with SQL where
|
|
|
123 |
* @param array $data filter settings
|
|
|
124 |
* @return array sql string and $params
|
|
|
125 |
*/
|
|
|
126 |
public function get_sql_filter($data) {
|
|
|
127 |
global $DB;
|
|
|
128 |
static $counter = 0;
|
|
|
129 |
$name = 'ex_text'.$counter++;
|
|
|
130 |
|
|
|
131 |
$operator = $data['operator'];
|
|
|
132 |
$value = $data['value'];
|
|
|
133 |
$field = $this->_field;
|
|
|
134 |
|
|
|
135 |
$params = array();
|
|
|
136 |
|
|
|
137 |
if ($operator != 5 and $value === '') {
|
|
|
138 |
return '';
|
|
|
139 |
}
|
|
|
140 |
|
|
|
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 '';
|
|
|
168 |
}
|
|
|
169 |
return array($res, $params);
|
|
|
170 |
}
|
|
|
171 |
|
|
|
172 |
/**
|
|
|
173 |
* Returns a human friendly description of the filter used as label.
|
|
|
174 |
* @param array $data filter settings
|
|
|
175 |
* @return string active filter label
|
|
|
176 |
*/
|
|
|
177 |
public function get_label($data) {
|
|
|
178 |
$operator = $data['operator'];
|
|
|
179 |
$value = $data['value'];
|
|
|
180 |
$operators = $this->getOperators();
|
|
|
181 |
|
|
|
182 |
$a = new stdClass();
|
|
|
183 |
$a->label = $this->_label;
|
|
|
184 |
$a->value = '"'.s($value).'"';
|
|
|
185 |
$a->operator = $operators[$operator];
|
|
|
186 |
|
|
|
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);
|
|
|
196 |
}
|
|
|
197 |
|
|
|
198 |
return '';
|
|
|
199 |
}
|
|
|
200 |
}
|