[ Index ] |
PHP Cross Reference of Unnamed Project |
[Summary view] [Print] [Text view]
1 <?php 2 // This file is part of Moodle - http://moodle.org/ 3 // 4 // Moodle is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU General Public License as published by 6 // the Free Software Foundation, either version 3 of the License, or 7 // (at your option) any later version. 8 // 9 // Moodle is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 // GNU General Public License for more details. 13 // 14 // You should have received a copy of the GNU General Public License 15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>. 16 17 /** 18 * This file contains the profile completion badge award criteria type class 19 * 20 * @package core 21 * @subpackage badges 22 * @copyright 2012 onwards Totara Learning Solutions Ltd {@link http://www.totaralms.com/} 23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 24 * @author Yuliya Bozhko <yuliya.bozhko@totaralms.com> 25 */ 26 27 defined('MOODLE_INTERNAL') || die(); 28 require_once($CFG->dirroot . "/user/lib.php"); 29 30 /** 31 * Profile completion badge award criteria 32 * 33 */ 34 class award_criteria_profile extends award_criteria { 35 36 /* @var int Criteria [BADGE_CRITERIA_TYPE_PROFILE] */ 37 public $criteriatype = BADGE_CRITERIA_TYPE_PROFILE; 38 39 public $required_param = 'field'; 40 public $optional_params = array(); 41 42 /** 43 * Add appropriate new criteria options to the form 44 * 45 */ 46 public function get_options(&$mform) { 47 global $DB; 48 49 $none = true; 50 $existing = array(); 51 $missing = array(); 52 53 // Note: cannot use user_get_default_fields() here because it is not possible to decide which fields user can modify. 54 $dfields = array('firstname', 'lastname', 'email', 'address', 'phone1', 'phone2', 'icq', 'skype', 'yahoo', 55 'aim', 'msn', 'department', 'institution', 'description', 'city', 'url', 'country'); 56 57 $sql = "SELECT uf.id as fieldid, uf.name as name, ic.id as categoryid, ic.name as categoryname, uf.datatype 58 FROM {user_info_field} uf 59 JOIN {user_info_category} ic 60 ON uf.categoryid = ic.id AND uf.visible <> 0 61 ORDER BY ic.sortorder ASC, uf.sortorder ASC"; 62 63 // Get custom fields. 64 $cfields = $DB->get_records_sql($sql); 65 $cfids = array_map(create_function('$o', 'return $o->fieldid;'), $cfields); 66 67 if ($this->id !== 0) { 68 $existing = array_keys($this->params); 69 $missing = array_diff($existing, array_merge($dfields, $cfids)); 70 } 71 72 if (!empty($missing)) { 73 $mform->addElement('header', 'category_errors', get_string('criterror', 'badges')); 74 $mform->addHelpButton('category_errors', 'criterror', 'badges'); 75 foreach ($missing as $m) { 76 $this->config_options($mform, array('id' => $m, 'checked' => true, 'name' => get_string('error:nosuchfield', 'badges'), 'error' => true)); 77 $none = false; 78 } 79 } 80 81 if (!empty($dfields)) { 82 $mform->addElement('header', 'first_header', $this->get_title()); 83 $mform->addHelpButton('first_header', 'criteria_' . $this->criteriatype, 'badges'); 84 foreach ($dfields as $field) { 85 $checked = false; 86 if (in_array($field, $existing)) { 87 $checked = true; 88 } 89 $this->config_options($mform, array('id' => $field, 'checked' => $checked, 'name' => get_user_field_name($field), 'error' => false)); 90 $none = false; 91 } 92 } 93 94 if (!empty($cfields)) { 95 foreach ($cfields as $field) { 96 if (!isset($currentcat) || $currentcat != $field->categoryid) { 97 $currentcat = $field->categoryid; 98 $mform->addElement('header', 'category_' . $currentcat, format_string($field->categoryname)); 99 } 100 $checked = false; 101 if (in_array($field->fieldid, $existing)) { 102 $checked = true; 103 } 104 $this->config_options($mform, array('id' => $field->fieldid, 'checked' => $checked, 'name' => $field->name, 'error' => false)); 105 $none = false; 106 } 107 } 108 109 // Add aggregation. 110 if (!$none) { 111 $mform->addElement('header', 'aggregation', get_string('method', 'badges')); 112 $agg = array(); 113 $agg[] =& $mform->createElement('radio', 'agg', '', get_string('allmethodprofile', 'badges'), 1); 114 $agg[] =& $mform->createElement('static', 'none_break', null, '<br/>'); 115 $agg[] =& $mform->createElement('radio', 'agg', '', get_string('anymethodprofile', 'badges'), 2); 116 $mform->addGroup($agg, 'methodgr', '', array(' '), false); 117 if ($this->id !== 0) { 118 $mform->setDefault('agg', $this->method); 119 } else { 120 $mform->setDefault('agg', BADGE_CRITERIA_AGGREGATION_ANY); 121 } 122 } 123 124 return array($none, get_string('noparamstoadd', 'badges')); 125 } 126 127 /** 128 * Get criteria details for displaying to users 129 * 130 * @return string 131 */ 132 public function get_details($short = '') { 133 global $DB, $OUTPUT; 134 $output = array(); 135 foreach ($this->params as $p) { 136 if (is_numeric($p['field'])) { 137 $str = $DB->get_field('user_info_field', 'name', array('id' => $p['field'])); 138 } else { 139 $str = get_user_field_name($p['field']); 140 } 141 if (!$str) { 142 $output[] = $OUTPUT->error_text(get_string('error:nosuchfield', 'badges')); 143 } else { 144 $output[] = $str; 145 } 146 } 147 148 if ($short) { 149 return implode(', ', $output); 150 } else { 151 return html_writer::alist($output, array(), 'ul'); 152 } 153 } 154 155 /** 156 * Review this criteria and decide if it has been completed 157 * 158 * @param int $userid User whose criteria completion needs to be reviewed. 159 * @param bool $filtered An additional parameter indicating that user list 160 * has been reduced and some expensive checks can be skipped. 161 * 162 * @return bool Whether criteria is complete 163 */ 164 public function review($userid, $filtered = false) { 165 global $DB; 166 167 // Users were already filtered by criteria completion, no checks required. 168 if ($filtered) { 169 return true; 170 } 171 172 $join = ''; 173 $whereparts = array(); 174 $sqlparams = array(); 175 $rule = ($this->method == BADGE_CRITERIA_AGGREGATION_ANY) ? ' OR ' : ' AND '; 176 177 foreach ($this->params as $param) { 178 if (is_numeric($param['field'])) { 179 // This is a custom field. 180 $idx = count($whereparts) + 1; 181 $join .= " LEFT JOIN {user_info_data} uid{$idx} ON uid{$idx}.userid = u.id AND uid{$idx}.fieldid = :fieldid{$idx} "; 182 $sqlparams["fieldid{$idx}"] = $param['field']; 183 $whereparts[] = "uid{$idx}.id IS NOT NULL"; 184 } else { 185 // This is a field from {user} table. 186 $whereparts[] = $DB->sql_isnotempty('u', "u.{$param['field']}", false, true); 187 } 188 } 189 190 $sqlparams['userid'] = $userid; 191 192 if ($whereparts) { 193 $where = " AND (" . implode($rule, $whereparts) . ")"; 194 } else { 195 $where = ''; 196 } 197 $sql = "SELECT 1 FROM {user} u " . $join . " WHERE u.id = :userid $where"; 198 $overall = $DB->record_exists_sql($sql, $sqlparams); 199 200 return $overall; 201 } 202 203 /** 204 * Returns array with sql code and parameters returning all ids 205 * of users who meet this particular criterion. 206 * 207 * @return array list($join, $where, $params) 208 */ 209 public function get_completed_criteria_sql() { 210 global $DB; 211 212 $join = ''; 213 $whereparts = array(); 214 $params = array(); 215 $rule = ($this->method == BADGE_CRITERIA_AGGREGATION_ANY) ? ' OR ' : ' AND '; 216 217 foreach ($this->params as $param) { 218 if (is_numeric($param['field'])) { 219 // This is a custom field. 220 $idx = count($whereparts); 221 $join .= " LEFT JOIN {user_info_data} uid{$idx} ON uid{$idx}.userid = u.id AND uid{$idx}.fieldid = :fieldid{$idx} "; 222 $params["fieldid{$idx}"] = $param['field']; 223 $whereparts[] = "uid{$idx}.id IS NOT NULL"; 224 } else { 225 $whereparts[] = $DB->sql_isnotempty('u', "u.{$param['field']}", false, true); 226 } 227 } 228 229 if ($whereparts) { 230 $where = " AND (" . implode($rule, $whereparts) . ")"; 231 } else { 232 $where = ''; 233 } 234 return array($join, $where, $params); 235 } 236 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Thu Aug 11 10:00:09 2016 | Cross-referenced by PHPXref 0.7.1 |