MDL-29400 Update the upload and uploadsingle assignment classes to so
[moodle.git] / user / filters / lib.php
blob91bc1137839daff4eb82f113dc9a24939beac676
1 <?php
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');
11 require_once($CFG->dirroot.'/user/filters/cohort.php');
12 require_once($CFG->dirroot.'/user/filters/user_filter_forms.php');
13 require_once($CFG->dirroot.'/user/filters/checkbox.php');
15 /**
16 * User filtering wrapper class.
18 class user_filtering {
19 var $_fields;
20 var $_addform;
21 var $_activeform;
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
29 function user_filtering($fieldnames=null, $baseurl=null, $extraparams=null) {
30 global $SESSION;
32 if (!isset($SESSION->user_filtering)) {
33 $SESSION->user_filtering = array();
36 if (empty($fieldnames)) {
37 $fieldnames = array('realname'=>0, 'lastname'=>1, 'firstname'=>1, 'email'=>1, 'city'=>1, 'country'=>1,
38 'confirmed'=>1, 'profile'=>1, 'courserole'=>1, 'systemrole'=>1, 'cohort'=>1,
39 'firstaccess'=>1, 'lastaccess'=>1, 'neveraccessed'=>1, 'timemodified'=>1,
40 'nevermodified'=>1, 'username'=>1, 'auth'=>1, 'mnethostid'=>1);
43 $this->_fields = array();
45 foreach ($fieldnames as $fieldname=>$advanced) {
46 if ($field = $this->get_field($fieldname, $advanced)) {
47 $this->_fields[$fieldname] = $field;
51 // fist the new filter form
52 $this->_addform = new user_add_filter_form($baseurl, array('fields'=>$this->_fields, 'extraparams'=>$extraparams));
53 if ($adddata = $this->_addform->get_data()) {
54 foreach($this->_fields as $fname=>$field) {
55 $data = $field->check_data($adddata);
56 if ($data === false) {
57 continue; // nothing new
59 if (!array_key_exists($fname, $SESSION->user_filtering)) {
60 $SESSION->user_filtering[$fname] = array();
62 $SESSION->user_filtering[$fname][] = $data;
64 // clear the form
65 $_POST = array();
66 $this->_addform = new user_add_filter_form($baseurl, array('fields'=>$this->_fields, 'extraparams'=>$extraparams));
69 // now the active filters
70 $this->_activeform = new user_active_filter_form($baseurl, array('fields'=>$this->_fields, 'extraparams'=>$extraparams));
71 if ($adddata = $this->_activeform->get_data()) {
72 if (!empty($adddata->removeall)) {
73 $SESSION->user_filtering = array();
75 } else if (!empty($adddata->removeselected) and !empty($adddata->filter)) {
76 foreach($adddata->filter as $fname=>$instances) {
77 foreach ($instances as $i=>$val) {
78 if (empty($val)) {
79 continue;
81 unset($SESSION->user_filtering[$fname][$i]);
83 if (empty($SESSION->user_filtering[$fname])) {
84 unset($SESSION->user_filtering[$fname]);
88 // clear+reload the form
89 $_POST = array();
90 $this->_activeform = new user_active_filter_form($baseurl, array('fields'=>$this->_fields, 'extraparams'=>$extraparams));
92 // now the active filters
95 /**
96 * Creates known user filter if present
97 * @param string $fieldname
98 * @param boolean $advanced
99 * @return object filter
101 function get_field($fieldname, $advanced) {
102 global $USER, $CFG, $DB, $SITE;
104 switch ($fieldname) {
105 case 'username': return new user_filter_text('username', get_string('username'), $advanced, 'username');
106 case 'realname': return new user_filter_text('realname', get_string('fullnameuser'), $advanced, $DB->sql_fullname());
107 case 'lastname': return new user_filter_text('lastname', get_string('lastname'), $advanced, 'lastname');
108 case 'firstname': return new user_filter_text('firstname', get_string('firstname'), $advanced, 'firstname');
109 case 'email': return new user_filter_text('email', get_string('email'), $advanced, 'email');
110 case 'city': return new user_filter_text('city', get_string('city'), $advanced, 'city');
111 case 'country': return new user_filter_select('country', get_string('country'), $advanced, 'country', get_string_manager()->get_list_of_countries(), $USER->country);
112 case 'confirmed': return new user_filter_yesno('confirmed', get_string('confirmed', 'admin'), $advanced, 'confirmed');
113 case 'profile': return new user_filter_profilefield('profile', get_string('profile'), $advanced);
114 case 'courserole': return new user_filter_courserole('courserole', get_string('courserole', 'filters'), $advanced);
115 case 'systemrole': return new user_filter_globalrole('systemrole', get_string('globalrole', 'role'), $advanced);
116 case 'firstaccess': return new user_filter_date('firstaccess', get_string('firstaccess', 'filters'), $advanced, 'firstaccess');
117 case 'lastaccess': return new user_filter_date('lastaccess', get_string('lastaccess'), $advanced, 'lastaccess');
118 case 'neveraccessed': return new user_filter_checkbox('neveraccessed', get_string('neveraccessed', 'filters'), $advanced, 'firstaccess', array('lastaccess_sck', 'lastaccess_eck', 'firstaccess_eck', 'firstaccess_sck'));
119 case 'timemodified': return new user_filter_date('timemodified', get_string('lastmodified'), $advanced, 'timemodified');
120 case 'nevermodified': return new user_filter_checkbox('nevermodified', get_string('nevermodified', 'filters'), $advanced, array('timemodified', 'timecreated'), array('timemodified_sck', 'timemodified_eck'));
121 case 'cohort': return new user_filter_cohort($advanced);
122 case 'auth':
123 $plugins = get_plugin_list('auth');
124 $choices = array();
125 foreach ($plugins as $auth => $unused) {
126 $choices[$auth] = get_string('pluginname', "auth_{$auth}");
128 return new user_filter_simpleselect('auth', get_string('authentication'), $advanced, 'auth', $choices);
130 case 'mnethostid':
131 // include all hosts even those deleted or otherwise problematic
132 if (!$hosts = $DB->get_records('mnet_host', null, 'id', 'id, wwwroot, name')) {
133 $hosts = array();
135 $choices = array();
136 foreach ($hosts as $host) {
137 if ($host->id == $CFG->mnet_localhost_id) {
138 $choices[$host->id] = format_string($SITE->fullname).' ('.get_string('local').')';
139 } else if (empty($host->wwwroot)) {
140 // All hosts
141 continue;
142 } else {
143 $choices[$host->id] = $host->name.' ('.$host->wwwroot.')';
146 if ($usedhosts = $DB->get_fieldset_sql("SELECT DISTINCT mnethostid FROM {user} WHERE deleted=0")) {
147 foreach ($usedhosts as $hostid) {
148 if (empty($hosts[$hostid])) {
149 $choices[$hostid] = 'id: '.$hostid.' ('.get_string('error').')';
153 if (count($choices) < 2) {
154 return null; // filter not needed
156 return new user_filter_simpleselect('mnethostid', get_string('mnetidprovider', 'mnet'), $advanced, 'mnethostid', $choices);
158 default: return null;
163 * Returns sql where statement based on active user filters
164 * @param string $extra sql
165 * @param array named params (recommended prefix ex)
166 * @return array sql string and $params
168 function get_sql_filter($extra='', array $params=null) {
169 global $SESSION;
171 $sqls = array();
172 if ($extra != '') {
173 $sqls[] = $extra;
175 $params = (array)$params;
177 if (!empty($SESSION->user_filtering)) {
178 foreach ($SESSION->user_filtering as $fname=>$datas) {
179 if (!array_key_exists($fname, $this->_fields)) {
180 continue; // filter not used
182 $field = $this->_fields[$fname];
183 foreach($datas as $i=>$data) {
184 list($s, $p) = $field->get_sql_filter($data);
185 $sqls[] = $s;
186 $params = $params + $p;
191 if (empty($sqls)) {
192 return array('', array());
193 } else {
194 $sqls = implode(' AND ', $sqls);
195 return array($sqls, $params);
200 * Print the add filter form.
202 function display_add() {
203 $this->_addform->display();
207 * Print the active filter form.
209 function display_active() {
210 $this->_activeform->display();
216 * The base user filter class. All abstract classes must be implemented.
218 class user_filter_type {
220 * The name of this filter instance.
222 var $_name;
225 * The label of this filter instance.
227 var $_label;
230 * Advanced form element flag
232 var $_advanced;
235 * Constructor
236 * @param string $name the name of the filter instance
237 * @param string $label the label of the filter instance
238 * @param boolean $advanced advanced form element flag
240 function user_filter_type($name, $label, $advanced) {
241 $this->_name = $name;
242 $this->_label = $label;
243 $this->_advanced = $advanced;
247 * Returns the condition to be used with SQL where
248 * @param array $data filter settings
249 * @return string the filtering condition or null if the filter is disabled
251 function get_sql_filter($data) {
252 print_error('mustbeoveride', 'debug', '', 'get_sql_filter');
256 * Retrieves data from the form data
257 * @param object $formdata data submited with the form
258 * @return mixed array filter data or false when filter not set
260 function check_data($formdata) {
261 print_error('mustbeoveride', 'debug', '', 'check_data');
265 * Adds controls specific to this filter in the form.
266 * @param object $mform a MoodleForm object to setup
268 function setupForm(&$mform) {
269 print_error('mustbeoveride', 'debug', '', 'setupForm');
273 * Returns a human friendly description of the filter used as label.
274 * @param array $data filter settings
275 * @return string active filter label
277 function get_label($data) {
278 print_error('mustbeoveride', 'debug', '', 'get_label');