Module: Course::Assessment::QuestionsConcern

Extended by:
ActiveSupport::Concern
Defined in:
app/models/concerns/course/assessment/questions_concern.rb

Instance Method Summary collapse

Instance Method Details

#attempt(submission) ⇒ Array<Course::Assessment::Answer>

Attempts the questions in the given submission without a current_answer.

This will create answers for questions without any current_answer, and return them in the same order as specified.

Parameters:

Returns:

  • (Array<Course::Assessment::Answer>)

    The answers for the questions, in the same order specified. Newly initialized answers will not be persisted.


14
15
16
17
18
19
20
# File 'app/models/concerns/course/assessment/questions_concern.rb', line 14

def attempt(submission)
  current_answers = submission.current_answers.map { |answer| [answer.question, answer] }.to_h

  map do |question|
    current_answers.fetch(question) { question.attempt(submission) }
  end
end

#next_unanswered(submission) ⇒ Course::Assessment::Question|nil

Return the next unanswered question.

Parameters:

Returns:


61
62
63
64
65
66
67
68
# File 'app/models/concerns/course/assessment/questions_concern.rb', line 61

def next_unanswered(submission)
  correctly_answered_questions = correctly_answered_questions(submission)
  return first if correctly_answered_questions.empty?

  reduce(nil) do |_, question|
    break question unless correctly_answered_questions.include?(question)
  end
end

#not_answered(submission) ⇒ Array<Course::Assessment::Question>

Returns the questions which do not have a answer.

Parameters:

Returns:


26
27
28
# File 'app/models/concerns/course/assessment/questions_concern.rb', line 26

def not_answered(submission)
  where.not(id: submission.answers.select(:question_id))
end

#not_correctly_answered(submission) ⇒ Array<Course::Assessment::Question>

Returns the questions which do not have a answer or correct answer.

Parameters:

Returns:


34
35
36
# File 'app/models/concerns/course/assessment/questions_concern.rb', line 34

def not_correctly_answered(submission)
  where.not(id: correctly_answered_question_ids(submission))
end

#step(submission, current_index) ⇒ Course::Assessment::Question

Return the question at the given index. The next unanswered question will be returned if the question at the index is not accessible.

Parameters:

  • submission (Course::Assessment::Submission)

    The submission which contains the answers.

  • current_index (Integer)

    The index of the question, it's zero based.

Returns:


45
46
47
48
49
50
51
52
53
54
# File 'app/models/concerns/course/assessment/questions_concern.rb', line 45

def step(submission, current_index)
  current_index = 0 if current_index < 0
  max_index = if submission.assessment.skippable?
                index(last)
              else
                index(next_unanswered(submission) || last)
              end

  to_a.fetch([current_index, max_index].min)
end