Class: QualtricsRenderer

Inherits:
Object
  • Object
show all
Defined in:
lib/ruql/renderers/qualtrics_renderer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(quiz, options = {}) ⇒ QualtricsRenderer

Returns a new instance of QualtricsRenderer.



5
6
7
8
9
# File 'lib/ruql/renderers/qualtrics_renderer.rb', line 5

def initialize(quiz, options={})
  @output = ''
  @quiz = quiz
  @template = options.delete('t') || options.delete('template')
end

Instance Attribute Details

#outputObject (readonly)

Returns the value of attribute output.



3
4
5
# File 'lib/ruql/renderers/qualtrics_renderer.rb', line 3

def output
  @output
end

Instance Method Details

#render(question, index, type = '') ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/ruql/renderers/qualtrics_renderer.rb', line 38

def render(question, index, type='')    
  output = ''
  output << "[[Question:MC:#{type}Answer]]\n"
  output << "[[ID:#{index}]]\n"
  if type == 'Multiple'
    question.question_text = "Select ALL that apply. " + question.question_text
  elsif type == 'Single'
    question.question_text = "Choose ONE answer. " + question.question_text
  end
  output << question.question_text << "\n"

  # answers - ignore randomization

  output << "[[AdvancedChoices]]\n"
  question.answers.each do |answer|
    output << "[[Choice]]\n"
    output << "#{answer.answer_text}\n"
  end
  if type == 'Multiple'
    output << "[[Choice]]\n"
    output << "<i>None of these answers are correct.</i>\n"
  end
  output
end

#render_question(q, index) ⇒ Object



28
29
30
31
32
33
34
35
36
# File 'lib/ruql/renderers/qualtrics_renderer.rb', line 28

def render_question(q,index)
  case q
  when SelectMultiple,TrueFalse then render(q, index, 'Multiple') # These are subclasses of MultipleChoice, should go first
  when MultipleChoice then render(q, index, 'Single')
  else
    @quiz.logger.error "Question #{index} (#{q.question_text[0,15]}...): Only written to handle multiple_choice, truefalse, or select_multiple questions at this time."
    ''
  end
end

#render_quizObject



11
12
13
14
15
16
17
18
19
20
21
# File 'lib/ruql/renderers/qualtrics_renderer.rb', line 11

def render_quiz
  quiz = @quiz                # make quiz object available in template's scope
  with_erb_template(IO.read(File.expand_path @template)) do
    output = ''
    @quiz.questions.each_with_index do |q,i|
      next_question = render_question q,i
      output << next_question
    end
    output
  end
end

#with_erb_template(template) ⇒ Object



23
24
25
26
# File 'lib/ruql/renderers/qualtrics_renderer.rb', line 23

def with_erb_template(template)
  # template will 'yield' back to render_quiz to render the questions
  @output = ERB.new(template).result(binding)
end