From 80403ad02d16ecb3ca5658814ab58cd9d9a5cc0b Mon Sep 17 00:00:00 2001 From: Mark Moser Date: Fri, 29 Jul 2016 09:46:47 -0500 Subject: [PATCH] better quiz access --- app/models/candidate.rb | 8 ++++ app/workers/candidate_quiz.rb | 30 ++++++++++++++ app/workers/candidate_quiz_question.rb | 56 ++++++++++++++++++++++++++ 3 files changed, 94 insertions(+) create mode 100644 app/workers/candidate_quiz.rb create mode 100644 app/workers/candidate_quiz_question.rb diff --git a/app/models/candidate.rb b/app/models/candidate.rb index 1248dd9..b6bce4a 100644 --- a/app/models/candidate.rb +++ b/app/models/candidate.rb @@ -18,6 +18,14 @@ class Candidate < ApplicationRecord answers.where.not(answer: nil) end + def fetch_question qid + CandidateQuiz.new(id).fetch_question(qid).first + end + + def my_quiz + CandidateQuiz.new(id).build_my_quiz + end + private def generate_test_hash diff --git a/app/workers/candidate_quiz.rb b/app/workers/candidate_quiz.rb new file mode 100644 index 0000000..706028f --- /dev/null +++ b/app/workers/candidate_quiz.rb @@ -0,0 +1,30 @@ +class CandidateQuiz + attr_reader :candidate_id + + def initialize candidate_id + @candidate_id = candidate_id + end + + def fetch_question qid + raw_quiz(qid).each_with_object([]) { |row, quiz| quiz << CandidateQuizQuestion.new(row) } + end + + def build_my_quiz + raw_quiz.each_with_object([]) { |row, quiz| quiz << CandidateQuizQuestion.new(row) } + end + + private + + def raw_quiz qid = nil + question = qid.nil? ? "" : " and q.id = #{qid} " + sql = "select c.id candidate_id, q.quiz_id, q.id question_id, a.id answer_id, q.sort + , q.question, q.category, q.input_type, q.input_options, a.answer + , ifnull(a.saved, false) saved, ifnull(a.submitted, false) submitted , a.updated_at + 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_id} #{question} + order by c.id, q.sort;" + ActiveRecord::Base.connection.exec_query(sql) + end +end diff --git a/app/workers/candidate_quiz_question.rb b/app/workers/candidate_quiz_question.rb new file mode 100644 index 0000000..a1839db --- /dev/null +++ b/app/workers/candidate_quiz_question.rb @@ -0,0 +1,56 @@ +class CandidateQuizQuestion + attr_reader :row + + def initialize row + @row = row + end + + def candidate_id + row["candidate_id"] + end + + def quiz_id + row["quiz_id"] + end + alias to_i quiz_id + + def question_id + row["question_id"] + end + + def answer_id + row["answer_id"] + end + + def question + row["question"] + end + + def category + row["category"] + end + + def input_type + row["input_type"] + end + + def input_options + YAML.load(row["input_options"].to_s) + end + + def answer + YAML.load(row["answer"].to_s) unless row['answer'].nil? + end + + def saved + row["saved"] + end + + def submitted + row["submitted"] + end + + def updated_at + row["updated_at"] + end +end