Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/user/filters/courserole.php
Go to the documentation of this file.
00001 <?php
00002 
00003 require_once($CFG->dirroot .'/user/filters/lib.php');
00004 
00008 class user_filter_courserole extends user_filter_type {
00015     function user_filter_courserole($name, $label, $advanced) {
00016         parent::user_filter_type($name, $label, $advanced);
00017     }
00018 
00023     function get_roles() {
00024         $context = get_context_instance(CONTEXT_SYSTEM);
00025         $roles = array(0=> get_string('anyrole','filters')) + get_default_enrol_roles($context);
00026         return $roles;
00027     }
00028 
00033     function get_course_categories() {
00034         global $CFG;
00035         require_once($CFG->dirroot.'/course/lib.php');
00036         $displaylist = array();
00037         $parentlist = array();
00038         make_categories_list($displaylist, $parentlist);
00039         return array(0=> get_string('anycategory', 'filters')) + $displaylist;
00040     }
00041 
00046     function setupForm(&$mform) {
00047         $objs = array();
00048         $objs[] =& $mform->createElement('select', $this->_name .'_rl', null, $this->get_roles());
00049         $objs[] =& $mform->createElement('select', $this->_name .'_ct', null, $this->get_course_categories());
00050         $objs[] =& $mform->createElement('text', $this->_name, null);
00051         $grp =& $mform->addElement('group', $this->_name.'_grp', $this->_label, $objs, '', false);
00052         if ($this->_advanced) {
00053             $mform->setAdvanced($this->_name.'_grp');
00054         }
00055     }
00056 
00062     function check_data($formdata) {
00063         $field    = $this->_name;
00064         $role     = $field .'_rl';
00065         $category = $field .'_ct';
00066 
00067         if (array_key_exists($field, $formdata)) {
00068             if (empty($formdata->$field) and empty($formdata->$role) and empty($formdata->$category)) {
00069                 // nothing selected
00070                 return false;
00071             }
00072             return array('value'      => (string)$formdata->$field,
00073                          'roleid'     => (int)$formdata->$role,
00074                          'categoryid' => (int)$formdata->$category);
00075         }
00076         return false;
00077     }
00078 
00084     function get_sql_filter($data) {
00085         global $CFG, $DB;
00086         static $counter = 0;
00087         $name = 'ex_courserole'.$counter++;
00088 
00089         $value      = $data['value'];
00090         $roleid     = $data['roleid'];
00091         $categoryid = $data['categoryid'];
00092 
00093         $params = array();
00094 
00095         if (empty($value) and empty($roleid) and empty($categoryid)) {
00096             return array('', $params);
00097         }
00098 
00099         $where = "b.contextlevel=50";
00100         if ($roleid) {
00101             $where .= " AND a.roleid = :roleid";
00102             $params['roleid'] = $roleid;
00103 
00104         }
00105         if ($categoryid) {
00106             $where .= " AND c.category = :categoryid";
00107             $params['categoryid'] = $categoryid;
00108         }
00109         if ($value) {
00110             $where .= " AND c.shortname = :$name";
00111             $params[$name] = $value;
00112         }
00113         return array("id IN (SELECT userid
00114                                FROM {role_assignments} a
00115                          INNER JOIN {context} b ON a.contextid=b.id
00116                          INNER JOIN {course} c ON b.instanceid=c.id
00117                               WHERE $where)", $params);
00118     }
00119 
00125     function get_label($data) {
00126         global $DB;
00127 
00128         $value      = $data['value'];
00129         $roleid     = $data['roleid'];
00130         $categoryid = $data['categoryid'];
00131 
00132         $a = new stdClass();
00133         $a->label = $this->_label;
00134 
00135         if ($roleid) {
00136             $rolename = $DB->get_field('role', 'name', array('id'=>$roleid));
00137             $a->rolename = '"'.format_string($rolename).'"';
00138         } else {
00139             $a->rolename = get_string('anyrole', 'filters');
00140         }
00141 
00142         if ($categoryid) {
00143             $catname = $DB->get_field('course_categories', 'name', array('id'=>$categoryid));
00144             $a->categoryname = '"'.format_string($catname).'"';
00145         } else {
00146             $a->categoryname = get_string('anycategory', 'filters');
00147         }
00148 
00149         if ($value) {
00150             $a->coursename = '"'.s($value).'"';
00151             if (!$DB->record_exists('course', array('shortname'=>$value))) {
00152                 return '<span class="notifyproblem">'.get_string('courserolelabelerror', 'filters', $a).'</span>';
00153             }
00154         } else {
00155             $a->coursename = get_string('anycourse', 'filters');
00156         }
00157 
00158         return get_string('courserolelabel', 'filters', $a);
00159     }
00160 }
 All Data Structures Namespaces Files Functions Variables Enumerations