MDL-38773 Purge coursecat cache on course restore
[moodle.git] / user / filters / profilefield.php
blobbd22fd7f8ea0f215584a56fe41202b9eeab3664a
1 <?php
3 require_once($CFG->dirroot.'/user/filters/lib.php');
5 /**
6 * User filter based on values of custom profile fields.
7 */
8 class user_filter_profilefield extends user_filter_type {
10 /**
11 * Constructor
12 * @param string $name the name of the filter instance
13 * @param string $label the label of the filter instance
14 * @param boolean $advanced advanced form element flag
16 function user_filter_profilefield($name, $label, $advanced) {
17 parent::user_filter_type($name, $label, $advanced);
20 /**
21 * Returns an array of comparison operators
22 * @return array of comparison operators
24 function get_operators() {
25 return array(0 => get_string('contains', 'filters'),
26 1 => get_string('doesnotcontain','filters'),
27 2 => get_string('isequalto','filters'),
28 3 => get_string('startswith','filters'),
29 4 => get_string('endswith','filters'),
30 5 => get_string('isempty','filters'),
31 6 => get_string('isnotdefined','filters'),
32 7 => get_string('isdefined','filters'));
35 /**
36 * Returns an array of custom profile fields
37 * @return array of profile fields
39 function get_profile_fields() {
40 global $DB;
41 if (!$fields = $DB->get_records('user_info_field', null, 'shortname', 'id,shortname')) {
42 return null;
44 $res = array(0 => get_string('anyfield', 'filters'));
45 foreach($fields as $k=>$v) {
46 $res[$k] = $v->shortname;
48 return $res;
51 /**
52 * Adds controls specific to this filter in the form.
53 * @param object $mform a MoodleForm object to setup
55 function setupForm(&$mform) {
56 $profile_fields = $this->get_profile_fields();
57 if (empty($profile_fields)) {
58 return;
60 $objs = array();
61 $objs[] =& $mform->createElement('select', $this->_name.'_fld', null, $profile_fields);
62 $objs[] =& $mform->createElement('select', $this->_name.'_op', null, $this->get_operators());
63 $objs[] =& $mform->createElement('text', $this->_name, null);
64 $grp =& $mform->addElement('group', $this->_name.'_grp', $this->_label, $objs, '', false);
65 $mform->setType($this->_name, PARAM_RAW);
66 if ($this->_advanced) {
67 $mform->setAdvanced($this->_name.'_grp');
71 /**
72 * Retrieves data from the form data
73 * @param object $formdata data submited with the form
74 * @return mixed array filter data or false when filter not set
76 function check_data($formdata) {
77 $profile_fields = $this->get_profile_fields();
79 if (empty($profile_fields)) {
80 return false;
83 $field = $this->_name;
84 $operator = $field.'_op';
85 $profile = $field.'_fld';
87 if (array_key_exists($profile, $formdata)) {
88 if ($formdata->$operator < 5 and $formdata->$field === '') {
89 return false;
92 return array('value' => (string)$formdata->$field,
93 'operator' => (int)$formdata->$operator,
94 'profile' => (int)$formdata->$profile);
98 /**
99 * Returns the condition to be used with SQL where
100 * @param array $data filter settings
101 * @return array sql string and $params
103 function get_sql_filter($data) {
104 global $CFG, $DB;
105 static $counter = 0;
106 $name = 'ex_profilefield'.$counter++;
108 $profile_fields = $this->get_profile_fields();
109 if (empty($profile_fields)) {
110 return '';
113 $profile = $data['profile'];
114 $operator = $data['operator'];
115 $value = $data['value'];
117 $params = array();
118 if (!array_key_exists($profile, $profile_fields)) {
119 return array('', array());
122 $where = "";
123 $op = " IN ";
125 if ($operator < 5 and $value === '') {
126 return '';
129 switch($operator) {
130 case 0: // contains
131 $where = $DB->sql_like('data', ":$name", false, false);
132 $params[$name] = "%$value%";
133 break;
134 case 1: // does not contain
135 $where = $DB->sql_like('data', ":$name", false, false, true);
136 $params[$name] = "%$value%";
137 break;
138 case 2: // equal to
139 $where = $DB->sql_like('data', ":$name", false, false);
140 $params[$name] = "$value";
141 break;
142 case 3: // starts with
143 $where = $DB->sql_like('data', ":$name", false, false);
144 $params[$name] = "$value%";
145 break;
146 case 4: // ends with
147 $where = $DB->sql_like('data', ":$name", false, false);
148 $params[$name] = "%$value";
149 break;
150 case 5: // empty
151 $where = "data = :$name";
152 $params[$name] = "";
153 break;
154 case 6: // is not defined
155 $op = " NOT IN "; break;
156 case 7: // is defined
157 break;
159 if ($profile) {
160 if ($where !== '') {
161 $where = " AND $where";
163 $where = "fieldid=$profile $where";
165 if ($where !== '') {
166 $where = "WHERE $where";
168 return array("id $op (SELECT userid FROM {user_info_data} $where)", $params);
172 * Returns a human friendly description of the filter used as label.
173 * @param array $data filter settings
174 * @return string active filter label
176 function get_label($data) {
177 $operators = $this->get_operators();
178 $profile_fields = $this->get_profile_fields();
180 if (empty($profile_fields)) {
181 return '';
184 $profile = $data['profile'];
185 $operator = $data['operator'];
186 $value = $data['value'];
188 if (!array_key_exists($profile, $profile_fields)) {
189 return '';
192 $a = new stdClass();
193 $a->label = $this->_label;
194 $a->value = $value;
195 $a->profile = $profile_fields[$profile];
196 $a->operator = $operators[$operator];
198 switch($operator) {
199 case 0: // contains
200 case 1: // doesn't contain
201 case 2: // equal to
202 case 3: // starts with
203 case 4: // ends with
204 return get_string('profilelabel', 'filters', $a);
205 case 5: // empty
206 case 6: // is not defined
207 case 7: // is defined
208 return get_string('profilelabelnovalue', 'filters', $a);
210 return '';