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');
15 * User filtering wrapper class.
17 class user_filtering
{
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) {
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;
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) {
79 unset($SESSION->user_filtering
[$fname][$i]);
81 if (empty($SESSION->user_filtering
[$fname])) {
82 unset($SESSION->user_filtering
[$fname]);
86 // clear+reload the form
88 $this->_activeform
= new user_active_filter_form($baseurl, array('fields'=>$this->_fields
, 'extraparams'=>$extraparams));
90 // now the active filters
94 * Creates known user filter if present
95 * @param string $fieldname
96 * @param boolean $advanced
97 * @return object filter
99 function get_field($fieldname, $advanced) {
100 global $USER, $CFG, $SITE;
102 switch ($fieldname) {
103 case 'username': return new user_filter_text('username', get_string('username'), $advanced, 'username');
104 case 'realname': return new user_filter_text('realname', get_string('fullname'), $advanced, sql_fullname());
105 case 'lastname': return new user_filter_text('lastname', get_string('lastname'), $advanced, 'lastname');
106 case 'firstname': return new user_filter_text('firstname', get_string('firstname'), $advanced, 'firstname');
107 case 'email': return new user_filter_text('email', get_string('email'), $advanced, 'email');
108 case 'city': return new user_filter_text('city', get_string('city'), $advanced, 'city');
109 case 'country': return new user_filter_select('country', get_string('country'), $advanced, 'country', get_list_of_countries(), $USER->country
);
110 case 'confirmed': return new user_filter_yesno('confirmed', get_string('confirmed', 'admin'), $advanced, 'confirmed');
111 case 'profile': return new user_filter_profilefield('profile', get_string('profile'), $advanced);
112 case 'courserole': return new user_filter_courserole('courserole', get_string('courserole', 'filters'), $advanced);
113 case 'systemrole': return new user_filter_globalrole('systemrole', get_string('globalrole', 'role'), $advanced);
114 case 'firstaccess': return new user_filter_date('firstaccess', get_string('firstaccess', 'filters'), $advanced, 'firstaccess');
115 case 'lastaccess': return new user_filter_date('lastaccess', get_string('lastaccess'), $advanced, 'lastaccess');
116 case 'lastlogin': return new user_filter_date('lastlogin', get_string('lastlogin'), $advanced, 'lastlogin');
118 $plugins = get_list_of_plugins('auth');
120 foreach ($plugins as $auth) {
121 $choices[$auth] = auth_get_plugin_title ($auth);
123 return new user_filter_simpleselect('auth', get_string('authentication'), $advanced, 'auth', $choices);
126 // include all hosts even those deleted or otherwise problematic
127 if (!$hosts = get_records('mnet_host', '', '', 'id', 'id, wwwroot, name')) {
131 foreach ($hosts as $host) {
132 if ($host->id
== $CFG->mnet_localhost_id
) {
133 $choices[$host->id
] = format_string($SITE->fullname
).' ('.get_string('local').')';
134 } else if (empty($host->wwwroot
)) {
138 $choices[$host->id
] = $host->name
.' ('.$host->wwwroot
.')';
141 if ($usedhosts = get_fieldset_sql("SELECT DISTINCT mnethostid FROM {$CFG->prefix}user WHERE deleted=0")) {
142 foreach ($usedhosts as $hostid) {
143 if (empty($hosts[$hostid])) {
144 $choices[$hostid] = 'id: '.$hostid.' ('.get_string('error').')';
148 if (count($choices) < 2) {
149 return null; // filter not needed
151 return new user_filter_simpleselect('mnethostid', 'mnethostid', $advanced, 'mnethostid', $choices);
153 default: return null;
158 * Returns sql where statement based on active user filters
159 * @param string $extra sql
162 function get_sql_filter($extra='') {
170 if (!empty($SESSION->user_filtering
)) {
171 foreach ($SESSION->user_filtering
as $fname=>$datas) {
172 if (!array_key_exists($fname, $this->_fields
)) {
173 continue; // filter not used
175 $field = $this->_fields
[$fname];
176 foreach($datas as $i=>$data) {
177 $sqls[] = $field->get_sql_filter($data);
185 return implode(' AND ', $sqls);
190 * Print the add filter form.
192 function display_add() {
193 $this->_addform
->display();
197 * Print the active filter form.
199 function display_active() {
200 $this->_activeform
->display();
206 * The base user filter class. All abstract classes must be implemented.
208 class user_filter_type
{
210 * The name of this filter instance.
215 * The label of this filter instance.
220 * Advanced form element flag
226 * @param string $name the name of the filter instance
227 * @param string $label the label of the filter instance
228 * @param boolean $advanced advanced form element flag
230 function user_filter_type($name, $label, $advanced) {
231 $this->_name
= $name;
232 $this->_label
= $label;
233 $this->_advanced
= $advanced;
237 * Returns the condition to be used with SQL where
238 * @param array $data filter settings
239 * @return string the filtering condition or null if the filter is disabled
241 function get_sql_filter($data) {
242 error('Abstract method get_sql_filter() called - must be implemented');
246 * Retrieves data from the form data
247 * @param object $formdata data submited with the form
248 * @return mixed array filter data or false when filter not set
250 function check_data($formdata) {
251 error('Abstract method check_data() called - must be implemented');
255 * Adds controls specific to this filter in the form.
256 * @param object $mform a MoodleForm object to setup
258 function setupForm(&$mform) {
259 error('Abstract method setupForm() called - must be implemented');
263 * Returns a human friendly description of the filter used as label.
264 * @param array $data filter settings
265 * @return string active filter label
267 function get_label($data) {
268 error('Abstract method get_label() called - must be implemented');