MDL-11517 reserved word MOD used in table alias in questions backup code
[moodle-pu.git] / user / filters / select.php
blob4b1777b651e1005b8164455f0857b0b936453a21
1 <?php //$Id$
3 require_once($CFG->dirroot . '/user/filters/lib.php');
5 /**
6 * Generic filter based on a list of values.
7 */
8 class user_filter_select extends user_filter_type {
9 /**
10 * operator used for comparison of data
12 var $_operator;
13 /**
14 * options for the list values
16 var $_options;
17 /**
18 * Constructor
19 * @param string $name the name of the filter instance
20 * @param string $label the label of the filter instance
21 * @param string $field the field used for filtering data
22 * @param string $value the value used for filtering data
23 * @param int $operator code of the comparison operator
25 function user_filter_select($name, $label, $field, $options, $value=null, $operator=null) {
26 parent::user_filter_type($name, $label, $field, $value);
27 $this->_operator = $operator;
28 $this->_options = $options;
31 /**
32 * Returns an array of comparison operators
33 * @return array of comparison operators
35 function getOperators() {
36 return array(
37 get_string('isanyvalue','filters'),
38 get_string('isequalto','filters'),
39 get_string('isnotequalto','filters'),
43 /**
44 * Adds controls specific to this filter in the form.
45 * @param object $mform a MoodleForm object to setup
47 function setupForm(&$mform) {
48 $objs = array();
49 $objs[] =& $mform->createElement('select', $this->_name . '_op', null, $this->getOperators());
50 $objs[] =& $mform->createElement('select', $this->_name, null, $this->_options);
51 $grp =& $mform->addElement('group', $this->_name . '_grp', $this->_label, $objs, '', false);
52 $grp->setHelpButton(array('select','','filters'));
53 $mform->setDefault($this->_name . '_op', $this->_operator);
54 $mform->setDefault($this->_name, $this->_value);
57 /**
58 * Retrieves data from the form data
59 * @param object $formdata data submited with the form
61 function checkData($formdata) {
62 $field = $this->_name;
63 $operator = $field . '_op';
64 $this->_value = (string)@$formdata->$field;
65 $this->_operator = (int)@$formdata->$operator;
68 /**
69 * Returns the condition to be used with SQL where
70 * @return string the filtering condition or null if the filter is disabled
72 function getSQLFilter() {
73 switch($this->_operator) {
74 default:
75 return null;
76 case 1: // equal to
77 $res = '="' . $this->_value . '"';
78 break;
79 case 2: // not equal to
80 $res = '<>"' . $this->_value . '"';
81 break;
83 return $this->_field . $res;
86 /**
87 * Returns a human friendly description of the filter.
88 * @return string filter description
90 function getDescription() {
91 $operators = $this->getOperators();
92 switch($this->_operator) {
93 case 1: // equal to
94 case 2: // not equal to
95 $res = $this->_label . ' ' . $operators[$this->_operator]. ' "' . $this->_options[stripslashes($this->_value)] . '"';
96 break;
98 return $res;