-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathupdate_grade.php
More file actions
77 lines (66 loc) · 2.21 KB
/
update_grade.php
File metadata and controls
77 lines (66 loc) · 2.21 KB
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
<?php
require_once "../config.php";
require_once "peer_util.php";
use \Tsugi\Core\LTIX;
use \Tsugi\Core\Result;
use \Tsugi\Grades\GradeUtil;
// Sanity checks
$LAUNCH = LTIX::requireData();
$p = $CFG->dbprefix;
// Check to see if we are updating the grade for the current
// user or another
$user_id = $USER->id;
if ( isset($_REQUEST['user_id']) ) $user_id = $_REQUEST['user_id'];
// Model
$row = loadAssignment();
$assn_json = null;
$assn_id = false;
if ( $row !== false ) {
$assn_json = json_decode(upgradeSubmission($row['json']));
$assn_id = $row['assn_id'];
}
if ( $assn_id == false ) {
$OUTPUT->jsonError('This assignment is not yet set up');
return;
}
// Compute the user's grade
$grade = computeGrade($assn_id, $assn_json, $user_id);
if ( $grade <= 0 ) {
$OUTPUT->jsonError('Nothing to grade for this user', $row);
return;
}
// Lookup the result row if we are grading the non-current user
$result = false;
$old_grade = null;
if ( $user_id != $USER->id ) {
$result = Result::lookupResultBypass($user_id);
} else {
// Get the old grade from the current user's result
$result = Result::lookupResultBypass($user_id);
}
// Get old grade if available
if ( $result && isset($result['grade']) ) {
$old_grade = floatval($result['grade']);
}
// Send the grade
$debug_log = array();
$status = LTIX::gradeSend($grade, $result, $debug_log); // This is the slow bit
if ( $status === true ) {
// Send notification to the student whose grade was changed (only if grade actually changed)
// Use LTI launch_presentation_return_url if available, otherwise fall back to index
$notification_url = null;
if ( is_object($LAUNCH) && method_exists($LAUNCH, 'returnUrl') ) {
$notification_url = $LAUNCH->returnUrl();
}
if ( empty($notification_url) ) {
$notification_url = addSession('index');
}
notifyGradeChange($user_id, $grade, $old_grade, $assn_json->title ?? null, $notification_url);
if ( $user_id != $USER->id ) {
$OUTPUT->jsonOutput(array("status" => $status, "debug" => $debug_log));
} else {
$OUTPUT->jsonOutput(array("status" => $status, "grade" => $grade, "debug" => $debug_log));
}
} else {
$OUTPUT->jsonError($status, $debug_log);
}