Class: Quiz

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

Constant Summary collapse

@@quizzes =
[]
@@default_options =
{
  :open_time => Time.now,
  :soft_close_time => Time.now + 24*60*60,
  :hard_close_time => Time.now + 24*60*60,
  :maximum_submissions => 1,
  :duration => 3600,
  :retry_delay => 600,
  :parameters =>  {
    :show_explanations => {
      :question => 'before_soft_close_time',
      :option => 'before_soft_close_time',
      :score => 'before_soft_close_time',
    }
  },
  :maximum_score => 1,
}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Quiz.



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

def initialize(title, options={})
  @output = ''
  @questions = options[:questions] || []
  @title = title
  @options = @@default_options.merge(options)
  @seed = srand
  @logger = Logger.new(STDERR)
  @logger.level = Logger.const_get (options.delete('l') ||
    options.delete('log') || 'warn').upcase
end

Instance Attribute Details

#loggerObject (readonly)

Returns the value of attribute logger.



28
29
30
# File 'lib/ruql/quiz.rb', line 28

def logger
  @logger
end

#optionsObject (readonly)

Returns the value of attribute options.



25
26
27
# File 'lib/ruql/quiz.rb', line 25

def options
  @options
end

#outputObject (readonly)

Returns the value of attribute output.



26
27
28
# File 'lib/ruql/quiz.rb', line 26

def output
  @output
end

#questionsObject (readonly)

Returns the value of attribute questions.



24
25
26
# File 'lib/ruql/quiz.rb', line 24

def questions
  @questions
end

#rendererObject (readonly)

Returns the value of attribute renderer.



23
24
25
# File 'lib/ruql/quiz.rb', line 23

def renderer
  @renderer
end

#seedObject (readonly)

Returns the value of attribute seed.



27
28
29
# File 'lib/ruql/quiz.rb', line 27

def seed
  @seed
end

#titleObject

Returns the value of attribute title.



29
30
31
# File 'lib/ruql/quiz.rb', line 29

def title
  @title
end

Class Method Details

.get_renderer(renderer) ⇒ Object



42
43
44
# File 'lib/ruql/quiz.rb', line 42

def self.get_renderer(renderer)
  Object.const_get(renderer.to_s + 'Renderer') rescue nil
end

.quiz(*args, &block) ⇒ Object



100
101
102
103
104
# File 'lib/ruql/quiz.rb', line 100

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

.quizzesObject



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

def self.quizzes ; @@quizzes ;  end

Instance Method Details

#choice_answer(*args, &block) ⇒ Object

this should really be done using mixins.



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

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

#fill_in(*args, &block) ⇒ Object



89
90
91
92
93
94
95
96
97
98
# File 'lib/ruql/quiz.rb', line 89

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

#num_questionsObject



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

def num_questions ; questions.length ; end

#pointsObject



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

def points ; questions.map(&:points).inject { |sum,points| sum + points } ; end

#random_seed(num) ⇒ Object



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

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

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



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

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

#select_multiple(*args, &block) ⇒ Object



73
74
75
76
77
78
79
80
81
82
# File 'lib/ruql/quiz.rb', line 73

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

#truefalse(*args) ⇒ Object



84
85
86
87
# File 'lib/ruql/quiz.rb', line 84

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