2 // This file is part of Moodle - http://moodle.org/
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.
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/>.
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');
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
{
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);
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);
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);
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
);
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);
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) {
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) {
116 $role = $DB->get_record('role', array('id' => $data['value']));
119 $a->label
= $this->_label
;
120 $a->value
= '"'.role_get_name($role).'"';
122 return get_string('globalrolelabel', 'filters', $a);