forked from instructure/canvas-lms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgrades_presenter.rb
More file actions
86 lines (74 loc) · 2.67 KB
/
Copy pathgrades_presenter.rb
File metadata and controls
86 lines (74 loc) · 2.67 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
#
# Copyright (C) 2013 - present 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 GradesPresenter
def initialize(enrollments)
@enrollments = enrollments
end
def student_enrollments
@student_enrollments ||= begin
current_enrollments.select { |e| e.student? }.index_by { |e| e.course }
end
end
def observed_enrollments
@observed_enrollments ||= begin
observer_enrollments.map { |e|
e.shard.activate do
StudentEnrollment.active.where(user_id: e.associated_user_id, course_id: e.course_id).first
end
}.uniq.compact
end
end
def course_grade_summaries
@course_grade_summaries ||= begin
teacher_enrollments.each_with_object({}) do |e, hash|
hash[e.course_id] = e.shard.activate do
Rails.cache.fetch(['computed_avg_grade_for', e.course].cache_key) do
student_enrollments = e.course.student_enrollments.not_fake.preload(:scores)
current_scores = student_enrollments.group_by(&:user_id).map do |_, enrollments|
enrollments.map(&:computed_current_score).compact.max
end.compact
score = (current_scores.sum.to_f * 100.0 / current_scores.length.to_f).round.to_f / 100.0 rescue nil
{:score => score, :students => current_scores.length }
end
end
end
end
end
def teacher_enrollments
@teacher_enrollments ||= current_enrollments.select { |e| e.instructor? }.index_by { |e| e.course }.values
end
def prior_enrollments
[]
end
def has_single_enrollment?
student_enrollments.length + teacher_enrollments.length + observed_enrollments.length == 1
end
def single_enrollment
student_enrollments.values.first || teacher_enrollments.first || observed_enrollments.first
end
private
def observer_enrollments
@observer_enrollments ||= begin
current_enrollments.select { |e| e.is_a?(ObserverEnrollment) && e.associated_user_id }
end
end
def current_enrollments
@current_enrollments ||= begin
@enrollments.select { |e| e.state_based_on_date == :active }
end
end
end