MDL-72626 lib: Upgrade RTLCSS to 1.0.1
[moodle.git] / user / filters / profilefield.php
blob328ad70c0bcb7ca89d4c271ea2a2cdaee3973978
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 * Profile field 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 * User filter based on values of custom profile fields.
31 * @copyright 1999 Martin Dougiamas http://dougiamas.com
32 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
34 class user_filter_profilefield extends user_filter_type {
36 /**
37 * Constructor
38 * @param string $name the name of the filter instance
39 * @param string $label the label of the filter instance
40 * @param boolean $advanced advanced form element flag
42 public function __construct($name, $label, $advanced) {
43 parent::__construct($name, $label, $advanced);
46 /**
47 * Old syntax of class constructor. Deprecated in PHP7.
49 * @deprecated since Moodle 3.1
51 public function user_filter_profilefield($name, $label, $advanced) {
52 debugging('Use of class name as constructor is deprecated', DEBUG_DEVELOPER);
53 self::__construct($name, $label, $advanced);
56 /**
57 * Returns an array of comparison operators
58 * @return array of comparison operators
60 public function get_operators() {
61 return array(0 => get_string('contains', 'filters'),
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'));
71 /**
72 * Returns an array of custom profile fields
73 * @return array of profile fields
75 public function get_profile_fields() {
76 global $CFG;
77 require_once($CFG->dirroot . '/user/profile/lib.php');
79 $fieldrecords = profile_get_custom_fields();
80 $fields = array_combine(array_keys($fieldrecords), array_column($fieldrecords, 'name'));
81 core_collator::asort($fields);
82 $res = array(0 => get_string('anyfield', 'filters'));
84 return $res + $fields;
87 /**
88 * Adds controls specific to this filter in the form.
89 * @param object $mform a MoodleForm object to setup
91 public function setupForm(&$mform) {
92 $profilefields = $this->get_profile_fields();
93 if (empty($profilefields)) {
94 return;
96 $objs = array();
97 $objs['field'] = $mform->createElement('select', $this->_name.'_fld', null, $profilefields);
98 $objs['op'] = $mform->createElement('select', $this->_name.'_op', null, $this->get_operators());
99 $objs['value'] = $mform->createElement('text', $this->_name, null);
100 $objs['field']->setLabel(get_string('profilefilterfield', 'filters'));
101 $objs['op']->setLabel(get_string('profilefilterlimiter', 'filters'));
102 $objs['value']->setLabel(get_string('valuefor', 'filters', $this->_label));
103 $grp =& $mform->addElement('group', $this->_name.'_grp', $this->_label, $objs, '', false);
104 $mform->setType($this->_name, PARAM_RAW);
105 if ($this->_advanced) {
106 $mform->setAdvanced($this->_name.'_grp');
111 * Retrieves data from the form data
112 * @param object $formdata data submited with the form
113 * @return mixed array filter data or false when filter not set
115 public function check_data($formdata) {
116 $profilefields = $this->get_profile_fields();
118 if (empty($profilefields)) {
119 return false;
122 $field = $this->_name;
123 $operator = $field.'_op';
124 $profile = $field.'_fld';
126 if (property_exists($formdata, $profile)) {
127 if ($formdata->$operator < 5 and $formdata->$field === '') {
128 return false;
131 return array('value' => (string)$formdata->$field,
132 'operator' => (int)$formdata->$operator,
133 'profile' => (int)$formdata->$profile);
138 * Returns the condition to be used with SQL where
139 * @param array $data filter settings
140 * @return array sql string and $params
142 public function get_sql_filter($data) {
143 global $CFG, $DB;
144 static $counter = 0;
145 $name = 'ex_profilefield'.$counter++;
147 $profilefields = $this->get_profile_fields();
148 if (empty($profilefields)) {
149 return '';
152 $profile = $data['profile'];
153 $operator = $data['operator'];
154 $value = $data['value'];
156 $params = array();
157 if (!array_key_exists($profile, $profilefields)) {
158 return array('', array());
161 $where = "";
162 $op = " IN ";
164 if ($operator < 5 and $value === '') {
165 return '';
168 switch($operator) {
169 case 0: // Contains.
170 $where = $DB->sql_like('data', ":$name", false, false);
171 $params[$name] = "%$value%";
172 break;
173 case 1: // Does not contain.
174 $where = $DB->sql_like('data', ":$name", false, false, true);
175 $params[$name] = "%$value%";
176 break;
177 case 2: // Equal to.
178 $where = $DB->sql_like('data', ":$name", false, false);
179 $params[$name] = "$value";
180 break;
181 case 3: // Starts with.
182 $where = $DB->sql_like('data', ":$name", false, false);
183 $params[$name] = "$value%";
184 break;
185 case 4: // Ends with.
186 $where = $DB->sql_like('data', ":$name", false, false);
187 $params[$name] = "%$value";
188 break;
189 case 5: // Empty.
190 $where = "data = :$name";
191 $params[$name] = "";
192 break;
193 case 6: // Is not defined.
194 $op = " NOT IN ";
195 break;
196 case 7: // Is defined.
197 break;
199 if ($profile) {
200 if ($where !== '') {
201 $where = " AND $where";
203 $where = "fieldid=$profile $where";
205 if ($where !== '') {
206 $where = "WHERE $where";
208 return array("id $op (SELECT userid FROM {user_info_data} $where)", $params);
212 * Returns a human friendly description of the filter used as label.
213 * @param array $data filter settings
214 * @return string active filter label
216 public function get_label($data) {
217 $operators = $this->get_operators();
218 $profilefields = $this->get_profile_fields();
220 if (empty($profilefields)) {
221 return '';
224 $profile = $data['profile'];
225 $operator = $data['operator'];
226 $value = $data['value'];
228 if (!array_key_exists($profile, $profilefields)) {
229 return '';
232 $a = new stdClass();
233 $a->label = $this->_label;
234 $a->value = $value;
235 $a->profile = $profilefields[$profile];
236 $a->operator = $operators[$operator];
238 switch($operator) {
239 case 0: // Contains.
240 case 1: // Doesn't contain.
241 case 2: // Equal to.
242 case 3: // Starts with.
243 case 4: // Ends with.
244 return get_string('profilelabel', 'filters', $a);
245 case 5: // Empty.
246 case 6: // Is not defined.
247 case 7: // Is defined.
248 return get_string('profilelabelnovalue', 'filters', $a);
250 return '';