-
Notifications
You must be signed in to change notification settings - Fork 481
/
teacher_feedback.rb
99 lines (88 loc) · 3 KB
/
teacher_feedback.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
# == Schema Information
#
# Table name: teacher_feedbacks
#
# id :integer not null, primary key
# comment :text(65535)
# student_id :integer
# level_id :integer not null
# teacher_id :integer not null
# created_at :datetime not null
# updated_at :datetime not null
# deleted_at :datetime
# performance :string(255)
# student_visit_count :integer
# student_first_visited_at :datetime
# student_last_visited_at :datetime
# script_level_id :integer not null
# seen_on_feedback_page_at :datetime
# script_id :integer not null
#
# Indexes
#
# index_feedback_on_student_and_level_and_teacher_id (student_id,level_id,teacher_id)
# index_teacher_feedbacks_on_teacher_id (teacher_id)
#
class TeacherFeedback < ApplicationRecord
acts_as_paranoid # use deleted_at column instead of deleting rows
validates_presence_of :student_id, :script_id, :level_id, :teacher_id, :script_level_id, unless: :deleted?
belongs_to :student, class_name: 'User'
has_many :student_sections, class_name: 'Section', through: :student, source: 'sections_as_student'
belongs_to :script
belongs_to :level
belongs_to :script_level
belongs_to :teacher, class_name: 'User'
validate :validate_script_and_script_level, on: :create
def validate_script_and_script_level
if script_level.script_id != script_id
errors.add(:script_id, 'script_id does not match script_level.script_id')
end
end
# Finds the script level associated with this object, using script id and
# level id.
def get_script_level
level.script_levels.find {|sl| sl.script_id == script_id}
end
def self.get_student_level_feedback(student_id, level_id, teacher_id)
where(
student_id: student_id,
level_id: level_id,
teacher_id: teacher_id
).latest
end
def self.get_all_feedback_for_section(student_ids, level_ids, teacher_id)
find(
where(
student_id: student_ids,
level_id: level_ids,
teacher_id: teacher_id
).group([:student_id, :level_id]).pluck('MAX(teacher_feedbacks.id)')
)
end
def self.latest_per_teacher
#Only select feedback from teachers who lead sections in which the student is still enrolled
find(
joins(:student_sections).
where('sections.user_id = teacher_id').
group([:teacher_id, :student_id]).
pluck('MAX(teacher_feedbacks.id)')
)
end
def self.latest
find_by(id: maximum(:id))
end
# Increments student_visit_count and related metrics timestamps for a TeacherFeedback.
def increment_visit_count
now = DateTime.now
if student_visit_count
self.student_visit_count += 1
else
self.student_visit_count = 1
end
unless student_first_visited_at
self.student_first_visited_at = now
end
self.student_last_visited_at = now
save
end
end