aa6c1ced |
1 | <?php |
c3260b07 |
2 | |
cd1edf9e |
3 | require_once($CFG->dirroot.'/user/filters/text.php'); |
4 | require_once($CFG->dirroot.'/user/filters/date.php'); |
5 | require_once($CFG->dirroot.'/user/filters/select.php'); |
6 | require_once($CFG->dirroot.'/user/filters/simpleselect.php'); |
7 | require_once($CFG->dirroot.'/user/filters/courserole.php'); |
8 | require_once($CFG->dirroot.'/user/filters/globalrole.php'); |
9 | require_once($CFG->dirroot.'/user/filters/profilefield.php'); |
10 | require_once($CFG->dirroot.'/user/filters/yesno.php'); |
85c199ae |
11 | require_once($CFG->dirroot.'/user/filters/cohort.php'); |
cd1edf9e |
12 | require_once($CFG->dirroot.'/user/filters/user_filter_forms.php'); |
13 | |
14 | |
c3260b07 |
15 | /** |
cd1edf9e |
16 | * User filtering wrapper class. |
c3260b07 |
17 | */ |
cd1edf9e |
18 | class user_filtering { |
19 | var $_fields; |
20 | var $_addform; |
21 | var $_activeform; |
22 | |
23 | /** |
24 | * Contructor |
25 | * @param array array of visible user fields |
26 | * @param string base url used for submission/return, null if the same of current page |
27 | * @param array extra page parameters |
28 | */ |
29 | function user_filtering($fieldnames=null, $baseurl=null, $extraparams=null) { |
30 | global $SESSION; |
31 | |
32 | if (!isset($SESSION->user_filtering)) { |
33 | $SESSION->user_filtering = array(); |
34 | } |
35 | |
36 | if (empty($fieldnames)) { |
37 | $fieldnames = array('realname'=>0, 'lastname'=>1, 'firstname'=>1, 'email'=>1, 'city'=>1, 'country'=>1, |
85c199ae |
38 | 'confirmed'=>1, 'profile'=>1, 'courserole'=>1, 'systemrole'=>1, 'cohort'=>1, |
cf98da1a |
39 | 'firstaccess'=>1, 'lastaccess'=>1, 'lastlogin'=>1, 'timemodified'=>1, 'username'=>1, 'auth'=>1, 'mnethostid'=>1); |
cd1edf9e |
40 | } |
41 | |
42 | $this->_fields = array(); |
43 | |
44 | foreach ($fieldnames as $fieldname=>$advanced) { |
45 | if ($field = $this->get_field($fieldname, $advanced)) { |
46 | $this->_fields[$fieldname] = $field; |
47 | } |
48 | } |
49 | |
50 | // fist the new filter form |
51 | $this->_addform = new user_add_filter_form($baseurl, array('fields'=>$this->_fields, 'extraparams'=>$extraparams)); |
294ce987 |
52 | if ($adddata = $this->_addform->get_data()) { |
cd1edf9e |
53 | foreach($this->_fields as $fname=>$field) { |
54 | $data = $field->check_data($adddata); |
55 | if ($data === false) { |
56 | continue; // nothing new |
57 | } |
58 | if (!array_key_exists($fname, $SESSION->user_filtering)) { |
59 | $SESSION->user_filtering[$fname] = array(); |
60 | } |
61 | $SESSION->user_filtering[$fname][] = $data; |
62 | } |
63 | // clear the form |
64 | $_POST = array(); |
65 | $this->_addform = new user_add_filter_form($baseurl, array('fields'=>$this->_fields, 'extraparams'=>$extraparams)); |
66 | } |
67 | |
68 | // now the active filters |
69 | $this->_activeform = new user_active_filter_form($baseurl, array('fields'=>$this->_fields, 'extraparams'=>$extraparams)); |
294ce987 |
70 | if ($adddata = $this->_activeform->get_data()) { |
cd1edf9e |
71 | if (!empty($adddata->removeall)) { |
72 | $SESSION->user_filtering = array(); |
c3260b07 |
73 | |
cd1edf9e |
74 | } else if (!empty($adddata->removeselected) and !empty($adddata->filter)) { |
75 | foreach($adddata->filter as $fname=>$instances) { |
76 | foreach ($instances as $i=>$val) { |
77 | if (empty($val)) { |
78 | continue; |
79 | } |
80 | unset($SESSION->user_filtering[$fname][$i]); |
81 | } |
48eb7bc9 |
82 | if (empty($SESSION->user_filtering[$fname])) { |
83 | unset($SESSION->user_filtering[$fname]); |
aa6c1ced |
84 | } |
cd1edf9e |
85 | } |
86 | } |
87 | // clear+reload the form |
88 | $_POST = array(); |
89 | $this->_activeform = new user_active_filter_form($baseurl, array('fields'=>$this->_fields, 'extraparams'=>$extraparams)); |
90 | } |
91 | // now the active filters |
92 | } |
93 | |
94 | /** |
95 | * Creates known user filter if present |
96 | * @param string $fieldname |
97 | * @param boolean $advanced |
98 | * @return object filter |
99 | */ |
100 | function get_field($fieldname, $advanced) { |
51471f2a |
101 | global $USER, $CFG, $DB, $SITE; |
cd1edf9e |
102 | |
103 | switch ($fieldname) { |
104 | case 'username': return new user_filter_text('username', get_string('username'), $advanced, 'username'); |
7e60297f |
105 | case 'realname': return new user_filter_text('realname', get_string('fullnameuser'), $advanced, $DB->sql_fullname()); |
cd1edf9e |
106 | case 'lastname': return new user_filter_text('lastname', get_string('lastname'), $advanced, 'lastname'); |
107 | case 'firstname': return new user_filter_text('firstname', get_string('firstname'), $advanced, 'firstname'); |
108 | case 'email': return new user_filter_text('email', get_string('email'), $advanced, 'email'); |
109 | case 'city': return new user_filter_text('city', get_string('city'), $advanced, 'city'); |
0aa759b0 |
110 | case 'country': return new user_filter_select('country', get_string('country'), $advanced, 'country', get_string_manager()->get_list_of_countries(), $USER->country); |
73b3a208 |
111 | case 'confirmed': return new user_filter_yesno('confirmed', get_string('confirmed', 'admin'), $advanced, 'confirmed'); |
cd1edf9e |
112 | case 'profile': return new user_filter_profilefield('profile', get_string('profile'), $advanced); |
113 | case 'courserole': return new user_filter_courserole('courserole', get_string('courserole', 'filters'), $advanced); |
114 | case 'systemrole': return new user_filter_globalrole('systemrole', get_string('globalrole', 'role'), $advanced); |
115 | case 'firstaccess': return new user_filter_date('firstaccess', get_string('firstaccess', 'filters'), $advanced, 'firstaccess'); |
116 | case 'lastaccess': return new user_filter_date('lastaccess', get_string('lastaccess'), $advanced, 'lastaccess'); |
117 | case 'lastlogin': return new user_filter_date('lastlogin', get_string('lastlogin'), $advanced, 'lastlogin'); |
cf98da1a |
118 | case 'timemodified': return new user_filter_date('timemodified', get_string('lastmodified'), $advanced, 'timemodified'); |
85c199ae |
119 | case 'cohort': return new user_filter_cohort($advanced); |
cd1edf9e |
120 | case 'auth': |
17da2e6f |
121 | $plugins = get_plugin_list('auth'); |
cd1edf9e |
122 | $choices = array(); |
17da2e6f |
123 | foreach ($plugins as $auth => $unused) { |
370f10b7 |
124 | $choices[$auth] = get_string('pluginname', "auth_{$auth}"); |
cd1edf9e |
125 | } |
126 | return new user_filter_simpleselect('auth', get_string('authentication'), $advanced, 'auth', $choices); |
127 | |
128 | case 'mnethostid': |
257d63df |
129 | // include all hosts even those deleted or otherwise problematic |
5d910388 |
130 | if (!$hosts = $DB->get_records('mnet_host', null, 'id', 'id, wwwroot, name')) { |
257d63df |
131 | $hosts = array(); |
cd1edf9e |
132 | } |
133 | $choices = array(); |
134 | foreach ($hosts as $host) { |
257d63df |
135 | if ($host->id == $CFG->mnet_localhost_id) { |
136 | $choices[$host->id] = format_string($SITE->fullname).' ('.get_string('local').')'; |
137 | } else if (empty($host->wwwroot)) { |
138 | // All hosts |
139 | continue; |
140 | } else { |
141 | $choices[$host->id] = $host->name.' ('.$host->wwwroot.')'; |
cd1edf9e |
142 | } |
cd1edf9e |
143 | } |
257d63df |
144 | if ($usedhosts = $DB->get_fieldset_sql("SELECT DISTINCT mnethostid FROM {user} WHERE deleted=0")) { |
145 | foreach ($usedhosts as $hostid) { |
146 | if (empty($hosts[$hostid])) { |
147 | $choices[$hostid] = 'id: '.$hostid.' ('.get_string('error').')'; |
148 | } |
aa6c1ced |
149 | } |
257d63df |
150 | } |
151 | if (count($choices) < 2) { |
cd1edf9e |
152 | return null; // filter not needed |
153 | } |
6fc35adf |
154 | return new user_filter_simpleselect('mnethostid', get_string('mnetidprovider', 'mnet'), $advanced, 'mnethostid', $choices); |
cd1edf9e |
155 | |
156 | default: return null; |
157 | } |
158 | } |
159 | |
160 | /** |
161 | * Returns sql where statement based on active user filters |
162 | * @param string $extra sql |
624a690b |
163 | * @param array named params (recommended prefix ex) |
164 | * @return array sql string and $params |
cd1edf9e |
165 | */ |
624a690b |
166 | function get_sql_filter($extra='', array $params=null) { |
cd1edf9e |
167 | global $SESSION; |
168 | |
169 | $sqls = array(); |
170 | if ($extra != '') { |
171 | $sqls[] = $extra; |
172 | } |
624a690b |
173 | $params = (array)$params; |
cd1edf9e |
174 | |
175 | if (!empty($SESSION->user_filtering)) { |
176 | foreach ($SESSION->user_filtering as $fname=>$datas) { |
177 | if (!array_key_exists($fname, $this->_fields)) { |
178 | continue; // filter not used |
179 | } |
180 | $field = $this->_fields[$fname]; |
181 | foreach($datas as $i=>$data) { |
624a690b |
182 | list($s, $p) = $field->get_sql_filter($data); |
183 | $sqls[] = $s; |
184 | $params = $params + $p; |
cd1edf9e |
185 | } |
186 | } |
187 | } |
188 | |
189 | if (empty($sqls)) { |
624a690b |
190 | return array('', array()); |
cd1edf9e |
191 | } else { |
624a690b |
192 | $sqls = implode(' AND ', $sqls); |
193 | return array($sqls, $params); |
cd1edf9e |
194 | } |
195 | } |
196 | |
197 | /** |
198 | * Print the add filter form. |
199 | */ |
200 | function display_add() { |
201 | $this->_addform->display(); |
202 | } |
203 | |
204 | /** |
205 | * Print the active filter form. |
206 | */ |
207 | function display_active() { |
208 | $this->_activeform->display(); |
209 | } |
210 | |
211 | } |
c3260b07 |
212 | |
213 | /** |
cd1edf9e |
214 | * The base user filter class. All abstract classes must be implemented. |
c3260b07 |
215 | */ |
216 | class user_filter_type { |
217 | /** |
218 | * The name of this filter instance. |
219 | */ |
220 | var $_name; |
cd1edf9e |
221 | |
c3260b07 |
222 | /** |
223 | * The label of this filter instance. |
224 | */ |
225 | var $_label; |
cd1edf9e |
226 | |
c3260b07 |
227 | /** |
cd1edf9e |
228 | * Advanced form element flag |
c3260b07 |
229 | */ |
cd1edf9e |
230 | var $_advanced; |
231 | |
c3260b07 |
232 | /** |
233 | * Constructor |
234 | * @param string $name the name of the filter instance |
235 | * @param string $label the label of the filter instance |
cd1edf9e |
236 | * @param boolean $advanced advanced form element flag |
c3260b07 |
237 | */ |
cd1edf9e |
238 | function user_filter_type($name, $label, $advanced) { |
239 | $this->_name = $name; |
240 | $this->_label = $label; |
241 | $this->_advanced = $advanced; |
c3260b07 |
242 | } |
cd1edf9e |
243 | |
c3260b07 |
244 | /** |
245 | * Returns the condition to be used with SQL where |
cd1edf9e |
246 | * @param array $data filter settings |
c3260b07 |
247 | * @return string the filtering condition or null if the filter is disabled |
248 | */ |
cd1edf9e |
249 | function get_sql_filter($data) { |
d3248238 |
250 | print_error('mustbeoveride', 'debug', '', 'get_sql_filter'); |
c3260b07 |
251 | } |
cd1edf9e |
252 | |
c3260b07 |
253 | /** |
254 | * Retrieves data from the form data |
255 | * @param object $formdata data submited with the form |
cd1edf9e |
256 | * @return mixed array filter data or false when filter not set |
c3260b07 |
257 | */ |
cd1edf9e |
258 | function check_data($formdata) { |
d3248238 |
259 | print_error('mustbeoveride', 'debug', '', 'check_data'); |
c3260b07 |
260 | } |
261 | |
262 | /** |
263 | * Adds controls specific to this filter in the form. |
264 | * @param object $mform a MoodleForm object to setup |
265 | */ |
266 | function setupForm(&$mform) { |
d3248238 |
267 | print_error('mustbeoveride', 'debug', '', 'setupForm'); |
c3260b07 |
268 | } |
cd1edf9e |
269 | |
c3260b07 |
270 | /** |
cd1edf9e |
271 | * Returns a human friendly description of the filter used as label. |
272 | * @param array $data filter settings |
273 | * @return string active filter label |
c3260b07 |
274 | */ |
cd1edf9e |
275 | function get_label($data) { |
d3248238 |
276 | print_error('mustbeoveride', 'debug', '', 'get_label'); |
c3260b07 |
277 | } |
278 | } |