candidate quiz progression work

This commit is contained in:
Mark Moser 2016-07-29 13:34:23 -05:00
parent a901186a74
commit 5be93e0d38
4 changed files with 44 additions and 8 deletions

View File

@ -3,10 +3,12 @@ class CandidateController < ApplicationController
end end
def question def question
candidate = Candidate.order("RAND()").first @status = QuizStatus.new(current_candidate)
@status = QuizStatus.new(candidate) qid = @status.current_question_id
@question = candidate.fetch_question(candidate.questions.order("RAND()")[[*0..9].sample].id)
# .where(input_type: 'live-coder') redirect_to :summary and return if qid.nil?
@question = current_candidate.fetch_question(qid)
end end
def update_question def update_question
@ -16,9 +18,10 @@ class CandidateController < ApplicationController
end end
def summary def summary
@candidate = Candidate.order("RAND()").first # Candidate.where(test_hash: '6NjnourLE6Y').first @quiz = current_candidate.my_quiz
@quiz = @candidate.my_quiz @status = QuizStatus.new(current_candidate)
@status = QuizStatus.new(@candidate)
redirect_to :question and return unless @status.current_question_id.nil?
end end
def update_summary def update_summary

View File

@ -24,7 +24,7 @@ class CandidateQuiz
inner join questions q on q.quiz_id = c.quiz_id 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 left join answers a on a.candidate_id = c.id AND a.question_id = q.id
where q.active = true and c.id = #{candidate_id} #{question} where q.active = true and c.id = #{candidate_id} #{question}
order by c.id, q.sort;" order by q.sort;"
ActiveRecord::Base.connection.exec_query(sql) ActiveRecord::Base.connection.exec_query(sql)
end end
end end

View File

@ -27,4 +27,16 @@ class QuizStatus
(answs / total * 100).round.to_i (answs / total * 100).round.to_i
end end
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
end end

View File

@ -77,4 +77,25 @@ class QuizStatusTest < ActiveSupport::TestCase
assert_equal 0, status.progress assert_equal 0, status.progress
end end
test "roy is on question 3" do
roy = candidates :roy
status = QuizStatus.new roy
assert_equal questions(:fed3).id, status.current_question_id
end
test "martha is on question 1" do
martha = candidates :martha
status = QuizStatus.new martha
assert_equal questions(:fed1).id, status.current_question_id
end
test "richard is summary" do
richard = candidates :richard
status = QuizStatus.new richard
assert_equal nil, status.current_question_id
end
end end