3 require_once($CFG->dirroot
.'/user/filters/lib.php');
6 * User filter based on roles in a course identified by its shortname.
8 class user_filter_courserole
extends user_filter_type
{
11 * @param string $name the name of the filter instance
12 * @param string $label the label of the filter instance
13 * @param boolean $advanced advanced form element flag
15 function user_filter_courserole($name, $label, $advanced) {
16 parent
::user_filter_type($name, $label, $advanced);
20 * Returns an array of available roles
21 * @return array of availble roles
23 function get_roles() {
24 $context = get_context_instance(CONTEXT_SYSTEM
);
25 $roles = array(0=> get_string('anyrole','filters')) +
get_assignable_roles($context);
30 * Returns an array of course categories
31 * @return array of course categories
33 function get_course_categories() {
35 require_once($CFG->dirroot
.'/course/lib.php');
36 $displaylist = array();
37 $parentlist = array();
38 make_categories_list($displaylist, $parentlist);
39 return array(0=> get_string('anycategory', 'filters')) +
$displaylist;
43 * Adds controls specific to this filter in the form.
44 * @param object $mform a MoodleForm object to setup
46 function setupForm(&$mform) {
48 $objs[] =& $mform->createElement('select', $this->_name
.'_rl', null, $this->get_roles());
49 $objs[] =& $mform->createElement('select', $this->_name
.'_ct', null, $this->get_course_categories());
50 $objs[] =& $mform->createElement('text', $this->_name
, null);
51 $grp =& $mform->addElement('group', $this->_name
.'_grp', $this->_label
, $objs, '', false);
52 if ($this->_advanced
) {
53 $mform->setAdvanced($this->_name
.'_grp');
58 * Retrieves data from the form data
59 * @param object $formdata data submited with the form
60 * @return mixed array filter data or false when filter not set
62 function check_data($formdata) {
63 $field = $this->_name
;
64 $role = $field .'_rl';
65 $category = $field .'_ct';
67 if (array_key_exists($field, $formdata)) {
68 if (empty($formdata->$field) and empty($formdata->$role) and empty($formdata->$category)) {
72 return array('value' => (string)$formdata->$field,
73 'roleid' => (int)$formdata->$role,
74 'categoryid' => (int)$formdata->$category);
80 * Returns the condition to be used with SQL where
81 * @param array $data filter settings
82 * @return array sql string and $params
84 function get_sql_filter($data) {
87 $name = 'ex_courserole'.$counter++
;
89 $value = $data['value'];
90 $roleid = $data['roleid'];
91 $categoryid = $data['categoryid'];
95 if (empty($value) and empty($roleid) and empty($categoryid)) {
96 return array('', $params);
99 $where = "b.contextlevel=50";
101 $where .= " AND a.roleid = :roleid";
102 $params['roleid'] = $roleid;
106 $where .= " AND c.category = :categoryid";
107 $params['categoryid'] = $categoryid;
110 $where .= " AND c.shortname = :$name";
111 $params[$name] = $value;
113 return array("id IN (SELECT userid
114 FROM {role_assignments} a
115 INNER JOIN {context} b ON a.contextid=b.id
116 INNER JOIN {course} c ON b.instanceid=c.id
117 WHERE $where)", $params);
121 * Returns a human friendly description of the filter used as label.
122 * @param array $data filter settings
123 * @return string active filter label
125 function get_label($data) {
128 $value = $data['value'];
129 $roleid = $data['roleid'];
130 $categoryid = $data['categoryid'];
133 $a->label
= $this->_label
;
136 $rolename = $DB->get_field('role', 'name', array('id'=>$roleid));
137 $a->rolename
= '"'.format_string($rolename).'"';
139 $a->rolename
= get_string('anyrole', 'filters');
143 $catname = $DB->get_field('course_categories', 'name', array('id'=>$categoryid));
144 $a->categoryname
= '"'.format_string($catname).'"';
146 $a->categoryname
= get_string('anycategory', 'filters');
150 $a->coursename
= '"'.s($value).'"';
151 if (!$DB->record_exists('course', array('shortname'=>$value))) {
152 return '<span class="notifyproblem">'.get_string('courserolelabelerror', 'filters', $a).'</span>';
155 $a->coursename
= get_string('anycourse', 'filters');
158 return get_string('courserolelabel', 'filters', $a);