[ 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 manual 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 29 /** 30 * Manual badge award criteria 31 * 32 */ 33 class award_criteria_manual extends award_criteria { 34 35 /* @var int Criteria [BADGE_CRITERIA_TYPE_MANUAL] */ 36 public $criteriatype = BADGE_CRITERIA_TYPE_MANUAL; 37 38 public $required_param = 'role'; 39 public $optional_params = array(); 40 41 /** 42 * Gets role name. 43 * If no such role exists this function returns null. 44 * 45 * @return string|null 46 */ 47 private function get_role_name($rid) { 48 global $DB, $PAGE; 49 $rec = $DB->get_record('role', array('id' => $rid)); 50 51 if ($rec) { 52 return role_get_name($rec, $PAGE->context, ROLENAME_ALIAS); 53 } else { 54 return null; 55 } 56 } 57 58 /** 59 * Add appropriate new criteria options to the form 60 * 61 */ 62 public function get_options(&$mform) { 63 global $PAGE; 64 $options = ''; 65 $none = true; 66 67 $roles = get_roles_with_capability('moodle/badges:awardbadge', CAP_ALLOW, $PAGE->context); 68 $roleids = array_map(create_function('$o', 'return $o->id;'), $roles); 69 $existing = array(); 70 $missing = array(); 71 72 if ($this->id !== 0) { 73 $existing = array_keys($this->params); 74 $missing = array_diff($existing, $roleids); 75 } 76 77 if (!empty($missing)) { 78 $mform->addElement('header', 'category_errors', get_string('criterror', 'badges')); 79 $mform->addHelpButton('category_errors', 'criterror', 'badges'); 80 foreach ($missing as $m) { 81 $this->config_options($mform, array('id' => $m, 'checked' => true, 'name' => get_string('error:nosuchrole', 'badges'), 'error' => true)); 82 $none = false; 83 } 84 } 85 86 if (!empty($roleids)) { 87 $mform->addElement('header', 'first_header', $this->get_title()); 88 $mform->addHelpButton('first_header', 'criteria_' . $this->criteriatype, 'badges'); 89 foreach ($roleids as $rid) { 90 $checked = false; 91 if (in_array($rid, $existing)) { 92 $checked = true; 93 } 94 $this->config_options($mform, array('id' => $rid, 'checked' => $checked, 'name' => self::get_role_name($rid), 'error' => false)); 95 $none = false; 96 } 97 } 98 99 // Add aggregation. 100 if (!$none) { 101 $mform->addElement('header', 'aggregation', get_string('method', 'badges')); 102 $agg = array(); 103 $agg[] =& $mform->createElement('radio', 'agg', '', get_string('allmethodmanual', 'badges'), 1); 104 $agg[] =& $mform->createElement('static', 'none_break', null, '<br/>'); 105 $agg[] =& $mform->createElement('radio', 'agg', '', get_string('anymethodmanual', 'badges'), 2); 106 $mform->addGroup($agg, 'methodgr', '', array(' '), false); 107 if ($this->id !== 0) { 108 $mform->setDefault('agg', $this->method); 109 } else { 110 $mform->setDefault('agg', BADGE_CRITERIA_AGGREGATION_ANY); 111 } 112 } 113 114 return array($none, get_string('noparamstoadd', 'badges')); 115 } 116 117 /** 118 * Get criteria details for displaying to users 119 * 120 * @return string 121 */ 122 public function get_details($short = '') { 123 global $OUTPUT; 124 $output = array(); 125 foreach ($this->params as $p) { 126 $str = self::get_role_name($p['role']); 127 if (!$str) { 128 $output[] = $OUTPUT->error_text(get_string('error:nosuchrole', 'badges')); 129 } else { 130 $output[] = $str; 131 } 132 } 133 134 if ($short) { 135 return implode(', ', $output); 136 } else { 137 return html_writer::alist($output, array(), 'ul'); 138 } 139 } 140 141 /** 142 * Review this criteria and decide if it has been completed 143 * 144 * @param int $userid User whose criteria completion needs to be reviewed. 145 * @param bool $filtered An additional parameter indicating that user list 146 * has been reduced and some expensive checks can be skipped. 147 * 148 * @return bool Whether criteria is complete 149 */ 150 public function review($userid, $filtered = false) { 151 global $DB; 152 153 // Roles should always have a parameter. 154 if (empty($this->params)) { 155 return false; 156 } 157 158 // Users were already filtered by criteria completion. 159 if ($filtered) { 160 return true; 161 } 162 163 $overall = false; 164 foreach ($this->params as $param) { 165 $crit = $DB->get_record('badge_manual_award', array('issuerrole' => $param['role'], 'recipientid' => $userid, 'badgeid' => $this->badgeid)); 166 if ($this->method == BADGE_CRITERIA_AGGREGATION_ALL) { 167 if (!$crit) { 168 return false; 169 } else { 170 $overall = true; 171 continue; 172 } 173 } else { 174 if (!$crit) { 175 $overall = false; 176 continue; 177 } else { 178 return true; 179 } 180 } 181 } 182 return $overall; 183 } 184 185 /** 186 * Returns array with sql code and parameters returning all ids 187 * of users who meet this particular criterion. 188 * 189 * @return array list($join, $where, $params) 190 */ 191 public function get_completed_criteria_sql() { 192 $join = ''; 193 $where = ''; 194 $params = array(); 195 196 if ($this->method == BADGE_CRITERIA_AGGREGATION_ANY) { 197 foreach ($this->params as $param) { 198 $roledata[] = " bma.issuerrole = :issuerrole{$param['role']} "; 199 $params["issuerrole{$param['role']}"] = $param['role']; 200 } 201 if (!empty($roledata)) { 202 $extraon = implode(' OR ', $roledata); 203 $join = " JOIN {badge_manual_award} bma ON bma.recipientid = u.id 204 AND bma.badgeid = :badgeid{$this->badgeid} AND ({$extraon})"; 205 $params["badgeid{$this->badgeid}"] = $this->badgeid; 206 } 207 return array($join, $where, $params); 208 } else { 209 foreach ($this->params as $param) { 210 $join .= " LEFT JOIN {badge_manual_award} bma{$param['role']} ON 211 bma{$param['role']}.recipientid = u.id AND 212 bma{$param['role']}.issuerrole = :issuerrole{$param['role']} "; 213 $where .= " AND bma{$param['role']}.issuerrole IS NOT NULL "; 214 $params["issuerrole{$param['role']}"] = $param['role']; 215 } 216 return array($join, $where, $params); 217 } 218 } 219 220 /** 221 * Delete this criterion 222 * 223 */ 224 public function delete() { 225 global $DB; 226 227 // Remove any records of manual award. 228 $DB->delete_records('badge_manual_award', array('badgeid' => $this->badgeid)); 229 230 parent::delete(); 231 } 232 }
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 |