2016-09-08 10:25:33 -05:00
|
|
|
# frozen_string_literal: true
|
2016-07-27 11:17:50 -05:00
|
|
|
class QuizStatus
|
|
|
|
attr_reader :candidate
|
|
|
|
|
|
|
|
def initialize candidate
|
|
|
|
@candidate = Candidate.find(candidate.to_i)
|
|
|
|
end
|
|
|
|
|
|
|
|
def started
|
2016-09-08 10:39:26 -05:00
|
|
|
candidate.answers.count.positive?
|
2016-07-27 11:17:50 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def on_summary
|
|
|
|
candidate.submitted_answers.count == candidate.questions.count
|
|
|
|
end
|
|
|
|
|
|
|
|
def completed
|
|
|
|
candidate.completed
|
|
|
|
end
|
|
|
|
|
|
|
|
def can_submit
|
2016-08-01 19:51:04 -05:00
|
|
|
on_summary &&
|
|
|
|
no_finish_later &&
|
|
|
|
candidate.answered_questions.count == candidate.questions.count
|
2016-07-27 11:17:50 -05:00
|
|
|
end
|
2016-07-27 22:16:12 -05:00
|
|
|
|
2016-07-27 22:53:14 -05:00
|
|
|
def progress
|
|
|
|
answs = candidate.answered_questions.count.to_f
|
|
|
|
total = candidate.quiz.questions.count.to_f
|
|
|
|
|
|
|
|
(answs / total * 100).round.to_i
|
|
|
|
end
|
2016-07-29 13:34:23 -05:00
|
|
|
|
2016-08-01 19:51:04 -05:00
|
|
|
def no_finish_later
|
|
|
|
sql = %(select count(a.id) todos
|
|
|
|
from answers a
|
|
|
|
inner join questions q on q.id = a.question_id
|
|
|
|
where q.input_type = 'live_code'
|
|
|
|
and a.answer like "%later: 'true'%"
|
|
|
|
and a.candidate_id = #{candidate.id})
|
|
|
|
result = ActiveRecord::Base.connection.exec_query(sql).to_hash.first
|
|
|
|
result['todos'].zero?
|
|
|
|
end
|
|
|
|
|
2016-07-29 13:34:23 -05:00
|
|
|
def current_question_id
|
|
|
|
sql = "select q.id question_id
|
|
|
|
from candidates c
|
|
|
|
inner join questions q on q.quiz_id = c.quiz_id
|
|
|
|
left join answers a on a.candidate_id = c.id AND a.question_id = q.id
|
|
|
|
where q.active = true and c.id = #{candidate.to_i}
|
|
|
|
and (a.id is null OR a.submitted is false)
|
|
|
|
order by q.sort limit 1;"
|
|
|
|
result = ActiveRecord::Base.connection.exec_query(sql).to_hash.first
|
|
|
|
result['question_id'] unless result.nil?
|
|
|
|
end
|
2016-07-27 11:17:50 -05:00
|
|
|
end
|