|
Moodle
2.2.1
http://www.collinsharper.com
|
00001 <?php 00002 00003 require_once($CFG->dirroot.'/user/filters/lib.php'); 00004 00008 class user_filter_profilefield extends user_filter_type { 00009 00016 function user_filter_profilefield($name, $label, $advanced) { 00017 parent::user_filter_type($name, $label, $advanced); 00018 } 00019 00024 function get_operators() { 00025 return array(0 => get_string('contains', 'filters'), 00026 1 => get_string('doesnotcontain','filters'), 00027 2 => get_string('isequalto','filters'), 00028 3 => get_string('startswith','filters'), 00029 4 => get_string('endswith','filters'), 00030 5 => get_string('isempty','filters'), 00031 6 => get_string('isnotdefined','filters'), 00032 7 => get_string('isdefined','filters')); 00033 } 00034 00039 function get_profile_fields() { 00040 global $DB; 00041 if (!$fields = $DB->get_records('user_info_field', null, 'shortname', 'id,shortname')) { 00042 return null; 00043 } 00044 $res = array(0 => get_string('anyfield', 'filters')); 00045 foreach($fields as $k=>$v) { 00046 $res[$k] = $v->shortname; 00047 } 00048 return $res; 00049 } 00050 00055 function setupForm(&$mform) { 00056 $profile_fields = $this->get_profile_fields(); 00057 if (empty($profile_fields)) { 00058 return; 00059 } 00060 $objs = array(); 00061 $objs[] =& $mform->createElement('select', $this->_name.'_fld', null, $profile_fields); 00062 $objs[] =& $mform->createElement('select', $this->_name.'_op', null, $this->get_operators()); 00063 $objs[] =& $mform->createElement('text', $this->_name, null); 00064 $grp =& $mform->addElement('group', $this->_name.'_grp', $this->_label, $objs, '', false); 00065 if ($this->_advanced) { 00066 $mform->setAdvanced($this->_name.'_grp'); 00067 } 00068 } 00069 00075 function check_data($formdata) { 00076 $profile_fields = $this->get_profile_fields(); 00077 00078 if (empty($profile_fields)) { 00079 return false; 00080 } 00081 00082 $field = $this->_name; 00083 $operator = $field.'_op'; 00084 $profile = $field.'_fld'; 00085 00086 if (array_key_exists($profile, $formdata)) { 00087 if ($formdata->$operator < 5 and $formdata->$field === '') { 00088 return false; 00089 } 00090 00091 return array('value' => (string)$formdata->$field, 00092 'operator' => (int)$formdata->$operator, 00093 'profile' => (int)$formdata->$profile); 00094 } 00095 } 00096 00102 function get_sql_filter($data) { 00103 global $CFG, $DB; 00104 static $counter = 0; 00105 $name = 'ex_profilefield'.$counter++; 00106 00107 $profile_fields = $this->get_profile_fields(); 00108 if (empty($profile_fields)) { 00109 return ''; 00110 } 00111 00112 $profile = $data['profile']; 00113 $operator = $data['operator']; 00114 $value = $data['value']; 00115 00116 $params = array(); 00117 if (!array_key_exists($profile, $profile_fields)) { 00118 return array('', array()); 00119 } 00120 00121 $where = ""; 00122 $op = " IN "; 00123 00124 if ($operator < 5 and $value === '') { 00125 return ''; 00126 } 00127 00128 switch($operator) { 00129 case 0: // contains 00130 $where = $DB->sql_like('data', ":$name", false, false); 00131 $params[$name] = "%$value%"; 00132 break; 00133 case 1: // does not contain 00134 $where = $DB->sql_like('data', ":$name", false, false, true); 00135 $params[$name] = "%$value%"; 00136 break; 00137 case 2: // equal to 00138 $where = $DB->sql_like('data', ":$name", false, false); 00139 $params[$name] = "$value"; 00140 break; 00141 case 3: // starts with 00142 $where = $DB->sql_like('data', ":$name", false, false); 00143 $params[$name] = "$value%"; 00144 break; 00145 case 4: // ends with 00146 $where = $DB->sql_like('data', ":$name", false, false); 00147 $params[$name] = "%$value"; 00148 break; 00149 case 5: // empty 00150 $where = "data = :$name"; 00151 $params[$name] = ""; 00152 break; 00153 case 6: // is not defined 00154 $op = " NOT IN "; break; 00155 case 7: // is defined 00156 break; 00157 } 00158 if ($profile) { 00159 if ($where !== '') { 00160 $where = " AND $where"; 00161 } 00162 $where = "fieldid=$profile $where"; 00163 } 00164 if ($where !== '') { 00165 $where = "WHERE $where"; 00166 } 00167 return array("id $op (SELECT userid FROM {user_info_data} $where)", $params); 00168 } 00169 00175 function get_label($data) { 00176 $operators = $this->get_operators(); 00177 $profile_fields = $this->get_profile_fields(); 00178 00179 if (empty($profile_fields)) { 00180 return ''; 00181 } 00182 00183 $profile = $data['profile']; 00184 $operator = $data['operator']; 00185 $value = $data['value']; 00186 00187 if (!array_key_exists($profile, $profile_fields)) { 00188 return ''; 00189 } 00190 00191 $a = new stdClass(); 00192 $a->label = $this->_label; 00193 $a->value = $value; 00194 $a->profile = $profile_fields[$profile]; 00195 $a->operator = $operators[$operator]; 00196 00197 switch($operator) { 00198 case 0: // contains 00199 case 1: // doesn't contain 00200 case 2: // equal to 00201 case 3: // starts with 00202 case 4: // ends with 00203 return get_string('profilelabel', 'filters', $a); 00204 case 5: // empty 00205 case 6: // is not defined 00206 case 7: // is defined 00207 return get_string('profilelabelnovalue', 'filters', $a); 00208 } 00209 return ''; 00210 } 00211 }