Merge branch 'wip-MDL-49439-master' of https://github.com/marinaglancy/moodle
[moodle.git] / badges / classes / observer.php
blob34cf105b9dcc76707df0232e1ddd49658bf980b5
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 /**
28 * Event observer for badges.
30 class core_badges_observer {
31 /**
32 * Triggered when 'course_module_completion_updated' event is triggered.
34 * @param \core\event\course_module_completion_updated $event
36 public static function course_module_criteria_review(\core\event\course_module_completion_updated $event) {
37 global $DB, $CFG;
39 if (!empty($CFG->enablebadges)) {
40 require_once($CFG->dirroot.'/lib/badgeslib.php');
42 $eventdata = $event->get_record_snapshot('course_modules_completion', $event->objectid);
43 $userid = $event->relateduserid;
44 $mod = $event->contextinstanceid;
46 if ($eventdata->completionstate == COMPLETION_COMPLETE
47 || $eventdata->completionstate == COMPLETION_COMPLETE_PASS
48 || $eventdata->completionstate == COMPLETION_COMPLETE_FAIL) {
49 // Need to take into account that there can be more than one badge with the same activity in its criteria.
50 if ($rs = $DB->get_records('badge_criteria_param', array('name' => 'module_' . $mod, 'value' => $mod))) {
51 foreach ($rs as $r) {
52 $bid = $DB->get_field('badge_criteria', 'badgeid', array('id' => $r->critid), MUST_EXIST);
53 $badge = new badge($bid);
54 if (!$badge->is_active() || $badge->is_issued($userid)) {
55 continue;
58 if ($badge->criteria[BADGE_CRITERIA_TYPE_ACTIVITY]->review($userid)) {
59 $badge->criteria[BADGE_CRITERIA_TYPE_ACTIVITY]->mark_complete($userid);
61 if ($badge->criteria[BADGE_CRITERIA_TYPE_OVERALL]->review($userid)) {
62 $badge->criteria[BADGE_CRITERIA_TYPE_OVERALL]->mark_complete($userid);
63 $badge->issue($userid);
72 /**
73 * Triggered when 'course_completed' event is triggered.
75 * @param \core\event\course_completed $event
77 public static function course_criteria_review(\core\event\course_completed $event) {
78 global $DB, $CFG;
80 if (!empty($CFG->enablebadges)) {
81 require_once($CFG->dirroot.'/lib/badgeslib.php');
83 $eventdata = $event->get_record_snapshot('course_completions', $event->objectid);
84 $userid = $event->relateduserid;
85 $courseid = $event->courseid;
87 // Need to take into account that course can be a part of course_completion and courseset_completion criteria.
88 if ($rs = $DB->get_records('badge_criteria_param', array('name' => 'course_' . $courseid, 'value' => $courseid))) {
89 foreach ($rs as $r) {
90 $crit = $DB->get_record('badge_criteria', array('id' => $r->critid), 'badgeid, criteriatype', MUST_EXIST);
91 $badge = new badge($crit->badgeid);
92 if (!$badge->is_active() || $badge->is_issued($userid)) {
93 continue;
96 if ($badge->criteria[$crit->criteriatype]->review($userid)) {
97 $badge->criteria[$crit->criteriatype]->mark_complete($userid);
99 if ($badge->criteria[BADGE_CRITERIA_TYPE_OVERALL]->review($userid)) {
100 $badge->criteria[BADGE_CRITERIA_TYPE_OVERALL]->mark_complete($userid);
101 $badge->issue($userid);
110 * Triggered when 'user_updated' event happens.
112 * @param \core\event\user_updated $event event generated when user profile is updated.
114 public static function profile_criteria_review(\core\event\user_updated $event) {
115 global $DB, $CFG;
117 if (!empty($CFG->enablebadges)) {
118 require_once($CFG->dirroot.'/lib/badgeslib.php');
119 $userid = $event->objectid;
121 if ($rs = $DB->get_records('badge_criteria', array('criteriatype' => BADGE_CRITERIA_TYPE_PROFILE))) {
122 foreach ($rs as $r) {
123 $badge = new badge($r->badgeid);
124 if (!$badge->is_active() || $badge->is_issued($userid)) {
125 continue;
128 if ($badge->criteria[BADGE_CRITERIA_TYPE_PROFILE]->review($userid)) {
129 $badge->criteria[BADGE_CRITERIA_TYPE_PROFILE]->mark_complete($userid);
131 if ($badge->criteria[BADGE_CRITERIA_TYPE_OVERALL]->review($userid)) {
132 $badge->criteria[BADGE_CRITERIA_TYPE_OVERALL]->mark_complete($userid);
133 $badge->issue($userid);