3 require_once($CFG->dirroot
.'/user/filters/lib.php');
6 * User filter based on global roles.
8 class user_filter_globalrole
extends user_filter_type
{
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);
21 * Returns an array of available roles
22 * @return array of availble roles
24 function get_roles() {
25 $context = get_context_instance(CONTEXT_SYSTEM
);
26 $roles = array(0=> get_string('anyrole','filters')) +
get_assignable_roles($context);
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 $obj->setHelpButton(array('globalrole', $this->_label
, 'filters'));
37 $mform->setDefault($this->_name
, 0);
38 if ($this->_advanced
) {
39 $mform->setAdvanced($this->_name
);
44 * Retrieves data from the form data
45 * @param object $formdata data submited with the form
46 * @return mixed array filter data or false when filter not set
48 function check_data($formdata) {
49 $field = $this->_name
;
51 if (array_key_exists($field, $formdata) and !empty($formdata->$field)) {
52 return array('value' => (int)$formdata->$field);
58 * Returns the condition to be used with SQL where
59 * @param array $data filter settings
60 * @return string the filtering condition or null if the filter is disabled
62 function get_sql_filter($data) {
64 $value = $data['value'];
66 $timenow = round(time(), 100);
68 return "id IN (SELECT userid
69 FROM {$CFG->prefix}role_assignments a
70 WHERE a.contextid=".SYSCONTEXTID
." AND a.roleid=$value AND a.timestart<$timenow
71 AND (a.timeend=0 OR a.timeend>$timenow))";
75 * Returns a human friendly description of the filter used as label.
76 * @param array $data filter settings
77 * @return string active filter label
79 function get_label($data) {
80 $rolename = get_field('role', 'name', 'id', $data['value']);
83 $a->label
= $this->_label
;
84 $a->value
= '"'.format_string($rolename).'"';
86 return get_string('globalrolelabel', 'filters', $a);