MDL-63086 block_rss_client: Move skip time calculation to task
[moodle.git] / user / filters / courserole.php
blobb4c5ec984f8a4fec6fff7390fe0cbc8874991f37
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 * Course 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 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 {
34 /**
35 * Constructor
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);
44 /**
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);
54 /**
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);
61 return $roles;
64 /**
65 * Returns an array of course categories
66 * @return array of course categories
68 public function get_course_categories() {
69 global $CFG;
70 require_once($CFG->libdir.'/coursecatlib.php');
71 return array(0 => get_string('anycategory', 'filters')) + coursecat::make_categories_list();
74 /**
75 * Adds controls specific to this filter in the form.
76 * @param moodleform $mform a MoodleForm object to setup
78 public function setupForm(&$mform) {
79 $objs = array();
80 $objs['role'] = $mform->createElement('select', $this->_name .'_rl', null, $this->get_roles());
81 $objs['role']->setLabel(get_string('courserole', 'filters'));
82 $objs['category'] = $mform->createElement('select', $this->_name .'_ct', null, $this->get_course_categories());
83 $objs['category']->setLabel(get_string('coursecategory', 'filters'));
84 $objs['value'] = $mform->createElement('text', $this->_name, null);
85 $objs['value']->setLabel(get_string('coursevalue', 'filters'));
86 $grp =& $mform->addElement('group', $this->_name.'_grp', $this->_label, $objs, '', false);
87 $mform->setType($this->_name, PARAM_TEXT);
88 if ($this->_advanced) {
89 $mform->setAdvanced($this->_name.'_grp');
93 /**
94 * Retrieves data from the form data
95 * @param stdClass $formdata data submited with the form
96 * @return mixed array filter data or false when filter not set
98 public function check_data($formdata) {
99 $field = $this->_name;
100 $role = $field .'_rl';
101 $category = $field .'_ct';
103 if (array_key_exists($field, $formdata)) {
104 if (empty($formdata->$field) and empty($formdata->$role) and empty($formdata->$category)) {
105 // Nothing selected.
106 return false;
108 return array('value' => (string)$formdata->$field,
109 'roleid' => (int)$formdata->$role,
110 'categoryid' => (int)$formdata->$category);
112 return false;
116 * Returns the condition to be used with SQL where
117 * @param array $data filter settings
118 * @return array sql string and $params
120 public function get_sql_filter($data) {
121 global $CFG, $DB;
122 static $counter = 0;
123 $pref = 'ex_courserole'.($counter++).'_';
125 $value = $data['value'];
126 $roleid = $data['roleid'];
127 $categoryid = $data['categoryid'];
129 $params = array();
131 if (empty($value) and empty($roleid) and empty($categoryid)) {
132 return array('', $params);
135 $where = "b.contextlevel=50";
136 if ($roleid) {
137 $where .= " AND a.roleid = :{$pref}roleid";
138 $params[$pref.'roleid'] = $roleid;
140 if ($categoryid) {
141 $where .= " AND c.category = :{$pref}categoryid";
142 $params[$pref.'categoryid'] = $categoryid;
144 if ($value) {
145 $where .= " AND c.shortname = :{$pref}course";
146 $params[$pref.'course'] = $value;
148 return array("id IN (SELECT userid
149 FROM {role_assignments} a
150 INNER JOIN {context} b ON a.contextid=b.id
151 INNER JOIN {course} c ON b.instanceid=c.id
152 WHERE $where)", $params);
156 * Returns a human friendly description of the filter used as label.
157 * @param array $data filter settings
158 * @return string active filter label
160 public function get_label($data) {
161 global $DB;
163 $value = $data['value'];
164 $roleid = $data['roleid'];
165 $categoryid = $data['categoryid'];
167 $a = new stdClass();
168 $a->label = $this->_label;
170 if ($roleid) {
171 $role = $DB->get_record('role', array('id' => $roleid));
172 $a->rolename = '"'.role_get_name($role).'"';
173 } else {
174 $a->rolename = get_string('anyrole', 'filters');
177 if ($categoryid) {
178 $catname = $DB->get_field('course_categories', 'name', array('id' => $categoryid));
179 $a->categoryname = '"'.format_string($catname).'"';
180 } else {
181 $a->categoryname = get_string('anycategory', 'filters');
184 if ($value) {
185 $a->coursename = '"'.s($value).'"';
186 if (!$DB->record_exists('course', array('shortname' => $value))) {
187 return '<span class="notifyproblem">'.get_string('courserolelabelerror', 'filters', $a).'</span>';
189 } else {
190 $a->coursename = get_string('anycourse', 'filters');
193 return get_string('courserolelabel', 'filters', $a);