MDL-21695 adding help and link strings
[moodle.git] / user / filters / simpleselect.php
blob3b4c01995c8f9ba0b95e9085ca758862ebd3ff3a
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 $mform->setHelpButton($this->_name, array('simpleselect', $this->_label, 'filters'));
38 if ($this->_advanced) {
39 $mform->setAdvanced($this->_name);
43 /**
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);
55 return false;
58 /**
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) {
64 static $counter = 0;
65 $name = 'ex_simpleselect'.$counter++;
67 $value = $data['value'];
68 $params = array();
69 $field = $this->_field;
70 if ($value == '') {
71 return '';
73 return array("$field=:$name", array($name=>$value));
76 /**
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'];
84 $a = new object();
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);