forked from instructure/canvas-lms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSetDefaultGradeDialog.coffee
More file actions
98 lines (85 loc) · 3.33 KB
/
Copy pathSetDefaultGradeDialog.coffee
File metadata and controls
98 lines (85 loc) · 3.33 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
define [
'i18n!gradebook'
'jquery'
'jst/SetDefaultGradeDialog'
'underscore'
'jquery.disableWhileLoading'
'jquery.instructure_forms'
'jqueryui/dialog'
'jquery.instructure_misc_plugins'
'vendor/jquery.ba-tinypubsub'
'compiled/jquery/fixDialogButtons'
# this is a partial needed by the 'SetDefaultGradeDialog' template
# since you cant declare a dependency in a handlebars file, we need to do it here
'jst/_grading_box'
], (I18n, $, setDefaultGradeDialogTemplate, _) ->
PAGE_SIZE = 50
class SetDefaultGradeDialog
constructor: ({@assignment, @students, @context_id, @selected_section}) ->
show: =>
templateLocals =
assignment: @assignment
showPointsPossible: (@assignment.points_possible || @assignment.points_possible == '0') && @assignment.grading_type != "gpa_scale"
url: "/courses/#{@context_id}/gradebook/update_submission"
inputName: 'default_grade'
templateLocals["assignment_grading_type_is_#{@assignment.grading_type}"] = true
@$dialog = $(setDefaultGradeDialogTemplate(templateLocals))
@$dialog.dialog(
resizable: false
width: 350
open: => @$dialog.find(".grading_box").focus()
close: => @$dialog.remove()
).fixDialogButtons()
$form = @$dialog
$(".ui-dialog-titlebar-close").focus()
$form.submit (e) =>
e.preventDefault()
formData = $form.getFormData()
if @gradeIsExcused(formData.default_grade)
$.flashError I18n.t('Default grade cannot be set to %{ex}', { ex: 'EX' })
else
submittingDfd = $.Deferred()
$form.disableWhileLoading(submittingDfd)
students = getStudents()
pages = (students.splice 0, PAGE_SIZE while students.length)
postDfds = pages.map (page) =>
studentParams = getParams(page, formData.default_grade)
params = _.extend {}, studentParams,
dont_overwrite_grades: not formData.overwrite_existing_grades
$.ajaxJSON $form.attr("action"), "POST", params
$.when(postDfds...).then (responses...) =>
responses = [responses] if postDfds.length == 1
submissions = getSubmissions(responses)
$.publish 'submissions_updated', [submissions]
alert(I18n.t 'alerts.scores_updated'
,
one: '1 Student score updated'
other: '%{count} Student scores updated'
,
count: submissions.length)
submittingDfd.resolve()
$("#set_default_grade").focus()
@$dialog.remove()
getStudents = =>
if @selected_section
_(@students).filter (s) =>
_.include(s.sections, @selected_section)
else
_(@students).values()
getParams = (page, grade) =>
_.chain(page)
.map (s) =>
prefix = "submissions[submission_#{s.id}]"
[["#{prefix}[assignment_id]", @assignment.id],
["#{prefix}[user_id]", s.id],
["#{prefix}[grade]", grade]]
.flatten(true)
.object()
.value()
getSubmissions = (responses) =>
_.chain(responses)
.map ([response, __]) ->
[s.submission for s in response]
.flatten().value()
gradeIsExcused: (grade) ->
_.isString(grade) && grade.toUpperCase() == 'EX'