Repository navigation
Expand file tree
/
Copy pathexport-udemy.php
More file actions
121 lines (109 loc) · 3.54 KB
/
Copy pathexport-udemy.php
File metadata and controls
121 lines (109 loc) · 3.54 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
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
<?php
/**
* Instructor endpoint: preview or download a Udemy export ZIP for the current assignment.
*
* POST JSON body:
* { "exercise": { ...assignment... }, "action": "preview"|"download" }
*
* preview → application/json compatibility report (no ZIP)
* download → application/zip on success, or application/json errors
*/
require_once "../config.php";
require_once "exercise.php";
require_once __DIR__ . '/export/udemy/Exporter.php';
use \Tsugi\Core\LTIX;
$LAUNCH = LTIX::requireData();
if (!$USER->instructor) {
http_response_code(403);
header('Content-Type: application/json');
echo json_encode(array('status' => 'failure', 'detail' => 'Instructor role required'));
return;
}
$raw = file_get_contents('php://input');
$body = $raw ? json_decode($raw, true) : null;
$exercise = null;
$action = 'preview';
if (is_array($body)) {
if (isset($body['exercise']) && is_array($body['exercise'])) {
$exercise = $body['exercise'];
} elseif (isset($body['type']) && $body['type'] === 'webgrader') {
$exercise = $body;
}
if (!empty($body['action'])) {
$action = (string) $body['action'];
}
}
if (!$exercise && isset($_POST['exercise'])) {
$exercise = json_decode($_POST['exercise'], true);
if (!empty($_POST['action'])) {
$action = (string) $_POST['action'];
}
}
// Fall back to the placement JSON when the client sends no body.
if (!$exercise && isset($LINK) && $LINK && method_exists($LINK, 'getJson')) {
$linkJson = $LINK->getJson();
if (is_string($linkJson) && strlen($linkJson)) {
$decoded = json_decode($linkJson, true);
if (is_array($decoded)) {
$exercise = $decoded;
}
}
}
if (!is_array($exercise)) {
http_response_code(400);
header('Content-Type: application/json');
echo json_encode(array(
'status' => 'failure',
'detail' => 'Expected JSON assignment object (exercise).',
));
return;
}
if ($action !== 'download' && $action !== 'preview') {
$action = 'preview';
}
try {
$package = UdemyExporter::build($exercise, array(
'repo_root' => __DIR__,
'preview_only' => ($action === 'preview'),
));
} catch (Exception $e) {
http_response_code(500);
header('Content-Type: application/json');
echo json_encode(array(
'status' => 'failure',
'detail' => $e->getMessage(),
));
return;
}
if ($action === 'download') {
if (!$package->ok || $package->zip_bytes === null) {
http_response_code(422);
header('Content-Type: application/json');
echo json_encode(array(
'status' => 'failure',
'detail' => 'Export is not downloadable. Review compatibility errors.',
'ok' => false,
'compatibility' => $package->compatibility,
'warnings' => $package->warnings,
'errors' => $package->errors,
'tests' => $package->test_statuses,
'repairs' => $package->repairs,
'compatibility_md' => $package->compatibility_md,
));
return;
}
UdemyZipBuilder::stream($package->zip_bytes, $package->download_name, true);
return;
}
header('Content-Type: application/json');
echo json_encode(array(
'status' => 'success',
'ok' => $package->ok,
'compatibility' => $package->compatibility,
'download_name' => $package->download_name,
'warnings' => $package->warnings,
'errors' => $package->errors,
'tests' => $package->test_statuses,
'repairs' => $package->repairs,
'compatibility_md' => $package->compatibility_md,
));