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
{
10 * operator used for comparison of data
14 * profile field to look at (0 - all fields)
19 * @param string $name the name of the filter instance
20 * @param string $label the label of the filter instance
21 * @param string $field the field used for filtering data
22 * @param string $value the value of the profile field (used for filtering data)
23 * @param int $profile_field id of the profile field to look in
24 * @param int $operator code of the comparison operator
26 function user_filter_profilefield($name, $label, $field='id', $value=null, $profile_field=0, $operator=0) {
27 parent
::user_filter_type($name, $label, $field, $value);
28 $this->_operator
= $operator;
29 $this->_profile_field
= $profile_field;
33 * Returns an array of comparison operators
34 * @return array of comparison operators
36 function getOperators() {
38 get_string('contains', 'filters'),
39 get_string('doesnotcontain','filters'),
40 get_string('isequalto','filters'),
41 get_string('startswith','filters'),
42 get_string('endswith','filters'),
43 get_string('isempty','filters'),
44 get_string('isnotdefined','filters'),
45 get_string('isdefined','filters'),
50 * Returns an array of custom profile fields
51 * @return array of profile fields
53 function getProfileFields() {
54 $fields =& get_records_select('user_info_field', '', 'shortname', 'id,shortname');
58 $res[0] = get_string('anyfield','filters');
59 foreach($fields as $k=>$v) {
60 $res[$k] = $v->shortname
;
66 * Adds controls specific to this filter in the form.
67 * @param object $mform a MoodleForm object to setup
69 function setupForm(&$mform) {
70 $profile_fields =& $this->getProfileFields();
71 if(empty($profile_fields)) {
75 $objs[] =& $mform->createElement('select', $this->_name
. '_fld', null, $profile_fields);
76 $objs[] =& $mform->createElement('select', $this->_name
. '_op', null, $this->getOperators());
77 $objs[] =& $mform->createElement('text', $this->_name
, null);
78 $grp =& $mform->addElement('group', $this->_name
. '_grp', $this->_label
, $objs, '', false);
79 $grp->setHelpButton(array('profilefield','','filters'));
80 $mform->setDefault($this->_name
. '_fld', $this->_profile_field
);
81 $mform->setDefault($this->_name
. '_op', $this->_operator
);
82 $mform->setDefault($this->_name
, $this->_value
);
86 * Retrieves data from the form data
87 * @param object $formdata data submited with the form
89 function checkData($formdata) {
90 $field = $this->_name
;
91 $operator = $field . '_op';
92 $profile_field = $field . '_fld';
93 $this->_value
= (string)@$formdata->$field;
94 $this->_operator
= (int)@$formdata->$operator;
95 $this->_profile_field
= (int)@$formdata->$profile_field;
99 * Returns the condition to be used with SQL where
100 * @return string the filtering condition or null if the filter is disabled
102 function getSQLFilter() {
106 switch($this->_operator
) {
108 if(empty($this->_value
)) {
111 $where = 'data ' . sql_ilike(). ' "%' . $this->_value
. '%"';
113 case 1: // does not contain
114 if(empty($this->_value
)) {
117 $where = 'data NOT ' . sql_ilike(). ' "%' . $this->_value
. '%"';
120 if(empty($this->_value
)) {
123 $where = 'data="' . $this->_value
. '"';
125 case 3: // starts with
126 if(empty($this->_value
)) {
129 $where = 'data ' . sql_ilike(). ' "' . $this->_value
. '%"';
132 if(empty($this->_value
)) {
135 $where = 'data ' . sql_ilike(). ' "%' . $this->_value
. '"';
140 case 6: // is not defined
143 case 7: // is defined
146 if(!empty($this->_profile_field
)) {
148 $where = ' AND ' . $where;
150 $where = 'fieldid=' . $this->_profile_field
. $where;
153 $where = ' WHERE ' . $where;
155 return $this->_field
. $op . "(SELECT userid FROM {$CFG->prefix}user_info_data" . $where . ')';
159 * Returns a human friendly description of the filter.
160 * @return string filter description
162 function getDescription() {
164 $operators =& $this->getOperators();
165 $profilefields =& $this->getProfileFields();
166 switch($this->_operator
) {
168 case 1: // doesn't contain
170 case 3: // starts with
172 $res = $this->_label
. ': '. $profilefields[$this->_profile_field
] . ' ' . $operators[$this->_operator
]. ' "' . stripslashes($this->_value
) . '"';
175 case 6: // is not defined
176 case 7: // is defined
177 $res = $this->_label
. ': '. $profilefields[$this->_profile_field
] . ' ' . $operators[$this->_operator
];