3 require_once($CFG->dirroot
.'/user/filters/lib.php');
6 * Generic filter based on a list of values.
8 class user_filter_simpleselect
extends user_filter_type
{
10 * options for the list values
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;
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
);
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);
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) {
64 $name = 'ex_simpleselect'.$counter++
;
66 $value = $data['value'];
68 $field = $this->_field
;
72 return array("$field=:$name", array($name=>$value));
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'];
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);