MDL-10873 If both site default and user pref are empty for studentsperpage, we assume...
[moodle-pu.git] / user / filters / text.php
blobfbcfa65934c66b29c6d9c4899e37bfdf7c427550
1 <?php //$Id$
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 /**
10 * operator used for comparison of data
12 var $_operator;
13 /**
14 * Constructor
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;
26 /**
27 * Returns an array of comparison operators
28 * @return array of comparison operators
30 function getOperators() {
31 return array(
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'),
41 /**
42 * Adds controls specific to this filter in the form.
43 * @param object $mform a MoodleForm object to setup
45 function setupForm(&$mform) {
46 $objs = array();
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);
55 /**
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;
66 /**
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) {
72 default:
73 return null;
74 case 0: // contains
75 if(empty($this->_value)) {
76 return null;
78 $res = ' ' . sql_ilike(). ' "%' . $this->_value . '%"';
79 break;
80 case 1: // does not contain
81 if(empty($this->_value)) {
82 return null;
84 $res = ' NOT ' . sql_ilike(). ' "%' . $this->_value . '%"';
85 break;
86 case 2: // equal to
87 if(empty($this->_value)) {
88 return null;
90 $res = '="' . $this->_value . '"';
91 break;
92 case 3: // starts with
93 if(empty($this->_value)) {
94 return null;
96 $res = ' ' . sql_ilike(). ' "' . $this->_value . '%"';
97 break;
98 case 4: // ends with
99 if(empty($this->_value)) {
100 return null;
102 $res = ' ' . sql_ilike(). ' "%' . $this->_value . '"';
103 break;
104 case 5: // empty
105 if(empty($this->_value)) {
106 return null;
108 $res = '=""';
109 break;
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) {
121 case 0: // contains
122 case 1: // doesn't contain
123 case 2: // equal to
124 case 3: // starts with
125 case 4: // ends with
126 $res = $operators[$this->_operator]. ' "' . stripslashes($this->_value) . '"';
127 break;
128 case 5: // empty
129 $res = $operators[$this->_operator];
130 break;
132 return $this->_label . ' ' . $res;