mirrored from git://git.moodle.org/moodle.git
/
courserole.php
157 lines (138 loc) · 5.27 KB
/
courserole.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
<?php
require_once($CFG->dirroot .'/user/filters/lib.php');
/**
* User filter based on roles in a course identified by its shortname.
*/
class user_filter_courserole extends user_filter_type {
/**
* Constructor
* @param string $name the name of the filter instance
* @param string $label the label of the filter instance
* @param boolean $advanced advanced form element flag
*/
function user_filter_courserole($name, $label, $advanced) {
parent::user_filter_type($name, $label, $advanced);
}
/**
* Returns an array of available roles
* @return array of availble roles
*/
function get_roles() {
$context = context_system::instance();
$roles = array(0=> get_string('anyrole','filters')) + get_default_enrol_roles($context);
return $roles;
}
/**
* Returns an array of course categories
* @return array of course categories
*/
function get_course_categories() {
global $CFG;
require_once($CFG->libdir.'/coursecatlib.php');
return array(0=> get_string('anycategory', 'filters')) + coursecat::make_categories_list();
}
/**
* Adds controls specific to this filter in the form.
* @param object $mform a MoodleForm object to setup
*/
function setupForm(&$mform) {
$objs = array();
$objs[] =& $mform->createElement('select', $this->_name .'_rl', null, $this->get_roles());
$objs[] =& $mform->createElement('select', $this->_name .'_ct', null, $this->get_course_categories());
$objs[] =& $mform->createElement('text', $this->_name, null);
$grp =& $mform->addElement('group', $this->_name.'_grp', $this->_label, $objs, '', false);
$mform->setType($this->_name, PARAM_ALPHANUMEXT);
if ($this->_advanced) {
$mform->setAdvanced($this->_name.'_grp');
}
}
/**
* Retrieves data from the form data
* @param object $formdata data submited with the form
* @return mixed array filter data or false when filter not set
*/
function check_data($formdata) {
$field = $this->_name;
$role = $field .'_rl';
$category = $field .'_ct';
if (array_key_exists($field, $formdata)) {
if (empty($formdata->$field) and empty($formdata->$role) and empty($formdata->$category)) {
// nothing selected
return false;
}
return array('value' => (string)$formdata->$field,
'roleid' => (int)$formdata->$role,
'categoryid' => (int)$formdata->$category);
}
return false;
}
/**
* Returns the condition to be used with SQL where
* @param array $data filter settings
* @return array sql string and $params
*/
function get_sql_filter($data) {
global $CFG, $DB;
static $counter = 0;
$pref = 'ex_courserole'.($counter++).'_';
$value = $data['value'];
$roleid = $data['roleid'];
$categoryid = $data['categoryid'];
$params = array();
if (empty($value) and empty($roleid) and empty($categoryid)) {
return array('', $params);
}
$where = "b.contextlevel=50";
if ($roleid) {
$where .= " AND a.roleid = :{$pref}roleid";
$params[$pref.'roleid'] = $roleid;
}
if ($categoryid) {
$where .= " AND c.category = :{$pref}categoryid";
$params[$pref.'categoryid'] = $categoryid;
}
if ($value) {
$where .= " AND c.shortname = :{$pref}course";
$params[$pref.'course'] = $value;
}
return array("id IN (SELECT userid
FROM {role_assignments} a
INNER JOIN {context} b ON a.contextid=b.id
INNER JOIN {course} c ON b.instanceid=c.id
WHERE $where)", $params);
}
/**
* Returns a human friendly description of the filter used as label.
* @param array $data filter settings
* @return string active filter label
*/
function get_label($data) {
global $DB;
$value = $data['value'];
$roleid = $data['roleid'];
$categoryid = $data['categoryid'];
$a = new stdClass();
$a->label = $this->_label;
if ($roleid) {
$role = $DB->get_record('role', array('id'=>$roleid));
$a->rolename = '"'.role_get_name($role).'"';
} else {
$a->rolename = get_string('anyrole', 'filters');
}
if ($categoryid) {
$catname = $DB->get_field('course_categories', 'name', array('id'=>$categoryid));
$a->categoryname = '"'.format_string($catname).'"';
} else {
$a->categoryname = get_string('anycategory', 'filters');
}
if ($value) {
$a->coursename = '"'.s($value).'"';
if (!$DB->record_exists('course', array('shortname'=>$value))) {
return '<span class="notifyproblem">'.get_string('courserolelabelerror', 'filters', $a).'</span>';
}
} else {
$a->coursename = get_string('anycourse', 'filters');
}
return get_string('courserolelabel', 'filters', $a);
}
}