1 |
efrain |
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 |
* Provides the {@see mod_subcourse\observers} class.
|
|
|
19 |
*
|
|
|
20 |
* @package mod_subcourse
|
|
|
21 |
* @copyright 2014 Vadim Dvorovenko (Vadimon@mail.ru)
|
|
|
22 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
23 |
*/
|
|
|
24 |
|
|
|
25 |
namespace mod_subcourse;
|
|
|
26 |
|
|
|
27 |
use completion_info;
|
|
|
28 |
|
|
|
29 |
defined('MOODLE_INTERNAL') || die();
|
|
|
30 |
|
|
|
31 |
require_once($CFG->dirroot . '/mod/subcourse/locallib.php');
|
|
|
32 |
|
|
|
33 |
/**
|
|
|
34 |
* Implements the module's event observers.
|
|
|
35 |
*
|
|
|
36 |
* @copyright 2017 David Mudrak <david@moodle.com>
|
|
|
37 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
38 |
*/
|
|
|
39 |
class observers {
|
|
|
40 |
|
|
|
41 |
/**
|
|
|
42 |
* User graded
|
|
|
43 |
*
|
|
|
44 |
* @param \core\event\user_graded $event
|
|
|
45 |
* @return void
|
|
|
46 |
*/
|
|
|
47 |
public static function user_graded(\core\event\user_graded $event) {
|
|
|
48 |
global $DB;
|
|
|
49 |
|
|
|
50 |
$courseid = $event->courseid;
|
|
|
51 |
$userid = $event->relateduserid;
|
|
|
52 |
|
|
|
53 |
$subcourses = $DB->get_records('subcourse', ['refcourse' => $courseid], '', 'id, course, refcourse, fetchpercentage');
|
|
|
54 |
|
|
|
55 |
foreach ($subcourses as $subcourse) {
|
|
|
56 |
subcourse_grades_update($subcourse->course, $subcourse->id, $subcourse->refcourse,
|
|
|
57 |
null, false, false, $userid, $subcourse->fetchpercentage);
|
|
|
58 |
}
|
|
|
59 |
}
|
|
|
60 |
|
|
|
61 |
/**
|
|
|
62 |
* Role assigned
|
|
|
63 |
*
|
|
|
64 |
* Every time new user is enrolled into course we should fetch all subcourses again,
|
|
|
65 |
* because user can be previously enrolled into subcourse
|
|
|
66 |
*
|
|
|
67 |
* @param \core\event\role_assigned $event
|
|
|
68 |
* @return void
|
|
|
69 |
*/
|
|
|
70 |
public static function role_assigned(\core\event\role_assigned $event) {
|
|
|
71 |
global $DB;
|
|
|
72 |
|
|
|
73 |
$courseid = $event->courseid;
|
|
|
74 |
$userid = $event->relateduserid;
|
|
|
75 |
|
|
|
76 |
$subcourses = $DB->get_records('subcourse', ['course' => $courseid], '', 'id, course, refcourse, fetchpercentage');
|
|
|
77 |
|
|
|
78 |
foreach ($subcourses as $subcourse) {
|
|
|
79 |
subcourse_grades_update($subcourse->course, $subcourse->id, $subcourse->refcourse,
|
|
|
80 |
null, false, false, $userid, $subcourse->fetchpercentage);
|
|
|
81 |
}
|
|
|
82 |
}
|
|
|
83 |
|
|
|
84 |
/**
|
|
|
85 |
* Handle the course_completed event.
|
|
|
86 |
*
|
|
|
87 |
* Notify all subcourse instances with the relevant completion rule enabled
|
|
|
88 |
* that the user completed the referenced course - so that they can be eventually
|
|
|
89 |
* marked as completed, too.
|
|
|
90 |
*
|
|
|
91 |
* @param \core\event\course_completed $event
|
|
|
92 |
* @return void
|
|
|
93 |
*/
|
|
|
94 |
public static function course_completed(\core\event\course_completed $event) {
|
|
|
95 |
global $CFG, $DB;
|
|
|
96 |
require_once($CFG->dirroot.'/lib/completionlib.php');
|
|
|
97 |
|
|
|
98 |
$courseid = $event->courseid;
|
|
|
99 |
$userid = $event->relateduserid;
|
|
|
100 |
|
|
|
101 |
// Get all subcourses that have the completed course as the referenced one.
|
|
|
102 |
$subcourses = $DB->get_records('subcourse', ['refcourse' => $courseid, 'completioncourse' => 1]);
|
|
|
103 |
|
|
|
104 |
if (empty($subcourses)) {
|
|
|
105 |
// No subcourse interested in this.
|
|
|
106 |
return;
|
|
|
107 |
}
|
|
|
108 |
|
|
|
109 |
// Load the courses where the subcourses are located in.
|
|
|
110 |
$courseids = [];
|
|
|
111 |
|
|
|
112 |
foreach ($subcourses as $subcourse) {
|
|
|
113 |
$courseids[$subcourse->course] = true;
|
|
|
114 |
}
|
|
|
115 |
|
|
|
116 |
$courses = $DB->get_records_list('course', 'id', array_keys($courseids), '', '*');
|
|
|
117 |
|
|
|
118 |
foreach ($subcourses as $subcourse) {
|
|
|
119 |
$course = $courses[$subcourse->course];
|
|
|
120 |
$cm = get_coursemodule_from_instance('subcourse', $subcourse->id, $course->id);
|
|
|
121 |
$completion = new completion_info($course);
|
|
|
122 |
|
|
|
123 |
if ($completion->is_enabled($cm)) {
|
|
|
124 |
// Notify the subcourse to check the completion status.
|
|
|
125 |
$completion->update_state($cm, COMPLETION_COMPLETE, $userid);
|
|
|
126 |
}
|
|
|
127 |
}
|
|
|
128 |
}
|
|
|
129 |
}
|