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 $mform->setHelpButton($this->_name
, array('simpleselect', $this->_label
, 'filters'));
38 if ($this->_advanced
) {
39 $mform->setAdvanced($this->_name
);
44 * Retrieves data from the form data
45 * @param object $formdata data submited with the form
46 * @return mixed array filter data or false when filter not set
48 function check_data($formdata) {
49 $field = $this->_name
;
51 if (array_key_exists($field, $formdata) and $formdata->$field !== '') {
52 return array('value'=>(string)$formdata->$field);
59 * Returns the condition to be used with SQL where
60 * @param array $data filter settings
61 * @return array sql string and $params
63 function get_sql_filter($data) {
65 $name = 'ex_simpleselect'.$counter++
;
67 $value = $data['value'];
69 $field = $this->_field
;
73 return array("$field=:$name", array($name=>$value));
77 * Returns a human friendly description of the filter used as label.
78 * @param array $data filter settings
79 * @return string active filter label
81 function get_label($data) {
82 $value = $data['value'];
85 $a->label
= $this->_label
;
86 $a->value
= '"'.s($this->_options
[$value]).'"';
87 $a->operator
= get_string('isequalto','filters');
89 return get_string('selectlabel', 'filters', $a);