forked from moodle/moodle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
select.php
159 lines (139 loc) · 5.02 KB
/
select.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Value select filter.
*
* @package core_user
* @category user
* @copyright 1999 Martin Dougiamas http://dougiamas.com
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
require_once($CFG->dirroot.'/user/filters/lib.php');
/**
* Generic filter based on a list of values.
* @copyright 1999 Martin Dougiamas http://dougiamas.com
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class user_filter_select extends user_filter_type {
/**
* options for the list values
* @var array
*/
public $_options;
/** @var string */
public $_field;
/** @var mixed|null */
public $_default;
/**
* Constructor
* @param string $name the name of the filter instance
* @param string $label the label of the filter instance
* @param boolean $advanced advanced form element flag
* @param string $field user table filed name
* @param array $options select options
* @param mixed $default option
*/
public function user_filter_select($name, $label, $advanced, $field, $options, $default=null) {
parent::user_filter_type($name, $label, $advanced);
$this->_field = $field;
$this->_options = $options;
$this->_default = $default;
}
/**
* Returns an array of comparison operators
* @return array of comparison operators
*/
public function get_operators() {
return array(0 => get_string('isanyvalue', 'filters'),
1 => get_string('isequalto', 'filters'),
2 => get_string('isnotequalto', 'filters'));
}
/**
* Adds controls specific to this filter in the form.
* @param moodleform $mform a MoodleForm object to setup
*/
public function setupForm(&$mform) {
$objs = array();
$objs[] = $mform->createElement('select', $this->_name.'_op', null, $this->get_operators());
$objs[] = $mform->createElement('select', $this->_name, null, $this->_options);
$grp =& $mform->addElement('group', $this->_name.'_grp', $this->_label, $objs, '', false);
$mform->disabledIf($this->_name, $this->_name.'_op', 'eq', 0);
if (!is_null($this->_default)) {
$mform->setDefault($this->_name, $this->_default);
}
if ($this->_advanced) {
$mform->setAdvanced($this->_name.'_grp');
}
}
/**
* Retrieves data from the form data
* @param stdClass $formdata data submited with the form
* @return mixed array filter data or false when filter not set
*/
public function check_data($formdata) {
$field = $this->_name;
$operator = $field.'_op';
if (array_key_exists($field, $formdata) and !empty($formdata->$operator)) {
return array('operator' => (int)$formdata->$operator,
'value' => (string)$formdata->$field);
}
return false;
}
/**
* Returns the condition to be used with SQL where
* @param array $data filter settings
* @return array sql string and $params
*/
public function get_sql_filter($data) {
static $counter = 0;
$name = 'ex_select'.$counter++;
$operator = $data['operator'];
$value = $data['value'];
$field = $this->_field;
$params = array();
switch($operator) {
case 1: // Equal to.
$res = "=:$name";
$params[$name] = $value;
break;
case 2: // Not equal to.
$res = "<>:$name";
$params[$name] = $value;
break;
default:
return array('', array());
}
return array($field.$res, $params);
}
/**
* Returns a human friendly description of the filter used as label.
* @param array $data filter settings
* @return string active filter label
*/
public function get_label($data) {
$operators = $this->get_operators();
$operator = $data['operator'];
$value = $data['value'];
if (empty($operator)) {
return '';
}
$a = new stdClass();
$a->label = $this->_label;
$a->value = '"'.s($this->_options[$value]).'"';
$a->operator = $operators[$operator];
return get_string('selectlabel', 'filters', $a);
}
}