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['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)) {
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 === '') {
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) {
135 $name = 'ex_profilefield'.$counter++
;
137 $profilefields = $this->get_profile_fields();
138 if (empty($profilefields)) {
142 $profile = $data['profile'];
143 $operator = $data['operator'];
144 $value = $data['value'];
147 if (!array_key_exists($profile, $profilefields)) {
148 return array('', array());
154 if ($operator < 5 and $value === '') {
160 $where = $DB->sql_like('data', ":$name", false, false);
161 $params[$name] = "%$value%";
163 case 1: // Does not contain.
164 $where = $DB->sql_like('data', ":$name", false, false, true);
165 $params[$name] = "%$value%";
168 $where = $DB->sql_like('data', ":$name", false, false);
169 $params[$name] = "$value";
171 case 3: // Starts with.
172 $where = $DB->sql_like('data', ":$name", false, false);
173 $params[$name] = "$value%";
175 case 4: // Ends with.
176 $where = $DB->sql_like('data', ":$name", false, false);
177 $params[$name] = "%$value";
180 $where = "data = :$name";
183 case 6: // Is not defined.
186 case 7: // Is defined.
191 $where = " AND $where";
193 $where = "fieldid=$profile $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)) {
214 $profile = $data['profile'];
215 $operator = $data['operator'];
216 $value = $data['value'];
218 if (!array_key_exists($profile, $profilefields)) {
223 $a->label
= $this->_label
;
225 $a->profile
= $profilefields[$profile];
226 $a->operator
= $operators[$operator];
230 case 1: // Doesn't contain.
232 case 3: // Starts with.
233 case 4: // Ends with.
234 return get_string('profilelabel', 'filters', $a);
236 case 6: // Is not defined.
237 case 7: // Is defined.
238 return get_string('profilelabelnovalue', 'filters', $a);