Fix a possible race condition in the PaintWeb DML code.
[moodle/mihaisucan.git] / user / filters / text.php
blob24d5497d776eeea1f66cdfa9660ab21a293dd9df
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 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 $grp->setHelpButton(array('text',$this->_label,'filters'));
46 $mform->disabledIf($this->_name, $this->_name.'_op', 'eq', 5);
47 if ($this->_advanced) {
48 $mform->setAdvanced($this->_name.'_grp');
52 /**
53 * Retrieves data from the form data
54 * @param object $formdata data submited with the form
55 * @return mixed array filter data or false when filter not set
57 function check_data($formdata) {
58 $field = $this->_name;
59 $operator = $field.'_op';
61 if (array_key_exists($operator, $formdata)) {
62 if ($formdata->$operator != 5 and $formdata->$field == '') {
63 // no data - no change except for empty filter
64 return false;
66 return array('operator'=>(int)$formdata->$operator, 'value'=>$formdata->$field);
69 return false;
72 /**
73 * Returns the condition to be used with SQL where
74 * @param array $data filter settings
75 * @return string the filtering condition or null if the filter is disabled
77 function get_sql_filter($data) {
78 $operator = $data['operator'];
79 $value = addslashes($data['value']);
80 $field = $this->_field;
82 if ($operator != 5 and $value === '') {
83 return '';
86 $ilike = sql_ilike();
88 switch($operator) {
89 case 0: // contains
90 $res = "$ilike '%$value%'"; break;
91 case 1: // does not contain
92 $res = "NOT $ilike '%$value%'"; break;
93 case 2: // equal to
94 $res = "$ilike '$value'"; break;
95 case 3: // starts with
96 $res = "$ilike '$value%'"; break;
97 case 4: // ends with
98 $res = "$ilike '%$value'"; break;
99 case 5: // empty
100 $res = "=''"; break;
101 default:
102 return '';
104 return $field.' '.$res;
108 * Returns a human friendly description of the filter used as label.
109 * @param array $data filter settings
110 * @return string active filter label
112 function get_label($data) {
113 $operator = $data['operator'];
114 $value = $data['value'];
115 $operators = $this->getOperators();
117 $a = new object();
118 $a->label = $this->_label;
119 $a->value = '"'.s($value).'"';
120 $a->operator = $operators[$operator];
123 switch ($operator) {
124 case 0: // contains
125 case 1: // doesn't contain
126 case 2: // equal to
127 case 3: // starts with
128 case 4: // ends with
129 return get_string('textlabel', 'filters', $a);
130 case 5: // empty
131 return get_string('textlabelnovalue', 'filters', $a);
134 return '';