Merge branch 'MDL-61894-master' of https://github.com/lucaboesch/moodle
[moodle.git] / user / filters / globalrole.php
blobe205bbaa9fe0203e7e79e230d1e86567b6bd6f11
1 <?php
2 // This file is part of Moodle - http://moodle.org/
3 //
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.
8 //
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/>.
17 /**
18 * Global role filter
20 * @package core_user
21 * @category user
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/lib.php');
28 /**
29 * User filter based on global roles.
30 * @copyright 1999 Martin Dougiamas http://dougiamas.com
31 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
33 class user_filter_globalrole extends user_filter_type {
35 /**
36 * Constructor
37 * @param string $name the name of the filter instance
38 * @param string $label the label of the filter instance
39 * @param boolean $advanced advanced form element flag
41 public function __construct($name, $label, $advanced) {
42 parent::__construct($name, $label, $advanced);
45 /**
46 * Old syntax of class constructor. Deprecated in PHP7.
48 * @deprecated since Moodle 3.1
50 public function user_filter_globalrole($name, $label, $advanced) {
51 debugging('Use of class name as constructor is deprecated', DEBUG_DEVELOPER);
52 self::__construct($name, $label, $advanced);
55 /**
56 * Returns an array of available roles
57 * @return array of availble roles
59 public function get_roles() {
60 $context = context_system::instance();
61 $roles = array(0 => get_string('anyrole', 'filters')) + get_assignable_roles($context);
62 return $roles;
65 /**
66 * Adds controls specific to this filter in the form.
67 * @param object $mform a MoodleForm object to setup
69 public function setupForm(&$mform) {
70 $obj =& $mform->addElement('select', $this->_name, $this->_label, $this->get_roles());
71 $mform->setDefault($this->_name, 0);
72 if ($this->_advanced) {
73 $mform->setAdvanced($this->_name);
77 /**
78 * Retrieves data from the form data
79 * @param object $formdata data submited with the form
80 * @return mixed array filter data or false when filter not set
82 public function check_data($formdata) {
83 $field = $this->_name;
85 if (array_key_exists($field, $formdata) and !empty($formdata->$field)) {
86 return array('value' => (int)$formdata->$field);
88 return false;
91 /**
92 * Returns the condition to be used with SQL where
93 * @param array $data filter settings
94 * @return array sql string and $params
96 public function get_sql_filter($data) {
97 global $CFG;
98 $value = (int)$data['value'];
100 $timenow = round(time(), 100);
102 $sql = "id IN (SELECT userid
103 FROM {role_assignments} a
104 WHERE a.contextid=".SYSCONTEXTID." AND a.roleid=$value)";
105 return array($sql, array());
109 * Returns a human friendly description of the filter used as label.
110 * @param array $data filter settings
111 * @return string active filter label
113 public function get_label($data) {
114 global $DB;
116 $role = $DB->get_record('role', array('id' => $data['value']));
118 $a = new stdClass();
119 $a->label = $this->_label;
120 $a->value = '"'.role_get_name($role).'"';
122 return get_string('globalrolelabel', 'filters', $a);