-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcourse_helper.rb
More file actions
163 lines (151 loc) · 4.7 KB
/
course_helper.rb
File metadata and controls
163 lines (151 loc) · 4.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
# typed: strict
# frozen_string_literal: true
module CourseHelper
extend T::Sig
include ActionView::Helpers::TextHelper
sig { params(sorted_sections_by_term: T::Array[[Term, T::Array[Section]]]).returns(T::Array[T::Hash[Symbol, T.untyped]]) }
def generate_enrollment_data(sorted_sections_by_term)
sorted_sections_by_term.filter_map do |term, sections|
enrollment_period_data = sections.sort.filter_map do |section|
data = section.enrollment_period_data_props
{ label: section.title, data: } if data.present?
end
quarter_start_data = sections.sort.filter_map do |section|
data = section.quarter_start_enrollment_data_props
{ label: section.title, data: } if data.present?
end
if enrollment_period_data.present? || quarter_start_data.present?
{
term: term.short_readable,
enrollmentPeriod: {
start: term.enrollment_start,
end: term.enrollment_end,
markers: term.enrollment_period_markers,
sections: enrollment_period_data,
isLive: term.live_enrollment?,
},
quarterStart: {
start: term.start_time,
end: term.end_of_week_two_time,
markers: term.quarter_start_markers,
sections: quarter_start_data,
isLive: term.first_two_weeks?,
},
}
end
end
end
sig { params(course: Course, instructor: T.nilable(Instructor)).returns(String) }
def course_title(course, instructor = nil)
if instructor
"#{instructor.full_label} - #{course.short_title}"
else
course.short_title
end
end
sig { params(course: Course, instructor: T.nilable(Instructor)).returns(String) }
def course_description(course, instructor = nil)
instructor_line = instructor.present? ? " with #{instructor.full_label}" : ""
"#{course.long_title}#{instructor_line} reviews, textbooks, enrollment charts and more. Provided by Hotseat, UCLA's premier source for professors and classes."
end
sig { params(course: Course).returns(T.nilable(String)) }
def course_badge_label(course)
return nil unless course.respond_to?(:section_count)
count = T.let(T.unsafe(course).section_count, T.nilable(Integer))
if count.nil? || count.zero?
"Not offered"
else
pluralize(count, "section")
end
end
# Returns the badge color for a course.
sig { params(badge_label: T.nilable(String), term_color: ColorHelper::Color).returns(T.nilable(ColorHelper::Color)) }
def course_badge_color(badge_label, term_color: ColorHelper::Color::Green)
case badge_label
when nil
nil
when "Not offered"
ColorHelper::Color::Gray
else
term_color
end
end
sig { params(course_detail: { label: Symbol, value: T.any(Float, Integer, String, T::Boolean) }).returns(T.nilable(String)) }
def course_detail_icon(course_detail)
label = course_detail[:label]
value = course_detail[:value]
case label
when :has_group_project
if value
"icons/user-group"
else
"icons/user"
end
when :final
if value == "none"
"icons/sun"
else
"icons/pencil-alt"
end
when :requires_attendance
if value
"icons/presentation-chart-bar"
else
"icons/sun"
end
when :midterm_count
if value.zero?
"icons/sun"
else
"icons/pencil-alt"
end
when :recommend_textbook
"icons/book-open"
end
end
sig { params(course_detail: { label: Symbol, value: T.any(Float, Integer, String, T::Boolean) }).returns(String) }
def course_detail_label(course_detail)
label = course_detail[:label]
value = course_detail[:value]
case label
when :has_group_project
has_group_project = T.cast(value, T::Boolean)
if has_group_project
"Has a group project"
else
"No group projects"
end
when :requires_attendance
requires_attendance = T.cast(value, T::Boolean)
if requires_attendance
"Attendance required"
else
"Attendance not required"
end
when :final
final_labels = {
"none" => "No final",
"10th" => "10th week final",
"finals" => "Finals week final",
}
final_labels[value]
when :midterm_count
if value.zero?
"No midterms"
else
pluralize(value, "midterm")
end
when :recommend_textbook
# Check if value is boolean
case value
when true
"Recommends textbook"
when false
"Does not recommend textbook"
else
# Not boolean, is a number
"#{number_to_percentage(value, precision: 0)} recommend the textbook"
end
end
end
end