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 * Simple 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_simpleselect
extends user_filter_type
{
35 * options for the list values
47 * @param string $name the name of the filter instance
48 * @param string $label the label of the filter instance
49 * @param boolean $advanced advanced form element flag
50 * @param string $field user table filed name
51 * @param array $options select options
53 public function __construct($name, $label, $advanced, $field, $options) {
54 parent
::__construct($name, $label, $advanced);
55 $this->_field
= $field;
56 $this->_options
= $options;
60 * Old syntax of class constructor. Deprecated in PHP7.
62 * @deprecated since Moodle 3.1
64 public function user_filter_simpleselect($name, $label, $advanced, $field, $options) {
65 debugging('Use of class name as constructor is deprecated', DEBUG_DEVELOPER
);
66 self
::__construct($name, $label, $advanced, $field, $options);
70 * Adds controls specific to this filter in the form.
71 * @param moodleform $mform a MoodleForm object to setup
73 public function setupForm(&$mform) {
74 $choices = array('' => get_string('anyvalue', 'filters')) +
$this->_options
;
75 $mform->addElement('select', $this->_name
, $this->_label
, $choices);
76 if ($this->_advanced
) {
77 $mform->setAdvanced($this->_name
);
82 * Retrieves data from the form data
83 * @param object $formdata data submited with the form
84 * @return mixed array filter data or false when filter not set
86 public function check_data($formdata) {
87 $field = $this->_name
;
89 if (property_exists($formdata, $field) and $formdata->$field !== '') {
90 return array('value' => (string)$formdata->$field);
97 * Returns the condition to be used with SQL where
98 * @param array $data filter settings
99 * @return array sql string and $params
101 public function get_sql_filter($data) {
103 $name = 'ex_simpleselect'.$counter++
;
105 $value = $data['value'];
107 $field = $this->_field
;
111 return array("$field=:$name", array($name => $value));
115 * Returns a human friendly description of the filter used as label.
116 * @param array $data filter settings
117 * @return string active filter label
119 public function get_label($data) {
120 $value = $data['value'];
123 $a->label
= $this->_label
;
124 $a->value
= '"'.s($this->_options
[$value]).'"';
125 $a->operator
= get_string('isequalto', 'filters');
127 return get_string('selectlabel', 'filters', $a);