2016-08-07 09:36:08 -05:00
|
|
|
class QuizController < ApplicationController
|
|
|
|
before_action :authorize_candidate
|
|
|
|
|
|
|
|
def question
|
|
|
|
qid = prep_status.current_question_id || params[:question_id]
|
|
|
|
redirect_to :summary and return if qid.nil?
|
|
|
|
prep_question qid
|
2016-08-08 17:08:20 -05:00
|
|
|
@answer = prep_answer qid
|
2016-08-07 09:36:08 -05:00
|
|
|
prep_instance_answer @question
|
|
|
|
end
|
|
|
|
|
|
|
|
def update_answer
|
2016-08-09 23:17:35 -05:00
|
|
|
@answer = prep_answer answer_params[:question_id]
|
2016-08-10 10:38:20 -05:00
|
|
|
prep_status
|
2016-08-09 23:17:35 -05:00
|
|
|
send "process_#{prep_question(answer_params[:question_id]).input_type}"
|
2016-08-10 10:38:20 -05:00
|
|
|
route_answer_xhr and return if request.xhr?
|
|
|
|
route_answer_html
|
2016-08-07 09:36:08 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def summary
|
|
|
|
@quiz = current_candidate.my_quiz
|
|
|
|
redirect_to :question and return unless prep_status.current_question_id.nil?
|
|
|
|
end
|
|
|
|
|
2016-08-10 10:38:20 -05:00
|
|
|
def submit_summary
|
2016-08-07 09:36:08 -05:00
|
|
|
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.'
|
2016-08-10 10:38:20 -05:00
|
|
|
redirect_to :summary, flash: { error: not_completed_error } and return unless prep_status.can_submit
|
2016-08-07 09:36:08 -05:00
|
|
|
redirect_to :thankyou and return if current_candidate.complete!
|
|
|
|
redirect_to :summary, flash: { error: record_error }
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def prep_question qid
|
|
|
|
@question = current_candidate.fetch_question(qid)
|
|
|
|
end
|
|
|
|
|
|
|
|
def prep_status
|
|
|
|
@status ||= QuizStatus.new(current_candidate)
|
|
|
|
end
|
|
|
|
|
|
|
|
def prep_instance_answer question
|
|
|
|
@answer = question.answer.nil? ? Answer.new : Answer.find(question.answer_id)
|
|
|
|
end
|
|
|
|
|
|
|
|
def answer_params
|
|
|
|
params.require(:answer).permit(
|
|
|
|
:question_id,
|
|
|
|
:answer_id,
|
|
|
|
:radio,
|
|
|
|
:text,
|
|
|
|
checkbox: [],
|
2016-08-11 17:44:30 -05:00
|
|
|
live_code: [:later, :html, :css, :js, :text]
|
2016-08-07 09:36:08 -05:00
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
def prep_answer qid = answer_params[:question_id]
|
|
|
|
answer_ids = { question_id: qid, candidate_id: current_candidate.to_i }
|
|
|
|
answer = Answer.find_or_create_by(answer_ids)
|
|
|
|
answer
|
|
|
|
end
|
|
|
|
|
2016-08-10 10:38:20 -05:00
|
|
|
def route_answer_html
|
2016-08-07 09:36:08 -05:00
|
|
|
if @answer.errors.present?
|
|
|
|
prep_question answer_params[:question_id]
|
2016-08-09 23:17:35 -05:00
|
|
|
flash[:error] = answer_params[:question_id].to_i
|
2016-08-07 09:36:08 -05:00
|
|
|
render :question
|
|
|
|
else
|
|
|
|
# TODO: change params.key? to submit = save/next/summary
|
|
|
|
redirect_to :saved and return if params.key?(:save)
|
|
|
|
redirect_to :question
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-08-10 10:38:20 -05:00
|
|
|
def route_answer_xhr
|
2016-08-09 23:17:35 -05:00
|
|
|
if @answer.errors.present?
|
|
|
|
render json: @answer.errors["answer"].to_json, status: 400
|
|
|
|
else
|
2016-08-10 10:38:20 -05:00
|
|
|
results = {
|
|
|
|
message: "Your answer has been updated successfully!",
|
|
|
|
can_submit: prep_status.can_submit,
|
|
|
|
progress: prep_status.progress
|
|
|
|
}
|
|
|
|
render json: results.to_json
|
2016-08-09 23:17:35 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-08-07 09:36:08 -05:00
|
|
|
def process_text
|
|
|
|
@answer.update(answer: answer_params[:text],
|
|
|
|
saved: params.key?(:save),
|
|
|
|
submitted: params.key?(:submit))
|
|
|
|
end
|
|
|
|
|
|
|
|
def process_radio
|
|
|
|
@answer.update(answer: answer_params[:radio],
|
|
|
|
saved: params.key?(:save),
|
|
|
|
submitted: params.key?(:submit))
|
|
|
|
end
|
|
|
|
|
|
|
|
def process_checkbox
|
|
|
|
@answer.update(answer: answer_params[:checkbox],
|
|
|
|
saved: params.key?(:save),
|
|
|
|
submitted: params.key?(:submit))
|
|
|
|
end
|
|
|
|
|
|
|
|
def process_live_code
|
|
|
|
@answer.update(answer: answer_params[:live_code].to_h,
|
|
|
|
saved: params.key?(:save),
|
|
|
|
submitted: params.key?(:submit))
|
|
|
|
end
|
|
|
|
|
|
|
|
def process_live_code_text
|
|
|
|
@answer.update(answer: answer_params[:live_code_text].to_h,
|
|
|
|
saved: params.key?(:save),
|
|
|
|
submitted: params.key?(:submit))
|
|
|
|
end
|
|
|
|
end
|