forked from moodle/moodle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
profilefield.php
252 lines (226 loc) · 8.37 KB
/
profilefield.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Profile field filter.
*
* @package core_user
* @category user
* @copyright 1999 Martin Dougiamas http://dougiamas.com
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
require_once($CFG->dirroot.'/user/filters/lib.php');
/**
* User filter based on values of custom profile fields.
*
* @copyright 1999 Martin Dougiamas http://dougiamas.com
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class user_filter_profilefield extends user_filter_type {
/**
* Constructor
* @param string $name the name of the filter instance
* @param string $label the label of the filter instance
* @param boolean $advanced advanced form element flag
*/
public function __construct($name, $label, $advanced) {
parent::__construct($name, $label, $advanced);
}
/**
* Old syntax of class constructor. Deprecated in PHP7.
*
* @deprecated since Moodle 3.1
*/
public function user_filter_profilefield($name, $label, $advanced) {
debugging('Use of class name as constructor is deprecated', DEBUG_DEVELOPER);
self::__construct($name, $label, $advanced);
}
/**
* Returns an array of comparison operators
* @return array of comparison operators
*/
public function get_operators() {
return array(0 => get_string('contains', 'filters'),
1 => get_string('doesnotcontain', 'filters'),
2 => get_string('isequalto', 'filters'),
3 => get_string('startswith', 'filters'),
4 => get_string('endswith', 'filters'),
5 => get_string('isempty', 'filters'),
6 => get_string('isnotdefined', 'filters'),
7 => get_string('isdefined', 'filters'));
}
/**
* Returns an array of custom profile fields
* @return array of profile fields
*/
public function get_profile_fields() {
global $DB;
if (!$fields = $DB->get_records('user_info_field', null, 'shortname', 'id,shortname')) {
return null;
}
$res = array(0 => get_string('anyfield', 'filters'));
foreach ($fields as $k => $v) {
$res[$k] = $v->shortname;
}
return $res;
}
/**
* Adds controls specific to this filter in the form.
* @param object $mform a MoodleForm object to setup
*/
public function setupForm(&$mform) {
$profilefields = $this->get_profile_fields();
if (empty($profilefields)) {
return;
}
$objs = array();
$objs['field'] = $mform->createElement('select', $this->_name.'_fld', null, $profilefields);
$objs['op'] = $mform->createElement('select', $this->_name.'_op', null, $this->get_operators());
$objs['value'] = $mform->createElement('text', $this->_name, null);
$objs['field']->setLabel(get_string('profilefilterfield', 'filters'));
$objs['op']->setLabel(get_string('profilefilterlimiter', 'filters'));
$objs['value']->setLabel(get_string('valuefor', 'filters', $this->_label));
$grp =& $mform->addElement('group', $this->_name.'_grp', $this->_label, $objs, '', false);
$mform->setType($this->_name, PARAM_RAW);
if ($this->_advanced) {
$mform->setAdvanced($this->_name.'_grp');
}
}
/**
* Retrieves data from the form data
* @param object $formdata data submited with the form
* @return mixed array filter data or false when filter not set
*/
public function check_data($formdata) {
$profilefields = $this->get_profile_fields();
if (empty($profilefields)) {
return false;
}
$field = $this->_name;
$operator = $field.'_op';
$profile = $field.'_fld';
if (array_key_exists($profile, $formdata)) {
if ($formdata->$operator < 5 and $formdata->$field === '') {
return false;
}
return array('value' => (string)$formdata->$field,
'operator' => (int)$formdata->$operator,
'profile' => (int)$formdata->$profile);
}
}
/**
* Returns the condition to be used with SQL where
* @param array $data filter settings
* @return array sql string and $params
*/
public function get_sql_filter($data) {
global $CFG, $DB;
static $counter = 0;
$name = 'ex_profilefield'.$counter++;
$profilefields = $this->get_profile_fields();
if (empty($profilefields)) {
return '';
}
$profile = $data['profile'];
$operator = $data['operator'];
$value = $data['value'];
$params = array();
if (!array_key_exists($profile, $profilefields)) {
return array('', array());
}
$where = "";
$op = " IN ";
if ($operator < 5 and $value === '') {
return '';
}
switch($operator) {
case 0: // Contains.
$where = $DB->sql_like('data', ":$name", false, false);
$params[$name] = "%$value%";
break;
case 1: // Does not contain.
$where = $DB->sql_like('data', ":$name", false, false, true);
$params[$name] = "%$value%";
break;
case 2: // Equal to.
$where = $DB->sql_like('data', ":$name", false, false);
$params[$name] = "$value";
break;
case 3: // Starts with.
$where = $DB->sql_like('data', ":$name", false, false);
$params[$name] = "$value%";
break;
case 4: // Ends with.
$where = $DB->sql_like('data', ":$name", false, false);
$params[$name] = "%$value";
break;
case 5: // Empty.
$where = "data = :$name";
$params[$name] = "";
break;
case 6: // Is not defined.
$op = " NOT IN ";
break;
case 7: // Is defined.
break;
}
if ($profile) {
if ($where !== '') {
$where = " AND $where";
}
$where = "fieldid=$profile $where";
}
if ($where !== '') {
$where = "WHERE $where";
}
return array("id $op (SELECT userid FROM {user_info_data} $where)", $params);
}
/**
* Returns a human friendly description of the filter used as label.
* @param array $data filter settings
* @return string active filter label
*/
public function get_label($data) {
$operators = $this->get_operators();
$profilefields = $this->get_profile_fields();
if (empty($profilefields)) {
return '';
}
$profile = $data['profile'];
$operator = $data['operator'];
$value = $data['value'];
if (!array_key_exists($profile, $profilefields)) {
return '';
}
$a = new stdClass();
$a->label = $this->_label;
$a->value = $value;
$a->profile = $profilefields[$profile];
$a->operator = $operators[$operator];
switch($operator) {
case 0: // Contains.
case 1: // Doesn't contain.
case 2: // Equal to.
case 3: // Starts with.
case 4: // Ends with.
return get_string('profilelabel', 'filters', $a);
case 5: // Empty.
case 6: // Is not defined.
case 7: // Is defined.
return get_string('profilelabelnovalue', 'filters', $a);
}
return '';
}
}