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 roles in a course identified by its shortname.
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_courserole
extends user_filter_type
{
36 * @param string $name the name of the filter instance
37 * @param string $label the label of the filter instance
38 * @param boolean $advanced advanced form element flag
40 public function __construct($name, $label, $advanced) {
41 parent
::__construct($name, $label, $advanced);
45 * Old syntax of class constructor. Deprecated in PHP7.
47 * @deprecated since Moodle 3.1
49 public function user_filter_courserole($name, $label, $advanced) {
50 debugging('Use of class name as constructor is deprecated', DEBUG_DEVELOPER
);
51 self
::__construct($name, $label, $advanced);
55 * Returns an array of available roles
56 * @return array of availble roles
58 public function get_roles() {
59 $context = context_system
::instance();
60 $roles = array(0 => get_string('anyrole', 'filters')) +
get_default_enrol_roles($context);
65 * Returns an array of course categories
66 * @return array of course categories
68 public function get_course_categories() {
69 return array(0 => get_string('anycategory', 'filters')) + core_course_category
::make_categories_list();
73 * Adds controls specific to this filter in the form.
74 * @param moodleform $mform a MoodleForm object to setup
76 public function setupForm(&$mform) {
78 $objs['role'] = $mform->createElement('select', $this->_name
.'_rl', null, $this->get_roles());
79 $objs['role']->setLabel(get_string('courserole', 'filters'));
80 $objs['category'] = $mform->createElement('select', $this->_name
.'_ct', null, $this->get_course_categories());
81 $objs['category']->setLabel(get_string('coursecategory', 'filters'));
82 $objs['value'] = $mform->createElement('text', $this->_name
, null);
83 $objs['value']->setLabel(get_string('coursevalue', 'filters'));
84 $grp =& $mform->addElement('group', $this->_name
.'_grp', $this->_label
, $objs, '', false);
85 $mform->setType($this->_name
, PARAM_TEXT
);
86 if ($this->_advanced
) {
87 $mform->setAdvanced($this->_name
.'_grp');
92 * Retrieves data from the form data
93 * @param stdClass $formdata data submited with the form
94 * @return mixed array filter data or false when filter not set
96 public function check_data($formdata) {
97 $field = $this->_name
;
98 $role = $field .'_rl';
99 $category = $field .'_ct';
101 if (array_key_exists($field, $formdata)) {
102 if (empty($formdata->$field) and empty($formdata->$role) and empty($formdata->$category)) {
106 return array('value' => (string)$formdata->$field,
107 'roleid' => (int)$formdata->$role,
108 'categoryid' => (int)$formdata->$category);
114 * Returns the condition to be used with SQL where
115 * @param array $data filter settings
116 * @return array sql string and $params
118 public function get_sql_filter($data) {
121 $pref = 'ex_courserole'.($counter++
).'_';
123 $value = $data['value'];
124 $roleid = $data['roleid'];
125 $categoryid = $data['categoryid'];
129 if (empty($value) and empty($roleid) and empty($categoryid)) {
130 return array('', $params);
133 $where = "b.contextlevel=50";
135 $where .= " AND a.roleid = :{$pref}roleid";
136 $params[$pref.'roleid'] = $roleid;
139 $where .= " AND c.category = :{$pref}categoryid";
140 $params[$pref.'categoryid'] = $categoryid;
143 $where .= " AND c.shortname = :{$pref}course";
144 $params[$pref.'course'] = $value;
146 return array("id IN (SELECT userid
147 FROM {role_assignments} a
148 INNER JOIN {context} b ON a.contextid=b.id
149 INNER JOIN {course} c ON b.instanceid=c.id
150 WHERE $where)", $params);
154 * Returns a human friendly description of the filter used as label.
155 * @param array $data filter settings
156 * @return string active filter label
158 public function get_label($data) {
161 $value = $data['value'];
162 $roleid = $data['roleid'];
163 $categoryid = $data['categoryid'];
166 $a->label
= $this->_label
;
169 $role = $DB->get_record('role', array('id' => $roleid));
170 $a->rolename
= '"'.role_get_name($role).'"';
172 $a->rolename
= get_string('anyrole', 'filters');
176 $catname = $DB->get_field('course_categories', 'name', array('id' => $categoryid));
177 $a->categoryname
= '"'.format_string($catname).'"';
179 $a->categoryname
= get_string('anycategory', 'filters');
183 $a->coursename
= '"'.s($value).'"';
184 if (!$DB->record_exists('course', array('shortname' => $value))) {
185 return '<span class="notifyproblem">'.get_string('courserolelabelerror', 'filters', $a).'</span>';
188 $a->coursename
= get_string('anycourse', 'filters');
191 return get_string('courserolelabel', 'filters', $a);