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'));
53 $mform->setDefault($this->_name
. '_op', $this->_operator
);
54 $mform->setDefault($this->_name
, $this->_value
);
58 * Retrieves data from the form data
59 * @param object $formdata data submited with the form
61 function checkData($formdata) {
62 $field = $this->_name
;
63 $operator = $field . '_op';
64 $this->_value
= (string)@$formdata->$field;
65 $this->_operator
= (int)@$formdata->$operator;
69 * Returns the condition to be used with SQL where
70 * @return string the filtering condition or null if the filter is disabled
72 function getSQLFilter() {
73 switch($this->_operator
) {
77 $res = '="' . $this->_value
. '"';
79 case 2: // not equal to
80 $res = '<>"' . $this->_value
. '"';
83 return $this->_field
. $res;
87 * Returns a human friendly description of the filter.
88 * @return string filter description
90 function getDescription() {
91 $operators = $this->getOperators();
92 switch($this->_operator
) {
94 case 2: // not equal to
95 $res = $this->_label
. ' ' . $operators[$this->_operator
]. ' "' . $this->_options
[stripslashes($this->_value
)] . '"';