-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathupdate_grade.php
More file actions
85 lines (74 loc) · 2.59 KB
/
Copy pathupdate_grade.php
File metadata and controls
85 lines (74 loc) · 2.59 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
77
78
79
80
81
82
83
84
<?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 (0.0-1.0, or -1 if there is no submission)
$grade = computeGrade($assn_id, $assn_json, $user_id);
if ( $grade < 0 ) {
$OUTPUT->jsonError('Nothing to grade for this user', $row);
return;
}
// 0 is a real score (submitted, nothing earned yet). Show it; do not pass 0% back to the LMS.
if ( $grade == 0 ) {
$payload = array("status" => true, "grade" => 0);
$updated_at = loadResultUpdatedAt($user_id);
if ( $updated_at ) $payload['updated_at'] = $updated_at;
$OUTPUT->jsonOutput($payload);
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 = gradeSendReleaseSession($grade, $result, $debug_log);
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);
$payload = array("status" => $status, "debug" => $debug_log);
if ( $user_id == $USER->id ) $payload['grade'] = $grade;
$updated_at = loadResultUpdatedAt($user_id);
if ( $updated_at ) $payload['updated_at'] = $updated_at;
$OUTPUT->jsonOutput($payload);
} else {
$OUTPUT->jsonError($status, $debug_log);
}