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, 'timemodified'=>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()) {
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()) {
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, $DB, $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('fullnameuser'), $advanced, $DB->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_string_manager()->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');
117 case 'timemodified': return new user_filter_date('timemodified', get_string('lastmodified'), $advanced, 'timemodified');
119 $plugins = get_plugin_list('auth');
121 foreach ($plugins as $auth => $unused) {
122 $choices[$auth] = get_string('pluginname', "auth_{$auth}");
124 return new user_filter_simpleselect('auth', get_string('authentication'), $advanced, 'auth', $choices);
127 // include all hosts even those deleted or otherwise problematic
128 if (!$hosts = $DB->get_records('mnet_host', null, 'id', 'id, wwwroot, name')) {
132 foreach ($hosts as $host) {
133 if ($host->id
== $CFG->mnet_localhost_id
) {
134 $choices[$host->id
] = format_string($SITE->fullname
).' ('.get_string('local').')';
135 } else if (empty($host->wwwroot
)) {
139 $choices[$host->id
] = $host->name
.' ('.$host->wwwroot
.')';
141 $choices[$host->id
] = $host->name
.' ('.$host->wwwroot
.')';
143 if ($usedhosts = $DB->get_fieldset_sql("SELECT DISTINCT mnethostid FROM {user} WHERE deleted=0")) {
144 foreach ($usedhosts as $hostid) {
145 if (empty($hosts[$hostid])) {
146 $choices[$hostid] = 'id: '.$hostid.' ('.get_string('error').')';
150 if (count($choices) < 2) {
151 return null; // filter not needed
153 return new user_filter_simpleselect('mnethostid', 'mnethostid', $advanced, 'mnethostid', $choices);
155 default: return null;
160 * Returns sql where statement based on active user filters
161 * @param string $extra sql
162 * @param array named params (recommended prefix ex)
163 * @return array sql string and $params
165 function get_sql_filter($extra='', array $params=null) {
172 $params = (array)$params;
174 if (!empty($SESSION->user_filtering
)) {
175 foreach ($SESSION->user_filtering
as $fname=>$datas) {
176 if (!array_key_exists($fname, $this->_fields
)) {
177 continue; // filter not used
179 $field = $this->_fields
[$fname];
180 foreach($datas as $i=>$data) {
181 list($s, $p) = $field->get_sql_filter($data);
183 $params = $params +
$p;
189 return array('', array());
191 $sqls = implode(' AND ', $sqls);
192 return array($sqls, $params);
197 * Print the add filter form.
199 function display_add() {
200 $this->_addform
->display();
204 * Print the active filter form.
206 function display_active() {
207 $this->_activeform
->display();
213 * The base user filter class. All abstract classes must be implemented.
215 class user_filter_type
{
217 * The name of this filter instance.
222 * The label of this filter instance.
227 * Advanced form element flag
233 * @param string $name the name of the filter instance
234 * @param string $label the label of the filter instance
235 * @param boolean $advanced advanced form element flag
237 function user_filter_type($name, $label, $advanced) {
238 $this->_name
= $name;
239 $this->_label
= $label;
240 $this->_advanced
= $advanced;
244 * Returns the condition to be used with SQL where
245 * @param array $data filter settings
246 * @return string the filtering condition or null if the filter is disabled
248 function get_sql_filter($data) {
249 print_error('mustbeoveride', 'debug', '', 'get_sql_filter');
253 * Retrieves data from the form data
254 * @param object $formdata data submited with the form
255 * @return mixed array filter data or false when filter not set
257 function check_data($formdata) {
258 print_error('mustbeoveride', 'debug', '', 'check_data');
262 * Adds controls specific to this filter in the form.
263 * @param object $mform a MoodleForm object to setup
265 function setupForm(&$mform) {
266 print_error('mustbeoveride', 'debug', '', 'setupForm');
270 * Returns a human friendly description of the filter used as label.
271 * @param array $data filter settings
272 * @return string active filter label
274 function get_label($data) {
275 print_error('mustbeoveride', 'debug', '', 'get_label');