3 require_once($CFG->dirroot
. '/user/filters/lib.php');
6 * Generic filter based on a list of values.
8 class user_filter_select
extends user_filter_type
{
10 * operator used for comparison of data
14 * options for the list values
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;
32 * Returns an array of comparison operators
33 * @return array of comparison operators
35 function getOperators() {
37 get_string('isanyvalue','filters'),
38 get_string('isequalto','filters'),
39 get_string('isnotequalto','filters'),
44 * Adds controls specific to this filter in the form.
45 * @param object $mform a MoodleForm object to setup
47 function setupForm(&$mform) {
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'));
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;
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
) {
75 $res = '="' . $this->_value
. '"';
77 case 2: // not equal to
78 $res = '<>"' . $this->_value
. '"';
81 return $this->_field
. $res;
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
) {
92 case 2: // not equal to
93 $res = $this->_label
. ' ' . $operators[$this->_operator
]. ' "' . $this->_options
[stripslashes($this->_value
)] . '"';