3 require_once($CFG->dirroot
.'/user/filters/lib.php');
6 * User filter based on values of custom profile fields.
8 class user_filter_profilefield
extends user_filter_type
{
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);
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'));
36 * Returns an array of custom profile fields
37 * @return array of profile fields
39 function get_profile_fields() {
41 if (!$fields = $DB->get_records('user_info_field', null, 'shortname', 'id,shortname')) {
44 $res = array(0 => get_string('anyfield', 'filters'));
45 foreach($fields as $k=>$v) {
46 $res[$k] = $v->shortname
;
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)) {
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->setHelpButton($this->_name
.'_grp', array('profilefield',$this->_label
,'filters'));
66 if ($this->_advanced
) {
67 $mform->setAdvanced($this->_name
.'_grp');
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)) {
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 === '') {
92 return array('value' => (string)$formdata->$field,
93 'operator' => (int)$formdata->$operator,
94 'profile' => (int)$formdata->$profile);
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) {
106 $name = 'ex_profilefield'.$counter++
;
108 $profile_fields = $this->get_profile_fields();
109 if (empty($profile_fields)) {
113 $profile = $data['profile'];
114 $operator = $data['operator'];
115 $value = $data['value'];
118 if (!array_key_exists($profile, $profile_fields)) {
119 return array('', array());
124 $ilike = $DB->sql_ilike();
126 if ($operator < 5 and $value === '') {
132 $where = "data $ilike :$name";
133 $params[$name] = "%$value%";
135 case 1: // does not contain
136 $where = "data NOT $ilike :$name";
137 $params[$name] = "%$value%";
140 $where = "data $ilike :$name";
141 $params[$name] = "$value";
143 case 3: // starts with
144 $where = "data $ilike :$name";
145 $params[$name] = "$value%";
148 $where = "data $ilike :$name";
149 $params[$name] = "%$value";
152 $where = "data=:$name";
155 case 6: // is not defined
156 $op = " NOT IN "; break;
157 case 7: // is defined
162 $where = " AND $where";
164 $where = "fieldid=$profile $where";
167 $where = "WHERE $where";
169 return array("id $op (SELECT userid FROM {user_info_data} $where)", $params);
173 * Returns a human friendly description of the filter used as label.
174 * @param array $data filter settings
175 * @return string active filter label
177 function get_label($data) {
178 $operators = $this->get_operators();
179 $profile_fields = $this->get_profile_fields();
181 if (empty($profile_fields)) {
185 $profile = $data['profile'];
186 $operator = $data['operator'];
187 $value = $data['value'];
189 if (!array_key_exists($profile, $profile_fields)) {
194 $a->label
= $this->_label
;
196 $a->profile
= $profile_fields[$profile];
197 $a->operator
= $operators[$operator];
201 case 1: // doesn't contain
203 case 3: // starts with
205 return get_string('profilelabel', 'filters', $a);
207 case 6: // is not defined
208 case 7: // is defined
209 return get_string('profilelabelnovalue', 'filters', $a);