Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/user/filters/cohort.php
Go to the documentation of this file.
00001 <?php
00002 
00003 defined('MOODLE_INTERNAL') || die();
00004 
00005 require_once($CFG->dirroot.'/user/filters/lib.php');
00006 
00010 class user_filter_cohort extends user_filter_type {
00015     function user_filter_cohort($advanced) {
00016         parent::user_filter_type('cohort', get_string('idnumber', 'core_cohort'), $advanced);
00017     }
00018 
00023     function getOperators() {
00024         return array(0 => get_string('contains', 'filters'),
00025                      1 => get_string('doesnotcontain','filters'),
00026                      2 => get_string('isequalto','filters'),
00027                      3 => get_string('startswith','filters'),
00028                      4 => get_string('endswith','filters'));
00029     }
00030 
00035     function setupForm(&$mform) {
00036         $objs = array();
00037         $objs[] =& $mform->createElement('select', $this->_name.'_op', null, $this->getOperators());
00038         $objs[] =& $mform->createElement('text', $this->_name, null);
00039         $grp =& $mform->addElement('group', $this->_name.'_grp', $this->_label, $objs, '', false);
00040         $mform->disabledIf($this->_name, $this->_name.'_op', 'eq', 5);
00041         if ($this->_advanced) {
00042             $mform->setAdvanced($this->_name.'_grp');
00043         }
00044         $mform->setDefault($this->_name.'_op', 2);
00045     }
00046 
00052     function check_data($formdata) {
00053         $field    = $this->_name;
00054         $operator = $field.'_op';
00055 
00056         if (array_key_exists($operator, $formdata)) {
00057             if ($formdata->$field == '') {
00058                 return false;
00059             }
00060             return array('operator'=>(int)$formdata->$operator, 'value'=>$formdata->$field);
00061         }
00062 
00063         return false;
00064     }
00065 
00071     function get_sql_filter($data) {
00072         global $DB;
00073         static $counter = 0;
00074         $name = 'ex_cohort'.$counter++;
00075 
00076         $operator = $data['operator'];
00077         $value    = $data['value'];
00078 
00079         $params = array();
00080 
00081         if ($value === '') {
00082             return '';
00083         }
00084 
00085         switch($operator) {
00086             case 0: // contains
00087                 $res = $DB->sql_like('idnumber', ":$name", false, false);
00088                 $params[$name] = "%$value%";
00089                 break;
00090             case 1: // does not contain
00091                 $res = $DB->sql_like('idnumber', ":$name", false, false, true);
00092                 $params[$name] = "%$value%";
00093                 break;
00094             case 2: // equal to
00095                 $res = $DB->sql_like('idnumber', ":$name", false, false);
00096                 $params[$name] = "$value";
00097                 break;
00098             case 3: // starts with
00099                 $res = $DB->sql_like('idnumber', ":$name", false, false);
00100                 $params[$name] = "$value%";
00101                 break;
00102             case 4: // ends with
00103                 $res = $DB->sql_like('idnumber', ":$name", false, false);
00104                 $params[$name] = "%$value";
00105                 break;
00106             default:
00107                 return '';
00108         }
00109 
00110         $sql = "id IN (SELECT userid
00111                          FROM {cohort_members}
00112                          JOIN {cohort} ON {cohort_members}.cohortid = {cohort}.id
00113                         WHERE $res)";
00114 
00115         return array($sql, $params);
00116     }
00117 
00123     function get_label($data) {
00124         $operator  = $data['operator'];
00125         $value     = $data['value'];
00126         $operators = $this->getOperators();
00127 
00128         $a = new stdClass();
00129         $a->label    = $this->_label;
00130         $a->value    = '"'.s($value).'"';
00131         $a->operator = $operators[$operator];
00132 
00133 
00134         switch ($operator) {
00135             case 0: // contains
00136             case 1: // doesn't contain
00137             case 2: // equal to
00138             case 3: // starts with
00139             case 4: // ends with
00140                 return get_string('textlabel', 'filters', $a);
00141         }
00142 
00143         return '';
00144     }
00145 }
 All Data Structures Namespaces Files Functions Variables Enumerations