Merge branch 'MDL-81457-main' of https://github.com/andrewnicols/moodle
[moodle.git] / user / filters / simpleselect.php
blob04f2c3b5f0dcf2dbeb5170d8f76db3069cd890cf
1 <?php
2 // This file is part of Moodle - http://moodle.org/
3 //
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.
8 //
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/>.
17 /**
18 * Simple value select filter.
20 * @package core_user
21 * @category user
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');
28 /**
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 {
34 /**
35 * options for the list values
36 * @var array
38 public $_options;
40 /**
41 * @var string
43 public $_field;
45 /**
46 * Constructor
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;
59 /**
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);
69 /**
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);
81 /**
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);
93 return false;
96 /**
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) {
102 static $counter = 0;
103 $name = 'ex_simpleselect'.$counter++;
105 $value = $data['value'];
106 $params = array();
107 $field = $this->_field;
108 if ($value == '') {
109 return '';
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'];
122 $a = new stdClass();
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);