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 user_filter_courserole($name, $label, $advanced) {
41 parent
::user_filter_type($name, $label, $advanced);
45 * Returns an array of available roles
46 * @return array of availble roles
48 public function get_roles() {
49 $context = context_system
::instance();
50 $roles = array(0 => get_string('anyrole', 'filters')) +
get_default_enrol_roles($context);
55 * Returns an array of course categories
56 * @return array of course categories
58 public function get_course_categories() {
60 require_once($CFG->libdir
.'/coursecatlib.php');
61 return array(0 => get_string('anycategory', 'filters')) + coursecat
::make_categories_list();
65 * Adds controls specific to this filter in the form.
66 * @param moodleform $mform a MoodleForm object to setup
68 public function setupForm(&$mform) {
70 $objs[] = $mform->createElement('select', $this->_name
.'_rl', null, $this->get_roles());
71 $objs[] = $mform->createElement('select', $this->_name
.'_ct', null, $this->get_course_categories());
72 $objs[] = $mform->createElement('text', $this->_name
, null);
73 $grp =& $mform->addElement('group', $this->_name
.'_grp', $this->_label
, $objs, '', false);
74 $mform->setType($this->_name
, PARAM_TEXT
);
75 if ($this->_advanced
) {
76 $mform->setAdvanced($this->_name
.'_grp');
81 * Retrieves data from the form data
82 * @param stdClass $formdata data submited with the form
83 * @return mixed array filter data or false when filter not set
85 public function check_data($formdata) {
86 $field = $this->_name
;
87 $role = $field .'_rl';
88 $category = $field .'_ct';
90 if (array_key_exists($field, $formdata)) {
91 if (empty($formdata->$field) and empty($formdata->$role) and empty($formdata->$category)) {
95 return array('value' => (string)$formdata->$field,
96 'roleid' => (int)$formdata->$role,
97 'categoryid' => (int)$formdata->$category);
103 * Returns the condition to be used with SQL where
104 * @param array $data filter settings
105 * @return array sql string and $params
107 public function get_sql_filter($data) {
110 $pref = 'ex_courserole'.($counter++
).'_';
112 $value = $data['value'];
113 $roleid = $data['roleid'];
114 $categoryid = $data['categoryid'];
118 if (empty($value) and empty($roleid) and empty($categoryid)) {
119 return array('', $params);
122 $where = "b.contextlevel=50";
124 $where .= " AND a.roleid = :{$pref}roleid";
125 $params[$pref.'roleid'] = $roleid;
128 $where .= " AND c.category = :{$pref}categoryid";
129 $params[$pref.'categoryid'] = $categoryid;
132 $where .= " AND c.shortname = :{$pref}course";
133 $params[$pref.'course'] = $value;
135 return array("id IN (SELECT userid
136 FROM {role_assignments} a
137 INNER JOIN {context} b ON a.contextid=b.id
138 INNER JOIN {course} c ON b.instanceid=c.id
139 WHERE $where)", $params);
143 * Returns a human friendly description of the filter used as label.
144 * @param array $data filter settings
145 * @return string active filter label
147 public function get_label($data) {
150 $value = $data['value'];
151 $roleid = $data['roleid'];
152 $categoryid = $data['categoryid'];
155 $a->label
= $this->_label
;
158 $role = $DB->get_record('role', array('id' => $roleid));
159 $a->rolename
= '"'.role_get_name($role).'"';
161 $a->rolename
= get_string('anyrole', 'filters');
165 $catname = $DB->get_field('course_categories', 'name', array('id' => $categoryid));
166 $a->categoryname
= '"'.format_string($catname).'"';
168 $a->categoryname
= get_string('anycategory', 'filters');
172 $a->coursename
= '"'.s($value).'"';
173 if (!$DB->record_exists('course', array('shortname' => $value))) {
174 return '<span class="notifyproblem">'.get_string('courserolelabelerror', 'filters', $a).'</span>';
177 $a->coursename
= get_string('anycourse', 'filters');
180 return get_string('courserolelabel', 'filters', $a);