Commit | Line | Data |
---|---|---|
aa6c1ced | 1 | <?php |
a2ed6e69 SH |
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 | * Profile 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 | */ | |
c3260b07 | 25 | |
cd1edf9e | 26 | require_once($CFG->dirroot.'/user/filters/lib.php'); |
c3260b07 | 27 | |
28 | /** | |
29 | * User filter based on values of custom profile fields. | |
a2ed6e69 SH |
30 | * |
31 | * @copyright 1999 Martin Dougiamas http://dougiamas.com | |
32 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | |
c3260b07 | 33 | */ |
34 | class user_filter_profilefield extends user_filter_type { | |
cd1edf9e | 35 | |
c3260b07 | 36 | /** |
37 | * Constructor | |
38 | * @param string $name the name of the filter instance | |
39 | * @param string $label the label of the filter instance | |
cd1edf9e | 40 | * @param boolean $advanced advanced form element flag |
c3260b07 | 41 | */ |
c4d0b752 MG |
42 | public function __construct($name, $label, $advanced) { |
43 | parent::__construct($name, $label, $advanced); | |
44 | } | |
45 | ||
46 | /** | |
47 | * Old syntax of class constructor. Deprecated in PHP7. | |
48 | * | |
49 | * @deprecated since Moodle 3.1 | |
50 | */ | |
a2ed6e69 | 51 | public function user_filter_profilefield($name, $label, $advanced) { |
c4d0b752 MG |
52 | debugging('Use of class name as constructor is deprecated', DEBUG_DEVELOPER); |
53 | self::__construct($name, $label, $advanced); | |
c3260b07 | 54 | } |
cd1edf9e | 55 | |
c3260b07 | 56 | /** |
57 | * Returns an array of comparison operators | |
58 | * @return array of comparison operators | |
59 | */ | |
a2ed6e69 | 60 | public function get_operators() { |
cd1edf9e | 61 | return array(0 => get_string('contains', 'filters'), |
a2ed6e69 SH |
62 | 1 => get_string('doesnotcontain', 'filters'), |
63 | 2 => get_string('isequalto', 'filters'), | |
64 | 3 => get_string('startswith', 'filters'), | |
65 | 4 => get_string('endswith', 'filters'), | |
66 | 5 => get_string('isempty', 'filters'), | |
67 | 6 => get_string('isnotdefined', 'filters'), | |
68 | 7 => get_string('isdefined', 'filters')); | |
c3260b07 | 69 | } |
cd1edf9e | 70 | |
c3260b07 | 71 | /** |
72 | * Returns an array of custom profile fields | |
73 | * @return array of profile fields | |
74 | */ | |
a2ed6e69 | 75 | public function get_profile_fields() { |
5d910388 | 76 | global $DB; |
52cf9227 PH |
77 | $order = $DB->sql_order_by_text('name'); |
78 | if (!$fields = $DB->get_records_menu('user_info_field', null, $order, 'id, name')) { | |
c3260b07 | 79 | return null; |
80 | } | |
cd1edf9e | 81 | $res = array(0 => get_string('anyfield', 'filters')); |
03098df6 PH |
82 | |
83 | return $res + $fields; | |
c3260b07 | 84 | } |
85 | ||
86 | /** | |
87 | * Adds controls specific to this filter in the form. | |
88 | * @param object $mform a MoodleForm object to setup | |
89 | */ | |
a2ed6e69 SH |
90 | public function setupForm(&$mform) { |
91 | $profilefields = $this->get_profile_fields(); | |
92 | if (empty($profilefields)) { | |
c3260b07 | 93 | return; |
94 | } | |
95 | $objs = array(); | |
ff6744c5 BB |
96 | $objs['field'] = $mform->createElement('select', $this->_name.'_fld', null, $profilefields); |
97 | $objs['op'] = $mform->createElement('select', $this->_name.'_op', null, $this->get_operators()); | |
98 | $objs['value'] = $mform->createElement('text', $this->_name, null); | |
99 | $objs['field']->setLabel(get_string('profilefilterfield', 'filters')); | |
100 | $objs['op']->setLabel(get_string('profilefilterlimiter', 'filters')); | |
101 | $objs['value']->setLabel(get_string('valuefor', 'filters', $this->_label)); | |
cd1edf9e | 102 | $grp =& $mform->addElement('group', $this->_name.'_grp', $this->_label, $objs, '', false); |
a8655ae7 | 103 | $mform->setType($this->_name, PARAM_RAW); |
cd1edf9e | 104 | if ($this->_advanced) { |
105 | $mform->setAdvanced($this->_name.'_grp'); | |
106 | } | |
c3260b07 | 107 | } |
cd1edf9e | 108 | |
c3260b07 | 109 | /** |
110 | * Retrieves data from the form data | |
111 | * @param object $formdata data submited with the form | |
cd1edf9e | 112 | * @return mixed array filter data or false when filter not set |
c3260b07 | 113 | */ |
a2ed6e69 SH |
114 | public function check_data($formdata) { |
115 | $profilefields = $this->get_profile_fields(); | |
1b452baf | 116 | |
a2ed6e69 | 117 | if (empty($profilefields)) { |
1b452baf | 118 | return false; |
119 | } | |
120 | ||
cd1edf9e | 121 | $field = $this->_name; |
122 | $operator = $field.'_op'; | |
123 | $profile = $field.'_fld'; | |
124 | ||
125 | if (array_key_exists($profile, $formdata)) { | |
126 | if ($formdata->$operator < 5 and $formdata->$field === '') { | |
127 | return false; | |
128 | } | |
129 | ||
130 | return array('value' => (string)$formdata->$field, | |
131 | 'operator' => (int)$formdata->$operator, | |
132 | 'profile' => (int)$formdata->$profile); | |
133 | } | |
c3260b07 | 134 | } |
135 | ||
136 | /** | |
137 | * Returns the condition to be used with SQL where | |
cd1edf9e | 138 | * @param array $data filter settings |
624a690b | 139 | * @return array sql string and $params |
c3260b07 | 140 | */ |
a2ed6e69 | 141 | public function get_sql_filter($data) { |
7e60297f | 142 | global $CFG, $DB; |
624a690b | 143 | static $counter = 0; |
144 | $name = 'ex_profilefield'.$counter++; | |
cd1edf9e | 145 | |
a2ed6e69 SH |
146 | $profilefields = $this->get_profile_fields(); |
147 | if (empty($profilefields)) { | |
1b452baf | 148 | return ''; |
149 | } | |
150 | ||
cd1edf9e | 151 | $profile = $data['profile']; |
152 | $operator = $data['operator']; | |
624a690b | 153 | $value = $data['value']; |
cd1edf9e | 154 | |
624a690b | 155 | $params = array(); |
a2ed6e69 | 156 | if (!array_key_exists($profile, $profilefields)) { |
624a690b | 157 | return array('', array()); |
aa6c1ced | 158 | } |
1b452baf | 159 | |
cd1edf9e | 160 | $where = ""; |
161 | $op = " IN "; | |
cd1edf9e | 162 | |
163 | if ($operator < 5 and $value === '') { | |
164 | return ''; | |
c3260b07 | 165 | } |
cd1edf9e | 166 | |
167 | switch($operator) { | |
a2ed6e69 | 168 | case 0: // Contains. |
f63ac65a | 169 | $where = $DB->sql_like('data', ":$name", false, false); |
aa6c1ced | 170 | $params[$name] = "%$value%"; |
624a690b | 171 | break; |
a2ed6e69 | 172 | case 1: // Does not contain. |
47586394 | 173 | $where = $DB->sql_like('data', ":$name", false, false, true); |
aa6c1ced | 174 | $params[$name] = "%$value%"; |
624a690b | 175 | break; |
a2ed6e69 | 176 | case 2: // Equal to. |
f63ac65a | 177 | $where = $DB->sql_like('data', ":$name", false, false); |
aa6c1ced | 178 | $params[$name] = "$value"; |
624a690b | 179 | break; |
a2ed6e69 | 180 | case 3: // Starts with. |
f63ac65a | 181 | $where = $DB->sql_like('data', ":$name", false, false); |
aa6c1ced | 182 | $params[$name] = "$value%"; |
624a690b | 183 | break; |
a2ed6e69 | 184 | case 4: // Ends with. |
f63ac65a | 185 | $where = $DB->sql_like('data', ":$name", false, false); |
aa6c1ced | 186 | $params[$name] = "%$value"; |
624a690b | 187 | break; |
a2ed6e69 | 188 | case 5: // Empty. |
f63ac65a | 189 | $where = "data = :$name"; |
aa6c1ced | 190 | $params[$name] = ""; |
624a690b | 191 | break; |
a2ed6e69 SH |
192 | case 6: // Is not defined. |
193 | $op = " NOT IN "; | |
194 | break; | |
195 | case 7: // Is defined. | |
cd1edf9e | 196 | break; |
197 | } | |
198 | if ($profile) { | |
199 | if ($where !== '') { | |
200 | $where = " AND $where"; | |
c3260b07 | 201 | } |
cd1edf9e | 202 | $where = "fieldid=$profile $where"; |
c3260b07 | 203 | } |
cd1edf9e | 204 | if ($where !== '') { |
205 | $where = "WHERE $where"; | |
c3260b07 | 206 | } |
624a690b | 207 | return array("id $op (SELECT userid FROM {user_info_data} $where)", $params); |
c3260b07 | 208 | } |
cd1edf9e | 209 | |
c3260b07 | 210 | /** |
cd1edf9e | 211 | * Returns a human friendly description of the filter used as label. |
212 | * @param array $data filter settings | |
213 | * @return string active filter label | |
c3260b07 | 214 | */ |
a2ed6e69 | 215 | public function get_label($data) { |
1b452baf | 216 | $operators = $this->get_operators(); |
a2ed6e69 | 217 | $profilefields = $this->get_profile_fields(); |
1b452baf | 218 | |
a2ed6e69 | 219 | if (empty($profilefields)) { |
1b452baf | 220 | return ''; |
221 | } | |
cd1edf9e | 222 | |
223 | $profile = $data['profile']; | |
224 | $operator = $data['operator']; | |
225 | $value = $data['value']; | |
226 | ||
a2ed6e69 | 227 | if (!array_key_exists($profile, $profilefields)) { |
1b452baf | 228 | return ''; |
aa6c1ced | 229 | } |
1b452baf | 230 | |
bf718f50 | 231 | $a = new stdClass(); |
cd1edf9e | 232 | $a->label = $this->_label; |
233 | $a->value = $value; | |
a2ed6e69 | 234 | $a->profile = $profilefields[$profile]; |
cd1edf9e | 235 | $a->operator = $operators[$operator]; |
236 | ||
237 | switch($operator) { | |
a2ed6e69 SH |
238 | case 0: // Contains. |
239 | case 1: // Doesn't contain. | |
240 | case 2: // Equal to. | |
241 | case 3: // Starts with. | |
242 | case 4: // Ends with. | |
cd1edf9e | 243 | return get_string('profilelabel', 'filters', $a); |
a2ed6e69 SH |
244 | case 5: // Empty. |
245 | case 6: // Is not defined. | |
246 | case 7: // Is defined. | |
cd1edf9e | 247 | return get_string('profilelabelnovalue', 'filters', $a); |
c3260b07 | 248 | } |
cd1edf9e | 249 | return ''; |
c3260b07 | 250 | } |
251 | } |