From 5be93e0d3810cbc3c19bc957f155df67c7f30480 Mon Sep 17 00:00:00 2001 From: Mark Moser Date: Fri, 29 Jul 2016 13:34:23 -0500 Subject: [PATCH] candidate quiz progression work --- app/controllers/candidate_controller.rb | 17 ++++++++++------- app/workers/candidate_quiz.rb | 2 +- app/workers/quiz_status.rb | 12 ++++++++++++ test/workers/quiz_status_test.rb | 21 +++++++++++++++++++++ 4 files changed, 44 insertions(+), 8 deletions(-) diff --git a/app/controllers/candidate_controller.rb b/app/controllers/candidate_controller.rb index 484c9d3..f9d47b1 100644 --- a/app/controllers/candidate_controller.rb +++ b/app/controllers/candidate_controller.rb @@ -3,10 +3,12 @@ class CandidateController < ApplicationController end def question - candidate = Candidate.order("RAND()").first - @status = QuizStatus.new(candidate) - @question = candidate.fetch_question(candidate.questions.order("RAND()")[[*0..9].sample].id) - # .where(input_type: 'live-coder') + @status = QuizStatus.new(current_candidate) + qid = @status.current_question_id + + redirect_to :summary and return if qid.nil? + + @question = current_candidate.fetch_question(qid) end def update_question @@ -16,9 +18,10 @@ class CandidateController < ApplicationController end def summary - @candidate = Candidate.order("RAND()").first # Candidate.where(test_hash: '6NjnourLE6Y').first - @quiz = @candidate.my_quiz - @status = QuizStatus.new(@candidate) + @quiz = current_candidate.my_quiz + @status = QuizStatus.new(current_candidate) + + redirect_to :question and return unless @status.current_question_id.nil? end def update_summary diff --git a/app/workers/candidate_quiz.rb b/app/workers/candidate_quiz.rb index 706028f..4329ef0 100644 --- a/app/workers/candidate_quiz.rb +++ b/app/workers/candidate_quiz.rb @@ -24,7 +24,7 @@ class CandidateQuiz 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_id} #{question} - order by c.id, q.sort;" + order by q.sort;" ActiveRecord::Base.connection.exec_query(sql) end end diff --git a/app/workers/quiz_status.rb b/app/workers/quiz_status.rb index 0a5a747..bca6a87 100644 --- a/app/workers/quiz_status.rb +++ b/app/workers/quiz_status.rb @@ -27,4 +27,16 @@ class QuizStatus (answs / total * 100).round.to_i 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 diff --git a/test/workers/quiz_status_test.rb b/test/workers/quiz_status_test.rb index 818d6b9..f7cccd5 100644 --- a/test/workers/quiz_status_test.rb +++ b/test/workers/quiz_status_test.rb @@ -77,4 +77,25 @@ class QuizStatusTest < ActiveSupport::TestCase assert_equal 0, status.progress 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