MDL-21695 adding help and link strings
[moodle.git] / user / filters / courserole.php
blobc8487e0392a187eea7d9f31a08bb6808de69426c
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 $displaylist = array();
35 $parentlist = array();
36 make_categories_list($displaylist, $parentlist);
37 return array(0=> get_string('anycategory', 'filters')) + $displaylist;
40 /**
41 * Adds controls specific to this filter in the form.
42 * @param object $mform a MoodleForm object to setup
44 function setupForm(&$mform) {
45 $objs = array();
46 $objs[] =& $mform->createElement('select', $this->_name .'_rl', null, $this->get_roles());
47 $objs[] =& $mform->createElement('select', $this->_name .'_ct', null, $this->get_course_categories());
48 $objs[] =& $mform->createElement('text', $this->_name, null);
49 $grp =& $mform->addElement('group', $this->_name.'_grp', $this->_label, $objs, '', false);
50 $mform->setHelpButton($this->_name.'_grp', array('courserole', $this->_label, 'filters'));
51 if ($this->_advanced) {
52 $mform->setAdvanced($this->_name.'_grp');
56 /**
57 * Retrieves data from the form data
58 * @param object $formdata data submited with the form
59 * @return mixed array filter data or false when filter not set
61 function check_data($formdata) {
62 $field = $this->_name;
63 $role = $field .'_rl';
64 $category = $field .'_ct';
66 if (array_key_exists($field, $formdata)) {
67 if (empty($formdata->$field) and empty($formdata->$role) and empty($formdata->$category)) {
68 // nothing selected
69 return false;
71 return array('value' => (string)$formdata->$field,
72 'roleid' => (int)$formdata->$role,
73 'categoryid' => (int)$formdata->$category);
75 return false;
78 /**
79 * Returns the condition to be used with SQL where
80 * @param array $data filter settings
81 * @return array sql string and $params
83 function get_sql_filter($data) {
84 global $CFG, $DB;
85 static $counter = 0;
86 $name = 'ex_courserole'.$counter++;
88 $value = $data['value'];
89 $roleid = (int)$data['roleid'];
90 $categoryid = (int)$data['categoryid'];
92 $params = array();
94 if (empty($value) and empty($roleid) and empty($categoryid)) {
95 return array('', $params);
98 $timenow = round(time(), 100); // rounding - enable sql caching
99 $where = "b.contextlevel=50 AND a.timestart<$timenow AND (a.timeend=0 OR a.timeend>$timenow)";
100 if ($roleid) {
101 $where .= " AND a.roleid=$roleid";
103 if ($categoryid) {
104 $where .= " AND c.category=$categoryid";
106 if ($value) {
107 $where .= " AND c.shortname ".$DB->sql_ilike()." :$name";
108 $params[$name] = $value;
110 return array("id IN (SELECT userid
111 FROM {role_assignments} a
112 INNER JOIN {context} b ON a.contextid=b.id
113 INNER JOIN {course} c ON b.instanceid=c.id
114 WHERE $where)", $params);
118 * Returns a human friendly description of the filter used as label.
119 * @param array $data filter settings
120 * @return string active filter label
122 function get_label($data) {
123 global $DB;
125 $value = $data['value'];
126 $roleid = $data['roleid'];
127 $categoryid = $data['categoryid'];
129 $a = new object();
130 $a->label = $this->_label;
132 if ($roleid) {
133 $rolename = $DB->get_field('role', 'name', array('id'=>$roleid));
134 $a->rolename = '"'.format_string($rolename).'"';
135 } else {
136 $a->rolename = get_string('anyrole', 'filters');
139 if ($categoryid) {
140 $catname = $DB->get_field('course_categories', 'name', array('id'=>$categoryid));
141 $a->categoryname = '"'.format_string($catname).'"';
142 } else {
143 $a->categoryname = get_string('anycategory', 'filters');
146 if ($value) {
147 $a->coursename = '"'.s($value).'"';
148 if (!$DB->record_exists('course', array('shortname'=>$value))) {
149 return '<span class="notifyproblem">'.get_string('courserolelabelerror', 'filters', $a).'</span>';
151 } else {
152 $a->coursename = get_string('anycourse', 'filters');
155 return get_string('courserolelabel', 'filters', $a);