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
{
11 * @param string $name the name of the filter instance
12 * @param string $label the label of the filter instance
13 * @param string $field the field used for filtering data
14 * @param int $value id of the role (used for filtering data); 0 = any role
16 function user_filter_globalrole($name, $label, $field='id', $value=0) {
17 parent
::user_filter_type($name, $label, $field, $value);
21 * Returns an array of available roles
22 * @return array of availble roles
25 $context =& get_context_instance(CONTEXT_SYSTEM
);
26 $roles =& array_merge(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->getRoles());
36 $obj->setHelpButton(array('globalrole','','filters'));
37 $mform->setDefault($this->_name
, $this->_value
);
41 * Retrieves data from the form data
42 * @param object $formdata data submited with the form
44 function checkData($formdata) {
45 $field = $this->_name
;
46 $this->_value
= (string)@$formdata->$field;
50 * Returns the condition to be used with SQL where
51 * @return string the filtering condition or null if the filter is disabled
53 function getSQLFilter() {
55 if(empty($this->_value
)) {
59 $where = 'WHERE contextlevel=10 AND roleid='. $this->_value
. ' AND timestart<' . $timenow .' AND (timeend=0 OR timeend>'. $timenow . ')';
60 return $this->_field
. " IN (SELECT userid FROM {$CFG->prefix}role_assignments a ".
61 "INNER JOIN {$CFG->prefix}context b ON a.contextid=b.id ".
66 * Returns a human friendly description of the filter.
67 * @return string filter description
69 function getDescription() {
70 $roles =& $this->getRoles();
71 return $this->_label
. ' is ' . $roles[$this->_value
];