forked from instructure/canvas-lms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgradebook_uploads.js
More file actions
385 lines (345 loc) · 14.6 KB
/
Copy pathgradebook_uploads.js
File metadata and controls
385 lines (345 loc) · 14.6 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
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
/**
* Copyright (C) 2011 Instructure, Inc.
*
* This file is part of Canvas.
*
* Canvas is free software: you can redistribute it and/or modify it under
* the terms of the GNU Affero General Public License as published by the Free
* Software Foundation, version 3 of the License.
*
* Canvas is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
* A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
* details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
define([
'i18n!gradebook',
'jquery',
'underscore',
'str/htmlEscape',
'jsx/shared/helpers/numberHelper',
'jsx/gradebook/uploads/wait_for_processing',
'jsx/gradebook/uploads/process_gradebook_upload',
'jsx/gradebook/shared/helpers/GradeFormatHelper',
'vendor/slickgrid' /* global Slick */,
'vendor/slickgrid/slick.editors' /* global.Slick.Editors */,
'jquery.instructure_forms' /* errorBox */,
'jquery.instructure_misc_helpers' /* /\.detect/ */,
'jquery.templateData' /* fillTemplateData */
], function (I18n, $, _, htmlEscape, numberHelper, waitForProcessing, ProcessGradebookUpload, GradeFormatHelper) {
var GradebookUploader = {
createGeneralFormatter: function (attribute) {
return function (row, cell, value) {
return value ? value[attribute] : '';
}
},
createNumberFormatter: function (attribute) {
return function (row, cell, value) {
return value ? GradeFormatHelper.formatGrade(value[attribute]) : '';
}
},
init: function(uploadedGradebook) {
var gradebookGrid,
$gradebook_grid = $("#gradebook_grid"),
$gradebook_grid_header = $("#gradebook_grid_header"),
rowsToHighlight = [],
self = this;
var gridData = {
columns: [
{
id:"student",
name:I18n.t('student', "Student"),
field:"student",
width:250,
cssClass:"cell-title",
formatter: self.createGeneralFormatter('name')
}
],
options: {
enableAddRow: false,
editable: true,
enableColumnReorder: false,
asyncEditorLoading: true,
rowHeight: 30
},
data: []
};
var labelData = {
columns: [{
id: 'assignmentGrouping',
name: '',
field: 'assignmentGrouping',
width: 250
}],
options: {
enableAddRow: false,
enableColumnReorder: false,
asyncEditorLoading: false
},
data: []
};
delete uploadedGradebook.missing_objects;
delete uploadedGradebook.original_submissions;
$.each(uploadedGradebook.assignments, function(){
var newGrade = {
id: this.id,
name: htmlEscape(I18n.t('To')),
field: this.id,
width: 125,
editor: Slick.Editors.UploadGradeCellEditor,
formatter: self.createNumberFormatter('grade'),
active: true,
previous_id: this.previous_id,
cssClass: "new-grade"
};
if (this.grading_type !== 'letter_grade') {
newGrade.editorFormatter = function (grade) {
return GradeFormatHelper.formatGrade(grade, {defaultValue: ''});
};
newGrade.editorParser = GradeFormatHelper.delocalizeGrade;
}
var conflictingGrade = {
id: this.id + "_conflicting",
width: 125,
formatter: self.createNumberFormatter('original_grade'),
field: this.id + "_conflicting",
name: htmlEscape(I18n.t('From')),
cssClass: 'conflicting-grade'
};
var assignmentHeaderColumn = {
id: this.id,
width: 250,
name: htmlEscape(this.title),
headerCssClass: "assignment"
};
labelData.columns.push(assignmentHeaderColumn);
gridData.columns.push(conflictingGrade);
gridData.columns.push(newGrade);
});
$.each(uploadedGradebook.students, function(index){
var row = {
student : this,
id : this.id
};
$.each(this.submissions, function(){
var originalGrade = parseInt(this.original_grade),
updatedGrade = parseInt(this.grade),
updateWillRemoveGrade = !isNaN(originalGrade) && isNaN(updatedGrade);
if ( (originalGrade > updatedGrade || updateWillRemoveGrade) &&
((this.grade || "").toUpperCase() !== "EX") ) {
rowsToHighlight.push({rowIndex: index, id: this.assignment_id});
}
row['assignmentId'] = this.assignment_id;
row[this.assignment_id] = this;
row[this.assignment_id + "_conflicting"] = this;
});
gridData.data.push(row);
row.active = true;
});
// if there are still assignments with changes detected.
if (gridData.columns.length > 1) {
if (uploadedGradebook.unchanged_assignments) {
$("#assignments_without_changes_alert").show();
}
var $gradebookGridForm = $("#gradebook_grid_form");
$gradebookGridForm.submit(function(e){
e.preventDefault();
$gradebookGridForm.disableWhileLoading(
ProcessGradebookUpload.upload(uploadedGradebook)
);
}).show();
$(window).resize(function(){
$gradebook_grid.height( $(window).height() - $gradebook_grid.offset().top - 150 );
var width = ((gridData.columns.length - 1) * 125) + 250;
$gradebook_grid.parent().width(width);
}).triggerHandler("resize");
gradebookGrid = new Slick.Grid($gradebook_grid, gridData.data, gridData.columns, gridData.options);
new Slick.Grid($gradebook_grid_header, labelData.data, labelData.columns, labelData.options);
gradebookGrid.onColumnHeaderClick = function(columnDef) { /*do nothing*/};
var gradeReviewRow = {};
for(var i = 0; i < rowsToHighlight.length; i++) {
var id = rowsToHighlight[i].id,
rowIndex = rowsToHighlight[i].rowIndex,
rowIndex= rowsToHighlight[i].rowIndex,
conflictingId = id + "_conflicting";
gradeReviewRow[rowIndex] = gradeReviewRow[rowIndex] || {};
gradeReviewRow[rowIndex][id] = 'right-highlight';
gradeReviewRow[rowIndex][conflictingId] = 'left-highlight';
gradebookGrid.invalidateRow(rowIndex);
}
gradebookGrid.setCellCssStyles("highlight-grade-change", gradeReviewRow);
gradebookGrid.render();
}
else {
$("#no_changes_detected").show();
}
if (uploadedGradebook.warning_messages.prevented_new_assignment_creation_in_closed_period) {
$("#prevented-new-assignment-in-closed-period").show();
}
if (uploadedGradebook.warning_messages.prevented_grading_ungradeable_submission) {
$("#prevented-grading-ungradeable-submission").show();
}
},
handleThingsNeedingToBeResolved: function() {
var processingDfd = waitForProcessing(ENV.progress);
processingDfd.fail(function(msg) {
alert(msg);
window.location = ENV.new_gradebook_upload_path;
});
processingDfd.done(function(uploadedGradebook) {
var needingReview = {},
possibilitiesToMergeWith = {};
// first, figure out if there is anything that needs to be resolved
$.each(["student", "assignment"], function(i, thing){
var $template = $("#" + thing + "_resolution_template").remove(),
$select = $template.find("select");
needingReview[thing] = [];
$.each(uploadedGradebook[thing+"s"], function(){
if (!this.previous_id) {
needingReview[thing].push(this);
}
});
if (needingReview[thing].length) {
$select.change(function(){
$(this).next(".points_possible_section").css({opacity: 0});
if($(this).val() > 0) { //if the thing that was selected is an id( not ignore or add )
$("#" + thing + "_resolution_template select option").removeAttr("disabled");
$("#" + thing + "_resolution_template select").each(function(){
if($(this).val() != "ignore" ) {
$("#" + thing + "_resolution_template select").not(this).find("option[value='" + $(this).val() + "']").attr("disabled", true);
}
});
}
else if ( $(this).val() === "new" ) {
$(this).next(".points_possible_section").css({opacity: 1});
}
});
$.each(uploadedGradebook.missing_objects[thing + 's'], function() {
$('<option value="' + this.id + '" >' + htmlEscape(this.name || this.title) + '</option>').appendTo($select);
});
$.each(needingReview[thing], function(i, record){
$template
.clone(true)
.fillTemplateData({
iterator: record.id,
data: {
name: record.name,
title: record.title,
points_possible: I18n.n(record.points_possible)
}
})
.appendTo("#gradebook_importer_resolution_section ." + thing + "_section table tbody")
.show()
.find("input.points_possible")
.change(function(){
var $this = $(this);
record.points_possible = numberHelper.parse($this.val());
$this.val(I18n.n(record.points_possible));
});
});
$("#gradebook_importer_resolution_section, #gradebook_importer_resolution_section ." + thing + "_section").show();
}
});
// end figuring out if thigs need to be resolved
if ( needingReview.student.length || needingReview.assignment.length ) {
// if there are things that need to be resolved, set up stuff for that form
$("#gradebook_importer_resolution_section").submit(function(e){
var returnFalse = false;
e.preventDefault();
$(this).find("select").each(function(){
if( !$(this).val() ) {
returnFalse = true;
$(this).errorBox(I18n.t('errors.select_an_option', "Please select an option"));
return false;
}
});
if(returnFalse) return false;
$(this).find("select").each(function(){
var $select = $(this),
parts = $select.attr("name").split("_"),
thing = parts[0],
id = parts[1],
val = $select.val();
switch(val){
case "new":
//do nothing
break;
case "ignore":
//remove the entry from the uploaded gradebook
for (var i in uploadedGradebook[thing+"s"]) {
if (id == uploadedGradebook[thing+"s"][i].id) {
uploadedGradebook[thing+"s"].splice(i, 1);
break;
}
}
break;
default:
//merge
var obj = _.detect(uploadedGradebook[thing+"s"], function(thng){
return id == thng.id;
});
obj.id = obj.previous_id = val;
if (thing === 'assignment') {
// find the original grade for this assignment for each student
$.each(uploadedGradebook['students'], function() {
var student = this;
var submission = _.detect(student.submissions, function(thng) {
return thng.assignment_id == id;
});
submission.assignment_id = val;
var original_submission = _.detect(uploadedGradebook.original_submissions, function(sub) {
return sub.user_id == student.id && sub.assignment_id == val;
});
if (original_submission) {
submission.original_grade = I18n.n(original_submission.score);
}
});
} else if (thing === 'student') {
// find the original grade for each assignment for this student
$.each(obj.submissions, function() {
var submission = this;
var original_submission = _.detect(uploadedGradebook.original_submissions, function(sub) {
return sub.user_id == obj.id && sub.assignment_id == submission.assignment_id;
});
if (original_submission) {
submission.original_grade = I18n.n(original_submission.score);
}
});
}
}
});
// remove assignments that have no changes
var indexes_to_delete = [];
$.each(uploadedGradebook.assignments, function(index){
if(uploadedGradebook.assignments[index].previous_id && _.all(uploadedGradebook.students, function(student){
var submission = student.submissions[index];
return parseFloat(submission.original_grade) == parseFloat(submission.grade) || (!submission.original_grade && !submission.grade);
})) {
indexes_to_delete.push(index);
}
});
_.each(indexes_to_delete.reverse(), function(index) {
uploadedGradebook.assignments.splice(index, 1);
$.each(uploadedGradebook.students, function() {
this.submissions.splice(index, 1);
});
});
if (indexes_to_delete.length != 0) {
uploadedGradebook.unchanged_assignments = true;
}
$(this).hide();
GradebookUploader.init(uploadedGradebook);
});
}
else {
// if there is nothing that needs to resolved, just skip to initialize slick grid.
GradebookUploader.init(uploadedGradebook);
}
});
}
};
return GradebookUploader;
});