Skip to content

Instantly share code, notes, and snippets.

@marvindpunongbayan
Last active May 8, 2020 16:27
Show Gist options
  • Save marvindpunongbayan/b9ec073520d1c91fbfa1cf09e8e1abc1 to your computer and use it in GitHub Desktop.
Save marvindpunongbayan/b9ec073520d1c91fbfa1cf09e8e1abc1 to your computer and use it in GitHub Desktop.
def calculate_score
questions = self.exam.questions.published
user_answers = self.answers
answered_questions_ids = user_answers.pluck(:exam_question_id)
# Counting the right answer to get the percentage
if exam.small_bite?
total_correct = 0
total_score = questions.not_essays.count
questions.each do |question|
already_answered = answered_questions_ids.include?(question.id)
if question.survey_category?
# Surveys have no correct answers, it should count as a progress
total_correct += 1 if already_answered
elsif question.essay?
# Essay has no correct answers, it should count as a progress
if already_answered
if (exam_answer = user_answers.find_by(exam_question_id: question.id)).present?
if exam_answer.rating_value.present?
total_correct += exam_answer.rating_value
total_score += 5
end
end
end
elsif question.selection?
# Selection has no correct answers, it should count as a progress
total += 1 if already_answered
elsif question.matching_type?
total_correct += 1 if already_answered
elsif (question.rating? || question.rating_with_neutral?)
total_correct += 1 if already_answered
elsif question.prioritization?
if (exam_answer = user_answers.find_by(exam_question_id: question.id)).present?
if self.check_answer(question, exam_answer.answer)
total_correct += 1
end
end
else
exam_answer = user_answers.find_by(exam_question_id: question.id)
total_correct += 1 if exam_answer.present? && self.check_answer(question, exam_answer.answer)
end
end
percentage = ((total_correct.to_f / total_score.to_f).to_f * 100)
self.update(score: percentage.to_i)
else
total = 0
questions.each do |question|
already_answered = answered_questions_ids.include?(question.id)
if question.survey_category? && already_answered
# Surveys have no correct answers, it should count as a progress
total += 1
elsif question.essay? && already_answered
# Essay has no correct answers, it should count as a progress
total += 1
elsif question.selection? && already_answered
# Selection has no correct answers, it should count as a progress
total += 1
elsif question.matching_type? && already_answered
total += 1
elsif (question.rating? || question.rating_with_neutral?) && already_answered
total += 1
elsif question.prioritization?
if (exam_answer = user_answers.find_by(exam_question_id: question.id)).present?
if self.check_answer(question, exam_answer.answer)
total += 1
end
end
else
exam_answer = user_answers.find_by(exam_question_id: question.id)
total += 1 if exam_answer.present? && self.check_answer(question, exam_answer.answer)
end
end
total_questions = questions.count
percentage = ((total.to_f / total_questions.to_f).to_f * 100)
if self.status != Status::COMPLETED
self.update(score: percentage.to_i)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment