forked from instructure/canvas-lms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathassignment_override.rb
More file actions
363 lines (307 loc) · 10.7 KB
/
Copy pathassignment_override.rb
File metadata and controls
363 lines (307 loc) · 10.7 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
#
# Copyright (C) 2012 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/>.
#
class AssignmentOverride < ActiveRecord::Base
include Workflow
include TextHelper
NOOP_MASTERY_PATHS = 1
SET_TYPE_NOOP = 'Noop'.freeze
simply_versioned :keep => 10
attr_accessor :dont_touch_assignment, :preloaded_student_ids, :changed_student_ids
belongs_to :assignment
belongs_to :quiz, class_name: 'Quizzes::Quiz'
belongs_to :set, :polymorphic => true
has_many :assignment_override_students, :dependent => :destroy, :validate => false
validates_presence_of :assignment_version, :if => :assignment
validates_presence_of :title, :workflow_state
validates :set_type, inclusion: ['CourseSection', 'Group', 'ADHOC', SET_TYPE_NOOP]
validates_length_of :title, :maximum => maximum_string_length, :allow_nil => true
concrete_set = lambda{ |override| ['CourseSection', 'Group'].include?(override.set_type) }
validates_presence_of :set, :set_id, :if => concrete_set
validates_uniqueness_of :set_id, :scope => [:assignment_id, :set_type, :workflow_state],
:if => lambda{ |override| override.assignment? && override.active? && concrete_set.call(override) }
validates_uniqueness_of :set_id, :scope => [:quiz_id, :set_type, :workflow_state],
:if => lambda{ |override| override.quiz? && override.active? && concrete_set.call(override) }
validate :if => concrete_set do |record|
if record.set && record.assignment && record.active?
case record.set
when CourseSection
record.errors.add :set, "not from assignment's course" unless record.set.course_id == record.assignment.context_id
when Group
valid_group_category_id = record.assignment.group_category_id || record.assignment.discussion_topic.try(:group_category_id)
record.errors.add :set, "not from assignment's group category" unless record.set.group_category_id == valid_group_category_id
end
end
end
validate :set_id, :unless => concrete_set do |record|
if record.set_type == 'ADHOC' && !record.set_id.nil?
record.errors.add :set_id, "must be nil with set_type ADHOC"
end
end
validate do |record|
if [record.assignment, record.quiz].all?(&:nil?)
record.errors.add :base, "assignment or quiz required"
end
end
validate do |record|
record.assignment_override_students.each do |s|
next if s.valid?
s.errors.each do |_, error|
record.errors.add(:assignment_override_students, error.type,
message: error.message)
end
end
end
after_save :update_cached_due_dates
after_save :touch_assignment, :if => :assignment
after_save :update_grading_period_grades
def set_not_empty?
overridable = assignment? ? assignment : quiz
['CourseSection', 'Group', SET_TYPE_NOOP].include?(self.set_type) ||
(set.any? && overridable.context.current_enrollments.where(user_id: set).exists?)
end
def update_grading_period_grades
return true unless due_at_overridden && due_at_changed? && !id_changed?
course = assignment&.context || quiz&.context || quiz&.assignment&.context
return true unless course&.grading_periods?
grading_period_was = GradingPeriod.for_date_in_course(date: due_at_was, course: course)
grading_period = GradingPeriod.for_date_in_course(date: due_at, course: course)
return true if grading_period_was&.id == grading_period&.id
students = applies_to_students.map(&:id)
return true if students.blank?
[grading_period_was, grading_period].compact.each do |gp|
course.recompute_student_scores(students, grading_period_id: gp)
end
true
end
private :update_grading_period_grades
def update_cached_due_dates
return unless assignment?
if due_at_overridden_changed? ||
(due_at_overridden && due_at_changed?) ||
(due_at_overridden && workflow_state_changed?)
DueDateCacher.recompute(assignment)
end
end
def touch_assignment
return true if assignment.nil? || dont_touch_assignment
assignment.touch
end
private :touch_assignment
def assignment?; !!assignment_id; end
def quiz?; !!quiz_id; end
workflow do
state :active
state :deleted
end
alias_method :destroy_permanently!, :destroy
def destroy
transaction do
self.assignment_override_students.reload.destroy_all
self.workflow_state = 'deleted'
self.save!
end
end
scope :active, -> { where(:workflow_state => 'active') }
scope :visible_students_only, -> (visible_ids) do
scope = select("assignment_overrides.*").
joins(:assignment_override_students).
distinct
if ActiveRecord::Relation === visible_ids
column = visible_ids.klass == User ? :id : visible_ids.select_values.first
scope = scope.primary_shard.activate {
scope.joins("INNER JOIN #{visible_ids.klass.quoted_table_name} ON assignment_override_students.user_id=#{visible_ids.klass.table_name}.#{column}")
}
next scope.merge(visible_ids.except(:select))
end
scope.where(
assignment_override_students: { user_id: visible_ids },
)
end
before_validation :default_values
def default_values
self.set_type ||= 'ADHOC'
if assignment
self.assignment_version = assignment.version_number
self.quiz = assignment.quiz
self.quiz_version = quiz.version_number if quiz
elsif quiz
self.quiz_version = quiz.version_number
self.assignment = quiz.assignment
self.assignment_version = assignment.version_number if assignment
end
set_title_if_needed
end
protected :default_values
def mastery_paths?
set_type == SET_TYPE_NOOP && set_id == NOOP_MASTERY_PATHS
end
# override set read accessor and set_id read/write accessors so that reading
# set/set_id or setting set_id while set_type=ADHOC doesn't try and find the
# ADHOC model
def set_id
read_attribute(:set_id)
end
def set
if self.set_type == 'ADHOC'
assignment_override_students.preload(:user).map(&:user)
elsif self.set_type == SET_TYPE_NOOP
nil
else
super
end
end
def set_id=(id)
if ['ADHOC', SET_TYPE_NOOP].include? self.set_type
write_attribute(:set_id, id)
else
super
end
end
def self.override(field)
define_method "override_#{field}" do |value|
send("#{field}_overridden=", true)
send("#{field}=", value)
end
define_method "clear_#{field}_override" do
send("#{field}_overridden=", false)
send("#{field}=", nil)
end
validates_inclusion_of "#{field}_overridden", :in => [false, true]
before_validation do |override|
if override.send("#{field}_overridden").nil?
override.send("#{field}_overridden=", false)
end
true
end
scope "overriding_#{field}", -> { where("#{field}_overridden" => true) }
end
def visible_student_overrides(visible_student_ids)
assignment_override_students.where(user_id: visible_student_ids).exists?
end
def self.visible_enrollments_for(overrides, user=nil)
return Enrollment.none if overrides.empty? || user.nil?
override = overrides.first
(override.assignment || override.quiz).context.enrollments_visible_to(user)
end
OVERRIDDEN_DATES = %i(due_at unlock_at lock_at).freeze
OVERRIDDEN_DATES.each do |field|
override field
end
def self.overridden_dates
OVERRIDDEN_DATES
end
def due_at=(new_due_at)
new_due_at = CanvasTime.fancy_midnight(new_due_at)
new_all_day, new_all_day_date = Assignment.all_day_interpretation(
:due_at => new_due_at,
:due_at_was => read_attribute(:due_at),
:all_day_was => read_attribute(:all_day),
:all_day_date_was => read_attribute(:all_day_date))
write_attribute(:due_at, new_due_at)
write_attribute(:all_day, new_all_day)
write_attribute(:all_day_date, new_all_day_date)
end
def lock_at=(new_lock_at)
write_attribute(:lock_at, CanvasTime.fancy_midnight(new_lock_at))
end
def availability_expired?
lock_at_overridden &&
lock_at.present? &&
lock_at <= Time.zone.now
end
def as_hash
{ :title => title,
:due_at => due_at,
:id => id,
:all_day => all_day,
:set_type => set_type,
:set_id => set_id,
:all_day_date => all_day_date,
:lock_at => lock_at,
:unlock_at => unlock_at,
:override => self }
end
def applies_to_students
# FIXME: exclude students for whom this override does not apply
# because a higher-priority override exists
case set_type
when 'ADHOC'
set
when 'CourseSection'
set.participating_students
when 'Group'
set.participants
else
[]
end
end
def applies_to_admins
case set_type
when 'CourseSection'
set.participating_admins
else
assignment.context.participating_admins
end
end
def notify_change?
self.assignment &&
self.assignment.context.available? &&
self.assignment.published? &&
self.assignment.created_at < 3.hours.ago &&
(workflow_state_changed? ||
due_at_overridden_changed? ||
due_at_overridden && !Assignment.due_dates_equal?(due_at, due_at_was))
end
def set_title_if_needed
if set_type != 'ADHOC' && set
self.title = set.name
elsif set_type == 'ADHOC' && set.any?
self.title ||= title_from_students(set)
else
self.title ||= "No Title"
end
end
def title_from_students(students)
return t("No Students") if students.blank?
t(:student_count,
{
one: '%{count} student',
other: '%{count} students'
},
count: students.count
)
end
def destroy_if_empty_set
return unless set_type == 'ADHOC'
self.assignment_override_students.reload if self.id_was.nil? # fixes a problem with rails 4.2 caching an empty association scope
self.destroy if set.empty?
end
has_a_broadcast_policy
set_broadcast_policy do |p|
p.dispatch :assignment_due_date_changed
p.to { applies_to_students }
p.whenever { |record| record.notify_change? }
p.filter_asset_by_recipient { |record, user|
# note that our asset for this message is an Assignment, not an AssignmentOverride
record.assignment.overridden_for(user)
}
p.dispatch :assignment_due_date_override_changed
p.to { applies_to_admins }
p.whenever { |record| record.notify_change? }
end
end