1 <?php 2 // This file is part of Moodle - http://moodle.org/ 3 // 4 // Moodle is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU General Public License as published by 6 // the Free Software Foundation, either version 3 of the License, or 7 // (at your option) any later version. 8 // 9 // Moodle is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 // GNU General Public License for more details. 13 // 14 // You should have received a copy of the GNU General Public License 15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>. 16 17 /** 18 * Cohort filter. 19 * 20 * @package core_user 21 * @category user 22 * @copyright 2011 Petr Skoda 23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 24 */ 25 26 defined('MOODLE_INTERNAL') || die(); 27 28 require_once($CFG->dirroot.'/user/filters/lib.php'); 29 30 /** 31 * Generic filter for cohort membership. 32 * @copyright 1999 Martin Dougiamas http://dougiamas.com 33 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 34 */ 35 class user_filter_cohort extends user_filter_type { 36 /** 37 * Constructor 38 * @param boolean $advanced advanced form element flag 39 */ 40 public function __construct($advanced) { 41 parent::__construct('cohort', get_string('idnumber', 'core_cohort'), $advanced); 42 } 43 44 /** 45 * Old syntax of class constructor. Deprecated in PHP7. 46 * 47 * @deprecated since Moodle 3.1 48 */ 49 public function user_filter_cohort($advanced) { 50 debugging('Use of class name as constructor is deprecated', DEBUG_DEVELOPER); 51 self::__construct($advanced); 52 } 53 54 /** 55 * Returns an array of comparison operators 56 * @return array of comparison operators 57 */ 58 public function getOperators() { 59 return array(0 => get_string('contains', 'filters'), 60 1 => get_string('doesnotcontain', 'filters'), 61 2 => get_string('isequalto', 'filters'), 62 3 => get_string('startswith', 'filters'), 63 4 => get_string('endswith', 'filters'), 64 5 => get_string('isempty', 'filters')); 65 } 66 67 /** 68 * Adds controls specific to this filter in the form. 69 * @param object $mform a MoodleForm object to setup 70 */ 71 public function setupForm(&$mform) { 72 $objs = array(); 73 $objs['select'] = $mform->createElement('select', $this->_name.'_op', null, $this->getOperators()); 74 $objs['text'] = $mform->createElement('text', $this->_name, null); 75 $objs['select']->setLabel(get_string('limiterfor', 'filters', $this->_label)); 76 $objs['text']->setLabel(get_string('valuefor', 'filters', $this->_label)); 77 $grp =& $mform->addElement('group', $this->_name.'_grp', $this->_label, $objs, '', false); 78 $mform->setType($this->_name, PARAM_RAW); 79 $mform->disabledIf($this->_name, $this->_name.'_op', 'eq', 5); 80 if ($this->_advanced) { 81 $mform->setAdvanced($this->_name.'_grp'); 82 } 83 $mform->setDefault($this->_name.'_op', 2); 84 } 85 86 /** 87 * Retrieves data from the form data 88 * @param object $formdata data submited with the form 89 * @return mixed array filter data or false when filter not set 90 */ 91 public function check_data($formdata) { 92 $field = $this->_name; 93 $operator = $field.'_op'; 94 95 if (property_exists($formdata, $operator)) { 96 if ($formdata->$operator != 5 and $formdata->$field == '') { 97 // No data - no change except for empty filter. 98 return false; 99 } 100 // If field value is set then use it, else it's null. 101 $fieldvalue = null; 102 if (isset($formdata->$field)) { 103 $fieldvalue = $formdata->$field; 104 } 105 return array('operator' => (int)$formdata->$operator, 'value' => $fieldvalue); 106 } 107 108 return false; 109 } 110 111 /** 112 * Returns the condition to be used with SQL where 113 * @param array $data filter settings 114 * @return array sql string and $params 115 */ 116 public function get_sql_filter($data) { 117 global $DB; 118 static $counter = 0; 119 $name = 'ex_cohort'.$counter++; 120 121 $operator = $data['operator']; 122 $value = $data['value']; 123 124 $params = array(); 125 126 if ($value === '') { 127 return ''; 128 } 129 130 $not = ''; 131 switch($operator) { 132 case 0: // Contains. 133 $res = $DB->sql_like('idnumber', ":$name", false, false); 134 $params[$name] = "%$value%"; 135 break; 136 case 1: // Does not contain. 137 $not = 'NOT'; 138 $res = $DB->sql_like('idnumber', ":$name", false, false); 139 $params[$name] = "%$value%"; 140 break; 141 case 2: // Equal to. 142 $res = $DB->sql_like('idnumber', ":$name", false, false); 143 $params[$name] = "$value"; 144 break; 145 case 3: // Starts with. 146 $res = $DB->sql_like('idnumber', ":$name", false, false); 147 $params[$name] = "$value%"; 148 break; 149 case 4: // Ends with. 150 $res = $DB->sql_like('idnumber', ":$name", false, false); 151 $params[$name] = "%$value"; 152 break; 153 case 5: // Empty. 154 $not = 'NOT'; 155 $res = '(idnumber IS NOT NULL AND idnumber <> :'.$name.')'; 156 $params[$name] = ''; 157 break; 158 default: 159 return ''; 160 } 161 162 $sql = "id $not IN (SELECT userid 163 FROM {cohort_members} 164 JOIN {cohort} ON {cohort_members}.cohortid = {cohort}.id 165 WHERE $res)"; 166 167 return array($sql, $params); 168 } 169 170 /** 171 * Returns a human friendly description of the filter used as label. 172 * @param array $data filter settings 173 * @return string active filter label 174 */ 175 public function get_label($data) { 176 $operator = $data['operator']; 177 $value = $data['value']; 178 $operators = $this->getOperators(); 179 180 $a = new stdClass(); 181 $a->label = $this->_label; 182 $a->value = '"'.s($value).'"'; 183 $a->operator = $operators[$operator]; 184 185 switch ($operator) { 186 case 0: // Contains. 187 case 1: // Doesn't contain. 188 case 2: // Equal to. 189 case 3: // Starts with. 190 case 4: // Ends with. 191 return get_string('textlabel', 'filters', $a); 192 case 5: // Empty. 193 return get_string('textlabelnovalue', 'filters', $a); 194 } 195 196 return ''; 197 } 198 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body