MDL-31329 timezones: updated to 2011n
[moodle.git] / user / filters / simpleselect.php
bloba1861e4352fa68e821398d8c6ed4ec485f1bbe00
1 <?php
3 require_once($CFG->dirroot.'/user/filters/lib.php');
5 /**
6 * Generic filter based on a list of values.
7 */
8 class user_filter_simpleselect extends user_filter_type {
9 /**
10 * options for the list values
12 var $_options;
14 var $_field;
16 /**
17 * Constructor
18 * @param string $name the name of the filter instance
19 * @param string $label the label of the filter instance
20 * @param boolean $advanced advanced form element flag
21 * @param string $field user table filed name
22 * @param array $options select options
24 function user_filter_simpleselect($name, $label, $advanced, $field, $options) {
25 parent::user_filter_type($name, $label, $advanced);
26 $this->_field = $field;
27 $this->_options = $options;
30 /**
31 * Adds controls specific to this filter in the form.
32 * @param object $mform a MoodleForm object to setup
34 function setupForm(&$mform) {
35 $choices = array(''=>get_string('anyvalue', 'filters')) + $this->_options;
36 $mform->addElement('select', $this->_name, $this->_label, $choices);
37 if ($this->_advanced) {
38 $mform->setAdvanced($this->_name);
42 /**
43 * Retrieves data from the form data
44 * @param object $formdata data submited with the form
45 * @return mixed array filter data or false when filter not set
47 function check_data($formdata) {
48 $field = $this->_name;
50 if (array_key_exists($field, $formdata) and $formdata->$field !== '') {
51 return array('value'=>(string)$formdata->$field);
54 return false;
57 /**
58 * Returns the condition to be used with SQL where
59 * @param array $data filter settings
60 * @return array sql string and $params
62 function get_sql_filter($data) {
63 static $counter = 0;
64 $name = 'ex_simpleselect'.$counter++;
66 $value = $data['value'];
67 $params = array();
68 $field = $this->_field;
69 if ($value == '') {
70 return '';
72 return array("$field=:$name", array($name=>$value));
75 /**
76 * Returns a human friendly description of the filter used as label.
77 * @param array $data filter settings
78 * @return string active filter label
80 function get_label($data) {
81 $value = $data['value'];
83 $a = new stdClass();
84 $a->label = $this->_label;
85 $a->value = '"'.s($this->_options[$value]).'"';
86 $a->operator = get_string('isequalto','filters');
88 return get_string('selectlabel', 'filters', $a);