Class: Quiz

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

Constant Summary collapse

@@quizzes =
[]
@@options =
{}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(title, options = {}) ⇒ Quiz

Returns a new instance of Quiz.



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

def initialize(title, options={})
  @output = ''
  @questions = options[:questions] || []
  @title = title
  @options = @@options.merge(options)
  @seed = srand
  @logger = Logger.new(STDERR)
  @logger.level = (@options['-V'] || @options['--verbose']) ? Logger::INFO : Logger::WARN
  #@quiz_yaml = yaml
end

Instance Attribute Details

#loggerObject (readonly)

Returns the value of attribute logger.



10
11
12
# File 'lib/ruql/quiz.rb', line 10

def logger
  @logger
end

#optionsObject (readonly)

Returns the value of attribute options.



7
8
9
# File 'lib/ruql/quiz.rb', line 7

def options
  @options
end

#outputObject (readonly)

Returns the value of attribute output.



8
9
10
# File 'lib/ruql/quiz.rb', line 8

def output
  @output
end

#questionsObject (readonly)

Returns the value of attribute questions.



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

def questions
  @questions
end

#rendererObject (readonly)

Returns the value of attribute renderer.



5
6
7
# File 'lib/ruql/quiz.rb', line 5

def renderer
  @renderer
end

#seedObject (readonly)

Returns the value of attribute seed.



9
10
11
# File 'lib/ruql/quiz.rb', line 9

def seed
  @seed
end

#titleObject

Returns the value of attribute title.



11
12
13
# File 'lib/ruql/quiz.rb', line 11

def title
  @title
end

Class Method Details

.optionsObject



18
# File 'lib/ruql/quiz.rb', line 18

def self.options ; @@options ; end

.quiz(title, args = {}, &block) ⇒ Object



147
148
149
150
151
# File 'lib/ruql/quiz.rb', line 147

def self.quiz(title, args={}, &block)
  quiz = Quiz.new(title, args)
  quiz.instance_eval(&block)
  @@quizzes << quiz
end

.quizzesObject



17
# File 'lib/ruql/quiz.rb', line 17

def self.quizzes ; @@quizzes ; end

.resetObject



13
14
15
16
# File 'lib/ruql/quiz.rb', line 13

def self.reset
  @@quizzes = []
  @@options = {}
end

.set_options(options) ⇒ Object



45
46
47
# File 'lib/ruql/quiz.rb', line 45

def self.set_options(options)
  @@options = options
end

Instance Method Details

#as_jsonObject



31
32
33
34
35
36
# File 'lib/ruql/quiz.rb', line 31

def as_json
  Hash(:title => @title,
    :questions => @questions.map(&:as_json),
    :seed => @seed
    )
end

#choice_answer(*args, &block) ⇒ Object

this should really be done using mixins.



83
84
85
86
87
88
89
90
91
92
# File 'lib/ruql/quiz.rb', line 83

def choice_answer(*args, &block)
  if args.first.is_a?(Hash) # no question text
    q = MultipleChoice.new('',*args)
  else
    text = args.shift
    q = MultipleChoice.new(text, *args)
  end
  q.instance_eval(&block)
  @questions << q
end


110
111
112
113
114
# File 'lib/ruql/quiz.rb', line 110

def dropdown(*args, &block)
  q = Dropdown.new(*args)
  q.instance_eval(&block)
  @questions << q
end

#fill_in(*args, &block) ⇒ Object



136
137
138
139
140
141
142
143
144
145
# File 'lib/ruql/quiz.rb', line 136

def fill_in(*args, &block)
  if args.first.is_a?(Hash) # no question text
    q = FillIn.new('', *args)
  else
    text = args.shift
    q = FillIn.new(text, *args)
  end
  q.instance_eval(&block)
  @questions << q
end

#get_open_assessment(*args, &block) ⇒ Object



127
128
129
130
131
132
133
134
# File 'lib/ruql/quiz.rb', line 127

def get_open_assessment(*args, &block)
  y = @quiz_yaml.shift
  raise "Cannot continue - You must have a yaml block for each peer evaluation question" if y.nil?
  yaml = y[1][0]
  q = OpenAssessment.new(*args, yaml)
  q.instance_eval(&block)
  q
end

#grouped_pointsObject



63
64
65
66
67
68
# File 'lib/ruql/quiz.rb', line 63

def grouped_points
  gq = grouped_questions
  groups.sum do |g|
    gq.detect { |q| q.question_group == g }.points
  end
end

#grouped_questionsObject



53
54
55
# File 'lib/ruql/quiz.rb', line 53

def grouped_questions
  questions.filter { |q| q.question_group.to_s != '' }.sort_by(&:question_group)
end

#groupsObject



57
# File 'lib/ruql/quiz.rb', line 57

def groups ; questions.map(&:question_group).uniq.reject { |g| g.to_s == '' } ; end

#num_questionsObject



74
75
76
# File 'lib/ruql/quiz.rb', line 74

def num_questions
  groups.length + ungrouped_questions.length
end

#open_assessment(*args, &block) ⇒ Object



116
117
118
119
# File 'lib/ruql/quiz.rb', line 116

def open_assessment(*args, &block)
  q = get_open_assessment(*args, &block)
  @questions << q
end

#pointsObject



70
71
72
# File 'lib/ruql/quiz.rb', line 70

def points
  ungrouped_points + grouped_points
end

#random_seed(num) ⇒ Object



78
79
80
# File 'lib/ruql/quiz.rb', line 78

def random_seed(num)
  @seed = num.to_i
end

#render_with(renderer, options = {}) ⇒ Object



38
39
40
41
42
43
# File 'lib/ruql/quiz.rb', line 38

def render_with(renderer,options={})
  srand @seed
  @renderer = renderer.send(:new,self,options)
  @renderer.render_quiz
  @output = @renderer.output
end

#select_multiple(*args, &block) ⇒ Object



94
95
96
97
98
99
100
101
102
103
# File 'lib/ruql/quiz.rb', line 94

def select_multiple(*args, &block)
  if args.first.is_a?(Hash) # no question text
    q = SelectMultiple.new('', *args)
  else
    text = args.shift
    q = SelectMultiple.new(text, *args)
  end
  q.instance_eval(&block)
  @questions << q
end

#simple_open_assessment(*args, &block) ⇒ Object



121
122
123
124
125
# File 'lib/ruql/quiz.rb', line 121

def simple_open_assessment(*args, &block)
  q = get_open_assessment(*args, &block)
  q.add_simple_question
  @questions << q
end

#truefalse(*args) ⇒ Object



105
106
107
108
# File 'lib/ruql/quiz.rb', line 105

def truefalse(*args)
  q = TrueFalse.new(*args)
  @questions << q
end

#ungrouped_pointsObject



59
60
61
# File 'lib/ruql/quiz.rb', line 59

def ungrouped_points
  ungrouped_questions.map(&:points).sum
end

#ungrouped_questionsObject



49
50
51
# File 'lib/ruql/quiz.rb', line 49

def ungrouped_questions
  questions.filter { |q| q.question_group.to_s == '' }
end