MDL-38773 Purge coursecat cache on course restore
[moodle.git] / user / filters / globalrole.php
blobed3345393dcda4431a498972434cc2ff6b949d50
1 <?php
3 require_once($CFG->dirroot.'/user/filters/lib.php');
5 /**
6 * User filter based on global roles.
7 */
8 class user_filter_globalrole extends user_filter_type {
10 /**
11 * Constructor
12 * @param string $name the name of the filter instance
13 * @param string $label the label of the filter instance
14 * @param boolean $advanced advanced form element flag
16 function user_filter_globalrole($name, $label, $advanced) {
17 parent::user_filter_type($name, $label, $advanced);
20 /**
21 * Returns an array of available roles
22 * @return array of availble roles
24 function get_roles() {
25 $context = context_system::instance();
26 $roles = array(0=> get_string('anyrole','filters')) + get_assignable_roles($context);
27 return $roles;
30 /**
31 * Adds controls specific to this filter in the form.
32 * @param object $mform a MoodleForm object to setup
34 function setupForm(&$mform) {
35 $obj =& $mform->addElement('select', $this->_name, $this->_label, $this->get_roles());
36 $mform->setDefault($this->_name, 0);
37 if ($this->_advanced) {
38 $mform->setAdvanced($this->_name);
42 /**
43 * Retrieves data from the form data
44 * @param object $formdata data submited with the form
45 * @return mixed array filter data or false when filter not set
47 function check_data($formdata) {
48 $field = $this->_name;
50 if (array_key_exists($field, $formdata) and !empty($formdata->$field)) {
51 return array('value' => (int)$formdata->$field);
53 return false;
56 /**
57 * Returns the condition to be used with SQL where
58 * @param array $data filter settings
59 * @return array sql string and $params
61 function get_sql_filter($data) {
62 global $CFG;
63 $value = (int)$data['value'];
65 $timenow = round(time(), 100);
67 $sql = "id IN (SELECT userid
68 FROM {role_assignments} a
69 WHERE a.contextid=".SYSCONTEXTID." AND a.roleid=$value)";
70 return array($sql, array());
73 /**
74 * Returns a human friendly description of the filter used as label.
75 * @param array $data filter settings
76 * @return string active filter label
78 function get_label($data) {
79 global $DB;
81 $role = $DB->get_record('role', array('id'=>$data['value']));
84 $a = new stdClass();
85 $a->label = $this->_label;
86 $a->value = '"'.role_get_name($role).'"';
88 return get_string('globalrolelabel', 'filters', $a);