MDL-64484 user: show full profile field name in filter element.
[moodle.git] / user / filters / profilefield.php
CommitLineData
aa6c1ced 1<?php
a2ed6e69
SH
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.
13//
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/>.
16
17/**
18 * Profile field filter.
19 *
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
24 */
c3260b07 25
cd1edf9e 26require_once($CFG->dirroot.'/user/filters/lib.php');
c3260b07 27
28/**
29 * User filter based on values of custom profile fields.
a2ed6e69
SH
30 *
31 * @copyright 1999 Martin Dougiamas http://dougiamas.com
32 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
c3260b07 33 */
34class user_filter_profilefield extends user_filter_type {
cd1edf9e 35
c3260b07 36 /**
37 * Constructor
38 * @param string $name the name of the filter instance
39 * @param string $label the label of the filter instance
cd1edf9e 40 * @param boolean $advanced advanced form element flag
c3260b07 41 */
c4d0b752
MG
42 public function __construct($name, $label, $advanced) {
43 parent::__construct($name, $label, $advanced);
44 }
45
46 /**
47 * Old syntax of class constructor. Deprecated in PHP7.
48 *
49 * @deprecated since Moodle 3.1
50 */
a2ed6e69 51 public function user_filter_profilefield($name, $label, $advanced) {
c4d0b752
MG
52 debugging('Use of class name as constructor is deprecated', DEBUG_DEVELOPER);
53 self::__construct($name, $label, $advanced);
c3260b07 54 }
cd1edf9e 55
c3260b07 56 /**
57 * Returns an array of comparison operators
58 * @return array of comparison operators
59 */
a2ed6e69 60 public function get_operators() {
cd1edf9e 61 return array(0 => get_string('contains', 'filters'),
a2ed6e69
SH
62 1 => get_string('doesnotcontain', 'filters'),
63 2 => get_string('isequalto', 'filters'),
64 3 => get_string('startswith', 'filters'),
65 4 => get_string('endswith', 'filters'),
66 5 => get_string('isempty', 'filters'),
67 6 => get_string('isnotdefined', 'filters'),
68 7 => get_string('isdefined', 'filters'));
c3260b07 69 }
cd1edf9e 70
c3260b07 71 /**
72 * Returns an array of custom profile fields
73 * @return array of profile fields
74 */
a2ed6e69 75 public function get_profile_fields() {
5d910388 76 global $DB;
03098df6 77 if (!$fields = $DB->get_records_menu('user_info_field', null, 'name', 'id, name')) {
c3260b07 78 return null;
79 }
cd1edf9e 80 $res = array(0 => get_string('anyfield', 'filters'));
03098df6
PH
81
82 return $res + $fields;
c3260b07 83 }
84
85 /**
86 * Adds controls specific to this filter in the form.
87 * @param object $mform a MoodleForm object to setup
88 */
a2ed6e69
SH
89 public function setupForm(&$mform) {
90 $profilefields = $this->get_profile_fields();
91 if (empty($profilefields)) {
c3260b07 92 return;
93 }
94 $objs = array();
ff6744c5
BB
95 $objs['field'] = $mform->createElement('select', $this->_name.'_fld', null, $profilefields);
96 $objs['op'] = $mform->createElement('select', $this->_name.'_op', null, $this->get_operators());
97 $objs['value'] = $mform->createElement('text', $this->_name, null);
98 $objs['field']->setLabel(get_string('profilefilterfield', 'filters'));
99 $objs['op']->setLabel(get_string('profilefilterlimiter', 'filters'));
100 $objs['value']->setLabel(get_string('valuefor', 'filters', $this->_label));
cd1edf9e 101 $grp =& $mform->addElement('group', $this->_name.'_grp', $this->_label, $objs, '', false);
a8655ae7 102 $mform->setType($this->_name, PARAM_RAW);
cd1edf9e 103 if ($this->_advanced) {
104 $mform->setAdvanced($this->_name.'_grp');
105 }
c3260b07 106 }
cd1edf9e 107
c3260b07 108 /**
109 * Retrieves data from the form data
110 * @param object $formdata data submited with the form
cd1edf9e 111 * @return mixed array filter data or false when filter not set
c3260b07 112 */
a2ed6e69
SH
113 public function check_data($formdata) {
114 $profilefields = $this->get_profile_fields();
1b452baf 115
a2ed6e69 116 if (empty($profilefields)) {
1b452baf 117 return false;
118 }
119
cd1edf9e 120 $field = $this->_name;
121 $operator = $field.'_op';
122 $profile = $field.'_fld';
123
124 if (array_key_exists($profile, $formdata)) {
125 if ($formdata->$operator < 5 and $formdata->$field === '') {
126 return false;
127 }
128
129 return array('value' => (string)$formdata->$field,
130 'operator' => (int)$formdata->$operator,
131 'profile' => (int)$formdata->$profile);
132 }
c3260b07 133 }
134
135 /**
136 * Returns the condition to be used with SQL where
cd1edf9e 137 * @param array $data filter settings
624a690b 138 * @return array sql string and $params
c3260b07 139 */
a2ed6e69 140 public function get_sql_filter($data) {
7e60297f 141 global $CFG, $DB;
624a690b 142 static $counter = 0;
143 $name = 'ex_profilefield'.$counter++;
cd1edf9e 144
a2ed6e69
SH
145 $profilefields = $this->get_profile_fields();
146 if (empty($profilefields)) {
1b452baf 147 return '';
148 }
149
cd1edf9e 150 $profile = $data['profile'];
151 $operator = $data['operator'];
624a690b 152 $value = $data['value'];
cd1edf9e 153
624a690b 154 $params = array();
a2ed6e69 155 if (!array_key_exists($profile, $profilefields)) {
624a690b 156 return array('', array());
aa6c1ced 157 }
1b452baf 158
cd1edf9e 159 $where = "";
160 $op = " IN ";
cd1edf9e 161
162 if ($operator < 5 and $value === '') {
163 return '';
c3260b07 164 }
cd1edf9e 165
166 switch($operator) {
a2ed6e69 167 case 0: // Contains.
f63ac65a 168 $where = $DB->sql_like('data', ":$name", false, false);
aa6c1ced 169 $params[$name] = "%$value%";
624a690b 170 break;
a2ed6e69 171 case 1: // Does not contain.
47586394 172 $where = $DB->sql_like('data', ":$name", false, false, true);
aa6c1ced 173 $params[$name] = "%$value%";
624a690b 174 break;
a2ed6e69 175 case 2: // Equal to.
f63ac65a 176 $where = $DB->sql_like('data', ":$name", false, false);
aa6c1ced 177 $params[$name] = "$value";
624a690b 178 break;
a2ed6e69 179 case 3: // Starts with.
f63ac65a 180 $where = $DB->sql_like('data', ":$name", false, false);
aa6c1ced 181 $params[$name] = "$value%";
624a690b 182 break;
a2ed6e69 183 case 4: // Ends with.
f63ac65a 184 $where = $DB->sql_like('data', ":$name", false, false);
aa6c1ced 185 $params[$name] = "%$value";
624a690b 186 break;
a2ed6e69 187 case 5: // Empty.
f63ac65a 188 $where = "data = :$name";
aa6c1ced 189 $params[$name] = "";
624a690b 190 break;
a2ed6e69
SH
191 case 6: // Is not defined.
192 $op = " NOT IN ";
193 break;
194 case 7: // Is defined.
cd1edf9e 195 break;
196 }
197 if ($profile) {
198 if ($where !== '') {
199 $where = " AND $where";
c3260b07 200 }
cd1edf9e 201 $where = "fieldid=$profile $where";
c3260b07 202 }
cd1edf9e 203 if ($where !== '') {
204 $where = "WHERE $where";
c3260b07 205 }
624a690b 206 return array("id $op (SELECT userid FROM {user_info_data} $where)", $params);
c3260b07 207 }
cd1edf9e 208
c3260b07 209 /**
cd1edf9e 210 * Returns a human friendly description of the filter used as label.
211 * @param array $data filter settings
212 * @return string active filter label
c3260b07 213 */
a2ed6e69 214 public function get_label($data) {
1b452baf 215 $operators = $this->get_operators();
a2ed6e69 216 $profilefields = $this->get_profile_fields();
1b452baf 217
a2ed6e69 218 if (empty($profilefields)) {
1b452baf 219 return '';
220 }
cd1edf9e 221
222 $profile = $data['profile'];
223 $operator = $data['operator'];
224 $value = $data['value'];
225
a2ed6e69 226 if (!array_key_exists($profile, $profilefields)) {
1b452baf 227 return '';
aa6c1ced 228 }
1b452baf 229
bf718f50 230 $a = new stdClass();
cd1edf9e 231 $a->label = $this->_label;
232 $a->value = $value;
a2ed6e69 233 $a->profile = $profilefields[$profile];
cd1edf9e 234 $a->operator = $operators[$operator];
235
236 switch($operator) {
a2ed6e69
SH
237 case 0: // Contains.
238 case 1: // Doesn't contain.
239 case 2: // Equal to.
240 case 3: // Starts with.
241 case 4: // Ends with.
cd1edf9e 242 return get_string('profilelabel', 'filters', $a);
a2ed6e69
SH
243 case 5: // Empty.
244 case 6: // Is not defined.
245 case 7: // Is defined.
cd1edf9e 246 return get_string('profilelabelnovalue', 'filters', $a);
c3260b07 247 }
cd1edf9e 248 return '';
c3260b07 249 }
250}