forked from integral-learning/moodle-mod_mumie
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gradesync.php
273 lines (246 loc) · 10.2 KB
/
gradesync.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* This file defines the class gradesync used to synchronize grades from MUMIE servers with the MOODLE gradebook
*
* @package mod_mumie
* @copyright 2017-2020 integral-learning GmbH (https://www.integral-learning.de/)
* @author Tobias Goltz ([email protected])
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace mod_mumie;
use mod_mumie\synchronization\payload;
use mod_mumie\synchronization\xapi_request;
use mod_mumie\synchronization\context\context_provider;
use auth_mumie\user\mumie_user_service;
use stdClass;
use context_course;
use auth_mumie\mumie_server;
use auth_mumie\user\mumie_user;
defined('MOODLE_INTERNAL') || die();
global $CFG;
require_once($CFG->dirroot . '/mod/mumie/lib.php');
require_once($CFG->dirroot . "/mod/mumie/locallib.php");
require_once($CFG->dirroot . '/auth/mumie/lib.php');
require_once($CFG->dirroot . '/auth/mumie/classes/mumie_server.php');
require_once($CFG->dirroot . '/mod/mumie/classes/mumie_duedate_extension.php');
require_once($CFG->dirroot . '/auth/mumie/classes/sso/user/mumie_user_service.php');
/**
* This file defines the class gradesync
*
* @package mod_mumie
* @copyright 2017-2020 integral-learning GmbH (https://www.integral-learning.de/)
* @author Tobias Goltz ([email protected])
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class gradesync {
/**
* Update grades for all MUMIE tasks that are currently displayed in the gradebook
* @return void
*/
public static function update() {
global $PAGE, $USER, $COURSE;
$isreportpage = strpos($PAGE->url, 'grader/index.php') !== false || strpos($PAGE->url, 'user/index.php') !== false;
$isoverviewpage = (strpos($PAGE->url, 'overview/index.php') !== false);
if ($isreportpage) {
$userid = $USER->id;
if (has_capability("mod/mumie:viewgrades", context_course::instance($COURSE->id), $USER)) {
// User id = 0 => update grades for all users!
$userid = 0;
}
foreach (self::get_mumie_tasks_from_course($COURSE->id) as $mumie) {
mumie_update_grades($mumie, $userid);
}
} else {
if ($isoverviewpage) {
foreach (self::get_all_mumie_tasks_for_user($USER->id) as $mumie) {
mumie_update_grades($mumie, $USER->id);
}
}
}
}
/**
* Get all graded MUMIE tasks that are used in this course
* @param int $courseid
* @return array All MUMIE tasks that are used in the given course
*/
public static function get_mumie_tasks_from_course(int $courseid) : array {
global $DB;
return $DB->get_records(MUMIE_TASK_TABLE, array("course" => $courseid, "isgraded" => 1));
}
/**
* Get all MUMIE tasks, that are in courses the user is enrolled in
*
* @param int $userid
* @return array All MUMIE tasks that are in courses, the user is enrolled in
*/
public static function get_all_mumie_tasks_for_user(int $userid) : array {
$allmumietasks = array();
foreach (enrol_get_all_users_courses($userid) as $course) {
$mumietasks = self::get_mumie_tasks_from_course($course->id);
if (count($mumietasks) > 0) {
$allmumietasks = array_merge($allmumietasks, $mumietasks);
}
}
return $allmumietasks;
}
/**
* Get grades from MUMIE server for a given MUMIE task
*
* @param stdClass $mumie instance of MUMIE task
* @param int $userid If userid = 0, update all users
* @return array grades for the given MUMIE task
*/
public static function get_mumie_grades(stdClass $mumie, int $userid) : ?array {
$mumieusers = self::get_mumie_users($mumie, $userid);
$mumieids = array(locallib::get_mumie_id($mumie));
$xapigrades = self::get_xapi_grades($mumie, $mumieusers, $mumieids);
if (empty($xapigrades)) {
return null;
}
$grades = array();
foreach ($xapigrades as $xapigrade) {
$grade = self::xapi_to_moodle_grade($xapigrade);
if (self::include_grade($mumie, $grades, $grade)) {
$grades[$grade->userid] = $grade;
}
}
return $grades;
}
/**
* Get an array of MUMIE users that can submit answers to a given MUMIE Task.
*
* If $userid is 0, all possible students are returned. Otherwise, the array will only contain the user with the given $userid.
* @param stdClass $mumie
* @param int $userid
* @return array
*/
private static function get_mumie_users(stdClass $mumie, int $userid): array {
global $COURSE;
if ($userid == 0) {
$mumieusers = array();
foreach (get_enrolled_users(context_course::instance($COURSE->id)) as $user) {
$mumieusers[] = mumie_user_service::get_user($user->id, $mumie);
}
return $mumieusers;
} else {
return array(mumie_user_service::get_user($userid, $mumie));
}
}
/**
* Get all grades a user has archived for a given MUMIE Task
*
* @param stdClass $mumie
* @param int $userid
* @return array
*/
public static function get_all_grades_for_user(stdClass $mumie, int $userid) : ?array {
$mumieusers = [mumie_user_service::get_user($userid, $mumie)];
$mumieids = array(locallib::get_mumie_id($mumie));
$grades = array();
$xapigrades = self::get_xapi_grades($mumie, $mumieusers, $mumieids);
if (empty($xapigrades)) {
return null;
}
foreach ($xapigrades as $xapigrade) {
$grades[] = self::xapi_to_moodle_grade($xapigrade);
}
return $grades;
}
/**
* Transform Xapi grade to moodle grade objects.
*
* @param stdClass $xapigrade
* @return stdClass
*/
private static function xapi_to_moodle_grade($xapigrade) : stdClass {
$grade = new stdClass();
$syncid = self::get_mumie_user_from_sync_id($xapigrade->actor->account->name);
$grade->userid = $syncid->get_moodle_id();
$grade->rawgrade = 100 * $xapigrade->result->score->raw;
$grade->timecreated = strtotime($xapigrade->timestamp);
return $grade;
}
/**
* Get a mumie_user from a syncid.
* @param string $syncid
* @return mumie_user|null
* @throws \dml_exception
*/
private static function get_mumie_user_from_sync_id(string $syncid): ?mumie_user {
$mumieid = substr(strrchr($syncid, "_"), 1);
return mumie_user_service::get_user_from_mumie_id($mumieid);
}
/**
* Indicate whether a grade was archived before the task was due and is the latest one currently available
*
* @param stdClass $mumie instance of MUMIE task we want to get grades for
* @param array $grades an array of all grades we have selected so far
* @param stdClass $potentialgrade the grade in question
* @return boolean Whether the grade should be added to $grades
*/
public static function include_grade(stdClass $mumie, array $grades, stdClass $potentialgrade) : bool {
$duedate = locallib::get_effective_duedate($potentialgrade->userid, $mumie);
if (!isset($duedate) || $duedate == 0) {
return true;
}
if ($duedate < $potentialgrade->timecreated) {
return false;
}
return self::is_latest_grade($grades, $potentialgrade);
}
/**
* True, if the given grade is currently the latest available one.
*
* @param array $grades List of the latest grades so far by user.
* @param stdClass $potentialgrade The grade we are testing
* @return boolean
*/
private static function is_latest_grade(array $grades, stdClass $potentialgrade) : bool {
return !isset($grades[$potentialgrade->userid])
|| $grades[$potentialgrade->userid]->timecreated < $potentialgrade->timecreated;
}
/**
* Get xapi grades for a MUMIE task instance
*
* @param stdClass $mumie instance of MUMIE task we want to get grades for
* @param array $mumieusers all users we want grades for
* @param array $mumieids mumieid of mumie instance as an array
* @return array all requested grades for the given MUMIE task
*/
public static function get_xapi_grades(stdClass $mumie, array $mumieusers, array $mumieids) : array {
global $CFG;
require_once($CFG->dirroot . "/mod/mumie/classes/grades/synchronization/payload.php");
require_once($CFG->dirroot . "/mod/mumie/classes/grades/synchronization/xapi_request.php");
require_once($CFG->dirroot . "/mod/mumie/classes/grades/synchronization/context/context_provider.php");
require_once($CFG->dirroot . "/auth/mumie/classes/sso/user/mumie_user.php");
$mumieserver = new mumie_server();
$mumieserver->set_urlprefix($mumie->server);
$syncids = array_map(
function ($user) {
return $user->get_sync_id();
},
$mumieusers
);
$payload = new payload($syncids, $mumie->mumie_coursefile, $mumieids, $mumie->lastsync, true);
if (context_provider::requires_context($mumie)) {
$context = context_provider::get_context(array($mumie), $mumieusers);
$payload->with_context($context);
}
$request = new xapi_request($mumieserver, $payload);
return $request->send();
}
}