Class: AutoQCMRenderer

Inherits:
Object
  • Object
show all
Includes:
TexOutput
Defined in:
lib/ruql/renderers/auto_qcm_renderer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from TexOutput

#add_newlines, #to_tex

Constructor Details

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

Returns a new instance of AutoQCMRenderer.



8
9
10
11
12
13
14
15
16
17
# File 'lib/ruql/renderers/auto_qcm_renderer.rb', line 8

def initialize(quiz, options={})
  @output = ''
  @quiz = quiz
  @template = options.delete('t') ||
    options.delete('template') ||
    File.join(Gem.loaded_specs['ruql'].full_gem_path, 'templates/autoqcm.tex.erb')
  @penalty = (options.delete('p') || options.delete('penalty') || '0').to_f
  @show_solutions = options.delete('s') || options.delete('solutions')
  
end

Instance Attribute Details

#outputObject (readonly)

Returns the value of attribute output.



6
7
8
# File 'lib/ruql/renderers/auto_qcm_renderer.rb', line 6

def output
  @output
end

Instance Method Details

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



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/ruql/renderers/auto_qcm_renderer.rb', line 53

def render(question, index, type='')    
  output = ''
  output << "\\begin{question#{type}}{q#{index}}\n"
  output << "  \\scoring{b=#{question.points},m=#{@penalty*question.points}}\n"
  if type == 'mult'
    question.question_text = "Select ALL that apply. " + question.question_text
  elsif type == ''
    question.question_text = "Choose ONE answer. " + question.question_text
  end
  output << "  " << to_tex(question.question_text) << "\n"

  # answers - ignore randomization

  output << "  \\begin{choices}\n"
  question.answers.each do |answer|
    answer_text = to_tex(answer.answer_text)
    answer_type = if answer.correct? then 'correct' else 'wrong' end
    output << "    \\#{answer_type}choice{#{answer_text}}\n"
    if @show_solutions and answer.explanation
      explanation = to_tex(answer.explanation)
      if answer_type == 'wrong'
        output << "{\\color{red}\\tab #{explanation}}"
      else
        output << "{\\color[rgb]{0,.5,0}\\tab #{explanation}}"
      end
    end
  end
  output << "  \\end{choices}\n"
  output << "\\end{question#{type}}\n\n"
  output
end

#render_question(q, index) ⇒ Object



37
38
39
40
41
42
43
44
45
# File 'lib/ruql/renderers/auto_qcm_renderer.rb', line 37

def render_question(q,index)
  case q
  when SelectMultiple,TrueFalse then render(q, index, 'mult') # These are subclasses of MultipleChoice, should go first
  when MultipleChoice then render(q, index)
  else
    @quiz.logger.error "Question #{index} (#{q.question_text[0,15]}...): AutoQCM can only handle multiple_choice, truefalse, or select_multiple questions"
    ''
  end
end

#render_quizObject



19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/ruql/renderers/auto_qcm_renderer.rb', line 19

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

#render_random_seedObject



47
48
49
50
51
# File 'lib/ruql/renderers/auto_qcm_renderer.rb', line 47

def render_random_seed
  seed = @quiz.seed
  @output << "\n%% Random seed: #{seed}\n"
  @output << "\\AMCrandomseed{#{seed}}\n\n"
end

#with_erb_template(template) ⇒ Object



32
33
34
35
# File 'lib/ruql/renderers/auto_qcm_renderer.rb', line 32

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