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 __construct($name, $label, $advanced, $field, $options, $default=null) {
56 parent
::__construct($name, $label, $advanced);
57 $this->_field
= $field;
58 $this->_options
= $options;
59 $this->_default
= $default;
63 * Old syntax of class constructor. Deprecated in PHP7.
65 * @deprecated since Moodle 3.1
67 public function user_filter_select($name, $label, $advanced, $field, $options, $default=null) {
68 debugging('Use of class name as constructor is deprecated', DEBUG_DEVELOPER
);
69 self
::__construct($name, $label, $advanced, $field, $options, $default=null);
73 * Returns an array of comparison operators
74 * @return array of comparison operators
76 public function get_operators() {
77 return array(0 => get_string('isanyvalue', 'filters'),
78 1 => get_string('isequalto', 'filters'),
79 2 => get_string('isnotequalto', 'filters'));
83 * Adds controls specific to this filter in the form.
84 * @param moodleform $mform a MoodleForm object to setup
86 public function setupForm(&$mform) {
88 $objs['limiter'] = $mform->createElement('select', $this->_name
.'_op', null, $this->get_operators());
89 $objs['limiter']->setLabel(get_string('limiterfor', 'filters', $this->_label
));
90 $objs['country'] = $mform->createElement('select', $this->_name
, null, $this->_options
);
91 $objs['country']->setLabel(get_string('valuefor', 'filters', $this->_label
));
92 $grp =& $mform->addElement('group', $this->_name
.'_grp', $this->_label
, $objs, '', false);
93 $mform->disabledIf($this->_name
, $this->_name
.'_op', 'eq', 0);
94 if (!is_null($this->_default
)) {
95 $mform->setDefault($this->_name
, $this->_default
);
97 if ($this->_advanced
) {
98 $mform->setAdvanced($this->_name
.'_grp');
103 * Retrieves data from the form data
104 * @param stdClass $formdata data submited with the form
105 * @return mixed array filter data or false when filter not set
107 public function check_data($formdata) {
108 $field = $this->_name
;
109 $operator = $field.'_op';
111 if (array_key_exists($field, $formdata) and !empty($formdata->$operator)) {
112 return array('operator' => (int)$formdata->$operator,
113 'value' => (string)$formdata->$field);
120 * Returns the condition to be used with SQL where
121 * @param array $data filter settings
122 * @return array sql string and $params
124 public function get_sql_filter($data) {
126 $name = 'ex_select'.$counter++
;
128 $operator = $data['operator'];
129 $value = $data['value'];
130 $field = $this->_field
;
137 $params[$name] = $value;
139 case 2: // Not equal to.
141 $params[$name] = $value;
144 return array('', array());
146 return array($field.$res, $params);
150 * Returns a human friendly description of the filter used as label.
151 * @param array $data filter settings
152 * @return string active filter label
154 public function get_label($data) {
155 $operators = $this->get_operators();
156 $operator = $data['operator'];
157 $value = $data['value'];
159 if (empty($operator)) {
164 $a->label
= $this->_label
;
165 $a->value
= '"'.s($this->_options
[$value]).'"';
166 $a->operator
= $operators[$operator];
168 return get_string('selectlabel', 'filters', $a);