-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathedit.php
More file actions
199 lines (170 loc) · 6.21 KB
/
edit.php
File metadata and controls
199 lines (170 loc) · 6.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
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
<?php
require_once "../config.php";
use \Tsugi\Util\U;
use \Tsugi\Util\Net;
use \Tsugi\Util\LTI13;
use \Tsugi\Core\LTIX;
use \Tsugi\Core\Settings;
use \Tsugi\UI\SettingsForm;
use \Tsugi\Blob\BlobUtil;
use \CloudConvert\Models\Task;
// No parameter means we require CONTEXT, USER, and LINK
$LAUNCH = LTIX::requireData();
$upload_max_size_bytes = BlobUtil::maxUploadBytes();
$upload_max_size = U::displaySize($upload_max_size_bytes);
// If settings were updated
if ( SettingsForm::handleSettingsPost() ) {
header( 'Location: '.addSession('edit.php') ) ;
return;
}
$next = U::safe_href(U::get($_GET, 'next', 'edit.php'));
$user_id = U::safe_href(U::get($_GET, 'user_id'));
if ( $user_id && ! $LAUNCH->user->instructor ) {
http_response_code(404);
die('Not authorized');
}
if ( ! $user_id ) $user_id = $LAUNCH->user->id;
// Load and parse the old JSON
$json = $LAUNCH->result->getJsonForUser($user_id);
$json = json_decode($json);
if ( $json == null ) $json = new \stdClass();
$lock = isset($json->lock) && $json->lock;
$annotations = isset($json->annotations) ? $json->annotations : array();
if ( is_object($annotations) ) $annotations = (array) $annotations;
$inst_note = $LAUNCH->result->getNote($user_id );
if( U::get($_POST, 'resetAnnotations') ) {
$json->annotations = array();
$_SESSION['success'] = 'Annotations reset';
$json = json_encode($json);
$LAUNCH->result->setJson($json);
header( 'Location: '.addSession('edit.php') ) ;
return;
}
// Sanity check input
if ($_SERVER['REQUEST_METHOD'] == 'POST' && count($_POST) < 1 ) {
$_SESSION['error'] = 'File upload size exceeded, please re-upload a smaller file';
error_log("Upload size exceeded");
header('Location: '.addSession('edit.php'));
return;
}
if ( count($_FILES) > 1 ) {
$_SESSION['error'] = 'Only one file allowed';
header( 'Location: '.addSession('edit.php') ) ;
return;
}
// Check all files to be within our size limit
$thefdes = null;
foreach($_FILES as $fdes) {
if ( $fdes['size'] > $upload_max_size_bytes ) {
$_SESSION['error'] = 'Error - '.$fdes['name'].' has a size of '.$fdes['size'].' (' . $upload_max_size . ' max size per file)';
header( 'Location: '.addSession('edit.php') ) ;
return;
}
$thefdes = $fdes;
}
if ( $thefdes ) {
$file_id = BlobUtil::uploadToBlob($thefdes);
$LAUNCH->result->setJsonKey('file_id', $file_id);
$LAUNCH->result->notifyReadyToGrade();
header( 'Location: '.addSession('index.php') ) ;
return;
}
$file_id = $LAUNCH->result->getJsonKey('file_id');
$menu = new \Tsugi\UI\MenuSet();
if ( $file_id ) {
$menu->addLeft(__('View'), 'index.php');
} else {
$menu->addLeft(__('Please upload your file'), false);
}
if ( $LAUNCH->user->instructor ) {
$submenu = new \Tsugi\UI\Menu();
$submenu->addLink(__('Student Data'), 'grades');
$submenu->addLink(__('Settings'), '#', /* push */ false, SettingsForm::attr());
if ( $CFG->launchactivity ) {
$submenu->addLink(__('Analytics'), 'analytics');
}
if ( strlen($inst_note ?? '') > 0 ) $menu->addRight(__('Note'), '#', /* push */ false, 'data-toggle="modal" data-target="#noteModal"');
$menu->addRight(__('Help'), '#', /* push */ false, 'data-toggle="modal" data-target="#helpModal"');
$menu->addRight(__('Instructor'), $submenu, /* push */ false);
} else {
if ( strlen($inst_note ?? '') > 0 ) $menu->addRight(__('Note'), '#', /* push */ false, 'data-toggle="modal" data-target="#noteModal"');
$menu->addRight(__('Help'), '#', /* push */ false, 'data-toggle="modal" data-target="#helpModal"');
$menu->addRight(__('Settings'), '#', /* push */ false, SettingsForm::attr());
}
// Render view
$OUTPUT->header();
// https://github.com/jitbit/HtmlSanitizer
$OUTPUT->bodyStart();
$OUTPUT->topNav($menu);
$OUTPUT->flashMessages();
SettingsForm::start();
?>
<p>
This program makes use of the following technologies:
<ul>
<li>JavaScript PDF Rendering library from Mozilla
<a href="https://mozilla.github.io/pdf.js/" target="_blank">PDF.JS</a> </li>
<li>JavaScript sticky grader
<a href="https://github.com/csev/stickygrader" target="_blank">Sticky Grader</a></li>
</ul>
</p>
<?php
SettingsForm::done();
SettingsForm::end();
$OUTPUT->helpModal("PDF Annotation Tool",
"You can upload a PDF document with this tool. You and your teacher can annotate your document with colored notes.");
if ( strlen($inst_note ?? '') > 0 ) {
echo($OUTPUT->modalString(__("Instructor Note"), htmlentities($inst_note), "noteModal"));
}
if ( $file_id ) {
echo("<p>Your file has been uploaded.</p>\n");
if ( count($annotations) > 0 ) {
echo("<p>".__('Annotations:').' '.count($annotations)."</p>\n");
?>
<p>
<form method="post" if="upload_form">
<input type="submit" id="submit" name="resetAnnotations" class="btn btn-warning"
onclick="return confirm('Are you sure you want to reset the annotations?');"
value="<?= __('Reset Annotations') ?>">
</form>
</p>
<?php
}
$OUTPUT->footer();
return;
}
?>
<span class="fa fa-file-pdf-o fa-3x" style="color: var(--primary); float:right;"></span>
<!-- https://stackoverflow.com/a/8667695 -->
<form action="<?= addSession('edit.php') ?>" method="post" enctype="multipart/form-data">
<p>
<label class="btn btn-default">
<input type="file" data-max-size="<?= $upload_max_size_bytes ?>"
data-max-text="Error: {0} is > <?= $upload_max_size ?>"
accept="application/pdf" name="result_<?= $LAUNCH->result->id ?>">
</label>
</p>
<p>
<input type="submit" id="submit" class="btn btn-primary" value="Submit"> (Max size <?= $upload_max_size ?>)
<span id="spinner" style="display:none;"><img src="<?= $OUTPUT->getSpinnerUrl() ?>"/></span>
</p>
</form>
<p>
Please select a PDF file to upload.
<?php
$OUTPUT->footerStart();
?>
<script>
console.debug("In Edit...");
// Set up the checking of uploaded file size in the browser
// Call this before adding more submit() events to these forms so the file check event runs first
// https://stackoverflow.com/questions/8212041/is-it-possible-to-validate-the-size-and-type-of-input-file-in-html5
// https://stackoverflow.com/a/11799218
tsugiCheckFileMaxSize();
$("#upload_form").submit(function(e) {
$("#spinner").show();
$("#submit").attr("disabled", true);
});
</script>
<?php
$OUTPUT->footerEnd();