weekly release 2.4.6+
[moodle.git] / user / filters / text.php
blob8be8f1fac253975efa1fc3c7efd50a7503ca880f
1 <?php
3 require_once($CFG->dirroot.'/user/filters/lib.php');
5 /**
6 * Generic filter for text fields.
7 */
8 class user_filter_text extends user_filter_type {
9 var $_field;
11 /**
12 * Constructor
13 * @param string $name the name of the filter instance
14 * @param string $label the label of the filter instance
15 * @param boolean $advanced advanced form element flag
16 * @param string $field user table filed name
18 function user_filter_text($name, $label, $advanced, $field) {
19 parent::user_filter_type($name, $label, $advanced);
20 $this->_field = $field;
23 /**
24 * Returns an array of comparison operators
25 * @return array of comparison operators
27 function getOperators() {
28 return array(0 => get_string('contains', 'filters'),
29 1 => get_string('doesnotcontain','filters'),
30 2 => get_string('isequalto','filters'),
31 3 => get_string('startswith','filters'),
32 4 => get_string('endswith','filters'),
33 5 => get_string('isempty','filters'));
36 /**
37 * Adds controls specific to this filter in the form.
38 * @param object $mform a MoodleForm object to setup
40 function setupForm(&$mform) {
41 $objs = array();
42 $objs[] =& $mform->createElement('select', $this->_name.'_op', null, $this->getOperators());
43 $objs[] =& $mform->createElement('text', $this->_name, null);
44 $grp =& $mform->addElement('group', $this->_name.'_grp', $this->_label, $objs, '', false);
45 $mform->disabledIf($this->_name, $this->_name.'_op', 'eq', 5);
46 if ($this->_advanced) {
47 $mform->setAdvanced($this->_name.'_grp');
51 /**
52 * Retrieves data from the form data
53 * @param object $formdata data submited with the form
54 * @return mixed array filter data or false when filter not set
56 function check_data($formdata) {
57 $field = $this->_name;
58 $operator = $field.'_op';
60 if (array_key_exists($operator, $formdata)) {
61 if ($formdata->$operator != 5 and $formdata->$field == '') {
62 // no data - no change except for empty filter
63 return false;
65 // If field value is set then use it, else it's null.
66 $fieldvalue = null;
67 if (isset($formdata->$field)) {
68 $fieldvalue = $formdata->$field;
70 return array('operator' => (int)$formdata->$operator, 'value' => $fieldvalue);
73 return false;
76 /**
77 * Returns the condition to be used with SQL where
78 * @param array $data filter settings
79 * @return array sql string and $params
81 function get_sql_filter($data) {
82 global $DB;
83 static $counter = 0;
84 $name = 'ex_text'.$counter++;
86 $operator = $data['operator'];
87 $value = $data['value'];
88 $field = $this->_field;
90 $params = array();
92 if ($operator != 5 and $value === '') {
93 return '';
96 switch($operator) {
97 case 0: // contains
98 $res = $DB->sql_like($field, ":$name", false, false);
99 $params[$name] = "%$value%";
100 break;
101 case 1: // does not contain
102 $res = $DB->sql_like($field, ":$name", false, false, true);
103 $params[$name] = "%$value%";
104 break;
105 case 2: // equal to
106 $res = $DB->sql_like($field, ":$name", false, false);
107 $params[$name] = "$value";
108 break;
109 case 3: // starts with
110 $res = $DB->sql_like($field, ":$name", false, false);
111 $params[$name] = "$value%";
112 break;
113 case 4: // ends with
114 $res = $DB->sql_like($field, ":$name", false, false);
115 $params[$name] = "%$value";
116 break;
117 case 5: // empty
118 $res = "$field = :$name";
119 $params[$name] = $DB->sql_empty();
120 break;
121 default:
122 return '';
124 return array($res, $params);
128 * Returns a human friendly description of the filter used as label.
129 * @param array $data filter settings
130 * @return string active filter label
132 function get_label($data) {
133 $operator = $data['operator'];
134 $value = $data['value'];
135 $operators = $this->getOperators();
137 $a = new stdClass();
138 $a->label = $this->_label;
139 $a->value = '"'.s($value).'"';
140 $a->operator = $operators[$operator];
143 switch ($operator) {
144 case 0: // contains
145 case 1: // doesn't contain
146 case 2: // equal to
147 case 3: // starts with
148 case 4: // ends with
149 return get_string('textlabel', 'filters', $a);
150 case 5: // empty
151 return get_string('textlabelnovalue', 'filters', $a);
154 return '';