Differences Between: [Versions 310 and 403] [Versions 311 and 403] [Versions 39 and 403] [Versions 400 and 403]
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 defined('MOODLE_INTERNAL') || die(); 18 19 use \core_badges\badge; 20 21 /** 22 * Event observer for badges. 23 * 24 * @package core_badges 25 * @copyright 2013 Rajesh Taneja <rajesh@moodle.com> 26 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 27 */ 28 class core_badges_observer { 29 /** 30 * Triggered when 'course_module_completion_updated' event is triggered. 31 * 32 * @param \core\event\course_module_completion_updated $event 33 */ 34 public static function course_module_criteria_review(\core\event\course_module_completion_updated $event) { 35 global $DB, $CFG; 36 37 if (!empty($CFG->enablebadges)) { 38 require_once($CFG->dirroot.'/lib/badgeslib.php'); 39 40 $eventdata = $event->get_record_snapshot('course_modules_completion', $event->objectid); 41 $userid = $event->relateduserid; 42 $mod = $event->contextinstanceid; 43 44 if ($eventdata->completionstate == COMPLETION_COMPLETE 45 || $eventdata->completionstate == COMPLETION_COMPLETE_PASS 46 || $eventdata->completionstate == COMPLETION_COMPLETE_FAIL) { 47 // Need to take into account that there can be more than one badge with the same activity in its criteria. 48 if ($rs = $DB->get_records('badge_criteria_param', array('name' => 'module_' . $mod, 'value' => $mod))) { 49 foreach ($rs as $r) { 50 $bid = $DB->get_field('badge_criteria', 'badgeid', array('id' => $r->critid), MUST_EXIST); 51 $badge = new badge($bid); 52 if (!$badge->is_active() || $badge->is_issued($userid)) { 53 continue; 54 } 55 56 if ($badge->criteria[BADGE_CRITERIA_TYPE_ACTIVITY]->review($userid)) { 57 $badge->criteria[BADGE_CRITERIA_TYPE_ACTIVITY]->mark_complete($userid); 58 59 if ($badge->criteria[BADGE_CRITERIA_TYPE_OVERALL]->review($userid)) { 60 $badge->criteria[BADGE_CRITERIA_TYPE_OVERALL]->mark_complete($userid); 61 $badge->issue($userid); 62 } 63 } 64 } 65 } 66 } 67 } 68 } 69 70 /** 71 * Triggered when '\core\event\competency_evidence_created' event is triggered. 72 * 73 * @param \core\event\competency_evidence_created $event 74 */ 75 public static function competency_criteria_review(\core\event\competency_evidence_created $event) { 76 global $DB, $CFG; 77 78 if (!empty($CFG->enablebadges)) { 79 require_once($CFG->dirroot.'/lib/badgeslib.php'); 80 81 if (!get_config('core_competency', 'enabled')) { 82 return; 83 } 84 85 $ucid = $event->other['usercompetencyid']; 86 $cid = $event->other['competencyid']; 87 $userid = $event->relateduserid; 88 89 if ($rs = $DB->get_records('badge_criteria_param', array('name' => 'competency_' . $cid, 'value' => $cid))) { 90 foreach ($rs as $r) { 91 $crit = $DB->get_record('badge_criteria', array('id' => $r->critid), 'badgeid, criteriatype', MUST_EXIST); 92 $badge = new badge($crit->badgeid); 93 // Only site badges are updated from site competencies. 94 if (!$badge->is_active() || $badge->is_issued($userid)) { 95 continue; 96 } 97 98 if ($badge->criteria[$crit->criteriatype]->review($userid)) { 99 $badge->criteria[$crit->criteriatype]->mark_complete($userid); 100 101 if ($badge->criteria[BADGE_CRITERIA_TYPE_OVERALL]->review($userid)) { 102 $badge->criteria[BADGE_CRITERIA_TYPE_OVERALL]->mark_complete($userid); 103 $badge->issue($userid); 104 } 105 } 106 } 107 } 108 } 109 } 110 111 /** 112 * Triggered when 'course_completed' event is triggered. 113 * 114 * @param \core\event\course_completed $event 115 */ 116 public static function course_criteria_review(\core\event\course_completed $event) { 117 global $DB, $CFG; 118 119 if (!empty($CFG->enablebadges)) { 120 require_once($CFG->dirroot.'/lib/badgeslib.php'); 121 122 $userid = $event->relateduserid; 123 $courseid = $event->courseid; 124 125 // Need to take into account that course can be a part of course_completion and courseset_completion criteria. 126 if ($rs = $DB->get_records('badge_criteria_param', array('name' => 'course_' . $courseid, 'value' => $courseid))) { 127 foreach ($rs as $r) { 128 $crit = $DB->get_record('badge_criteria', array('id' => $r->critid), 'badgeid, criteriatype', MUST_EXIST); 129 $badge = new badge($crit->badgeid); 130 if (!$badge->is_active() || $badge->is_issued($userid)) { 131 continue; 132 } 133 134 if ($badge->criteria[$crit->criteriatype]->review($userid)) { 135 $badge->criteria[$crit->criteriatype]->mark_complete($userid); 136 137 if ($badge->criteria[BADGE_CRITERIA_TYPE_OVERALL]->review($userid)) { 138 $badge->criteria[BADGE_CRITERIA_TYPE_OVERALL]->mark_complete($userid); 139 $badge->issue($userid); 140 } 141 } 142 } 143 } 144 } 145 } 146 147 /** 148 * Triggered when 'badge_awarded' event happens. 149 * 150 * @param \core\event\badge_awarded $event event generated when a badge is awarded. 151 */ 152 public static function badge_criteria_review(\core\event\badge_awarded $event) { 153 global $DB, $CFG; 154 155 if (!empty($CFG->enablebadges)) { 156 require_once($CFG->dirroot.'/lib/badgeslib.php'); 157 $userid = $event->relateduserid; 158 159 if ($rs = $DB->get_records('badge_criteria', array('criteriatype' => BADGE_CRITERIA_TYPE_BADGE))) { 160 foreach ($rs as $r) { 161 $badge = new badge($r->badgeid); 162 if (!$badge->is_active() || $badge->is_issued($userid)) { 163 continue; 164 } 165 166 if ($badge->criteria[BADGE_CRITERIA_TYPE_BADGE]->review($userid)) { 167 $badge->criteria[BADGE_CRITERIA_TYPE_BADGE]->mark_complete($userid); 168 169 if ($badge->criteria[BADGE_CRITERIA_TYPE_OVERALL]->review($userid)) { 170 $badge->criteria[BADGE_CRITERIA_TYPE_OVERALL]->mark_complete($userid); 171 $badge->issue($userid); 172 } 173 } 174 } 175 } 176 } 177 } 178 /** 179 * Triggered when 'user_updated' event happens. 180 * 181 * @param \core\event\user_updated $event event generated when user profile is updated. 182 */ 183 public static function profile_criteria_review(\core\event\user_updated $event) { 184 global $DB, $CFG; 185 186 if (!empty($CFG->enablebadges)) { 187 require_once($CFG->dirroot.'/lib/badgeslib.php'); 188 $userid = $event->objectid; 189 190 if ($rs = $DB->get_records('badge_criteria', array('criteriatype' => BADGE_CRITERIA_TYPE_PROFILE))) { 191 foreach ($rs as $r) { 192 $badge = new badge($r->badgeid); 193 if (!$badge->is_active() || $badge->is_issued($userid)) { 194 continue; 195 } 196 197 if ($badge->criteria[BADGE_CRITERIA_TYPE_PROFILE]->review($userid)) { 198 $badge->criteria[BADGE_CRITERIA_TYPE_PROFILE]->mark_complete($userid); 199 200 if ($badge->criteria[BADGE_CRITERIA_TYPE_OVERALL]->review($userid)) { 201 $badge->criteria[BADGE_CRITERIA_TYPE_OVERALL]->mark_complete($userid); 202 $badge->issue($userid); 203 } 204 } 205 } 206 } 207 } 208 } 209 210 /** 211 * Triggered when the 'cohort_member_added' event happens. 212 * 213 * @param \core\event\cohort_member_added $event generated when a user is added to a cohort 214 */ 215 public static function cohort_criteria_review(\core\event\cohort_member_added $event) { 216 global $DB, $CFG; 217 218 if (!empty($CFG->enablebadges)) { 219 require_once($CFG->dirroot.'/lib/badgeslib.php'); 220 $cohortid = $event->objectid; 221 $userid = $event->relateduserid; 222 223 // Get relevant badges. 224 $badgesql = "SELECT badgeid 225 FROM {badge_criteria_param} cp 226 JOIN {badge_criteria} c ON cp.critid = c.id 227 WHERE c.criteriatype = ? 228 AND cp.name = ?"; 229 $badges = $DB->get_records_sql($badgesql, array(BADGE_CRITERIA_TYPE_COHORT, "cohort_{$cohortid}")); 230 if (empty($badges)) { 231 return; 232 } 233 234 foreach ($badges as $b) { 235 $badge = new badge($b->badgeid); 236 if (!$badge->is_active()) { 237 continue; 238 } 239 if ($badge->is_issued($userid)) { 240 continue; 241 } 242 243 if ($badge->criteria[BADGE_CRITERIA_TYPE_COHORT]->review($userid)) { 244 $badge->criteria[BADGE_CRITERIA_TYPE_COHORT]->mark_complete($userid); 245 246 if ($badge->criteria[BADGE_CRITERIA_TYPE_OVERALL]->review($userid)) { 247 $badge->criteria[BADGE_CRITERIA_TYPE_OVERALL]->mark_complete($userid); 248 $badge->issue($userid); 249 } 250 } 251 } 252 } 253 } 254 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body