forked from instructure/canvas-lms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathassignment_student_visibility.rb
More file actions
57 lines (47 loc) · 1.89 KB
/
Copy pathassignment_student_visibility.rb
File metadata and controls
57 lines (47 loc) · 1.89 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
class AssignmentStudentVisibility < ActiveRecord::Base
include VisibilityPluckingHelper
belongs_to :user
belongs_to :assignment
belongs_to :course
# create_or_update checks for !readonly? before persisting
def readonly?
true
end
def self.visible_assignment_ids_in_course_by_user(opts)
visible_object_ids_in_course_by_user(:assignment_id, opts)
end
def self.assignments_with_user_visibilities(course, assignments)
only_visible_to_overrides, visible_to_everyone = assignments.partition(&:only_visible_to_overrides)
assignment_visibilities = {}
if only_visible_to_overrides.any?
options = { course_id: course.id, assignment_id: only_visible_to_overrides.map(&:id) }
assignment_visibilities.merge!(users_with_visibility_by_assignment(options))
end
if visible_to_everyone.any?
assignment_visibilities.merge!(
assignments_visible_to_all_students(visible_to_everyone)
)
end
assignment_visibilities
end
def self.assignments_visible_to_all_students(assignments_visible_to_everyone)
assignments_visible_to_everyone.each_with_object({}) do |assignment, assignment_visibilities|
# if an assignment is visible to everyone, we do not care about the contents
# of its assignment_visibilities. instead of setting this to an array of every
# student's ID, we set it to an empty array to save time when calling to_json
assignment_visibilities[assignment.id] = []
end
end
def self.users_with_visibility_by_assignment(opts)
users_with_visibility_by_object_id(:assignment_id, opts)
end
def self.visible_assignment_ids_for_user(user_id, course_ids=nil)
opts = {user_id: user_id}
if course_ids
opts[:course_id] = course_ids
end
self.where(opts).pluck(:assignment_id)
end
# readonly? is not checked in destroy though
before_destroy { |record| raise ActiveRecord::ReadOnlyRecord }
end