MDL-10689:
[moodle-linuxchix.git] / user / filters / select.php
blob03c2ee9c07a3e5b4eed12106cb4a7f0caaf50219
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'));
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 1: // equal to
75 $res = '="' . $this->_value . '"';
76 break;
77 case 2: // not equal to
78 $res = '<>"' . $this->_value . '"';
79 break;
81 return $this->_field . $res;
84 /**
85 * Returns a human friendly description of the filter.
86 * @return string filter description
88 function getDescription() {
89 $operators = $this->getOperators();
90 switch($this->_operator) {
91 case 1: // equal to
92 case 2: // not equal to
93 $res = $this->_label . ' ' . $operators[$this->_operator]. ' "' . $this->_options[stripslashes($this->_value)] . '"';
94 break;
96 return $res;