-
Notifications
You must be signed in to change notification settings - Fork 38
/
odsa_exercise_attempt.rb
346 lines (332 loc) · 14.8 KB
/
odsa_exercise_attempt.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
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
# == Schema Information
#
# Table name: odsa_exercise_attempts
#
# id :bigint not null, primary key
# user_id :bigint not null
# inst_book_id :bigint
# inst_section_id :bigint
# inst_book_section_exercise_id :bigint
# worth_credit :boolean not null
# time_done :datetime not null
# time_taken :bigint not null
# count_hints :bigint not null
# hint_used :boolean not null
# points_earned :decimal(5, 2) not null
# earned_proficiency :boolean not null
# count_attempts :bigint not null
# ip_address :string(20) not null
# question_name :string(50) not null
# request_type :string(50)
# created_at :datetime
# updated_at :datetime
# correct :boolean
# pe_score :decimal(5, 2)
# pe_steps_fixed :bigint
# inst_course_offering_exercise_id :bigint
# inst_module_section_exercise_id :bigint
# answer :string(255)
# question_id :integer
#
# Indexes
#
# fk_rails_6944f2321b (inst_module_section_exercise_id)
# odsa_exercise_attempts_inst_book_id_fk (inst_book_id)
# odsa_exercise_attempts_inst_book_section_exercise_id_fk (inst_book_section_exercise_id)
# odsa_exercise_attempts_inst_course_offering_exercise_id_fk (inst_course_offering_exercise_id)
# odsa_exercise_attempts_inst_section_id_fk (inst_section_id)
# odsa_exercise_attempts_user_id_fk (user_id)
#
class OdsaExerciseAttempt < ApplicationRecord
#~ Relationships ............................................................
belongs_to :user
belongs_to :inst_book
belongs_to :inst_section
belongs_to :inst_book_section_exercise
belongs_to :inst_course_offering_exercise
belongs_to :inst_module_section_exercise
#~ Validation ...............................................................
validate :required_fields
def required_fields
if !(inst_book_section_exercise_id.present? or inst_course_offering_exercise_id.present? or inst_module_section_exercise_id.present?)
errors.add(:base, "inst_book_section_exercise_id or inst_course_offering_exercise_id or inst_module_section_exercise_id must be present")
end
end
#~ Constants ................................................................
#~ Hooks ....................................................................
after_create :update_exercise_progress
#~ Class methods ............................................................
#~ Instance methods .........................................................
def update_exercise_progress
hasBook = !inst_book_section_exercise_id.blank?
if hasBook
@inst_chapter_module = inst_book_section_exercise.get_chapter_module
if @inst_chapter_module.due_dates.nil? or @inst_chapter_module.due_dates > Time.now
if self.request_type === 'PE'
update_pe_exercise_progress
elsif self.request_type === 'AE'
update_ae_exercise_progress
elsif self.request_type === 'PI'
update_pi_exercise_progress
else
update_ka_exercise_progress
end
end
else
if self.request_type === 'PE'
update_pe_exercise_progress
elsif self.request_type === 'AE'
update_ae_exercise_progress
elsif self.request_type === 'PI'
update_pi_exercise_progress
else
update_ka_exercise_progress
end
end
end
def update_ka_exercise_progress
hasBook = !inst_book_section_exercise_id.blank?
has_standalone_module = !inst_module_section_exercise_id.blank?
if hasBook
@inst_chapter_module = inst_book_section_exercise.get_chapter_module
inst_exercise = InstExercise.find_by(id: inst_book_section_exercise.inst_exercise_id)
book_progress = OdsaBookProgress.get_progress(user_id, inst_book_id)
module_progress = OdsaModuleProgress.get_progress(user_id, @inst_chapter_module.id, inst_book_id)
elsif has_standalone_module
inst_exercise = InstExercise.find_by(id: inst_module_section_exercise.inst_exercise)
module_progress = OdsaModuleProgress.get_standalone_progress(user_id, inst_module_section_exercise.inst_module_version_id)
else
inst_exercise = InstExercise.find_by(id: inst_course_offering_exercise.inst_exercise_id)
end
exercise_progress = self.get_exercise_progress
exercise_progress.first_done ||= DateTime.now
exercise_progress.last_done = DateTime.now
# first_response = (self.count_attempts == 1 and self.count_hints == 0) ||
# (self.count_attempts == 0 and self.count_hints == 1)
if hasBook
book_progress.update_started(inst_exercise)
end
proficient = false
if self.correct
exercise_progress['total_correct'] += 1
if self.worth_credit
exercise_progress['total_worth_credit'] += 1
exercise_progress['current_score'] += 1
exercise_progress['highest_score'] = [exercise_progress['highest_score'], exercise_progress['current_score']].max
proficient = false
if hasBook
proficient = book_progress.update_proficiency(exercise_progress)
elsif has_standalone_module
proficient = exercise_progress.highest_score >= inst_module_section_exercise.threshold
else
proficient = exercise_progress.highest_score >= inst_course_offering_exercise.threshold
end
if proficient
self.earned_proficiency = true
if hasBook
self.points_earned = inst_book_section_exercise.points
elsif has_standalone_module
self.points_earned = inst_module_section_exercise.points
else
self.points_earned = inst_course_offering_exercise.points
end
self.save!
exercise_progress.proficient_date ||= DateTime.now
end
if exercise_progress['correct_exercises'].to_s.strip.length == 0
exercise_progress['correct_exercises'] = self['question_name']
else
exercise_progress['correct_exercises'] += ',' + self['question_name']
end
end
# when student answer an exercise correctly from first time then clear the hint
if self.request_type != 'hint'
exercise_progress['hinted_exercise'] = ""
end
else
if self.count_hints == 0 and self.request_type != 'hint' and self.count_attempts == 1
# Only count wrong answer at most once per problem
if exercise_progress['current_score'] - 1 > 0
exercise_progress['current_score'] -= 1
else
exercise_progress['current_score'] = 0
end
end
end
# save exercise_name to hinted_exercise so that student won't get credit if he saw the hint then refreshes the page
if self.request_type == 'hint' and inst_exercise.short_name.include? "Summ"
exercise_progress['hinted_exercise'] = self['question_name']
end
exercise_progress.save!
if proficient
if hasBook
module_progress.update_proficiency(inst_exercise)
elsif has_standalone_module
module_progress.update_proficiency(inst_module_section_exercise)
end
end
end
def update_pe_exercise_progress
hasBook = !inst_book_section_exercise_id.blank?
has_standalone_module = !inst_module_section_exercise_id.blank?
if hasBook
@inst_chapter_module = inst_book_section_exercise.get_chapter_module
inst_exercise = InstExercise.find_by(id: inst_book_section_exercise.inst_exercise_id)
book_progress = OdsaBookProgress.get_progress(user_id, inst_book_id)
module_progress = OdsaModuleProgress.get_progress(user_id, @inst_chapter_module.id, inst_book_id)
elsif has_standalone_module
inst_exercise = InstExercise.find(inst_module_section_exercise.inst_exercise_id)
module_progress = OdsaModuleProgress.get_standalone_progress(user_id, inst_module_section_exercise.inst_module_version_id)
else
inst_exercise = InstExercise.find(inst_course_offering_exercise.inst_exercise_id)
end
exercise_progress = self.get_exercise_progress
exercise_progress.first_done ||= DateTime.now
exercise_progress.last_done = DateTime.now
if hasBook
book_progress.update_started(inst_exercise)
end
if self.correct
self.earned_proficiency = true
if hasBook
self.points_earned = inst_book_section_exercise.points
Rails.logger.info(inst_book_section_exercise.points)
elsif has_standalone_module
self.points_earned = inst_module_section_exercise.points
else
self.points_earned = inst_course_offering_exercise.points
end
self.save!
exercise_progress['total_correct'] += 1
exercise_progress['total_worth_credit'] += 1
exercise_progress['current_score'] = self.points_earned
exercise_progress['highest_score'] = self.points_earned
exercise_progress.proficient_date ||= DateTime.now
exercise_progress.save!
if hasBook
module_progress.update_proficiency(inst_exercise)
book_progress.update_proficiency(exercise_progress)
elsif has_standalone_module
module_progress.update_proficiency(inst_module_section_exercise)
end
else
exercise_progress.save!
end
end
def update_ae_exercise_progress
hasBook = !inst_book_section_exercise_id.blank?
has_standalone_module = !inst_module_section_exercise_id.blank?
if hasBook
@inst_chapter_module = inst_book_section_exercise.get_chapter_module
inst_exercise = InstExercise.find_by(id: inst_book_section_exercise.inst_exercise_id)
book_progress = OdsaBookProgress.get_progress(user_id, inst_book_id)
module_progress = OdsaModuleProgress.get_progress(user_id, @inst_chapter_module.id, inst_book_id)
elsif has_standalone_module
inst_exercise = InstExercise.find(inst_module_section_exercise.inst_exercise_id)
module_progress = OdsaModuleProgress.get_standalone_progress(user_id, inst_module_section_exercise.inst_module_version_id)
else
inst_exercise = InstExercise.find(inst_course_offering_exercise.inst_exercise_id)
end
exercise_progress = self.get_exercise_progress
exercise_progress.first_done ||= DateTime.now
exercise_progress.last_done = DateTime.now
if hasBook
book_progress.update_started(inst_exercise)
end
if self.correct
self.earned_proficiency = true
if hasBook
if self.request_type == 'AE'
self.points_earned = inst_book_section_exercise.points * self.pe_score
else
self.points_earned = inst_book_section_exercise.points
end
elsif has_standalone_module
self.points_earned = inst_module_section_exercise.points
else
self.points_earned = inst_course_offering_exercise.points
end
self.save!
exercise_progress['total_correct'] += 1
exercise_progress['total_worth_credit'] += 1
exercise_progress['current_score'] = self.points_earned
exercise_progress['highest_score'] = self.points_earned
exercise_progress.proficient_date ||= DateTime.now
exercise_progress.save!
if hasBook
module_progress.update_proficiency(inst_exercise)
book_progress.update_proficiency(exercise_progress)
elsif has_standalone_module
module_progress.update_proficiency(inst_module_section_exercise)
end
else
exercise_progress.save!
end
end
def update_pi_exercise_progress
Rails.logger.info("update_pi_exercise_progress")
hasBook = !inst_book_section_exercise_id.blank?
has_standalone_module = !inst_module_section_exercise_id.blank?
if hasBook
Rails.logger.info("update_pi_exercise_progress -> hasBook")
@inst_chapter_module = inst_book_section_exercise.get_chapter_module
inst_exercise = InstExercise.find_by(id: inst_book_section_exercise.inst_exercise_id)
book_progress = OdsaBookProgress.get_progress(user_id, inst_book_id)
module_progress = OdsaModuleProgress.get_progress(user_id, @inst_chapter_module.id, inst_book_id)
elsif has_standalone_module
inst_exercise = InstExercise.find(inst_module_section_exercise.inst_exercise_id)
module_progress = OdsaModuleProgress.get_standalone_progress(user_id, inst_module_section_exercise.inst_module_version_id)
else
inst_exercise = InstExercise.find(inst_course_offering_exercise.inst_exercise_id)
end
exercise_progress = self.get_exercise_progress
exercise_progress.first_done ||= DateTime.now
exercise_progress.last_done = DateTime.now
if hasBook
Rails.logger.info("update_pi_exercise_progress -> hasBook 2222")
book_progress.update_started(inst_exercise)
end
if self.correct and self.finished_frame
self.earned_proficiency = true
if hasBook
self.points_earned = inst_book_section_exercise.points
Rails.logger.info(inst_book_section_exercise.points)
elsif has_standalone_module
self.points_earned = inst_module_section_exercise.points
else
self.points_earned = inst_course_offering_exercise.points
end
self.save!
exercise_progress['total_correct'] += 1
exercise_progress['total_worth_credit'] += 1
exercise_progress['current_score'] = self.points_earned
exercise_progress['highest_score'] = self.points_earned
exercise_progress.proficient_date ||= DateTime.now
exercise_progress.save!
if hasBook
Rails.logger.info("update_pi_exercise_progress -> update_module_progress_procifiency")
module_progress.update_proficiency(inst_exercise)
Rails.logger.info("update_pi_exercise_progress -> update_book_progress_procifiency")
book_progress.update_proficiency(exercise_progress)
elsif has_standalone_module
module_progress.update_proficiency(inst_module_section_exercise)
end
else
exercise_progress.save!
end
end
def get_exercise_progress
if !inst_book_section_exercise_id.blank?
return OdsaExerciseProgress.where("user_id=? and inst_book_section_exercise_id=?",
user.id,
inst_book_section_exercise.id).first
elsif !inst_module_section_exercise_id.blank?
return OdsaExerciseProgress.find_by(user_id: user_id,
inst_module_section_exercise_id: inst_module_section_exercise_id)
else
return OdsaExerciseProgress.find_by(user_id: user_id,
inst_course_offering_exercise_id: inst_course_offering_exercise_id)
end
end
#~ Private instance methods .................................................
end