better quiz access
This commit is contained in:
		| @@ -18,6 +18,14 @@ class Candidate < ApplicationRecord | |||||||
|     answers.where.not(answer: nil) |     answers.where.not(answer: nil) | ||||||
|   end |   end | ||||||
|  |  | ||||||
|  |   def fetch_question qid | ||||||
|  |     CandidateQuiz.new(id).fetch_question(qid).first | ||||||
|  |   end | ||||||
|  |  | ||||||
|  |   def my_quiz | ||||||
|  |     CandidateQuiz.new(id).build_my_quiz | ||||||
|  |   end | ||||||
|  |  | ||||||
|   private |   private | ||||||
|  |  | ||||||
|     def generate_test_hash |     def generate_test_hash | ||||||
|   | |||||||
							
								
								
									
										30
									
								
								app/workers/candidate_quiz.rb
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										30
									
								
								app/workers/candidate_quiz.rb
									
									
									
									
									
										Normal file
									
								
							| @@ -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 | ||||||
							
								
								
									
										56
									
								
								app/workers/candidate_quiz_question.rb
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										56
									
								
								app/workers/candidate_quiz_question.rb
									
									
									
									
									
										Normal file
									
								
							| @@ -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 | ||||||
		Reference in New Issue
	
	Block a user