MDL-11517 reserved word MOD used in table alias in questions backup code
[moodle-pu.git] / user / filters / courserole.php
blobf3cde9143bd0fc0d948cbde3d6fab1b9cfa03db6
1 <?php //$Id$
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 * User role (0 = any role)
12 var $_roleid;
13 /**
14 * Course category in which to search the course (0 = all categories).
16 var $_categoryid;
17 /**
18 * Constructor
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;
32 /**
33 * Returns an array of available roles
34 * @return array of availble roles
36 function getRoles() {
37 $context =& get_context_instance(CONTEXT_SYSTEM);
38 $roles =& array_merge(array(0=> get_string('anyrole','filters')), get_assignable_roles($context));
39 return $roles;
42 /**
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);
53 /**
54 * Adds controls specific to this filter in the form.
55 * @param object $mform a MoodleForm object to setup
57 function setupForm(&$mform) {
58 $objs = array();
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);
69 /**
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;
82 /**
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() {
87 global $CFG;
88 if(empty($this->_value) && empty($this->_roleid) && empty($this->_categoryid)) {
89 return null;
91 $timenow = time();
92 $where = 'WHERE b.contextlevel=50 AND timestart<' . $timenow .' AND (timeend=0 OR timeend>'. $timenow . ')';
93 if($this->_roleid) {
94 $where.= ' AND roleid='. $this->_roleid;
96 if($this->_categoryid) {
97 $where .= ' AND category=' . $this->_categoryid;
99 if($this->_value) {
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 ".
105 $where . ')';
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]. '"';
116 } else {
117 $rolename = get_string('anyrole','filters');
119 if ($this->_categoryid) {
120 $categories=& $this->getCourseCategories();
121 $categoryname = '"' . $categories[$this->_categoryid]. '"';
122 } else {
123 $categoryname = get_string('anycategory', 'filters');
125 if ($this->_value) {
126 $coursename = '"' . stripslashes($this->_value). '"';
127 } else {
128 $coursename = get_string('anycourse','filters');
130 return $this->_label . ' is ' . $rolename. ' in ' . $coursename . ' from ' . $categoryname;