2 // This file is part of Moodle - http://moodle.org/
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
18 * This file contains the User Filter API.
22 * @copyright 1999 Martin Dougiamas http://dougiamas.com
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 require_once($CFG->dirroot
.'/user/filters/text.php');
27 require_once($CFG->dirroot
.'/user/filters/date.php');
28 require_once($CFG->dirroot
.'/user/filters/select.php');
29 require_once($CFG->dirroot
.'/user/filters/simpleselect.php');
30 require_once($CFG->dirroot
.'/user/filters/courserole.php');
31 require_once($CFG->dirroot
.'/user/filters/globalrole.php');
32 require_once($CFG->dirroot
.'/user/filters/profilefield.php');
33 require_once($CFG->dirroot
.'/user/filters/yesno.php');
34 require_once($CFG->dirroot
.'/user/filters/anycourses.php');
35 require_once($CFG->dirroot
.'/user/filters/cohort.php');
36 require_once($CFG->dirroot
.'/user/filters/user_filter_forms.php');
37 require_once($CFG->dirroot
.'/user/filters/checkbox.php');
40 * User filtering wrapper class.
42 * @copyright 1999 Martin Dougiamas http://dougiamas.com
43 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
45 class user_filtering
{
48 /** @var \user_add_filter_form */
50 /** @var \user_active_filter_form */
55 * @param array $fieldnames array of visible user fields
56 * @param string $baseurl base url used for submission/return, null if the same of current page
57 * @param array $extraparams extra page parameters
59 public function __construct($fieldnames = null, $baseurl = null, $extraparams = null) {
62 if (!isset($SESSION->user_filtering
)) {
63 $SESSION->user_filtering
= array();
66 if (empty($fieldnames)) {
67 // As a start, add all fields as advanced fields (which are only available after clicking on "Show more").
68 $fieldnames = array('realname' => 1, 'lastname' => 1, 'firstname' => 1, 'username' => 1, 'email' => 1, 'city' => 1,
69 'country' => 1, 'confirmed' => 1, 'suspended' => 1, 'profile' => 1, 'courserole' => 1,
70 'anycourses' => 1, 'systemrole' => 1, 'cohort' => 1, 'firstaccess' => 1, 'lastaccess' => 1,
71 'neveraccessed' => 1, 'timemodified' => 1, 'nevermodified' => 1, 'auth' => 1, 'mnethostid' => 1,
72 'idnumber' => 1, 'institution' => 1, 'department' => 1, 'lastip' => 1);
74 // Get the config which filters the admin wanted to show by default.
75 $userfiltersdefault = get_config('core', 'userfiltersdefault');
77 // If the admin did not enable any filter, the form will not make much sense if all fields are hidden behind
78 // "Show more". Thus, we enable the 'realname' filter automatically.
79 if ($userfiltersdefault == '') {
80 $userfiltersdefault = array('realname');
82 // Otherwise, we split the enabled filters into an array.
84 $userfiltersdefault = explode(',', $userfiltersdefault);
87 // Show these fields by default which the admin has enabled in the config.
88 foreach ($userfiltersdefault as $key) {
89 $fieldnames[$key] = 0;
93 $this->_fields
= array();
95 foreach ($fieldnames as $fieldname => $advanced) {
96 if ($field = $this->get_field($fieldname, $advanced)) {
97 $this->_fields
[$fieldname] = $field;
101 // Fist the new filter form.
102 $this->_addform
= new user_add_filter_form($baseurl, array('fields' => $this->_fields
, 'extraparams' => $extraparams));
103 if ($adddata = $this->_addform
->get_data()) {
104 foreach ($this->_fields
as $fname => $field) {
105 $data = $field->check_data($adddata);
106 if ($data === false) {
107 continue; // Nothing new.
109 if (!array_key_exists($fname, $SESSION->user_filtering
)) {
110 $SESSION->user_filtering
[$fname] = array();
112 $SESSION->user_filtering
[$fname][] = $data;
116 $this->_addform
= new user_add_filter_form($baseurl, array('fields' => $this->_fields
, 'extraparams' => $extraparams));
119 // Now the active filters.
120 $this->_activeform
= new user_active_filter_form($baseurl, array('fields' => $this->_fields
, 'extraparams' => $extraparams));
121 if ($adddata = $this->_activeform
->get_data()) {
122 if (!empty($adddata->removeall
)) {
123 $SESSION->user_filtering
= array();
125 } else if (!empty($adddata->removeselected
) and !empty($adddata->filter
)) {
126 foreach ($adddata->filter
as $fname => $instances) {
127 foreach ($instances as $i => $val) {
131 unset($SESSION->user_filtering
[$fname][$i]);
133 if (empty($SESSION->user_filtering
[$fname])) {
134 unset($SESSION->user_filtering
[$fname]);
138 // Clear+reload the form.
140 $this->_activeform
= new user_active_filter_form($baseurl, array('fields' => $this->_fields
, 'extraparams' => $extraparams));
142 // Now the active filters.
146 * Creates known user filter if present
147 * @param string $fieldname
148 * @param boolean $advanced
149 * @return object filter
151 public function get_field($fieldname, $advanced) {
152 global $USER, $CFG, $DB, $SITE;
154 switch ($fieldname) {
155 case 'username': return new user_filter_text('username', get_string('username'), $advanced, 'username');
156 case 'realname': return new user_filter_text('realname', get_string('fullnameuser'), $advanced, $DB->sql_fullname());
157 case 'lastname': return new user_filter_text('lastname', get_string('lastname'), $advanced, 'lastname');
158 case 'firstname': return new user_filter_text('firstname', get_string('firstname'), $advanced, 'firstname');
159 case 'email': return new user_filter_text('email', get_string('email'), $advanced, 'email');
160 case 'city': return new user_filter_text('city', get_string('city'), $advanced, 'city');
161 case 'country': return new user_filter_select('country', get_string('country'), $advanced, 'country', get_string_manager()->get_list_of_countries(), $USER->country
);
162 case 'confirmed': return new user_filter_yesno('confirmed', get_string('confirmed', 'admin'), $advanced, 'confirmed');
163 case 'suspended': return new user_filter_yesno('suspended', get_string('suspended', 'auth'), $advanced, 'suspended');
164 case 'profile': return new user_filter_profilefield('profile', get_string('profilefields', 'admin'), $advanced);
165 case 'courserole': return new user_filter_courserole('courserole', get_string('courserole', 'filters'), $advanced);
167 return new user_filter_anycourses('anycourses', get_string('anycourses', 'filters'), $advanced, 'user_enrolments');
168 case 'systemrole': return new user_filter_globalrole('systemrole', get_string('globalrole', 'role'), $advanced);
169 case 'firstaccess': return new user_filter_date('firstaccess', get_string('firstaccess', 'filters'), $advanced, 'firstaccess');
170 case 'lastaccess': return new user_filter_date('lastaccess', get_string('lastaccess'), $advanced, 'lastaccess');
171 case 'neveraccessed': return new user_filter_checkbox('neveraccessed', get_string('neveraccessed', 'filters'), $advanced, 'firstaccess', array('lastaccess_sck', 'lastaccess_eck', 'firstaccess_eck', 'firstaccess_sck'));
172 case 'timemodified': return new user_filter_date('timemodified', get_string('lastmodified'), $advanced, 'timemodified');
173 case 'nevermodified': return new user_filter_checkbox('nevermodified', get_string('nevermodified', 'filters'), $advanced, array('timemodified', 'timecreated'), array('timemodified_sck', 'timemodified_eck'));
174 case 'cohort': return new user_filter_cohort($advanced);
175 case 'idnumber': return new user_filter_text('idnumber', get_string('idnumber'), $advanced, 'idnumber');
176 case 'institution': return new user_filter_text('institution', get_string('institution'), $advanced, 'institution');
177 case 'department': return new user_filter_text('department', get_string('department'), $advanced, 'department');
178 case 'lastip': return new user_filter_text('lastip', get_string('lastip'), $advanced, 'lastip');
180 $plugins = core_component
::get_plugin_list('auth');
182 foreach ($plugins as $auth => $unused) {
183 $choices[$auth] = get_string('pluginname', "auth_{$auth}");
185 return new user_filter_simpleselect('auth', get_string('authentication'), $advanced, 'auth', $choices);
188 // Include all hosts even those deleted or otherwise problematic.
189 if (!$hosts = $DB->get_records('mnet_host', null, 'id', 'id, wwwroot, name')) {
193 foreach ($hosts as $host) {
194 if ($host->id
== $CFG->mnet_localhost_id
) {
195 $choices[$host->id
] = format_string($SITE->fullname
).' ('.get_string('local').')';
196 } else if (empty($host->wwwroot
)) {
200 $choices[$host->id
] = $host->name
.' ('.$host->wwwroot
.')';
203 if ($usedhosts = $DB->get_fieldset_sql("SELECT DISTINCT mnethostid FROM {user} WHERE deleted=0")) {
204 foreach ($usedhosts as $hostid) {
205 if (empty($hosts[$hostid])) {
206 $choices[$hostid] = 'id: '.$hostid.' ('.get_string('error').')';
210 if (count($choices) < 2) {
211 return null; // Filter not needed.
213 return new user_filter_simpleselect('mnethostid', get_string('mnetidprovider', 'mnet'), $advanced, 'mnethostid', $choices);
221 * Returns sql where statement based on active user filters
222 * @param string $extra sql
223 * @param array $params named params (recommended prefix ex)
224 * @return array sql string and $params
226 public function get_sql_filter($extra='', array $params=null) {
233 $params = (array)$params;
235 if (!empty($SESSION->user_filtering
)) {
236 foreach ($SESSION->user_filtering
as $fname => $datas) {
237 if (!array_key_exists($fname, $this->_fields
)) {
238 continue; // Filter not used.
240 $field = $this->_fields
[$fname];
241 foreach ($datas as $i => $data) {
242 list($s, $p) = $field->get_sql_filter($data);
244 $params = $params +
$p;
250 return array('', array());
252 $sqls = implode(' AND ', $sqls);
253 return array($sqls, $params);
258 * Print the add filter form.
260 public function display_add() {
261 $this->_addform
->display();
265 * Print the active filter form.
267 public function display_active() {
268 $this->_activeform
->display();
274 * The base user filter class. All abstract classes must be implemented.
276 * @copyright 1999 Martin Dougiamas http://dougiamas.com
277 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
279 class user_filter_type
{
281 * The name of this filter instance.
287 * The label of this filter instance.
293 * Advanced form element flag
300 * @param string $name the name of the filter instance
301 * @param string $label the label of the filter instance
302 * @param boolean $advanced advanced form element flag
304 public function __construct($name, $label, $advanced) {
305 $this->_name
= $name;
306 $this->_label
= $label;
307 $this->_advanced
= $advanced;
311 * Old syntax of class constructor. Deprecated in PHP7.
313 * @deprecated since Moodle 3.1
315 public function user_filter_type($name, $label, $advanced) {
316 debugging('Use of class name as constructor is deprecated', DEBUG_DEVELOPER
);
317 self
::__construct($name, $label, $advanced);
321 * Returns the condition to be used with SQL where
322 * @param array $data filter settings
323 * @return string the filtering condition or null if the filter is disabled
325 public function get_sql_filter($data) {
326 print_error('mustbeoveride', 'debug', '', 'get_sql_filter');
330 * Retrieves data from the form data
331 * @param stdClass $formdata data submited with the form
332 * @return mixed array filter data or false when filter not set
334 public function check_data($formdata) {
335 print_error('mustbeoveride', 'debug', '', 'check_data');
339 * Adds controls specific to this filter in the form.
340 * @param moodleform $mform a MoodleForm object to setup
342 public function setupForm(&$mform) {
343 print_error('mustbeoveride', 'debug', '', 'setupForm');
347 * Returns a human friendly description of the filter used as label.
348 * @param array $data filter settings
349 * @return string active filter label
351 public function get_label($data) {
352 print_error('mustbeoveride', 'debug', '', 'get_label');