3 require_once($CFG->dirroot
.'/user/filters/lib.php');
6 * Generic filter for text fields.
8 class user_filter_text
extends user_filter_type
{
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;
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'));
37 * Adds controls specific to this filter in the form.
38 * @param object $mform a MoodleForm object to setup
40 function setupForm(&$mform) {
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');
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
65 // If field value is set then use it, else it's null.
67 if (isset($formdata->$field)) {
68 $fieldvalue = $formdata->$field;
70 return array('operator' => (int)$formdata->$operator, 'value' => $fieldvalue);
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) {
84 $name = 'ex_text'.$counter++
;
86 $operator = $data['operator'];
87 $value = $data['value'];
88 $field = $this->_field
;
92 if ($operator != 5 and $value === '') {
98 $res = $DB->sql_like($field, ":$name", false, false);
99 $params[$name] = "%$value%";
101 case 1: // does not contain
102 $res = $DB->sql_like($field, ":$name", false, false, true);
103 $params[$name] = "%$value%";
106 $res = $DB->sql_like($field, ":$name", false, false);
107 $params[$name] = "$value";
109 case 3: // starts with
110 $res = $DB->sql_like($field, ":$name", false, false);
111 $params[$name] = "$value%";
114 $res = $DB->sql_like($field, ":$name", false, false);
115 $params[$name] = "%$value";
118 $res = "$field = :$name";
119 $params[$name] = $DB->sql_empty();
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();
138 $a->label
= $this->_label
;
139 $a->value
= '"'.s($value).'"';
140 $a->operator
= $operators[$operator];
145 case 1: // doesn't contain
147 case 3: // starts with
149 return get_string('textlabel', 'filters', $a);
151 return get_string('textlabelnovalue', 'filters', $a);