/
instructor_tool.js
480 lines (401 loc) · 15.4 KB
/
instructor_tool.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
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
function InstructorToolBlock(runtime, element) {
'use strict';
var $element = $(element);
// Pagination
$(document).ajaxSend(function(event, jqxhr, options) {
if (options.url.indexOf('get_result_page') !== -1) {
options.data = JSON.stringify(options.data);
}
});
var Result = Backbone.Model.extend({
initialize: function(attrs, options) {
_.each(_.zip(Result.properties, options.values), function(pair) {
this.set(pair[0], pair[1]);
}, this);
}
}, { properties: ['section', 'subsection', 'unit', 'type', 'question', 'answer', 'username'] });
var Results = Backbone.PageableCollection.extend({
model: Result,
state: {
order: 0
},
url: runtime.handlerUrl(element, 'get_result_page'),
parseState: function(response) {
return {
totalRecords: response.num_results,
pageSize: response.page_size
};
},
parseRecords: function(response) {
return _.map(response.display_data, function(row) {
return new Result(null, { values: row });
});
},
fetchOptions: {
reset: true,
type: 'POST',
contentType: 'application/json',
processData: false
},
getFirstPage: function() {
Backbone.PageableCollection.prototype
.getFirstPage.call(this, this.fetchOptions);
},
getPreviousPage: function() {
Backbone.PageableCollection.prototype
.getPreviousPage.call(this, this.fetchOptions);
},
getNextPage: function() {
Backbone.PageableCollection.prototype
.getNextPage.call(this, this.fetchOptions);
},
getLastPage: function() {
Backbone.PageableCollection.prototype
.getLastPage.call(this, this.fetchOptions);
},
getCurrentPage: function() {
return this.state.currentPage;
},
getTotalPages: function() {
return this.state.totalPages;
}
});
var ResultsView = Backbone.View.extend({
initialize: function() {
this.listenTo(this.collection, 'reset', this.render);
this.listenTo(this, 'rendered', this._show);
this.listenTo(this, 'processing', this._hide);
this.listenTo(this, 'error', this._hide);
this.listenTo(this, 'update', this._updateInfo);
},
render: function() {
this._insertRecords();
this._updateControls();
this.$('#total-pages').text(this.collection.getTotalPages() || 0);
this.trigger('rendered');
return this;
},
_insertRecords: function() {
var tbody = this.$('tbody');
tbody.empty();
this.collection.each(function(result, index) {
var row = $('<tr>');
_.each(Result.properties, function(name) {
row.append($('<td>').text(result.get(name)));
});
tbody.append(row);
}, this);
if (this.collection.getTotalPages()) {
this.$('#current-page').text(this.collection.getCurrentPage());
} else {
this.$('#current-page').text(0);
}
},
_show: function() {
this.$el.show(700);
},
_hide: function() {
this.$el.hide();
},
_updateInfo: function(info) {
var $exportInfo = this.$('.data-export-info');
$exportInfo.empty();
$exportInfo.append($('<p>').text(info));
},
events: {
'click #first-page': '_firstPage',
'click #prev-page': '_prevPage',
'click #next-page': '_nextPage',
'click #last-page': '_lastPage'
},
_firstPage: function() {
this.collection.getFirstPage();
this._updateControls();
},
_prevPage: function() {
if (this.collection.hasPreviousPage()) {
this.collection.getPreviousPage();
}
this._updateControls();
},
_nextPage: function() {
if (this.collection.hasNextPage()) {
this.collection.getNextPage();
}
this._updateControls();
},
_lastPage: function() {
this.collection.getLastPage();
this._updateControls();
},
_updateControls: function() {
var currentPage = this.collection.getCurrentPage(),
totalPages = this.collection.getTotalPages() || 0,
backward = ["#first-page", "#prev-page"],
forward = ["#next-page", "#last-page"];
this._enable(backward, currentPage > 1);
this._enable(forward, currentPage < totalPages);
},
_enable: function(controls, condition) {
_.each(controls, function(control) {
this.$(control).prop('disabled', !condition);
}, this);
}
});
var resultsView = new ResultsView({
collection: new Results([]),
el: $element.find('#results')
});
// Status area
var StatusView = Backbone.View.extend({
initialize: function() {
this.listenTo(this, 'processing', this._showSpinner);
this.listenTo(this, 'notify', this._displayMessage);
this.listenTo(this, 'stopped', this._empty);
this.listenTo(resultsView, 'rendered', this._empty);
},
_showSpinner: function() {
this.$el.empty();
this.$el.append(
$('<i>').addClass('icon fa fa-spinner fa-spin')
).css('text-align', 'center');
},
_displayMessage: function(message) {
this.$el.append($('<p>').text(message));
},
_empty: function() {
this.$el.empty();
}
});
var statusView = new StatusView({
el: $element.find('.data-export-status')
});
// Set up gettext in case it isn't available in the client runtime:
if (typeof gettext == "undefined") {
window.gettext = function gettext_stub(string) { return string; };
window.ngettext = function ngettext_stub(strA, strB, n) { return n == 1 ? strA : strB; };
}
var $startButton = $element.find('.data-export-start');
var $cancelButton = $element.find('.data-export-cancel');
var $downloadButton = $element.find('.data-export-download');
var $deleteButton = $element.find('.data-export-delete');
var $blockTypes = $element.find("select[name='block_types']");
var $rootBlockId = $element.find("select[name='root_block_id']");
var $usernames = $element.find("input[name='usernames']");
var $matchString = $element.find("input[name='match_string']");
var $resultTable = $element.find('.data-export-results');
var status;
function getStatus() {
$.ajax({
type: 'POST',
url: runtime.handlerUrl(element, 'get_status'),
data: '{}',
success: updateStatus,
dataType: 'json'
});
}
function updateStatus(newStatus) {
var statusChanged = ! _.isEqual(newStatus, status);
status = newStatus;
if (status.export_pending) {
// Keep polling for status updates when an export is running.
setTimeout(getStatus, 1000);
}
if (statusChanged) updateView();
}
// Block types with answers we can export
var questionBlockTypes = ['pb-mcq', 'pb-mrq', 'pb-rating', 'pb-answer'];
// Fetch this course's blocks from the REST API, and add them to the
// list of blocks in the Section/Question drop-down list.
function getCourseBlocks() {
$.ajax({
type: 'GET',
url: $rootBlockId.data('course-blocks-api'),
data: {
course_id: $element.data('course-id'),
requested_fields: 'name,display_name,block_type,children',
student_view_data: questionBlockTypes.join(','),
all_blocks: true,
depth: 'all'
},
success: updateBlockOptions,
dataType: 'json'
});
}
// Appends the blocks returned by the Course Blocks API as options for
// the Section/Question drop-down list, arranged as a tree.
function updateBlockOptions(data) {
// Constructs an <option> element from the given block to add to the
// list of root blocks.
//
// Uses the block's:
// * question, name, or display name as the label.
// * depth in the course to indent the label, to make the tree
// structure more visible.
// * 'enabled' attribute to decide whether the <option>
// element is selectable, i.e. available as a download filter.
//
// Returns the <option> element so that it can be enabled later,
// if it's found to have a descendant that is enabled.
var appendBlock = function(block) {
var blockId = block.id,
padding = Array(2*block.depth).join(' '),
disabled = (block.enabled ? undefined : 'disabled'),
labelAttr,
label,
$option;
// Merge any fields exposed by student_view_data, so they can be
// candidates for the label attribute.
block = _.extend(block, block['student_view_data']);
// Find the best label attribute available for the block.
labelAttr = _.find(
['question', 'name', 'display_name'],
function(attr) {
return block[attr];
}
);
label = padding + (block[labelAttr] || blockId);
$option = $('<option>', {value: blockId, html: label, disabled: disabled});
$rootBlockId.append($option);
return $option;
},
// Builds the tree of course blocks.
buildTree = function(block, ancestors) {
// Omit pb-choice blocks
if (block.type == 'pb-choice') return;
// Enable the exportable blocks, and their ancestors.
if (_.contains(questionBlockTypes, block.type)) {
block.enabled = true;
for (var i = ancestors.length; i > 0; --i) {
var ancestor = ancestors[i-1];
// No need to continue; these ancestors are already enabled.
if (ancestor.enabled) break;
ancestor.enabled = true;
ancestor.element.removeAttr('disabled');
}
}
block.depth = ancestors.length;
block.element = appendBlock(block);
// Recurse over all the child blocks, including the current block as an ancestor.
var childAncestors = ancestors.concat([block]);
_.each(block.children, function(child_id) {
buildTree(data.blocks[child_id], childAncestors);
});
},
root = data.blocks[data.root];
// Label the root block as "All"
root.name = gettext('All');
// Remove any existing options
$rootBlockId.empty();
// Build the course blocks tree from the root.
buildTree(root, []);
}
function disableActions() {
$startButton.prop('disabled', true);
$cancelButton.prop('disabled', true);
$downloadButton.prop('disabled', true);
$deleteButton.prop('disabled', true);
}
function showInfo(info) {
resultsView.trigger('update', info);
}
function showResults() {
if (status.last_export_result) {
$resultTable.show();
}
}
function hideResults() {
resultsView.trigger('processing');
}
function showSpinner() {
statusView.trigger('processing');
}
function hideSpinner() {
statusView.trigger('stopped');
}
function showStatusMessage(message) {
statusView.trigger('notify', message);
}
function handleError(data) {
// Shim to make the XBlock JsonHandlerError response work with our format.
status = {'last_export_result': JSON.parse(data.responseText), 'export_pending': false};
updateView();
}
function updateView() {
var startTime;
$startButton.toggle(!status.export_pending).prop('disabled', false);
$cancelButton.toggle(status.export_pending).prop('disabled', false);
$downloadButton.toggle(Boolean(status.download_url)).prop('disabled', false);
$deleteButton.toggle(Boolean(status.last_export_result)).prop('disabled', false);
if (status.last_export_result) {
if (status.last_export_result.error) {
hideResults();
hideSpinner();
showStatusMessage(_.template(
gettext('Data export failed. Reason: <%= error %>'),
{'error': status.last_export_result.error}
));
} else {
startTime = new Date(status.last_export_result.start_timestamp * 1000);
showInfo(
_.template(
ngettext(
'Results retrieved on <%= creation_time %> (<%= seconds %> second).',
'Results retrieved on <%= creation_time %> (<%= seconds %> seconds).',
status.last_export_result.generation_time_s.toFixed(1)
),
{
'creation_time': startTime.toString(),
'seconds': status.last_export_result.generation_time_s.toFixed(1)
}
));
resultsView.collection.getFirstPage();
}
} else {
if (status.export_pending) {
showStatusMessage(gettext('The report is currently being generated…'));
} else {
hideSpinner();
}
}
}
function addHandler($button, handlerName, form_submit) {
$button.on('click', function() {
var data;
if (form_submit) {
data = {
block_types: $blockTypes.val(),
root_block_id: $rootBlockId.val(),
usernames: $usernames.val(),
match_string: $matchString.val()
};
data = JSON.stringify(data);
} else {
data = '{}';
}
$.ajax({
type: 'POST',
url: runtime.handlerUrl(element, handlerName),
data: data,
success: updateStatus,
error: handleError,
dataType: 'json'
});
showSpinner();
disableActions();
});
}
addHandler($startButton, 'start_export', true);
addHandler($cancelButton, 'cancel_export');
addHandler($deleteButton, 'delete_export');
$startButton.on('click', hideResults);
$cancelButton.on('click', showResults);
$deleteButton.on('click', hideResults);
$downloadButton.on('click', function() {
window.location.href = status.download_url;
});
showSpinner();
disableActions();
getCourseBlocks();
getStatus();
}