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