Merge branch 'MDL-50771' of git://github.com/stronk7/moodle
[moodle.git] / user / filters / profilefield.php
blob10278b9575f1f1cc8026f1295cfb618db370cb69
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 user_filter_profilefield($name, $label, $advanced) {
43 parent::user_filter_type($name, $label, $advanced);
46 /**
47 * Returns an array of comparison operators
48 * @return array of comparison operators
50 public function get_operators() {
51 return array(0 => get_string('contains', 'filters'),
52 1 => get_string('doesnotcontain', 'filters'),
53 2 => get_string('isequalto', 'filters'),
54 3 => get_string('startswith', 'filters'),
55 4 => get_string('endswith', 'filters'),
56 5 => get_string('isempty', 'filters'),
57 6 => get_string('isnotdefined', 'filters'),
58 7 => get_string('isdefined', 'filters'));
61 /**
62 * Returns an array of custom profile fields
63 * @return array of profile fields
65 public function get_profile_fields() {
66 global $DB;
67 if (!$fields = $DB->get_records('user_info_field', null, 'shortname', 'id,shortname')) {
68 return null;
70 $res = array(0 => get_string('anyfield', 'filters'));
71 foreach ($fields as $k => $v) {
72 $res[$k] = $v->shortname;
74 return $res;
77 /**
78 * Adds controls specific to this filter in the form.
79 * @param object $mform a MoodleForm object to setup
81 public function setupForm(&$mform) {
82 $profilefields = $this->get_profile_fields();
83 if (empty($profilefields)) {
84 return;
86 $objs = array();
87 $objs['field'] = $mform->createElement('select', $this->_name.'_fld', null, $profilefields);
88 $objs['op'] = $mform->createElement('select', $this->_name.'_op', null, $this->get_operators());
89 $objs['value'] = $mform->createElement('text', $this->_name, null);
90 $objs['field']->setLabel(get_string('profilefilterfield', 'filters'));
91 $objs['op']->setLabel(get_string('profilefilterlimiter', 'filters'));
92 $objs['value']->setLabel(get_string('valuefor', 'filters', $this->_label));
93 $grp =& $mform->addElement('group', $this->_name.'_grp', $this->_label, $objs, '', false);
94 $mform->setType($this->_name, PARAM_RAW);
95 if ($this->_advanced) {
96 $mform->setAdvanced($this->_name.'_grp');
101 * Retrieves data from the form data
102 * @param object $formdata data submited with the form
103 * @return mixed array filter data or false when filter not set
105 public function check_data($formdata) {
106 $profilefields = $this->get_profile_fields();
108 if (empty($profilefields)) {
109 return false;
112 $field = $this->_name;
113 $operator = $field.'_op';
114 $profile = $field.'_fld';
116 if (array_key_exists($profile, $formdata)) {
117 if ($formdata->$operator < 5 and $formdata->$field === '') {
118 return false;
121 return array('value' => (string)$formdata->$field,
122 'operator' => (int)$formdata->$operator,
123 'profile' => (int)$formdata->$profile);
128 * Returns the condition to be used with SQL where
129 * @param array $data filter settings
130 * @return array sql string and $params
132 public function get_sql_filter($data) {
133 global $CFG, $DB;
134 static $counter = 0;
135 $name = 'ex_profilefield'.$counter++;
137 $profilefields = $this->get_profile_fields();
138 if (empty($profilefields)) {
139 return '';
142 $profile = $data['profile'];
143 $operator = $data['operator'];
144 $value = $data['value'];
146 $params = array();
147 if (!array_key_exists($profile, $profilefields)) {
148 return array('', array());
151 $where = "";
152 $op = " IN ";
154 if ($operator < 5 and $value === '') {
155 return '';
158 switch($operator) {
159 case 0: // Contains.
160 $where = $DB->sql_like('data', ":$name", false, false);
161 $params[$name] = "%$value%";
162 break;
163 case 1: // Does not contain.
164 $where = $DB->sql_like('data', ":$name", false, false, true);
165 $params[$name] = "%$value%";
166 break;
167 case 2: // Equal to.
168 $where = $DB->sql_like('data', ":$name", false, false);
169 $params[$name] = "$value";
170 break;
171 case 3: // Starts with.
172 $where = $DB->sql_like('data', ":$name", false, false);
173 $params[$name] = "$value%";
174 break;
175 case 4: // Ends with.
176 $where = $DB->sql_like('data', ":$name", false, false);
177 $params[$name] = "%$value";
178 break;
179 case 5: // Empty.
180 $where = "data = :$name";
181 $params[$name] = "";
182 break;
183 case 6: // Is not defined.
184 $op = " NOT IN ";
185 break;
186 case 7: // Is defined.
187 break;
189 if ($profile) {
190 if ($where !== '') {
191 $where = " AND $where";
193 $where = "fieldid=$profile $where";
195 if ($where !== '') {
196 $where = "WHERE $where";
198 return array("id $op (SELECT userid FROM {user_info_data} $where)", $params);
202 * Returns a human friendly description of the filter used as label.
203 * @param array $data filter settings
204 * @return string active filter label
206 public function get_label($data) {
207 $operators = $this->get_operators();
208 $profilefields = $this->get_profile_fields();
210 if (empty($profilefields)) {
211 return '';
214 $profile = $data['profile'];
215 $operator = $data['operator'];
216 $value = $data['value'];
218 if (!array_key_exists($profile, $profilefields)) {
219 return '';
222 $a = new stdClass();
223 $a->label = $this->_label;
224 $a->value = $value;
225 $a->profile = $profilefields[$profile];
226 $a->operator = $operators[$operator];
228 switch($operator) {
229 case 0: // Contains.
230 case 1: // Doesn't contain.
231 case 2: // Equal to.
232 case 3: // Starts with.
233 case 4: // Ends with.
234 return get_string('profilelabel', 'filters', $a);
235 case 5: // Empty.
236 case 6: // Is not defined.
237 case 7: // Is defined.
238 return get_string('profilelabelnovalue', 'filters', $a);
240 return '';