-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.php
664 lines (589 loc) · 25.2 KB
/
index.php
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
<?php
require_once('lib/logging.php');
require_once('lib/config.php');
require_once('lib/state.php');
State::fromInitialPost();
if (!State::courseId() || !State::canvasDomain()) {
header('HTTP/1.1 403 Forbidden');
echo 'Connect to this LTI using your Canvas course.';
exit();
}
if (!State::refreshToken()) {
// log in
$uri = 'https://' . State::canvasDomain() . '/login/oauth2/auth?client_id=' . urlencode(Config::clientId(State::canvasDomain())) . '&response_type=code&redirect_uri=' . urlencode(Config::oauthCallbackURI()) . '&state=' . State::oauthState();
header('Location: ' . $uri);
} else {
// refresh the access token
$data = array('client_id' => Config::clientId(State::canvasDomain()),
// 'redirect_uri' => urlencode($uri),
'client_secret' => rawurlencode(Config::clientSecret(State::canvasDomain())),
'refresh_token' => State::refreshToken(),
'grant_type' => 'refresh_token');
$ch = curl_init('https://' . State::canvasDomain() . '/login/oauth2/token');
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
curl_close($ch);
$result = json_decode($result, true);
State::setAccessToken($result['access_token']);
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<title>Digital Dummy</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
<meta name="apple-mobile-web-app-capable" content="yes">
<link rel="apple-touch-icon" sizes="144x144" href="img/apple-icon-144x144.png">
<link rel="icon" type="image/png" href="img/digitaldummy.png">
<link rel="stylesheet" href="bower_components/normalize-css/normalize.css">
<link rel="stylesheet" href="bower_components/css-modal/build/modal.css">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.5.0/css/font-awesome.min.css">
<link rel="stylesheet" href="bower_components/toastr/toastr.min.css">
<link rel="stylesheet" href="css/dailysnapshot.css">
<link rel="stylesheet" href="css/pong.css">
<link rel="stylesheet" href="bower_components/trumbowyg/dist/ui/trumbowyg.min.css">
<script src="js/jquery-2.1.4.min.js"></script>
<script src="bower_components/toastr/toastr.min.js"></script>
<script src='bower_components/fastclick/lib/fastclick.js'></script>
<script src="js/anchorme.js"></script>
<script src="js/moment.min.js"></script>
<script src="js/upload.js?v1.1"></script>
<script>
<?php
//ensure blacklist exists
file_put_contents('service/blacklists/' . State::getKey(), '', FILE_APPEND | LOCK_EX);
?>
// include object blacklist
var blacklist = <?php echo json_encode(file('service/blacklists/' . State::getKey(), FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES)); ?>;
</script>
</head>
<body>
<header>
<h1>Digital Dummy</h1>
<h2>You've moved mountains today!</h2>
<a id="mobile-upload-link" target="mobileupload" href="<? echo State::createUploadLink(); ?>">easy upload version <i
class="fa fa-mobile" aria-hidden="true"></i></a> <a id="github-link" target="gitrepo" href="#">follow the project
on Github <i class="fa fa-github" aria-hidden="true"></i></a>
<div id="select-wrapper" style="display:none;">
<select id="student-filter" title="Student filter">
<option>Show all</option>
</select>
</div>
</header>
<form enctype="multipart/form-data" id="upload-form">
<div id="file-upload-wrapper">
<i class="fa fa-upload"></i>
<span>media</span>
<input name="file" type="file" id="file-upload"/>
</div>
<a id="text-upload-wrapper" href="#modal-text">
<i class="fa fa-plus-square"></i>
<span>text</span>
</a>
<progress></progress>
</form>
<section class="modal--show" id="modal-text" tabindex="-1"
role="dialog" aria-labelledby="modal-label" aria-hidden="true">
<div class="modal-inner">
<form id="text-upload-form">
<div class="modal-content">
<div id="submission-text" placeholder="Type your submission text here."></div>
<button type="submit" id="text-submit">submit text</button>
</div>
</form>
</div>
<a href="#!" class="modal-close" title="Close this modal" data-close="Close"
data-dismiss="modal">?</a>
</section>
<section id="comment-box" style="display: none;" class="container">
<form id="comment-upload-form">
<div id="comment-text" placeholder="Type your comment here."></div>
<button type="submit" id="comment-submit">post</button>
</form>
</section>
<section id="student-blog" class="container">
<div class="pong-loader">
Loading…
</div>
</section>
<script>
console.log('clearing cache');
localStorage.clear();
// write the upload link
console.log('Your easy upload link: ', '<?echo State::createUploadLink();?>');
var canvasDomain = 'https://<?php echo State::canvasDomain(); ?>',
storeId = '<?php echo base64_encode(State::courseId() . ',' . State::assignmentId() . ',' . State::canvasDomain()); ?>',
currentUserId = null,
students = null;
function getStudentNameForId(id) {
return students.filter(function (user) {
return user.id === id;
})[0].sortable_name;
}
function loadStudents() {
console.log('loading students');
students = JSON.parse(localStorage.getItem(storeId + '/students'));
if (students) {
showStudents();
}
var selectElement = document.getElementById('student-filter');
selectElement.addEventListener('change', function () {
var selectedIndex = selectElement.selectedIndex;
if (selectedIndex === 0) {
currentUserId = null;
students.forEach(function (student) {
loadSubmission(student.id);
});
} else {
currentUserId = students[selectedIndex - 1].id;
loadSubmission(currentUserId);
}
});
console.log('fetching students');
$.getJSON("service/students.php", function (resp) {
console.log('students received');
var cached = localStorage.getItem(storeId + '/students');
if (!cached) {
cached = '';
}
localStorage.setItem(storeId + '/students', JSON.stringify(resp));
if (cached.localeCompare(JSON.stringify(resp)) != 0) {
students = resp;
showStudents();
}
});
function showStudents() {
console.log('showing students', students);
// skip if students list is empty
if (students.length == 0) {
var section = document.getElementById('student-blog');
section.innerHTML = '<h2>No submissions yet...</h2>';
}
var selectElement = document.getElementById('student-filter'),
commentElement = document.getElementById('comment-box'),
uploadForm = document.getElementById('upload-form');
selectElement.parentNode.style.display = "block";
selectElement.innerHTML = '';
var firstOption = document.createElement('option');
firstOption.innerHTML = 'Show all';
selectElement.appendChild(firstOption);
// sort the user names
students.sort(function (a, b) {
return a.sortable_name.localeCompare(b.sortable_name);
});
students.forEach(function (student) {
var option = document.createElement('option');
option.innerHTML = student.sortable_name;
selectElement.appendChild(option);
});
selectElement.selectedIndex = 1;
if (students.length > 0 && currentUserId != students[0].id) {
currentUserId = students[0].id;
loadSubmission(currentUserId);
}
// this is a not-so-nice test to see if the user is a student or a teacher
if (students.length < 2) {
document.body.classList.add('isstudent');
document.body.classList.remove('isteacher');
selectElement.setAttribute('disabled', 'true');
selectElement.parentNode.setAttribute('disabled', 'true');
uploadForm.style.display = 'block';
} else {
document.body.classList.remove('isstudent');
document.body.classList.add('isteacher');
// teachers cannot upload (sad if you're a teacher AND a student TODO)
uploadForm.style.display = 'none';
}
}
}
function loadSubmission(id) {
console.log('getting', id);
showSubmissions();
$.get("service/singlesubmission.php", {user: id},
function (resp) {
console.log('receieved', id);
var submission = resp;
if (submission.length === 1) {
localStorage.setItem(storeId + '/submission/' + id, JSON.stringify(submission[0]));
}
showSubmissions();
});
function showSubmissions() {
console.log('showing', currentUserId);
var submissions = [];
if (currentUserId) {
var submission = JSON.parse(localStorage.getItem(storeId + '/submission/' + currentUserId));
if (submission) {
submissions.push(submission);
}
} else {
students.forEach(function (student) {
var submission = JSON.parse(localStorage.getItem(storeId + '/submission/' + student.id));
if (submission) {
submissions.push(submission);
}
});
}
var commentElement = document.getElementById('comment-box');
var articles = [];
if (!currentUserId) {
submissions.forEach(function (submission) {
articles = articles.concat(submission.submission_history);
});
commentElement.style.display = 'none';
} else if (submissions.length > 0) {
articles = articles.concat(submissions[0].submission_comments).concat(submissions[0].submission_history);
if (students.length > 1) {
commentElement.style.display = 'block';
} else {
commentElement.style.display = 'none';
}
}
showData(articles);
function showData(articles) {
console.log('showing articles', articles);
var section = document.getElementById('student-blog');
section.innerHTML = '';
// filter out submissions that are not comments, text entries, or attached files (should not occur in practice)
articles = articles.filter(function (article) {
return article.comment || article.body || article.attachments;
});
articles.sort(function (a, b) {
// sort by submission date and let attachments go before comments (new to old)
var aa = a.submitted_at ? new Date(a.submitted_at) : new Date(new Date(a.created_at).valueOf() - 20000),
bb = b.submitted_at ? new Date(b.submitted_at) : new Date(new Date(b.created_at).valueOf() - 20000);
return bb - aa;
});
var dateString = '';
var hiddenArticles = articles;
appendFrom(hiddenArticles, 10);
function appendFrom(attempts, pageLength) {
for (var i = 0; i < pageLength; i++) {
var attempt = attempts.shift();
if (!attempt) {
break;
}
append(attempt);
}
}
function append(attempt) {
var date = attempt.submitted_at ? attempt.submitted_at : attempt.created_at;
var newDateString = moment(date).format('dddd, MMMM Do');
if (newDateString !== dateString) {
dateString = newDateString;
var header = document.createElement('h4');
var dateHeader = document.createElement('time');
dateHeader.setAttribute('datetime', date);
dateHeader.innerHTML = dateString;
header.appendChild(dateHeader);
section.appendChild(header);
}
if (attempt.attachments) {
var img, audio, video, mediaSource, iframe, item_blacklisted = false;
attempt.attachments.forEach(function (attachment) {
var article = document.createElement('article');
article.classList.add('row');
// tag blacklisted items
var blacklistId = 'aid/' + attachment.id;
console.log(blacklistId);
if (blacklist.indexOf(blacklistId) > -1) {
item_blacklisted = true;
article.classList.add('blacklisted');
}
// add metaheader
var metaheader = document.createElement('header');
var time = document.createElement('time');
time.innerHTML = moment(date).format('LT');
metaheader.appendChild(time);
if (!currentUserId) {
var author = document.createElement('span');
author.classList.add('row-author');
author.innerHTML = getStudentNameForId(attempt.user_id);
metaheader.appendChild(author);
}
var deleteButton = document.createElement('i');
deleteButton.setAttribute('class', 'fa fa-times');
deleteButton.classList.add((blacklist.indexOf(blacklistId) > -1) ? 'undeleteButton' : 'deleteButton');
deleteButton.setAttribute('title', 'Hide or unhide your post here for others.');
metaheader.appendChild(deleteButton);
article.appendChild(metaheader);
deleteButton.addEventListener('click', function (e) {
if (deleteButton.classList.contains('deleteButton')) {
deleteButton.classList.remove('deleteButton');
deleteButton.classList.add('undeleteButton');
article.classList.add('blacklisted');
$.post('service/blacklists/add.php', {id: blacklistId})
} else {
deleteButton.classList.add('deleteButton');
deleteButton.classList.remove('undeleteButton');
article.classList.remove('blacklisted');
$.post('service/blacklists/remove.php', {id: blacklistId})
}
});
var contentType = attachment['content-type'];
switch (contentType) {
case 'image/gif':
case 'image/png':
case 'image/jpeg':
case 'image/jpg':
img = document.createElement('img');
img.src = attachment.url;
img.classList.add('blog-image');
article.appendChild(img);
break;
case 'audio/aac':
case 'audio/mp4':
case 'audio/mpeg':
case 'audio/ogg':
case 'audio/wav':
case 'audio/webm':
audio = document.createElement('audio');
audio.setAttribute('controls', 'true');
audio.src = attachment.url;
audio.classList.add('blog-image');
mediaSource = document.createElement('source');
mediaSource.setAttribute('src', attachment.url);
mediaSource.setAttribute('type', contentType);
audio.appendChild(mediaSource);
article.appendChild(audio);
break;
case 'video/quicktime':
case 'video/mp4':
case 'video/ogg':
case 'video/webm':
video = document.createElement('video');
video.setAttribute('controls', 'true');
video.src = attachment.url;
video.classList.add('blog-image');
mediaSource = document.createElement('source');
mediaSource.setAttribute('src', attachment.url);
mediaSource.setAttribute('type', contentType);
video.appendChild(mediaSource);
article.appendChild(video);
break;
case 'application/pdf':
case 'application/msword':
case 'application/vnd.openxmlformats-officedocument.wordprocessingml.document':
case 'application/vnd.ms-powerpoint':
case 'application/vnd.openxmlformats-officedocument.presentationml.presentation':
case 'application/vnd.ms-excel':
case 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet':
case 'text/plain':
case 'application/x-python':
case 'text/x-python':
case 'text/javascript':
case 'application/x-javascript':
case 'text/xml':
case 'application/xml':
case 'text/css':
case 'text/x-markdown':
case 'text/x-script.perl':
case 'text/x-c':
case 'text/x-m':
case 'application/json':
if (window.self !== window.top) {
iframe = document.createElement('iframe');
iframe.setAttribute(item_blacklisted ? 'data-src' : 'src', canvasDomain + attachment.preview_url);
iframe.classList.add('blog-embed');
article.appendChild(iframe);
break;
}
default:
console.log('unknown mime:', contentType, attachment);
var icon = document.createElement('i');
icon.setAttribute('class', 'fa fa-2x file-icon');
switch (contentType) {
case 'application/zip':
case 'application/x-rar-compressed':
icon.classList.add('fa-file-zip-o');
break;
default:
icon.classList.add('fa-file-image-o');
break;
}
article.appendChild(icon);
var anchor = document.createElement('a');
anchor.innerHTML = attachment.display_name;
anchor.href = attachment.url;
article.classList.add('file');
article.appendChild(anchor);
break;
}
section.appendChild(article);
}
);
}
if (attempt.comment) {
var article = document.createElement('article');
article.classList.add('row');
// tag blacklisted items
var blacklistId = 'cid/' + attempt.id;
console.log(blacklistId);
if (blacklist.indexOf(blacklistId) > -1) {
article.classList.add('blacklisted');
}
if (attempt.author_id === currentUserId) {
article.classList.add('byself');
}
// add metaheader TODO REMOVE CODE DUPLICATE except for author id
var metaheader = document.createElement('header');
var time = document.createElement('time');
time.innerHTML = moment(date).format('LT');
metaheader.appendChild(time);
if (!currentUserId) {
var author = document.createElement('span');
author.classList.add('row-author');
author.innerHTML = getStudentNameForId(attempt.author_id);
metaheader.appendChild(author);
}
var deleteButton = document.createElement('i');
deleteButton.setAttribute('class', 'fa fa-times');
deleteButton.classList.add((blacklist.indexOf(blacklistId) > -1) ? 'undeleteButton' : 'deleteButton');
deleteButton.setAttribute('title', 'Hide or unhide your comment here for others.');
metaheader.appendChild(deleteButton);
article.appendChild(metaheader);
deleteButton.addEventListener('click', function (e) {
console.log('clicked');
if (deleteButton.classList.contains('deleteButton')) {
deleteButton.classList.remove('deleteButton');
deleteButton.classList.add('undeleteButton');
article.classList.add('blacklisted');
$.post('service/blacklists/add.php', {id: blacklistId})
} else {
deleteButton.classList.add('deleteButton');
deleteButton.classList.remove('undeleteButton');
article.classList.remove('blacklisted');
$.post('service/blacklists/remove.php', {id: blacklistId})
}
});
article.appendChild(metaheader);
// add author to comment
var authorHeader = document.createElement('div');
authorHeader.classList.add('commentauthor');
authorHeader.innerHTML = attempt.author_name.split(" ")[0] + ' says:';
article.appendChild(authorHeader);
var paragraph = document.createElement('p');
paragraph.innerHTML = anchorme.js(attempt.comment); // replaces links!
article.classList.add('comment');
article.appendChild(paragraph);
section.appendChild(article);
}
if (attempt.body) {
var article = document.createElement('article');
article.classList.add('row');
// tag blacklisted items
var blacklistId = 'bid/' + attempt.id + '/' + attempt.attempt;
console.log(blacklistId);
if (blacklist.indexOf(blacklistId) > -1) {
article.classList.add('blacklisted');
}
// add metaheader TODO REMOVE CODE DUPLICATE except for author id
var metaheader = document.createElement('header');
var time = document.createElement('time');
time.innerHTML = moment(date).format('LT');
metaheader.appendChild(time);
if (!currentUserId) {
var author = document.createElement('span');
author.classList.add('row-author');
author.innerHTML = getStudentNameForId(attempt.user_id);
metaheader.appendChild(author);
}
var deleteButton = document.createElement('i');
deleteButton.setAttribute('class', 'fa fa-times');
deleteButton.classList.add((blacklist.indexOf(blacklistId) > -1) ? 'undeleteButton' : 'deleteButton');
deleteButton.setAttribute('title', 'Hide or unhide your post here for others.');
metaheader.appendChild(deleteButton);
article.appendChild(metaheader);
deleteButton.addEventListener('click', function (e) {
console.log('clicked');
if (deleteButton.classList.contains('deleteButton')) {
deleteButton.classList.remove('deleteButton');
deleteButton.classList.add('undeleteButton');
article.classList.add('blacklisted');
$.post('service/blacklists/add.php', {id: blacklistId})
} else {
deleteButton.classList.add('deleteButton');
deleteButton.classList.remove('undeleteButton');
article.classList.remove('blacklisted');
$.post('service/blacklists/remove.php', {id: blacklistId})
}
});
article.appendChild(metaheader);
var paragraph = document.createElement('div');
paragraph.innerHTML = anchorme.js(attempt.body.replace(/\n/g, '<br>')); // replaces links!
article.classList.add('textpost');
article.appendChild(paragraph);
section.appendChild(article);
}
}
window.onscroll = respondToScroll;
function respondToScroll() {
if (window.scrollY + 1.5 * window.innerHeight - document.body.clientHeight > 0) {
appendFrom(hiddenArticles, 10);
}
}
document.body.appendChild(section);
}
}
}
$(function () {
loadStudents();
});
</script>
<script>
if ('addEventListener' in document) {
document.addEventListener('DOMContentLoaded', function () {
FastClick.attach(document.body);
}, false);
}
$("a").click(function (event) {
event.preventDefault();
window.location = $(this).attr("href");
});
</script>
<script src="bower_components/css-modal/modal.js"></script>
<script src="bower_components/trumbowyg/dist/trumbowyg.min.js"></script>
<script>
$('#submission-text').trumbowyg({
mobile: true,
tablet: true,
fullscreenable: false,
btns: ['viewHTML',
'|', 'formatting',
'|', 'btnGrp-design',
'|', 'link',
'|', 'btnGrp-justify',
'|', 'btnGrp-lists']
}
);
$('#comment-text').trumbowyg({
// mobile: true,
// tablet: true,
fullscreenable: false,
autogrow: true,
btns: ['viewHTML',
'|', 'formatting',
'|', 'btnGrp-design',
'|', 'link',
'|', 'btnGrp-justify',
'|', 'btnGrp-lists']
}
);
// check in-frame for mobile upload link
// if (window != window.top) {
document.getElementById('mobile-upload-link').style.display = 'inline-block';
// }
document.getElementById('github-link').onclick = function (e) {
e.preventDefault();
window.open('https://github.com/olafjanssen/digitaldummy');
};
document.getElementById('mobile-upload-link').onclick = function (e) {
e.preventDefault();
window.open('<? echo State::createUploadLink(); ?>');
};
</script>
</body>
</html>