Merge branch 'MDL-27053-dev-code-warning' of git://github.com/mudrd8mz/moodle
[moodle.git] / user / filters / courserole.php
blobe8172ecbe4737755f8a3323953ea1544a8febe5b
1 <?php
3 require_once($CFG->dirroot .'/user/filters/lib.php');
5 /**
6 * User filter based on roles in a course identified by its shortname.
7 */
8 class user_filter_courserole extends user_filter_type {
9 /**
10 * Constructor
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);
19 /**
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);
26 return $roles;
29 /**
30 * Returns an array of course categories
31 * @return array of course categories
33 function get_course_categories() {
34 global $CFG;
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;
42 /**
43 * Adds controls specific to this filter in the form.
44 * @param object $mform a MoodleForm object to setup
46 function setupForm(&$mform) {
47 $objs = array();
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');
57 /**
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)) {
69 // nothing selected
70 return false;
72 return array('value' => (string)$formdata->$field,
73 'roleid' => (int)$formdata->$role,
74 'categoryid' => (int)$formdata->$category);
76 return false;
79 /**
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) {
85 global $CFG, $DB;
86 static $counter = 0;
87 $name = 'ex_courserole'.$counter++;
89 $value = $data['value'];
90 $roleid = $data['roleid'];
91 $categoryid = $data['categoryid'];
93 $params = array();
95 if (empty($value) and empty($roleid) and empty($categoryid)) {
96 return array('', $params);
99 $where = "b.contextlevel=50";
100 if ($roleid) {
101 $where .= " AND a.roleid = :roleid";
102 $params['roleid'] = $roleid;
105 if ($categoryid) {
106 $where .= " AND c.category = :categoryid";
107 $params['categoryid'] = $categoryid;
109 if ($value) {
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) {
126 global $DB;
128 $value = $data['value'];
129 $roleid = $data['roleid'];
130 $categoryid = $data['categoryid'];
132 $a = new stdClass();
133 $a->label = $this->_label;
135 if ($roleid) {
136 $rolename = $DB->get_field('role', 'name', array('id'=>$roleid));
137 $a->rolename = '"'.format_string($rolename).'"';
138 } else {
139 $a->rolename = get_string('anyrole', 'filters');
142 if ($categoryid) {
143 $catname = $DB->get_field('course_categories', 'name', array('id'=>$categoryid));
144 $a->categoryname = '"'.format_string($catname).'"';
145 } else {
146 $a->categoryname = get_string('anycategory', 'filters');
149 if ($value) {
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>';
154 } else {
155 $a->coursename = get_string('anycourse', 'filters');
158 return get_string('courserolelabel', 'filters', $a);