MDL-21695 adding help and link strings
[moodle.git] / user / filters / profilefield.php
bloba286c175c27dde97e2941f446a73bdadf9da7486
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->setHelpButton($this->_name.'_grp', array('profilefield',$this->_label,'filters'));
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 ";
124 $ilike = $DB->sql_ilike();
126 if ($operator < 5 and $value === '') {
127 return '';
130 switch($operator) {
131 case 0: // contains
132 $where = "data $ilike :$name";
133 $params[$name] = "%$value%";
134 break;
135 case 1: // does not contain
136 $where = "data NOT $ilike :$name";
137 $params[$name] = "%$value%";
138 break;
139 case 2: // equal to
140 $where = "data $ilike :$name";
141 $params[$name] = "$value";
142 break;
143 case 3: // starts with
144 $where = "data $ilike :$name";
145 $params[$name] = "$value%";
146 break;
147 case 4: // ends with
148 $where = "data $ilike :$name";
149 $params[$name] = "%$value";
150 break;
151 case 5: // empty
152 $where = "data=:$name";
153 $params[$name] = "";
154 break;
155 case 6: // is not defined
156 $op = " NOT IN "; break;
157 case 7: // is defined
158 break;
160 if ($profile) {
161 if ($where !== '') {
162 $where = " AND $where";
164 $where = "fieldid=$profile $where";
166 if ($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)) {
182 return '';
185 $profile = $data['profile'];
186 $operator = $data['operator'];
187 $value = $data['value'];
189 if (!array_key_exists($profile, $profile_fields)) {
190 return '';
193 $a = new object();
194 $a->label = $this->_label;
195 $a->value = $value;
196 $a->profile = $profile_fields[$profile];
197 $a->operator = $operators[$operator];
199 switch($operator) {
200 case 0: // contains
201 case 1: // doesn't contain
202 case 2: // equal to
203 case 3: // starts with
204 case 4: // ends with
205 return get_string('profilelabel', 'filters', $a);
206 case 5: // empty
207 case 6: // is not defined
208 case 7: // is defined
209 return get_string('profilelabelnovalue', 'filters', $a);
211 return '';