3 require_once($CFG->dirroot
. '/user/filters/lib.php');
6 * Generic filter for text fields.
8 class user_filter_text
extends user_filter_type
{
10 * operator used for comparison of data
15 * @param string $name the name of the filter instance
16 * @param string $label the label of the filter instance
17 * @param string $field the field used for filtering data
18 * @param string $value the value used for filtering data
19 * @param int $operator code of the comparison operator
21 function user_filter_text($name, $label, $field, $value=null, $operator=0) {
22 parent
::user_filter_type($name, $label, $field, $value);
23 $this->_operator
= $operator;
27 * Returns an array of comparison operators
28 * @return array of comparison operators
30 function getOperators() {
32 get_string('contains', 'filters'),
33 get_string('doesnotcontain','filters'),
34 get_string('isequalto','filters'),
35 get_string('startswith','filters'),
36 get_string('endswith','filters'),
37 get_string('isempty','filters'),
42 * Adds controls specific to this filter in the form.
43 * @param object $mform a MoodleForm object to setup
45 function setupForm(&$mform) {
47 $objs[] =& $mform->createElement('select', $this->_name
. '_op', null, $this->getOperators());
48 $objs[] =& $mform->createElement('text', $this->_name
, null);
49 $grp =& $mform->addElement('group', $this->_name
. '_grp', $this->_label
, $objs, '', false);
50 $grp->setHelpButton(array('text','','filters'));
51 $mform->setDefault($this->_name
, $this->_value
);
52 $mform->setDefault($this->_name
. '_op', $this->_operator
);
56 * Retrieves data from the form data
57 * @param object $formdata data submited with the form
59 function checkData($formdata) {
60 $field = $this->_name
;
61 $operator = $field . '_op';
62 $this->_value
= (string)@$formdata->$field;
63 $this->_operator
= (int)@$formdata->$operator;
67 * Returns the condition to be used with SQL where
68 * @return string the filtering condition or null if the filter is disabled
70 function getSQLFilter() {
71 switch($this->_operator
) {
75 if(empty($this->_value
)) {
78 $res = ' ' . sql_ilike(). ' "%' . $this->_value
. '%"';
80 case 1: // does not contain
81 if(empty($this->_value
)) {
84 $res = ' NOT ' . sql_ilike(). ' "%' . $this->_value
. '%"';
87 if(empty($this->_value
)) {
90 $res = '="' . $this->_value
. '"';
92 case 3: // starts with
93 if(empty($this->_value
)) {
96 $res = ' ' . sql_ilike(). ' "' . $this->_value
. '%"';
99 if(empty($this->_value
)) {
102 $res = ' ' . sql_ilike(). ' "%' . $this->_value
. '"';
105 if(empty($this->_value
)) {
111 return $this->_field
. $res;
115 * Returns a human friendly description of the filter.
116 * @return string filter description
118 function getDescription() {
119 $operators = $this->getOperators();
120 switch($this->_operator
) {
122 case 1: // doesn't contain
124 case 3: // starts with
126 $res = $operators[$this->_operator
]. ' "' . stripslashes($this->_value
) . '"';
129 $res = $operators[$this->_operator
];
132 return $this->_label
. ' ' . $res;