MDL-35768 Added format-specific options to edit course and section forms
[moodle.git] / user / filters / lib.php
blob6c6a8395be7f1f978ff46fb21f9d406468d13ba7
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, 'suspended'=>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 'suspended': return new user_filter_yesno('suspended', get_string('suspended', 'auth'), $advanced, 'suspended');
114 case 'profile': return new user_filter_profilefield('profile', get_string('profile'), $advanced);
115 case 'courserole': return new user_filter_courserole('courserole', get_string('courserole', 'filters'), $advanced);
116 case 'systemrole': return new user_filter_globalrole('systemrole', get_string('globalrole', 'role'), $advanced);
117 case 'firstaccess': return new user_filter_date('firstaccess', get_string('firstaccess', 'filters'), $advanced, 'firstaccess');
118 case 'lastaccess': return new user_filter_date('lastaccess', get_string('lastaccess'), $advanced, 'lastaccess');
119 case 'neveraccessed': return new user_filter_checkbox('neveraccessed', get_string('neveraccessed', 'filters'), $advanced, 'firstaccess', array('lastaccess_sck', 'lastaccess_eck', 'firstaccess_eck', 'firstaccess_sck'));
120 case 'timemodified': return new user_filter_date('timemodified', get_string('lastmodified'), $advanced, 'timemodified');
121 case 'nevermodified': return new user_filter_checkbox('nevermodified', get_string('nevermodified', 'filters'), $advanced, array('timemodified', 'timecreated'), array('timemodified_sck', 'timemodified_eck'));
122 case 'cohort': return new user_filter_cohort($advanced);
123 case 'auth':
124 $plugins = get_plugin_list('auth');
125 $choices = array();
126 foreach ($plugins as $auth => $unused) {
127 $choices[$auth] = get_string('pluginname', "auth_{$auth}");
129 return new user_filter_simpleselect('auth', get_string('authentication'), $advanced, 'auth', $choices);
131 case 'mnethostid':
132 // include all hosts even those deleted or otherwise problematic
133 if (!$hosts = $DB->get_records('mnet_host', null, 'id', 'id, wwwroot, name')) {
134 $hosts = array();
136 $choices = array();
137 foreach ($hosts as $host) {
138 if ($host->id == $CFG->mnet_localhost_id) {
139 $choices[$host->id] = format_string($SITE->fullname).' ('.get_string('local').')';
140 } else if (empty($host->wwwroot)) {
141 // All hosts
142 continue;
143 } else {
144 $choices[$host->id] = $host->name.' ('.$host->wwwroot.')';
147 if ($usedhosts = $DB->get_fieldset_sql("SELECT DISTINCT mnethostid FROM {user} WHERE deleted=0")) {
148 foreach ($usedhosts as $hostid) {
149 if (empty($hosts[$hostid])) {
150 $choices[$hostid] = 'id: '.$hostid.' ('.get_string('error').')';
154 if (count($choices) < 2) {
155 return null; // filter not needed
157 return new user_filter_simpleselect('mnethostid', get_string('mnetidprovider', 'mnet'), $advanced, 'mnethostid', $choices);
159 default: return null;
164 * Returns sql where statement based on active user filters
165 * @param string $extra sql
166 * @param array named params (recommended prefix ex)
167 * @return array sql string and $params
169 function get_sql_filter($extra='', array $params=null) {
170 global $SESSION;
172 $sqls = array();
173 if ($extra != '') {
174 $sqls[] = $extra;
176 $params = (array)$params;
178 if (!empty($SESSION->user_filtering)) {
179 foreach ($SESSION->user_filtering as $fname=>$datas) {
180 if (!array_key_exists($fname, $this->_fields)) {
181 continue; // filter not used
183 $field = $this->_fields[$fname];
184 foreach($datas as $i=>$data) {
185 list($s, $p) = $field->get_sql_filter($data);
186 $sqls[] = $s;
187 $params = $params + $p;
192 if (empty($sqls)) {
193 return array('', array());
194 } else {
195 $sqls = implode(' AND ', $sqls);
196 return array($sqls, $params);
201 * Print the add filter form.
203 function display_add() {
204 $this->_addform->display();
208 * Print the active filter form.
210 function display_active() {
211 $this->_activeform->display();
217 * The base user filter class. All abstract classes must be implemented.
219 class user_filter_type {
221 * The name of this filter instance.
223 var $_name;
226 * The label of this filter instance.
228 var $_label;
231 * Advanced form element flag
233 var $_advanced;
236 * Constructor
237 * @param string $name the name of the filter instance
238 * @param string $label the label of the filter instance
239 * @param boolean $advanced advanced form element flag
241 function user_filter_type($name, $label, $advanced) {
242 $this->_name = $name;
243 $this->_label = $label;
244 $this->_advanced = $advanced;
248 * Returns the condition to be used with SQL where
249 * @param array $data filter settings
250 * @return string the filtering condition or null if the filter is disabled
252 function get_sql_filter($data) {
253 print_error('mustbeoveride', 'debug', '', 'get_sql_filter');
257 * Retrieves data from the form data
258 * @param object $formdata data submited with the form
259 * @return mixed array filter data or false when filter not set
261 function check_data($formdata) {
262 print_error('mustbeoveride', 'debug', '', 'check_data');
266 * Adds controls specific to this filter in the form.
267 * @param object $mform a MoodleForm object to setup
269 function setupForm(&$mform) {
270 print_error('mustbeoveride', 'debug', '', 'setupForm');
274 * Returns a human friendly description of the filter used as label.
275 * @param array $data filter settings
276 * @return string active filter label
278 function get_label($data) {
279 print_error('mustbeoveride', 'debug', '', 'get_label');