forked from instructure/canvas-lms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsubmission_list.rb
399 lines (354 loc) · 14.3 KB
/
submission_list.rb
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
#
# Copyright (C) 2011-12 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/>.
#
# Contains a dictionary of arrays with hashes in them. This is so that
# we can get all the submissions for a course grouped by date and
# ordered by date, person, then assignment. Since working with this is
# a loop in a loop in a loop, it gets a little awkward for controllers
# and views, so I contain it in a class with some helper methods. The
# dictionary comes from facets, a stable and mature library that's been
# around for years. A dictionary is just an ordered hash.
#
# To use this:
#
# s = SubmissionList.new(course)
# s.each {|e| # lists each submission hash in the right order }
# s.each_day {|e| # lists each day with an array of submission hashes }
#
# The submission hash has some very useful meta data in there:
#
# :grader => printable name of the grader, or Graded on submission if unknown
# :grader_id => user_id of the grader
# :previous_grade => the grade previous to this one, or nil
# :current_grade => the most current grade, the last submission for this assignment and student
# :new_grade => the new grade this submission received
# :assignment_name => a printable name of the assignment
# :student_user_id => the user_id of the student
# :course_id => the course id
# :assignment_id => the assignment id
# :student_name => a printable name of the student
# :graded_on => the day (not the time) the submission was made
# :score_before_regrade => the score prior to regrading
#
# The version data is actually pulled from some yaml storage through
# simply_versioned.
class SubmissionList
VALID_KEYS = [
:assignment_id, :assignment_name, :attachment_id, :attachment_ids,
:body, :course_id, :created_at, :current_grade, :current_graded_at,
:current_grader, :grade_matches_current_submission, :graded_at,
:graded_on, :grader, :grader_id, :group_id, :id, :new_grade,
:new_graded_at, :new_grader, :previous_grade, :previous_graded_at,
:previous_grader, :process_attempts, :processed, :published_grade,
:published_score, :safe_grader_id, :score, :student_entered_score,
:student_user_id, :submission_id, :student_name, :submission_type,
:updated_at, :url, :user_id, :workflow_state, :score_before_regrade
].freeze
class << self
# Shortcut for SubmissionList.each(course) { ... }
def each(course, &block)
sl = new(course)
sl.each(&block)
end
def each_day(course, &block)
sl = new(course)
sl.each_day(&block)
end
def days(course)
new(course).days
end
def submission_entries(course)
new(course).submission_entries
end
def list(course)
new(course).list
end
end
# The course
attr_reader :course
# The dictionary of submissions
attr_reader :list
def initialize(course)
raise ArgumentError, "Must provide a course." unless course && course.is_a?(Course)
@course = course
process
end
# An iterator on a sorted and filtered list of submission versions.
def each(&block)
self.submission_entries.each do |entry|
yield(entry)
end
end
# An iterator on the day only, not each submission
def each_day(&block)
self.list.each &block
end
# An array of days with an array of grader open structs for that day and course.
# TODO - This needes to be refactored, it is way slow and I cant figure out why.
def days
# start = Time.now
# current = Time.now
# puts "----------------------------------------------"
# puts "starting"
# puts "---------------------------------------------------------------------------------"
self.list.map do |day, value|
# puts "-----------------------------------------------item #{Time.now - current}----------------------------"
# current = Time.now
OpenObject.new(:date => day, :graders => graders_for_day(day))
end
# puts "----------------------------------------------"
# puts Time.now - start
# puts "---------------------------------------------------------------------------------"
# foo
end
# A filtered list of hashes of all submission versions that change the
# grade with all the meta data finally included. This list can be sorted
# and displayed.
def submission_entries
return @submission_entries if @submission_entries
@submission_entries = filtered_submissions.map do |s|
entry = current_grade_map[s[:id]]
s[:current_grade] = entry.grade
s[:current_graded_at] = entry.graded_at
s[:current_grader] = entry.grader
s
end
trim_keys(@submission_entries)
end
# A cleaner look at a SubmissionList
def inspect
"SubmissionList: course: #{self.course.name} submissions used: #{self.submission_entries.size} days used: #{self.list.keys.inspect} graders: #{self.graders.map(&:name).inspect}"
end
protected
# Returns an array of graders with an array of assignment open structs
def graders_for_day(day)
hsh = self.list[day].inject({}) do |h, submission|
grader = submission[:grader]
h[grader] ||= OpenObject.new(
:assignments => assignments_for_grader_and_day(grader, day),
:name => grader,
:grader_id => submission[:grader_id]
)
h
end
hsh.values
end
# Returns an array of assignments with an array of submission open structs.
def assignments_for_grader_and_day(grader, day)
start = Time.now
hsh = submission_entries.find_all {|e| e[:grader] == grader and e[:graded_on] == day}.inject({}) do |h, submission|
assignment = submission[:assignment_name]
h[assignment] ||= OpenObject.new(
:name => assignment,
:assignment_id => submission[:assignment_id],
:submissions => []
)
h[assignment].submissions << OpenObject.new(submission)
h
end
hsh.each do |k, v|
v.submission_count = v.submissions.size
end
# puts "-------------------------------Time Spent in assignments_for_grader_and_day: #{Time.now-start}-------------------------------"
hsh.values
end
# Produce @list, wich is a sorted, filtered, list of submissions with
# all the meta data we need and no banned keys included.
def process
@list = self.submission_entries.sort_by { |a| [a[:graded_at] ? -a[:graded_at].to_f : CanvasSort::Last, a[:safe_grader_id], a[:assignment_id]] }.
inject(Dictionary.new) do |d, se|
d[se[:graded_on]] ||= []
d[se[:graded_on]] << se
d
end
end
# A hash of the current grades of each submission, keyed by submission.id
def current_grade_map
@current_grade_map ||= self.course.submissions.inject({}) do |hash, submission|
grader = if submission.grader_id.present?
self.grader_map[submission.grader_id].try(:name)
end
grader ||= I18n.t('gradebooks.history.graded_on_submission', 'Graded on submission')
hash[submission.id] = OpenObject.new(:grade => submission.grade,
:graded_at => submission.graded_at,
:grader => grader)
hash
end
end
# Ensures that the final product only has approved keys in it. This
# makes our final product much more yummy.
def trim_keys(list)
list.each do |hsh|
hsh.delete_if { |key, v| ! VALID_KEYS.include?(key) }
end
end
# Creates a list of any submissions that change the grade. Adds:
# * previous_grade
# * previous_graded_at
# * previous_grader
# * new_grade
# * new_graded_at
# * new_grader
# * current_grade
# * current_graded_at
# * current_grader
def filtered_submissions
return @filtered_submissions if @filtered_submissions
# Sorts by submission then updated at in ascending order. So:
# submission 1 1/1/2009, submission 1 1/15/2009, submission 2 1/1/2009
full_hash_list.sort_by! { |a| [a[:id], a[:updated_at]] }
prior_submission_id, prior_grade, prior_score, prior_graded_at, prior_grader = nil
@filtered_submissions = full_hash_list.inject([]) do |l, h|
# Don't update prior_grade or prior_submission unless there is a grade.
# Also don't add it to the list. Just pass the list to the next
# iteration of the block.
next(l) unless h[:score]
# If the submission is different (not null for the first one, or just
# different than the last one), set the previous_grade to nil (this is
# the first version that changes a grade), set the new_grade to this
# version's grade, and add this to the list.
if prior_submission_id != h[:submission_id]
h[:previous_grade] = nil
h[:previous_graded_at] = nil
h[:previous_grader] = nil
h[:new_grade] = h[:grade]
h[:new_score] = h[:score]
h[:new_graded_at] = h[:graded_at]
h[:new_grader] = h[:grader]
l << h
# If the prior_grade is different than the grade for this version, the
# grade for this submission has been changed. That's because we know
# that this submission must be the same as the prior submission.
# Set the prevous grade and the new grade and add this to the list.
# Remove the old submission so that it doesn't show up twice in the
# grade history.
elsif prior_score != h[:score]
l.pop if prior_graded_at.try(:to_date) == h[:graded_at].try(:to_date) && prior_grader == h[:grader]
h[:previous_grade] = prior_grade
h[:previous_graded_at] = prior_graded_at
h[:previous_grader] = prior_grader
h[:new_grade] = h[:grade]
h[:new_score] = h[:score]
h[:new_graded_at] = h[:graded_at]
h[:new_grader] = h[:grader]
l << h
end
# At this point, we are only working with versions that have changed a
# grade. Go ahead and save that grade and save this version as the
# prior version and iterate.
prior_grade = h[:grade]
prior_score = h[:score]
prior_graded_at = h[:graded_at]
prior_grader = h[:grader]
prior_submission_id = h[:submission_id]
l
end
end
# A list of all versions in YAML format
def yaml_list
@yaml_list ||= self.course.submissions.map {|s| s.versions.map { |v| v.yaml } }.flatten
end
# A list of hashes. All the versions of all the submissions for a
# course, unfiltered and unsorted.
def raw_hash_list
@hash_list ||= begin
hash_list = yaml_list.map { |y| YAML.load(y).symbolize_keys }
add_regrade_info(hash_list)
end
end
# This method will add regrade details to the existing raw_hash_list
def add_regrade_info(hash_list)
quiz_submission_ids = hash_list.map { |y| y[:quiz_submission_id]}.compact
return hash_list if quiz_submission_ids.blank?
quiz_submissions = Quizzes::QuizSubmission.where("id IN (?) AND score_before_regrade IS NOT NULL", quiz_submission_ids)
quiz_submissions.each do |qs|
matches = hash_list.select { |a| a[:id] == qs.submission_id}
matches.each do |h|
h[:score_before_regrade] = qs.score_before_regrade
h[:grader_id] = 'regraded'
end
end
hash_list
end
# Still a list of unsorted, unfiltered hashes, but the meta data is inserted at this point
def full_hash_list
@full_hash_list ||= self.raw_hash_list.map do |h|
h[:grader] = if h[:grader_id] && self.grader_map[h[:grader_id]]
self.grader_map[h[:grader_id]].name
elsif h[:grader_id] == 'regraded'
I18n.t('gradebooks.history.regraded', "Regraded")
else
I18n.t('gradebooks.history.graded_on_submission', 'Graded on submission')
end
h[:safe_grader_id] = h[:grader_id] ? h[:grader_id] : 0
h[:assignment_name] = self.assignment_map[h[:assignment_id]].title
h[:student_user_id] = h[:user_id]
h[:student_name] = self.student_map[h[:user_id]].name
h[:course_id] = self.course.id
h[:submission_id] = h[:id]
h[:graded_on] = h[:graded_at].in_time_zone.to_date if h[:graded_at]
h
end
end
# A unique list of all grader ids
def all_grader_ids
@all_grader_ids ||= raw_hash_list.map { |e| e[:grader_id] }.uniq.compact
end
# A complete list of all graders that have graded submissions for this
# course as User models
def graders
@graders ||= User.where(:id => all_grader_ids).all
end
# A hash of graders by their ids, for easy lookup in full_hash_list
def grader_map
@grader_map ||= graders.inject({}) do |h, g|
h[g.id] = g
h
end
end
# A unique list of all student ids
def all_student_ids
@all_student_ids ||= raw_hash_list.map { |e| e[:user_id] }.uniq.compact
end
# A complete list of all students that have submissions for this course
# as User models
def students
@students ||= User.where(:id => all_student_ids).all
end
# A hash of students by their ids, for easy lookup in full_hash_list
def student_map
@student_map ||= students.inject({}) do |h, s|
h[s.id] = s
h
end
end
# A unique list of all assignment ids
def all_assignment_ids
@all_assignment_ids ||= raw_hash_list.map { |e| e[:assignment_id] }.uniq.compact
end
# A complete list of assignments that have submissions for this course
def assignments
@assignments ||= Assignment.where(:id => all_assignment_ids).all
end
# A hash of assignments by their ids, for easy lookup in full_hash_list
def assignment_map
@assignment_map ||= assignments.inject({}) do |h, a|
h[a.id] = a
h
end
end
end