2 // This file is part of Moodle - http://moodle.org/
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
18 * Value select filter.
22 * @copyright 1999 Martin Dougiamas http://dougiamas.com
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 require_once($CFG->dirroot
.'/user/filters/lib.php');
29 * Generic filter based on a list of values.
30 * @copyright 1999 Martin Dougiamas http://dougiamas.com
31 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
33 class user_filter_select
extends user_filter_type
{
35 * options for the list values
43 /** @var mixed|null */
48 * @param string $name the name of the filter instance
49 * @param string $label the label of the filter instance
50 * @param boolean $advanced advanced form element flag
51 * @param string $field user table filed name
52 * @param array $options select options
53 * @param mixed $default option
55 public function user_filter_select($name, $label, $advanced, $field, $options, $default=null) {
56 parent
::user_filter_type($name, $label, $advanced);
57 $this->_field
= $field;
58 $this->_options
= $options;
59 $this->_default
= $default;
63 * Returns an array of comparison operators
64 * @return array of comparison operators
66 public function get_operators() {
67 return array(0 => get_string('isanyvalue', 'filters'),
68 1 => get_string('isequalto', 'filters'),
69 2 => get_string('isnotequalto', 'filters'));
73 * Adds controls specific to this filter in the form.
74 * @param moodleform $mform a MoodleForm object to setup
76 public function setupForm(&$mform) {
78 $objs[] = $mform->createElement('select', $this->_name
.'_op', null, $this->get_operators());
79 $objs[] = $mform->createElement('select', $this->_name
, null, $this->_options
);
80 $grp =& $mform->addElement('group', $this->_name
.'_grp', $this->_label
, $objs, '', false);
81 $mform->disabledIf($this->_name
, $this->_name
.'_op', 'eq', 0);
82 if (!is_null($this->_default
)) {
83 $mform->setDefault($this->_name
, $this->_default
);
85 if ($this->_advanced
) {
86 $mform->setAdvanced($this->_name
.'_grp');
91 * Retrieves data from the form data
92 * @param stdClass $formdata data submited with the form
93 * @return mixed array filter data or false when filter not set
95 public function check_data($formdata) {
96 $field = $this->_name
;
97 $operator = $field.'_op';
99 if (array_key_exists($field, $formdata) and !empty($formdata->$operator)) {
100 return array('operator' => (int)$formdata->$operator,
101 'value' => (string)$formdata->$field);
108 * Returns the condition to be used with SQL where
109 * @param array $data filter settings
110 * @return array sql string and $params
112 public function get_sql_filter($data) {
114 $name = 'ex_select'.$counter++
;
116 $operator = $data['operator'];
117 $value = $data['value'];
118 $field = $this->_field
;
125 $params[$name] = $value;
127 case 2: // Not equal to.
129 $params[$name] = $value;
132 return array('', array());
134 return array($field.$res, $params);
138 * Returns a human friendly description of the filter used as label.
139 * @param array $data filter settings
140 * @return string active filter label
142 public function get_label($data) {
143 $operators = $this->get_operators();
144 $operator = $data['operator'];
145 $value = $data['value'];
147 if (empty($operator)) {
152 $a->label
= $this->_label
;
153 $a->value
= '"'.s($this->_options
[$value]).'"';
154 $a->operator
= $operators[$operator];
156 return get_string('selectlabel', 'filters', $a);