31be94ed61871bef6ae8ef9ff937ca4543eb28b6
[moodle.git] / badges / classes / observer.php
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/>.
17 /**
18  * Local stuff for category enrolment plugin.
19  *
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
23  */
25 defined('MOODLE_INTERNAL') || die();
27 /**
28  * Event observer for badges.
29  */
30 class core_badges_observer {
31     /**
32      * Triggered when 'course_module_completion_updated' event is triggered.
33      *
34      * @param \core\event\course_module_completion_updated $event
35      */
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;
56                         }
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);
64                             }
65                         }
66                     }
67                 }
68             }
69         }
70     }
72     /**
73      * Triggered when 'course_completed' event is triggered.
74      *
75      * @param \core\event\course_completed $event
76      */
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;
94                     }
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);
102                         }
103                     }
104                 }
105             }
106         }
107     }
109     /**
110      * Triggered when 'badge_awarded' event happens.
111      *
112      * @param \core\event\badge_awarded $event event generated when a badge is awarded.
113      */
114     public static function badge_criteria_review(\core\event\badge_awarded $event) {
115         global $DB, $CFG;
117         if (!empty($CFG->enablebadges)) {
118             require_once($CFG->dirroot.'/lib/badgeslib.php');
119             $userid = $event->relateduserid;
121             if ($rs = $DB->get_records('badge_criteria', array('criteriatype' => BADGE_CRITERIA_TYPE_BADGE))) {
122                 foreach ($rs as $r) {
123                     $badge = new badge($r->badgeid);
124                     if (!$badge->is_active() || $badge->is_issued($userid)) {
125                         continue;
126                     }
128                     if ($badge->criteria[BADGE_CRITERIA_TYPE_BADGE]->review($userid)) {
129                         $badge->criteria[BADGE_CRITERIA_TYPE_BADGE]->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);
134                         }
135                     }
136                 }
137             }
138         }
139     }
140     /**
141      * Triggered when 'user_updated' event happens.
142      *
143      * @param \core\event\user_updated $event event generated when user profile is updated.
144      */
145     public static function profile_criteria_review(\core\event\user_updated $event) {
146         global $DB, $CFG;
148         if (!empty($CFG->enablebadges)) {
149             require_once($CFG->dirroot.'/lib/badgeslib.php');
150             $userid = $event->objectid;
152             if ($rs = $DB->get_records('badge_criteria', array('criteriatype' => BADGE_CRITERIA_TYPE_PROFILE))) {
153                 foreach ($rs as $r) {
154                     $badge = new badge($r->badgeid);
155                     if (!$badge->is_active() || $badge->is_issued($userid)) {
156                         continue;
157                     }
159                     if ($badge->criteria[BADGE_CRITERIA_TYPE_PROFILE]->review($userid)) {
160                         $badge->criteria[BADGE_CRITERIA_TYPE_PROFILE]->mark_complete($userid);
162                         if ($badge->criteria[BADGE_CRITERIA_TYPE_OVERALL]->review($userid)) {
163                             $badge->criteria[BADGE_CRITERIA_TYPE_OVERALL]->mark_complete($userid);
164                             $badge->issue($userid);
165                         }
166                     }
167                 }
168             }
169         }
170     }