Merge branch 'MDL-68861-master' of git://github.com/vmdef/moodle
[moodle.git] / badges / classes / observer.php
blob511d2f454eabbd92a406a194b611b99001cff1a7
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.
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/>.
17 /**
18 * Local stuff for category enrolment plugin.
20 * @package core_badges
21 * @copyright 2013 Rajesh Taneja <rajesh@moodle.com>
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 defined('MOODLE_INTERNAL') || die();
27 use \core_badges\badge;
28 /**
29 * Event observer for badges.
31 class core_badges_observer {
32 /**
33 * Triggered when 'course_module_completion_updated' event is triggered.
35 * @param \core\event\course_module_completion_updated $event
37 public static function course_module_criteria_review(\core\event\course_module_completion_updated $event) {
38 global $DB, $CFG;
40 if (!empty($CFG->enablebadges)) {
41 require_once($CFG->dirroot.'/lib/badgeslib.php');
43 $eventdata = $event->get_record_snapshot('course_modules_completion', $event->objectid);
44 $userid = $event->relateduserid;
45 $mod = $event->contextinstanceid;
47 if ($eventdata->completionstate == COMPLETION_COMPLETE
48 || $eventdata->completionstate == COMPLETION_COMPLETE_PASS
49 || $eventdata->completionstate == COMPLETION_COMPLETE_FAIL) {
50 // Need to take into account that there can be more than one badge with the same activity in its criteria.
51 if ($rs = $DB->get_records('badge_criteria_param', array('name' => 'module_' . $mod, 'value' => $mod))) {
52 foreach ($rs as $r) {
53 $bid = $DB->get_field('badge_criteria', 'badgeid', array('id' => $r->critid), MUST_EXIST);
54 $badge = new badge($bid);
55 if (!$badge->is_active() || $badge->is_issued($userid)) {
56 continue;
59 if ($badge->criteria[BADGE_CRITERIA_TYPE_ACTIVITY]->review($userid)) {
60 $badge->criteria[BADGE_CRITERIA_TYPE_ACTIVITY]->mark_complete($userid);
62 if ($badge->criteria[BADGE_CRITERIA_TYPE_OVERALL]->review($userid)) {
63 $badge->criteria[BADGE_CRITERIA_TYPE_OVERALL]->mark_complete($userid);
64 $badge->issue($userid);
73 /**
74 * Triggered when '\core\event\competency_evidence_created' event is triggered.
76 * @param \core\event\competency_evidence_created $event
78 public static function competency_criteria_review(\core\event\competency_evidence_created $event) {
79 global $DB, $CFG;
81 if (!empty($CFG->enablebadges)) {
82 require_once($CFG->dirroot.'/lib/badgeslib.php');
84 if (!get_config('core_competency', 'enabled')) {
85 return;
88 $ucid = $event->other['usercompetencyid'];
89 $cid = $event->other['competencyid'];
90 $userid = $event->relateduserid;
92 if ($rs = $DB->get_records('badge_criteria_param', array('name' => 'competency_' . $cid, 'value' => $cid))) {
93 foreach ($rs as $r) {
94 $crit = $DB->get_record('badge_criteria', array('id' => $r->critid), 'badgeid, criteriatype', MUST_EXIST);
95 $badge = new badge($crit->badgeid);
96 // Only site badges are updated from site competencies.
97 if (!$badge->is_active() || $badge->is_issued($userid)) {
98 continue;
101 if ($badge->criteria[$crit->criteriatype]->review($userid)) {
102 $badge->criteria[$crit->criteriatype]->mark_complete($userid);
104 if ($badge->criteria[BADGE_CRITERIA_TYPE_OVERALL]->review($userid)) {
105 $badge->criteria[BADGE_CRITERIA_TYPE_OVERALL]->mark_complete($userid);
106 $badge->issue($userid);
115 * Triggered when 'course_completed' event is triggered.
117 * @param \core\event\course_completed $event
119 public static function course_criteria_review(\core\event\course_completed $event) {
120 global $DB, $CFG;
122 if (!empty($CFG->enablebadges)) {
123 require_once($CFG->dirroot.'/lib/badgeslib.php');
125 $userid = $event->relateduserid;
126 $courseid = $event->courseid;
128 // Need to take into account that course can be a part of course_completion and courseset_completion criteria.
129 if ($rs = $DB->get_records('badge_criteria_param', array('name' => 'course_' . $courseid, 'value' => $courseid))) {
130 foreach ($rs as $r) {
131 $crit = $DB->get_record('badge_criteria', array('id' => $r->critid), 'badgeid, criteriatype', MUST_EXIST);
132 $badge = new badge($crit->badgeid);
133 if (!$badge->is_active() || $badge->is_issued($userid)) {
134 continue;
137 if ($badge->criteria[$crit->criteriatype]->review($userid)) {
138 $badge->criteria[$crit->criteriatype]->mark_complete($userid);
140 if ($badge->criteria[BADGE_CRITERIA_TYPE_OVERALL]->review($userid)) {
141 $badge->criteria[BADGE_CRITERIA_TYPE_OVERALL]->mark_complete($userid);
142 $badge->issue($userid);
151 * Triggered when 'badge_awarded' event happens.
153 * @param \core\event\badge_awarded $event event generated when a badge is awarded.
155 public static function badge_criteria_review(\core\event\badge_awarded $event) {
156 global $DB, $CFG;
158 if (!empty($CFG->enablebadges)) {
159 require_once($CFG->dirroot.'/lib/badgeslib.php');
160 $userid = $event->relateduserid;
162 if ($rs = $DB->get_records('badge_criteria', array('criteriatype' => BADGE_CRITERIA_TYPE_BADGE))) {
163 foreach ($rs as $r) {
164 $badge = new badge($r->badgeid);
165 if (!$badge->is_active() || $badge->is_issued($userid)) {
166 continue;
169 if ($badge->criteria[BADGE_CRITERIA_TYPE_BADGE]->review($userid)) {
170 $badge->criteria[BADGE_CRITERIA_TYPE_BADGE]->mark_complete($userid);
172 if ($badge->criteria[BADGE_CRITERIA_TYPE_OVERALL]->review($userid)) {
173 $badge->criteria[BADGE_CRITERIA_TYPE_OVERALL]->mark_complete($userid);
174 $badge->issue($userid);
182 * Triggered when 'user_updated' event happens.
184 * @param \core\event\user_updated $event event generated when user profile is updated.
186 public static function profile_criteria_review(\core\event\user_updated $event) {
187 global $DB, $CFG;
189 if (!empty($CFG->enablebadges)) {
190 require_once($CFG->dirroot.'/lib/badgeslib.php');
191 $userid = $event->objectid;
193 if ($rs = $DB->get_records('badge_criteria', array('criteriatype' => BADGE_CRITERIA_TYPE_PROFILE))) {
194 foreach ($rs as $r) {
195 $badge = new badge($r->badgeid);
196 if (!$badge->is_active() || $badge->is_issued($userid)) {
197 continue;
200 if ($badge->criteria[BADGE_CRITERIA_TYPE_PROFILE]->review($userid)) {
201 $badge->criteria[BADGE_CRITERIA_TYPE_PROFILE]->mark_complete($userid);
203 if ($badge->criteria[BADGE_CRITERIA_TYPE_OVERALL]->review($userid)) {
204 $badge->criteria[BADGE_CRITERIA_TYPE_OVERALL]->mark_complete($userid);
205 $badge->issue($userid);
214 * Triggered when the 'cohort_member_added' event happens.
216 * @param \core\event\cohort_member_added $event generated when a user is added to a cohort
218 public static function cohort_criteria_review(\core\event\cohort_member_added $event) {
219 global $DB, $CFG;
221 if (!empty($CFG->enablebadges)) {
222 require_once($CFG->dirroot.'/lib/badgeslib.php');
223 $cohortid = $event->objectid;
224 $userid = $event->relateduserid;
226 // Get relevant badges.
227 $badgesql = "SELECT badgeid
228 FROM {badge_criteria_param} cp
229 JOIN {badge_criteria} c ON cp.critid = c.id
230 WHERE c.criteriatype = ?
231 AND cp.name = ?";
232 $badges = $DB->get_records_sql($badgesql, array(BADGE_CRITERIA_TYPE_COHORT, "cohort_{$cohortid}"));
233 if (empty($badges)) {
234 return;
237 foreach ($badges as $b) {
238 $badge = new badge($b->badgeid);
239 if (!$badge->is_active()) {
240 continue;
242 if ($badge->is_issued($userid)) {
243 continue;
246 if ($badge->criteria[BADGE_CRITERIA_TYPE_COHORT]->review($userid)) {
247 $badge->criteria[BADGE_CRITERIA_TYPE_COHORT]->mark_complete($userid);
249 if ($badge->criteria[BADGE_CRITERIA_TYPE_OVERALL]->review($userid)) {
250 $badge->criteria[BADGE_CRITERIA_TYPE_OVERALL]->mark_complete($userid);
251 $badge->issue($userid);