adding current groupid to grade_export class - soon to be used in plugins
[moodle-pu.git] / user / filters / globalrole.php
blob20222113ee53fd32a008c069a1a0d20e8dee67ae
1 <?php //$Id$
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 {
9 /**
10 * Constructor
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);
20 /**
21 * Returns an array of available roles
22 * @return array of availble roles
24 function getRoles() {
25 $context =& get_context_instance(CONTEXT_SYSTEM);
26 $roles =& array_merge(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->getRoles());
36 $obj->setHelpButton(array('globalrole','','filters'));
37 $mform->setDefault($this->_name, $this->_value);
40 /**
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;
49 /**
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() {
54 global $CFG;
55 if(empty($this->_value)) {
56 return null;
58 $timenow = time();
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 ".
62 $where . ')';
65 /**
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];