-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdiscussion-download.user.js
395 lines (355 loc) · 12.2 KB
/
discussion-download.user.js
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
// ==UserScript==
// @name Canvas discussion topic information download
// @author WenChen Hol
// @namespace https://github.com/clearnz/canvas-report-tools/
// @description For Canvas users, this tool generates a .CSV download of the quiz submissions information
// @downloadURL https://github.com/clearnz/canvas-report-tools/raw/master/discusson-download.user.js
// @include https://*/courses/*/discussion_topics/*
// @require https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js
// @require https://ajax.googleapis.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.js
// @require https://flexiblelearning.auckland.ac.nz/javascript/filesaver.js
// @require https://flexiblelearning.auckland.ac.nz/javascript/jszip.min.js
// @version 0.1.5
// @grant none
// ==/UserScript==
// based on code from James Jones' Canvancement https://github.com/jamesjonesmath/canvancement
(function () {
'use strict';
var participants = {};
//
//
var discussionPosts = [
]; // quiz_submissions is array of objects
var pending = - 1;
var fetched = 0;
var needsFetched = 0;
var reporttype;
var ajaxPool;
var courseId;
var discussionId;
var today = new Date();
var dd = today.getDate();
var mm = today.getMonth() + 1;
var yyyy = today.getFullYear();
var zip = new JSZip();
var debug = 0;
if (dd < 10) {
dd = '0' + dd;
}
if (mm < 10) {
mm = '0' + mm;
}
today = (yyyy-2000 ) + '-' + mm + '-' + dd + '-' + Math.floor(Date.now() /1000) ;
var aborted = false;
addDiscussionDownloadButton();
function addDiscussionDownloadButton() {
if ($('#discusson-topic-download').length === 0) {
$('.pull-right').first().append('<a href="javascript:void(0)" id="discusson-topic-download" class="btn"><i class="icon-download"></i> Download for TurnItIn</a>');
$('#discusson-topic-download').one('click', {
type: 2
}, discussionSubmissionReport);
}
return;
}
function abortAll() {
for (var i = 0; i < ajaxPool.length; i++) {
ajaxPool[i].abort();
}
ajaxPool = [
];
}
function setupPool() {
try {
ajaxPool = [
];
$.ajaxSetup({
'beforeSend': function (jqXHR) {
ajaxPool.push(jqXHR);
},
'complete': function (jqXHR) {
var i = ajaxPool.indexOf(jqXHR);
if (i > - 1) {
ajaxPool.splice(i, 1);
}
}
});
} catch (e) {
throw new Exception('Error configuring AJAX pool');
}
}
function discussionSubmissionReport(e) { //gets the student list
pending = 0;
fetched = 0;
reporttype = e.data.type;
aborted = false;
setupPool();
courseId = getCourseId();
discussionId = getdiscussionId();
if (debug) console.log( courseId, discussionId );
//var url = '/api/v1/courses/' + courseId + '/discussion_topics/' + discussionId + "/view";
//progressbar();
pending = 0;
getDiscussionSubmissions( courseId, discussionId );
}
function nextURL(linkTxt) { //if more than 100 students, gets the URL for the rest of the list
var url = null;
if (linkTxt) {
var links = linkTxt.split(',');
var nextRegEx = new RegExp('^<(.*)>; rel="next"$');
for (var i = 0; i < links.length; i++) {
var matches = nextRegEx.exec(links[i]);
if (matches) {
url = matches[1];
}
}
}
return url;
}
function getDiscussionSubmissions( courseId, discussionId ) { //cycles through student list
pending = 0;
fetched = 0;
//needsFetched = Object.getOwnPropertyNames(userData).length;
if (debug) console.log( "getDiscussionSubmissions:" );
var url = '/api/v1/courses/'+ courseId + '/discussion_topics/' + discussionId + '/view?per_page=50';
getDiscussionPosts( url, courseId, discussionId );
}
function getDiscussionPosts( url, courseId, discussionId ) { //get peer review data
var tmpQuizSubmissions;
var tmpStudent;
var tmpUrl;
if (debug) console.log( "in getDiscussionPosts:" );
try {
if (aborted) {
throw new Error('Aborted');
}
jQuery("#doing").html( "Fetching Discission Post information <img src='https://flexiblelearning.auckland.ac.nz/images/spinner.gif'/>" );
pending++;
progressbar(fetched, needsFetched);
$.getJSON(url, function (adata, status, jqXHR) {
//get participants: id, display_name
for ( var i=0; i < adata["participants"].length; i++ ) {
tmpStudent = adata["participants"][i];
participants[ tmpStudent.id ] = tmpStudent;
}
//get views: userid, message
discussionPosts.push.apply(discussionPosts, adata["view"]);
url = nextURL(jqXHR.getResponseHeader('Link'));
if (url) {
getDiscussionPosts( url, courseId, discussionId );
}
pending--;
fetched+=50;
progressbar(fetched, needsFetched);
if (debug) console.log( "pending:", pending );
if (pending <= 0 && !aborted) {
makeReport( courseId, discussionId );
}
}).fail(function () {
pending--;
fetched+=50;
progressbar(fetched, needsFetched);
if (!aborted) {
console.log('Some report data failed to load');
}
});
} catch (e) {
errorHandler(e);
}
}
function getCourseId() { //identifies course ID from URL
var courseId = null;
if (debug) console.log( "in getCourseId: window.location", window.location.href );
try {
var courseRegex = new RegExp('/courses/([0-9]+)');
var matches = courseRegex.exec(window.location.href);
if (matches) {
courseId = matches[1];
} else {
throw new Error('Unable to detect Course ID');
}
} catch (e) {
errorHandler(e);
}
return courseId;
}
function getdiscussionId() { //identifies quiz ID from URL
var discussionId = null;
if (debug) console.log( "in getdiscussionId: window.location", window.location.href );
try {
var quizRegex = new RegExp('/discussion_topics/([0-9]+)');
var matches = quizRegex.exec(window.location.href);
if (matches) {
discussionId = matches[1];
} else {
throw new Error('Unable to detect discussion ID');
}
} catch (e) {
errorHandler(e);
}
return discussionId;
}
function makeReport( courseId, discussionId ) { //generates CSV of data
var csv;
var discussionTitle="";
var tmpPost;
var tmpName;
var blob;
var savename;
try {
discussionTitle=document.title.split( ":" )[1].replace(/[^\w]/g, "");
} catch(e){
discussionTitle=document.title.replace(/[^\w]/g, "");
}
if (debug) console.log( "discussionTitle:", discussionTitle );
if (debug) console.log( "participants:", participants );
if (debug) console.log( "discussionPosts:", discussionPosts );
//put participants into student array
for (var i = 0; i < discussionPosts.length; i++) {
try{
tmpPost = discussionPosts[i].message.replace(/<\/?[^>]+(>|$)/g, "");
} catch(e){
tmpPost = "";
}
try {
tmpName = participants[discussionPosts[i].user_id].display_name.replace(/ /g, "");
} catch(e){
tmpName= "";
}
if ( tmpName !="" && tmpPost!="" ) {
//blob = new Blob([csv], {
// 'type': 'text/plain;charset=utf-8'
//});
savename = 'course-' + courseId + '-discussion-' + discussionTitle + '-' + tmpName + '-' + discussionPosts[i].id + '.txt';
if (debug) console.log( "savename:", savename );
if (debug) console.log( "post:", tmpPost );
zip.file( savename, tmpPost );
//saveAs(blob, savename);
//process reply array
if ( 'replies' in discussionPosts[i] ) {
processReplies( courseId, discussionTitle, tmpName, discussionPosts[i].replies );
}
}
}
zip.generateAsync({type:"blob"})
.then(function(content) {
// Force down of the Zip file
saveAs(content, discussionTitle+".zip");
});
}
function processReplies( courseId, discussionTitle, msgOwner, replyAr ){
var tmpReplyObj;
var tmpName;
var tmpPost;
var savename;
for (var i = 0; i < replyAr.length; i++) {
tmpReplyObj = replyAr[i];
try {
tmpName = participants[tmpReplyObj.user_id].display_name.replace(/ /g, "");
} catch(e){
tmpName= "";
}
try{
tmpPost = tmpReplyObj.message.replace(/<\/?[^>]+(>|$)/g, "");
} catch(e){
tmpPost = "";
}
if ( tmpName !="" && tmpPost!="" && tmpPost.length>20 ) {
// blob = new Blob([csv], {
// 'type': 'text/plain;charset=utf-8'
// });
savename = 'course-' + courseId + '-discussion-' + discussionTitle + '-' + tmpName + '-reply-' + msgOwner + '-' + tmpReplyObj.id + '.txt';
if (debug) console.log( "savename:", savename );
if (debug) console.log( "post:", tmpPost );
zip.file( savename, tmpPost );
//saveAs(blob, savename);
//process reply array
if ( 'replies' in tmpReplyObj ) {
processReplies( courseId, discussionTitle, tmpName, tmpReplyObj.replies );
}
}
}
}
function createDiscussionPost() {
var t="";
//loop through
return t;
}
////////////////////////
function excelDate(timestamp) {
var d;
try {
if (!timestamp) {
return '';
}
timestamp = timestamp.replace('Z', '.000Z');
var dt = new Date(timestamp);
if (typeof dt !== 'object') {
return '';
}
d = dt.getFullYear() + '-' + pad(1 + dt.getMonth()) + '-' + pad(dt.getDate()) + ' ' + pad(dt.getHours()) + ':' + pad(dt.getMinutes()) + ':' + pad(dt.getSeconds());
//d = ""+ pad(dt.getFullYear()-2000) + pad(1 + dt.getMonth()) + pad(dt.getDate()) + ' ' + pad(dt.getHours()) + ':' + pad(dt.getMinutes()) + ':' + pad(dt.getSeconds());
} catch (e) {
errorHandler(e);
}
return d;
function pad(n) {
return n < 10 ? '0' + n : n;
}
}
function progressbar(x, n) {
try {
if (typeof x === 'undefined' || typeof n == 'undefined') {
if ($('#jj_progress_dialog').length === 0) {
$('body').append('<div id="jj_progress_dialog"></div>');
$('#jj_progress_dialog').append('<div id="jj_progressbar"></div><small>It may take a few mins for large courses</small><br><small id="doing"></small>');
$('#jj_progress_dialog').dialog({
'title': 'Fetching Report',
'autoOpen': false,
'buttons': [
{
'text': 'Cancel',
'click': function () {
$('#quiz-submissions-report').one('click', {
type: 2
}, discussionSubmissionReport);
if (debug) console.log( "done set submission report link" );
$(this).dialog('close');
aborted = true;
abortAll();
resetData();
}
}
]
});
}
if ($('#jj_progress_dialog').dialog('isOpen')) {
$('#jj_progress_dialog').dialog('close');
} else {
$('#jj_progressbar').progressbar({
//'value': false
'value': 0
});
$('#jj_progress_dialog').dialog('open');
}
} else {
if (!aborted) {
var val = n > 0 ? Math.round(100 * x / n) : false;
$('#jj_progressbar').progressbar('option', 'value', val);
}
}
} catch (e) {
errorHandler(e);
}
}
function resetData(){
userData = {};
quiz_submissions = [];
attemptAr = new Object();
pending = - 1;
fetched = 0;
needsFetched = 0;
}
function errorHandler(e) {
console.log(e.name + ': ' + e.message);
}
}) ();