-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstudent-save.php
More file actions
63 lines (53 loc) · 1.98 KB
/
Copy pathstudent-save.php
File metadata and controls
63 lines (53 loc) · 1.98 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
<?php
/**
* Learner endpoint: save student source into lti_result.json.
* Body: application/json webgrader-submission object.
*/
require_once "../config.php";
require_once "exercise.php";
use \Tsugi\Core\LTIX;
header('Content-Type: application/json');
$LAUNCH = LTIX::requireData();
if (!$USER || !$USER->id) {
http_response_code(403);
echo json_encode(array('status' => 'failure', 'detail' => 'Login required'));
return;
}
if (!$RESULT || !method_exists($RESULT, 'setJson')) {
http_response_code(400);
echo json_encode(array('status' => 'failure', 'detail' => 'No result context'));
return;
}
$raw = file_get_contents('php://input');
$submission = null;
if ($raw && strlen($raw) > 0) {
$submission = json_decode($raw, true);
}
if (!is_array($submission) || !isset($submission['files']) || !is_array($submission['files'])) {
http_response_code(400);
echo json_encode(array('status' => 'failure', 'detail' => 'Expected webgrader-submission JSON with files'));
return;
}
$payload = array(
'schema' => 'webgrader-submission',
'version' => 1,
'files' => array(
'html' => isset($submission['files']['html']) ? (string) $submission['files']['html'] : '',
'css' => isset($submission['files']['css']) ? (string) $submission['files']['css'] : '',
'javascript' => isset($submission['files']['javascript'])
? (string) $submission['files']['javascript'] : '',
),
'source_revision' => isset($submission['source_revision'])
? (int) $submission['source_revision'] : 0,
'last_run_revision' => isset($submission['last_run_revision'])
? (int) $submission['last_run_revision'] : 0,
'updated_at' => gmdate('c'),
);
$encoded = json_encode($payload);
if ($encoded === false || strlen($encoded) > 500000) {
http_response_code(400);
echo json_encode(array('status' => 'failure', 'detail' => 'Submission too large'));
return;
}
$RESULT->setJson($encoded);
echo json_encode(array('status' => 'success'));