2 // This file is part of Moodle - http://moodle.org/
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.
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/>.
18 * Profile field filter.
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');
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
{
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);
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'));
62 * Returns an array of custom profile fields
63 * @return array of profile fields
65 public function get_profile_fields() {
67 if (!$fields = $DB->get_records('user_info_field', null, 'shortname', 'id,shortname')) {
70 $res = array(0 => get_string('anyfield', 'filters'));
71 foreach ($fields as $k => $v) {
72 $res[$k] = $v->shortname
;
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)) {
87 $objs[] = $mform->createElement('select', $this->_name
.'_fld', null, $profilefields);
88 $objs[] = $mform->createElement('select', $this->_name
.'_op', null, $this->get_operators());
89 $objs[] = $mform->createElement('text', $this->_name
, null);
90 $grp =& $mform->addElement('group', $this->_name
.'_grp', $this->_label
, $objs, '', false);
91 $mform->setType($this->_name
, PARAM_RAW
);
92 if ($this->_advanced
) {
93 $mform->setAdvanced($this->_name
.'_grp');
98 * Retrieves data from the form data
99 * @param object $formdata data submited with the form
100 * @return mixed array filter data or false when filter not set
102 public function check_data($formdata) {
103 $profilefields = $this->get_profile_fields();
105 if (empty($profilefields)) {
109 $field = $this->_name
;
110 $operator = $field.'_op';
111 $profile = $field.'_fld';
113 if (array_key_exists($profile, $formdata)) {
114 if ($formdata->$operator < 5 and $formdata->$field === '') {
118 return array('value' => (string)$formdata->$field,
119 'operator' => (int)$formdata->$operator,
120 'profile' => (int)$formdata->$profile);
125 * Returns the condition to be used with SQL where
126 * @param array $data filter settings
127 * @return array sql string and $params
129 public function get_sql_filter($data) {
132 $name = 'ex_profilefield'.$counter++
;
134 $profilefields = $this->get_profile_fields();
135 if (empty($profilefields)) {
139 $profile = $data['profile'];
140 $operator = $data['operator'];
141 $value = $data['value'];
144 if (!array_key_exists($profile, $profilefields)) {
145 return array('', array());
151 if ($operator < 5 and $value === '') {
157 $where = $DB->sql_like('data', ":$name", false, false);
158 $params[$name] = "%$value%";
160 case 1: // Does not contain.
161 $where = $DB->sql_like('data', ":$name", false, false, true);
162 $params[$name] = "%$value%";
165 $where = $DB->sql_like('data', ":$name", false, false);
166 $params[$name] = "$value";
168 case 3: // Starts with.
169 $where = $DB->sql_like('data', ":$name", false, false);
170 $params[$name] = "$value%";
172 case 4: // Ends with.
173 $where = $DB->sql_like('data', ":$name", false, false);
174 $params[$name] = "%$value";
177 $where = "data = :$name";
180 case 6: // Is not defined.
183 case 7: // Is defined.
188 $where = " AND $where";
190 $where = "fieldid=$profile $where";
193 $where = "WHERE $where";
195 return array("id $op (SELECT userid FROM {user_info_data} $where)", $params);
199 * Returns a human friendly description of the filter used as label.
200 * @param array $data filter settings
201 * @return string active filter label
203 public function get_label($data) {
204 $operators = $this->get_operators();
205 $profilefields = $this->get_profile_fields();
207 if (empty($profilefields)) {
211 $profile = $data['profile'];
212 $operator = $data['operator'];
213 $value = $data['value'];
215 if (!array_key_exists($profile, $profilefields)) {
220 $a->label
= $this->_label
;
222 $a->profile
= $profilefields[$profile];
223 $a->operator
= $operators[$operator];
227 case 1: // Doesn't contain.
229 case 3: // Starts with.
230 case 4: // Ends with.
231 return get_string('profilelabel', 'filters', $a);
233 case 6: // Is not defined.
234 case 7: // Is defined.
235 return get_string('profilelabelnovalue', 'filters', $a);