|
Moodle
2.2.1
http://www.collinsharper.com
|
00001 <?php 00002 00003 require_once($CFG->dirroot.'/user/filters/text.php'); 00004 require_once($CFG->dirroot.'/user/filters/date.php'); 00005 require_once($CFG->dirroot.'/user/filters/select.php'); 00006 require_once($CFG->dirroot.'/user/filters/simpleselect.php'); 00007 require_once($CFG->dirroot.'/user/filters/courserole.php'); 00008 require_once($CFG->dirroot.'/user/filters/globalrole.php'); 00009 require_once($CFG->dirroot.'/user/filters/profilefield.php'); 00010 require_once($CFG->dirroot.'/user/filters/yesno.php'); 00011 require_once($CFG->dirroot.'/user/filters/cohort.php'); 00012 require_once($CFG->dirroot.'/user/filters/user_filter_forms.php'); 00013 require_once($CFG->dirroot.'/user/filters/checkbox.php'); 00014 00018 class user_filtering { 00019 var $_fields; 00020 var $_addform; 00021 var $_activeform; 00022 00029 function user_filtering($fieldnames=null, $baseurl=null, $extraparams=null) { 00030 global $SESSION; 00031 00032 if (!isset($SESSION->user_filtering)) { 00033 $SESSION->user_filtering = array(); 00034 } 00035 00036 if (empty($fieldnames)) { 00037 $fieldnames = array('realname'=>0, 'lastname'=>1, 'firstname'=>1, 'email'=>1, 'city'=>1, 'country'=>1, 00038 'confirmed'=>1, 'suspended'=>1, 'profile'=>1, 'courserole'=>1, 'systemrole'=>1, 'cohort'=>1, 00039 'firstaccess'=>1, 'lastaccess'=>1, 'neveraccessed'=>1, 'timemodified'=>1, 00040 'nevermodified'=>1, 'username'=>1, 'auth'=>1, 'mnethostid'=>1); 00041 } 00042 00043 $this->_fields = array(); 00044 00045 foreach ($fieldnames as $fieldname=>$advanced) { 00046 if ($field = $this->get_field($fieldname, $advanced)) { 00047 $this->_fields[$fieldname] = $field; 00048 } 00049 } 00050 00051 // fist the new filter form 00052 $this->_addform = new user_add_filter_form($baseurl, array('fields'=>$this->_fields, 'extraparams'=>$extraparams)); 00053 if ($adddata = $this->_addform->get_data()) { 00054 foreach($this->_fields as $fname=>$field) { 00055 $data = $field->check_data($adddata); 00056 if ($data === false) { 00057 continue; // nothing new 00058 } 00059 if (!array_key_exists($fname, $SESSION->user_filtering)) { 00060 $SESSION->user_filtering[$fname] = array(); 00061 } 00062 $SESSION->user_filtering[$fname][] = $data; 00063 } 00064 // clear the form 00065 $_POST = array(); 00066 $this->_addform = new user_add_filter_form($baseurl, array('fields'=>$this->_fields, 'extraparams'=>$extraparams)); 00067 } 00068 00069 // now the active filters 00070 $this->_activeform = new user_active_filter_form($baseurl, array('fields'=>$this->_fields, 'extraparams'=>$extraparams)); 00071 if ($adddata = $this->_activeform->get_data()) { 00072 if (!empty($adddata->removeall)) { 00073 $SESSION->user_filtering = array(); 00074 00075 } else if (!empty($adddata->removeselected) and !empty($adddata->filter)) { 00076 foreach($adddata->filter as $fname=>$instances) { 00077 foreach ($instances as $i=>$val) { 00078 if (empty($val)) { 00079 continue; 00080 } 00081 unset($SESSION->user_filtering[$fname][$i]); 00082 } 00083 if (empty($SESSION->user_filtering[$fname])) { 00084 unset($SESSION->user_filtering[$fname]); 00085 } 00086 } 00087 } 00088 // clear+reload the form 00089 $_POST = array(); 00090 $this->_activeform = new user_active_filter_form($baseurl, array('fields'=>$this->_fields, 'extraparams'=>$extraparams)); 00091 } 00092 // now the active filters 00093 } 00094 00101 function get_field($fieldname, $advanced) { 00102 global $USER, $CFG, $DB, $SITE; 00103 00104 switch ($fieldname) { 00105 case 'username': return new user_filter_text('username', get_string('username'), $advanced, 'username'); 00106 case 'realname': return new user_filter_text('realname', get_string('fullnameuser'), $advanced, $DB->sql_fullname()); 00107 case 'lastname': return new user_filter_text('lastname', get_string('lastname'), $advanced, 'lastname'); 00108 case 'firstname': return new user_filter_text('firstname', get_string('firstname'), $advanced, 'firstname'); 00109 case 'email': return new user_filter_text('email', get_string('email'), $advanced, 'email'); 00110 case 'city': return new user_filter_text('city', get_string('city'), $advanced, 'city'); 00111 case 'country': return new user_filter_select('country', get_string('country'), $advanced, 'country', get_string_manager()->get_list_of_countries(), $USER->country); 00112 case 'confirmed': return new user_filter_yesno('confirmed', get_string('confirmed', 'admin'), $advanced, 'confirmed'); 00113 case 'suspended': return new user_filter_yesno('suspended', get_string('suspended', 'auth'), $advanced, 'suspended'); 00114 case 'profile': return new user_filter_profilefield('profile', get_string('profile'), $advanced); 00115 case 'courserole': return new user_filter_courserole('courserole', get_string('courserole', 'filters'), $advanced); 00116 case 'systemrole': return new user_filter_globalrole('systemrole', get_string('globalrole', 'role'), $advanced); 00117 case 'firstaccess': return new user_filter_date('firstaccess', get_string('firstaccess', 'filters'), $advanced, 'firstaccess'); 00118 case 'lastaccess': return new user_filter_date('lastaccess', get_string('lastaccess'), $advanced, 'lastaccess'); 00119 case 'neveraccessed': return new user_filter_checkbox('neveraccessed', get_string('neveraccessed', 'filters'), $advanced, 'firstaccess', array('lastaccess_sck', 'lastaccess_eck', 'firstaccess_eck', 'firstaccess_sck')); 00120 case 'timemodified': return new user_filter_date('timemodified', get_string('lastmodified'), $advanced, 'timemodified'); 00121 case 'nevermodified': return new user_filter_checkbox('nevermodified', get_string('nevermodified', 'filters'), $advanced, array('timemodified', 'timecreated'), array('timemodified_sck', 'timemodified_eck')); 00122 case 'cohort': return new user_filter_cohort($advanced); 00123 case 'auth': 00124 $plugins = get_plugin_list('auth'); 00125 $choices = array(); 00126 foreach ($plugins as $auth => $unused) { 00127 $choices[$auth] = get_string('pluginname', "auth_{$auth}"); 00128 } 00129 return new user_filter_simpleselect('auth', get_string('authentication'), $advanced, 'auth', $choices); 00130 00131 case 'mnethostid': 00132 // include all hosts even those deleted or otherwise problematic 00133 if (!$hosts = $DB->get_records('mnet_host', null, 'id', 'id, wwwroot, name')) { 00134 $hosts = array(); 00135 } 00136 $choices = array(); 00137 foreach ($hosts as $host) { 00138 if ($host->id == $CFG->mnet_localhost_id) { 00139 $choices[$host->id] = format_string($SITE->fullname).' ('.get_string('local').')'; 00140 } else if (empty($host->wwwroot)) { 00141 // All hosts 00142 continue; 00143 } else { 00144 $choices[$host->id] = $host->name.' ('.$host->wwwroot.')'; 00145 } 00146 } 00147 if ($usedhosts = $DB->get_fieldset_sql("SELECT DISTINCT mnethostid FROM {user} WHERE deleted=0")) { 00148 foreach ($usedhosts as $hostid) { 00149 if (empty($hosts[$hostid])) { 00150 $choices[$hostid] = 'id: '.$hostid.' ('.get_string('error').')'; 00151 } 00152 } 00153 } 00154 if (count($choices) < 2) { 00155 return null; // filter not needed 00156 } 00157 return new user_filter_simpleselect('mnethostid', get_string('mnetidprovider', 'mnet'), $advanced, 'mnethostid', $choices); 00158 00159 default: return null; 00160 } 00161 } 00162 00169 function get_sql_filter($extra='', array $params=null) { 00170 global $SESSION; 00171 00172 $sqls = array(); 00173 if ($extra != '') { 00174 $sqls[] = $extra; 00175 } 00176 $params = (array)$params; 00177 00178 if (!empty($SESSION->user_filtering)) { 00179 foreach ($SESSION->user_filtering as $fname=>$datas) { 00180 if (!array_key_exists($fname, $this->_fields)) { 00181 continue; // filter not used 00182 } 00183 $field = $this->_fields[$fname]; 00184 foreach($datas as $i=>$data) { 00185 list($s, $p) = $field->get_sql_filter($data); 00186 $sqls[] = $s; 00187 $params = $params + $p; 00188 } 00189 } 00190 } 00191 00192 if (empty($sqls)) { 00193 return array('', array()); 00194 } else { 00195 $sqls = implode(' AND ', $sqls); 00196 return array($sqls, $params); 00197 } 00198 } 00199 00203 function display_add() { 00204 $this->_addform->display(); 00205 } 00206 00210 function display_active() { 00211 $this->_activeform->display(); 00212 } 00213 00214 } 00215 00219 class user_filter_type { 00223 var $_name; 00224 00228 var $_label; 00229 00233 var $_advanced; 00234 00241 function user_filter_type($name, $label, $advanced) { 00242 $this->_name = $name; 00243 $this->_label = $label; 00244 $this->_advanced = $advanced; 00245 } 00246 00252 function get_sql_filter($data) { 00253 print_error('mustbeoveride', 'debug', '', 'get_sql_filter'); 00254 } 00255 00261 function check_data($formdata) { 00262 print_error('mustbeoveride', 'debug', '', 'check_data'); 00263 } 00264 00269 function setupForm(&$mform) { 00270 print_error('mustbeoveride', 'debug', '', 'setupForm'); 00271 } 00272 00278 function get_label($data) { 00279 print_error('mustbeoveride', 'debug', '', 'get_label'); 00280 } 00281 }