[ Index ] |
PHP Cross Reference of moodle-2.8 |
[Summary view] [Print] [Text view]
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 * Text field filter 19 * 20 * @package core_user 21 * @category user 22 * @copyright 1999 Martin Dougiamas http://dougiamas.com 23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 24 */ 25 26 require_once($CFG->dirroot.'/user/filters/lib.php'); 27 28 /** 29 * Generic filter for text fields. 30 * @copyright 1999 Martin Dougiamas http://dougiamas.com 31 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 32 */ 33 class user_filter_text extends user_filter_type { 34 /** @var string */ 35 public $_field; 36 37 /** 38 * Constructor 39 * @param string $name the name of the filter instance 40 * @param string $label the label of the filter instance 41 * @param boolean $advanced advanced form element flag 42 * @param string $field user table filed name 43 */ 44 public function user_filter_text($name, $label, $advanced, $field) { 45 parent::user_filter_type($name, $label, $advanced); 46 $this->_field = $field; 47 } 48 49 /** 50 * Returns an array of comparison operators 51 * @return array of comparison operators 52 */ 53 public function getOperators() { 54 return array(0 => get_string('contains', 'filters'), 55 1 => get_string('doesnotcontain', 'filters'), 56 2 => get_string('isequalto', 'filters'), 57 3 => get_string('startswith', 'filters'), 58 4 => get_string('endswith', 'filters'), 59 5 => get_string('isempty', 'filters')); 60 } 61 62 /** 63 * Adds controls specific to this filter in the form. 64 * @param object $mform a MoodleForm object to setup 65 */ 66 public function setupForm(&$mform) { 67 $objs = array(); 68 $objs[] = $mform->createElement('select', $this->_name.'_op', null, $this->getOperators()); 69 $objs[] = $mform->createElement('text', $this->_name, null); 70 $grp =& $mform->addElement('group', $this->_name.'_grp', $this->_label, $objs, '', false); 71 $mform->setType($this->_name, PARAM_RAW); 72 $mform->disabledIf($this->_name, $this->_name.'_op', 'eq', 5); 73 if ($this->_advanced) { 74 $mform->setAdvanced($this->_name.'_grp'); 75 } 76 } 77 78 /** 79 * Retrieves data from the form data 80 * @param object $formdata data submited with the form 81 * @return mixed array filter data or false when filter not set 82 */ 83 public function check_data($formdata) { 84 $field = $this->_name; 85 $operator = $field.'_op'; 86 87 if (array_key_exists($operator, $formdata)) { 88 if ($formdata->$operator != 5 and $formdata->$field == '') { 89 // No data - no change except for empty filter. 90 return false; 91 } 92 // If field value is set then use it, else it's null. 93 $fieldvalue = null; 94 if (isset($formdata->$field)) { 95 $fieldvalue = $formdata->$field; 96 } 97 return array('operator' => (int)$formdata->$operator, 'value' => $fieldvalue); 98 } 99 100 return false; 101 } 102 103 /** 104 * Returns the condition to be used with SQL where 105 * @param array $data filter settings 106 * @return array sql string and $params 107 */ 108 public function get_sql_filter($data) { 109 global $DB; 110 static $counter = 0; 111 $name = 'ex_text'.$counter++; 112 113 $operator = $data['operator']; 114 $value = $data['value']; 115 $field = $this->_field; 116 117 $params = array(); 118 119 if ($operator != 5 and $value === '') { 120 return ''; 121 } 122 123 switch($operator) { 124 case 0: // Contains. 125 $res = $DB->sql_like($field, ":$name", false, false); 126 $params[$name] = "%$value%"; 127 break; 128 case 1: // Does not contain. 129 $res = $DB->sql_like($field, ":$name", false, false, true); 130 $params[$name] = "%$value%"; 131 break; 132 case 2: // Equal to. 133 $res = $DB->sql_like($field, ":$name", false, false); 134 $params[$name] = "$value"; 135 break; 136 case 3: // Starts with. 137 $res = $DB->sql_like($field, ":$name", false, false); 138 $params[$name] = "$value%"; 139 break; 140 case 4: // Ends with. 141 $res = $DB->sql_like($field, ":$name", false, false); 142 $params[$name] = "%$value"; 143 break; 144 case 5: // Empty. 145 $res = "$field = :$name"; 146 $params[$name] = ''; 147 break; 148 default: 149 return ''; 150 } 151 return array($res, $params); 152 } 153 154 /** 155 * Returns a human friendly description of the filter used as label. 156 * @param array $data filter settings 157 * @return string active filter label 158 */ 159 public function get_label($data) { 160 $operator = $data['operator']; 161 $value = $data['value']; 162 $operators = $this->getOperators(); 163 164 $a = new stdClass(); 165 $a->label = $this->_label; 166 $a->value = '"'.s($value).'"'; 167 $a->operator = $operators[$operator]; 168 169 switch ($operator) { 170 case 0: // Contains. 171 case 1: // Doesn't contain. 172 case 2: // Equal to. 173 case 3: // Starts with. 174 case 4: // Ends with. 175 return get_string('textlabel', 'filters', $a); 176 case 5: // Empty. 177 return get_string('textlabelnovalue', 'filters', $a); 178 } 179 180 return ''; 181 } 182 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Fri Nov 28 20:29:05 2014 | Cross-referenced by PHPXref 0.7.1 |