class Questions::MultipleSelect

Public Class Methods

new(index, question_json) click to toggle source
# File lib/erik-multiple-select.rb, line 5
def initialize(index, question_json)
  @index = index
  @text = question_json['question']
  @options = question_json['options']
  @answer = question_json['answer']
end
schema() click to toggle source
# File lib/erik-multiple-select.rb, line 31
def self.schema
  {
    "properties": {
      "question": {
        "type": "string"
      },
      "type": {
        "type": "string",
        "pattern": "^multiple-select$"
      },
      "options": {
        "type": "array",
        "items": {
          "type": "string"
        }
      },
      "answer": {
        "type": "array",
        "items": {
          "type": "integer"
        }
      }
    },
    "required": [
      "question",
      "type",
      "options",
      "answer"
    ],
    "additionalProperties": false
  }
end

Public Instance Methods

mark(answer) click to toggle source
# File lib/erik-multiple-select.rb, line 16
def mark(answer)
  correct = answer == @answer.collect { |a| a.to_s }
  QuestionFeedback.new(correct, correct ? nil : feedback_string)
end
name() click to toggle source
# File lib/erik-multiple-select.rb, line 12
def name
  @text
end
to_html() click to toggle source
# File lib/erik-multiple-select.rb, line 21
def to_html
  @options.collect.with_index do |option, index|
    id = "q-#{@index}-#{index}"
    %(<div>
        <input id="#{id}" type="checkbox" name="q-#{@index}[]" value="#{index}" />
        <label for="#{id}">#{option}</label>
      </div>)
  end.join("\n")
end

Private Instance Methods

feedback_string() click to toggle source
# File lib/erik-multiple-select.rb, line 66
def feedback_string
  answers = @answer.collect{ |a| %("#{@options[a]}") }.join(' and ')
  "The correct answers were #{answers}."
end