2016-07-27 22:16:12 -05:00
|
|
|
class CandidateController < ApplicationController
|
2016-08-02 18:02:20 -05:00
|
|
|
before_action :authorize_candidate, except: [:login, :validate, :live_coder]
|
|
|
|
before_action :send_to_oops, only: [:login]
|
|
|
|
|
|
|
|
def login
|
|
|
|
login_candidate
|
|
|
|
redirect_to :thankyou and return if current_candidate && current_candidate.completed?
|
|
|
|
redirect_to :welcome if current_candidate
|
|
|
|
|
|
|
|
flash[:error] = "Sorry, incorrect test id" if params[:test_hash].present?
|
|
|
|
end
|
2016-07-31 18:54:12 -05:00
|
|
|
|
2016-07-27 22:16:12 -05:00
|
|
|
def welcome
|
2016-08-02 18:30:15 -05:00
|
|
|
render :welcome_back if current_candidate.answers.count > 0
|
2016-07-27 22:16:12 -05:00
|
|
|
end
|
|
|
|
|
2016-07-31 18:54:12 -05:00
|
|
|
def saved
|
|
|
|
end
|
|
|
|
|
2016-08-02 18:02:20 -05:00
|
|
|
def oops
|
|
|
|
end
|
|
|
|
|
2016-07-31 18:54:12 -05:00
|
|
|
def thankyou
|
|
|
|
redirect_to root_path if session[:test_id].nil?
|
|
|
|
reset_session
|
|
|
|
end
|
|
|
|
|
2016-07-27 22:16:12 -05:00
|
|
|
def question
|
2016-08-03 10:46:03 -05:00
|
|
|
qid = prep_status.current_question_id || params[:question_id]
|
2016-07-29 13:34:23 -05:00
|
|
|
redirect_to :summary and return if qid.nil?
|
2016-07-31 18:54:12 -05:00
|
|
|
prep_question qid
|
2016-08-02 13:49:03 -05:00
|
|
|
prep_instance_answer @question
|
2016-07-27 22:16:12 -05:00
|
|
|
end
|
|
|
|
|
2016-08-01 10:33:02 -05:00
|
|
|
def update_answer
|
2016-08-03 10:46:03 -05:00
|
|
|
qid = answer_params[:question_id] || prep_status.current_question_id
|
2016-08-01 15:58:20 -05:00
|
|
|
@answer = prep_answer qid
|
2016-08-01 10:33:02 -05:00
|
|
|
send "process_#{prep_question(qid).input_type}"
|
|
|
|
end
|
|
|
|
|
2016-07-31 18:54:12 -05:00
|
|
|
def live_coder
|
2016-08-02 13:49:03 -05:00
|
|
|
prep_question params[:question_id]
|
|
|
|
prep_instance_answer @question
|
|
|
|
prep_answer params[:question_id]
|
2016-07-31 18:54:12 -05:00
|
|
|
render layout: false
|
2016-07-28 08:47:34 -05:00
|
|
|
end
|
|
|
|
|
2016-07-27 22:16:12 -05:00
|
|
|
def summary
|
2016-07-29 13:34:23 -05:00
|
|
|
@quiz = current_candidate.my_quiz
|
2016-08-01 09:50:01 -05:00
|
|
|
redirect_to :question and return unless prep_status.current_question_id.nil?
|
2016-07-28 20:54:08 -05:00
|
|
|
end
|
|
|
|
|
2016-07-31 18:54:12 -05:00
|
|
|
def update_summary
|
2016-08-02 16:27:27 -05:00
|
|
|
prep_status
|
|
|
|
not_completed_error = 'You must complete all questions to submit your test.'
|
|
|
|
record_error = 'There was a problem with your submission. Please try again later.'
|
|
|
|
redirect_to :summary, flash: { error: not_completed_error } and return unless @status.can_submit
|
|
|
|
redirect_to :thankyou and return if current_candidate.complete!
|
|
|
|
redirect_to :summary, flash: { error: record_error }
|
2016-07-28 08:47:34 -05:00
|
|
|
end
|
2016-07-28 09:37:46 -05:00
|
|
|
|
2016-07-31 18:54:12 -05:00
|
|
|
def validate
|
|
|
|
candidate = Candidate.find_by(test_hash: params['test_id'])
|
2016-08-01 16:43:05 -05:00
|
|
|
redirect_to(root_path, flash: { error: "Sorry, incorrect test id" }) and return if candidate.nil?
|
2016-07-31 18:54:12 -05:00
|
|
|
|
|
|
|
session[:test_id] = candidate.test_hash
|
|
|
|
redirect_to :thankyou and return if candidate.completed?
|
2016-08-02 18:02:20 -05:00
|
|
|
redirect_to :welcome
|
2016-07-29 11:00:04 -05:00
|
|
|
end
|
|
|
|
|
2016-07-29 11:53:01 -05:00
|
|
|
private
|
|
|
|
|
2016-08-02 18:02:20 -05:00
|
|
|
def login_candidate
|
|
|
|
candidate = Candidate.find_by(test_hash: params['test_id'])
|
|
|
|
return false if candidate.nil?
|
|
|
|
|
|
|
|
session[:test_id] = candidate.test_hash
|
|
|
|
end
|
|
|
|
|
|
|
|
def send_to_oops
|
|
|
|
redirect_to oops_path if current_candidate
|
|
|
|
end
|
|
|
|
|
2016-07-31 18:54:12 -05:00
|
|
|
def prep_question qid
|
|
|
|
@question = current_candidate.fetch_question(qid)
|
|
|
|
end
|
|
|
|
|
2016-08-01 09:50:01 -05:00
|
|
|
def prep_status
|
|
|
|
@status ||= QuizStatus.new(current_candidate)
|
|
|
|
end
|
|
|
|
|
2016-08-02 13:49:03 -05:00
|
|
|
def prep_instance_answer question
|
|
|
|
@answer = question.answer.nil? ? Answer.new : Answer.find(question.answer_id)
|
|
|
|
end
|
|
|
|
|
2016-07-30 16:25:09 -05:00
|
|
|
def answer_params
|
|
|
|
params.require(:answer).permit(
|
|
|
|
:question_id, :answer_id,
|
2016-08-01 15:15:57 -05:00
|
|
|
:radio, :text, checkbox: [], live_code: [:later, :html, :css, :js]
|
2016-07-30 16:25:09 -05:00
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2016-08-01 15:58:20 -05:00
|
|
|
def prep_answer qid = answer_params[:question_id]
|
|
|
|
answer_ids = { question_id: qid, candidate_id: current_candidate.to_i }
|
2016-07-31 18:54:12 -05:00
|
|
|
answer = Answer.find_or_create_by(answer_ids)
|
|
|
|
answer
|
|
|
|
end
|
|
|
|
|
2016-08-01 09:50:01 -05:00
|
|
|
def route_answer
|
2016-07-31 18:54:12 -05:00
|
|
|
if @answer.errors.present?
|
2016-08-01 09:50:01 -05:00
|
|
|
prep_status
|
2016-07-31 18:54:12 -05:00
|
|
|
prep_question answer_params[:question_id]
|
2016-08-01 09:50:01 -05:00
|
|
|
flash[:answer_error] = answer_params[:question_id].to_i
|
2016-07-31 18:54:12 -05:00
|
|
|
render :question
|
|
|
|
else
|
2016-08-04 09:55:51 -05:00
|
|
|
flash.delete(:answer_error)
|
2016-07-31 18:54:12 -05:00
|
|
|
# TODO: change params.key? to submit = save/next/summary
|
2016-08-03 10:46:03 -05:00
|
|
|
# redirect_to :summary and return if params.key?(:update)
|
2016-07-31 18:54:12 -05:00
|
|
|
redirect_to :saved and return if params.key?(:save)
|
|
|
|
redirect_to :question
|
|
|
|
end
|
2016-07-29 11:53:01 -05:00
|
|
|
end
|
2016-08-01 10:33:02 -05:00
|
|
|
|
|
|
|
def process_text
|
|
|
|
@answer.update(answer: answer_params[:text],
|
2016-08-01 15:15:57 -05:00
|
|
|
saved: params.key?(:save),
|
|
|
|
submitted: params.key?(:submit))
|
2016-08-01 10:33:02 -05:00
|
|
|
route_answer
|
|
|
|
end
|
|
|
|
|
|
|
|
def process_radio
|
|
|
|
@answer.update(answer: answer_params[:radio],
|
2016-08-01 15:15:57 -05:00
|
|
|
saved: params.key?(:save),
|
|
|
|
submitted: params.key?(:submit))
|
2016-08-01 10:33:02 -05:00
|
|
|
route_answer
|
|
|
|
end
|
|
|
|
|
|
|
|
def process_checkbox
|
|
|
|
@answer.update(answer: answer_params[:checkbox],
|
2016-08-01 15:15:57 -05:00
|
|
|
saved: params.key?(:save),
|
|
|
|
submitted: params.key?(:submit))
|
2016-08-01 10:33:02 -05:00
|
|
|
route_answer
|
|
|
|
end
|
|
|
|
|
|
|
|
def process_live_code
|
2016-08-01 15:15:57 -05:00
|
|
|
@answer.update(answer: answer_params[:live_code].to_h,
|
|
|
|
saved: params.key?(:save),
|
|
|
|
submitted: params.key?(:submit))
|
2016-08-01 10:33:02 -05:00
|
|
|
route_answer
|
|
|
|
end
|
2016-07-27 22:16:12 -05:00
|
|
|
end
|