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
{
10 * User role (0 = any role)
14 * Course category in which to search the course (0 = all categories).
19 * @param string $name the name of the filter instance
20 * @param string $label the label of the filter instance
21 * @param string $field the field used for filtering data
22 * @param string $value the shortname of the course (used for filtering data)
23 * @param int $categoryid id of the category
24 * @param int $roleid id of the role
26 function user_filter_courserole($name, $label, $field='id', $value=null, $categoryid=0, $roleid=0) {
27 parent
::user_filter_type($name, $label, $field, $value);
28 $this->_roleid
= $roleid;
29 $this->_categoryid
= $categoryid;
33 * Returns an array of available roles
34 * @return array of availble roles
37 $context =& get_context_instance(CONTEXT_SYSTEM
);
38 $roles =& array_merge(array(0=> get_string('anyrole','filters')), get_assignable_roles($context));
43 * Returns an array of course categories
44 * @return array of course categories
46 function getCourseCategories() {
47 $displaylist = array();
48 $parentlist = array();
49 make_categories_list($displaylist, $parentlist);
50 return array_merge(array(0=> get_string('anycategory', 'filters')), $displaylist);
54 * Adds controls specific to this filter in the form.
55 * @param object $mform a MoodleForm object to setup
57 function setupForm(&$mform) {
59 $objs[] =& $mform->createElement('select', $this->_name
. '_rl', null, $this->getRoles());
60 $objs[] =& $mform->createElement('select', $this->_name
. '_ct', null, $this->getCourseCategories());
61 $objs[] =& $mform->createElement('text', $this->_name
, null);
62 $grp =& $mform->addElement('group', $this->_name
. '_grp', $this->_label
, $objs, '', false);
63 $grp->setHelpButton(array('courserole','','filters'));
64 $mform->setDefault($this->_name
, $this->_value
);
65 $mform->setDefault($this->_name
. '_rl', $this->_roleid
);
66 $mform->setDefault($this->_name
. '_ct', $this->_categoryid
);
70 * Retrieves data from the form data
71 * @param object $formdata data submited with the form
73 function checkData($formdata) {
74 $field = $this->_name
;
75 $role = $field . '_rl';
76 $category = $field . '_ct';
77 $this->_value
= (string)@$formdata->$field;
78 $this->_roleid
= (int)@$formdata->$role;
79 $this->_categoryid
= (int)@$formdata->$category;
83 * Returns the condition to be used with SQL where
84 * @return string the filtering condition or null if the filter is disabled
86 function getSQLFilter() {
88 if(empty($this->_value
) && empty($this->_roleid
) && empty($this->_categoryid
)) {
92 $where = 'WHERE b.contextlevel=50 AND timestart<' . $timenow .' AND (timeend=0 OR timeend>'. $timenow . ')';
94 $where.= ' AND roleid='. $this->_roleid
;
96 if($this->_categoryid
) {
97 $where .= ' AND category=' . $this->_categoryid
;
100 $where .= ' AND shortname="' . $this->_value
. '"';
102 return $this->_field
. " IN (SELECT userid FROM {$CFG->prefix}role_assignments a ".
103 "INNER JOIN {$CFG->prefix}context b ON a.contextid=b.id ".
104 "INNER JOIN {$CFG->prefix}course c ON b.instanceid=c.id ".
109 * Returns a human friendly description of the filter.
110 * @return string filter description
112 function getDescription() {
113 if ($this->_roleid
) {
114 $roles =& $this->getRoles();
115 $rolename = '"' . $roles[$this->_roleid
]. '"';
117 $rolename = get_string('anyrole','filters');
119 if ($this->_categoryid
) {
120 $categories=& $this->getCourseCategories();
121 $categoryname = '"' . $categories[$this->_categoryid
]. '"';
123 $categoryname = get_string('anycategory', 'filters');
126 $coursename = '"' . stripslashes($this->_value
). '"';
128 $coursename = get_string('anycourse','filters');
130 return $this->_label
. ' is ' . $rolename. ' in ' . $coursename . ' from ' . $categoryname;