"MDL-8642, setting of auto scrolling"
[moodle-linuxchix.git] / user / filters / lib.php
blob85db4d69c3b59df72779bd24ab93f95c3b43663d
1 <?php //$Id$
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/user_filter_forms.php');
14 /**
15 * User filtering wrapper class.
17 class user_filtering {
18 var $_fields;
19 var $_addform;
20 var $_activeform;
22 /**
23 * Contructor
24 * @param array array of visible user fields
25 * @param string base url used for submission/return, null if the same of current page
26 * @param array extra page parameters
28 function user_filtering($fieldnames=null, $baseurl=null, $extraparams=null) {
29 global $SESSION;
31 if (!isset($SESSION->user_filtering)) {
32 $SESSION->user_filtering = array();
35 if (empty($fieldnames)) {
36 $fieldnames = array('realname'=>0, 'lastname'=>1, 'firstname'=>1, 'email'=>1, 'city'=>1, 'country'=>1,
37 'confirmed'=>1, 'profile'=>1, 'courserole'=>1, 'systemrole'=>1,
38 'firstaccess'=>1, 'lastaccess'=>1, 'lastlogin'=>1, 'username'=>1, 'auth'=>1, 'mnethostid'=>1);
41 $this->_fields = array();
43 foreach ($fieldnames as $fieldname=>$advanced) {
44 if ($field = $this->get_field($fieldname, $advanced)) {
45 $this->_fields[$fieldname] = $field;
49 // fist the new filter form
50 $this->_addform = new user_add_filter_form($baseurl, array('fields'=>$this->_fields, 'extraparams'=>$extraparams));
51 if ($adddata = $this->_addform->get_data(false)) {
52 foreach($this->_fields as $fname=>$field) {
53 $data = $field->check_data($adddata);
54 if ($data === false) {
55 continue; // nothing new
57 if (!array_key_exists($fname, $SESSION->user_filtering)) {
58 $SESSION->user_filtering[$fname] = array();
60 $SESSION->user_filtering[$fname][] = $data;
62 // clear the form
63 $_POST = array();
64 $this->_addform = new user_add_filter_form($baseurl, array('fields'=>$this->_fields, 'extraparams'=>$extraparams));
67 // now the active filters
68 $this->_activeform = new user_active_filter_form($baseurl, array('fields'=>$this->_fields, 'extraparams'=>$extraparams));
69 if ($adddata = $this->_activeform->get_data(false)) {
70 if (!empty($adddata->removeall)) {
71 $SESSION->user_filtering = array();
73 } else if (!empty($adddata->removeselected) and !empty($adddata->filter)) {
74 foreach($adddata->filter as $fname=>$instances) {
75 foreach ($instances as $i=>$val) {
76 if (empty($val)) {
77 continue;
79 unset($SESSION->user_filtering[$fname][$i]);
83 // clear+reload the form
84 $_POST = array();
85 $this->_activeform = new user_active_filter_form($baseurl, array('fields'=>$this->_fields, 'extraparams'=>$extraparams));
87 // now the active filters
90 /**
91 * Creates known user filter if present
92 * @param string $fieldname
93 * @param boolean $advanced
94 * @return object filter
96 function get_field($fieldname, $advanced) {
97 global $USER;
99 switch ($fieldname) {
100 case 'username': return new user_filter_text('username', get_string('username'), $advanced, 'username');
101 case 'realname': return new user_filter_text('realname', get_string('fullname'), $advanced, sql_fullname());
102 case 'lastname': return new user_filter_text('lastname', get_string('lastname'), $advanced, 'lastname');
103 case 'firstname': return new user_filter_text('firstname', get_string('firstname'), $advanced, 'firstname');
104 case 'email': return new user_filter_text('email', get_string('email'), $advanced, 'email');
105 case 'city': return new user_filter_text('city', get_string('city'), $advanced, 'city');
106 case 'country': return new user_filter_select('country', get_string('country'), $advanced, 'country', get_list_of_countries(), $USER->country);
107 case 'confirmed': return new user_filter_yesno('confirmed', get_string('confirm'), $advanced, 'confirmed');
108 case 'profile': return new user_filter_profilefield('profile', get_string('profile'), $advanced);
109 case 'courserole': return new user_filter_courserole('courserole', get_string('courserole', 'filters'), $advanced);
110 case 'systemrole': return new user_filter_globalrole('systemrole', get_string('globalrole', 'role'), $advanced);
111 case 'firstaccess': return new user_filter_date('firstaccess', get_string('firstaccess', 'filters'), $advanced, 'firstaccess');
112 case 'lastaccess': return new user_filter_date('lastaccess', get_string('lastaccess'), $advanced, 'lastaccess');
113 case 'lastlogin': return new user_filter_date('lastlogin', get_string('lastlogin'), $advanced, 'lastlogin');
114 case 'auth':
115 $plugins = get_list_of_plugins('auth');
116 $choices = array();
117 foreach ($plugins as $auth) {
118 $choices[$auth] = get_string("auth_{$auth}title", 'auth');
120 return new user_filter_simpleselect('auth', get_string('authentication'), $advanced, 'auth', $choices);
122 case 'mnethostid':
123 if (!$hosts = get_records('mnet_host', '', '', 'id', 'id, wwwroot, name')) {
124 return null;
126 $choices = array();
127 foreach ($hosts as $host) {
128 if (empty($host->wwwroot)) {
129 continue; // skip all hosts
131 $choices[$host->id] = $host->name.' ('.$host->wwwroot.')';
133 if (count($choices < 2)) {
134 return null; // filter not needed
136 return new user_filter_simpleselect('mnethostid', 'mnethostid', $advanced, 'mnethostid', $choices);
138 default: return null;
143 * Returns sql where statement based on active user filters
144 * @param string $extra sql
145 * @return string
147 function get_sql_filter($extra='') {
148 global $SESSION;
150 $sqls = array();
151 if ($extra != '') {
152 $sqls[] = $extra;
155 if (!empty($SESSION->user_filtering)) {
156 foreach ($SESSION->user_filtering as $fname=>$datas) {
157 if (!array_key_exists($fname, $this->_fields)) {
158 continue; // filter not used
160 $field = $this->_fields[$fname];
161 foreach($datas as $i=>$data) {
162 $sqls[] = $field->get_sql_filter($data);
167 if (empty($sqls)) {
168 return '';
169 } else {
170 return implode(' AND ', $sqls);
175 * Print the add filter form.
177 function display_add() {
178 $this->_addform->display();
182 * Print the active filter form.
184 function display_active() {
185 $this->_activeform->display();
191 * The base user filter class. All abstract classes must be implemented.
193 class user_filter_type {
195 * The name of this filter instance.
197 var $_name;
200 * The label of this filter instance.
202 var $_label;
205 * Advanced form element flag
207 var $_advanced;
210 * Constructor
211 * @param string $name the name of the filter instance
212 * @param string $label the label of the filter instance
213 * @param boolean $advanced advanced form element flag
215 function user_filter_type($name, $label, $advanced) {
216 $this->_name = $name;
217 $this->_label = $label;
218 $this->_advanced = $advanced;
222 * Returns the condition to be used with SQL where
223 * @param array $data filter settings
224 * @return string the filtering condition or null if the filter is disabled
226 function get_sql_filter($data) {
227 error('Abstract method get_sql_filter() called - must be implemented');
231 * Retrieves data from the form data
232 * @param object $formdata data submited with the form
233 * @return mixed array filter data or false when filter not set
235 function check_data($formdata) {
236 error('Abstract method check_data() called - must be implemented');
240 * Adds controls specific to this filter in the form.
241 * @param object $mform a MoodleForm object to setup
243 function setupForm(&$mform) {
244 error('Abstract method setupForm() called - must be implemented');
248 * Returns a human friendly description of the filter used as label.
249 * @param array $data filter settings
250 * @return string active filter label
252 function get_label($data) {
253 error('Abstract method get_label() called - must be implemented');