Class: Ruql::Prairielearn::Question

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

Constant Summary collapse

@@dirnames =

keep track of titles seen, since subdir names must be unique

{}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(question, partial_credit, extra_tags, default_topic, path = '.') ⇒ Question

Returns a new instance of Question.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/ruql/prairielearn/question.rb', line 12

def initialize(question,partial_credit,extra_tags,default_topic,path='.')
  @gem_root = Gem.loaded_specs['ruql-prairielearn'].full_gem_path
  @@path ||= path
  @question = question
  @partial_credit = partial_credit
  @extra_tags = extra_tags
  @json_template =            File.join(@gem_root, 'templates', 'info.json.erb')
  @multiple_choice_template = File.join(@gem_root, 'templates', 'pl-multiple-choice.html.erb')
  @select_multiple_template = File.join(@gem_root, 'templates', 'pl-checkbox.html.erb')
  @uuid = SecureRandom.uuid
  @tags = @question.question_tags
  @digest = @question.question_text.strip[0,40] + '...'
  @title = (@tags.empty? ? @uuid : @tags.shift)
  @title = @title.capitalize unless @title =~ /[A-Z]/ # Capitalize if all lowercase
  @topic = (t = @tags.detect { |tag| tag =~ /^topic:/ }) ?
             @tags.delete(t).gsub(/^topic:/, '') :
             default_topic
  if (group = question.question_group.to_s) != ''
    @tags << "group:#{group}"
  end
  @tags += extra_tags
  @none_of_the_above = question.answers.any? { |a| a =~ /^none of (these|the above)$/i }
  @question_dir = nil
end

Instance Attribute Details

#digestObject (readonly)

Returns the value of attribute digest.



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

def digest
  @digest
end

#none_of_the_aboveObject (readonly)

Returns the value of attribute none_of_the_above.



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

def none_of_the_above
  @none_of_the_above
end

#partial_creditObject (readonly)

Returns the value of attribute partial_credit.



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

def partial_credit
  @partial_credit
end

#questionObject (readonly)

Returns the value of attribute question.



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

def question
  @question
end

#question_dirObject (readonly)

Returns the value of attribute question_dir.



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

def question_dir
  @question_dir
end

#tagsObject (readonly)

Returns the value of attribute tags.



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

def tags
  @tags
end

#titleObject (readonly)

Returns the value of attribute title.



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

def title
  @title
end

#topicObject (readonly)

Returns the value of attribute topic.



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

def topic
  @topic
end

#uuidObject (readonly)

Returns the value of attribute uuid.



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

def uuid
  @uuid
end

Class Method Details

.clean_up_after_error!Object

If anything goes wrong, delete any files we created



49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/ruql/prairielearn/question.rb', line 49

def self.clean_up_after_error!
  @@dirnames.each_pair do |dir,num|
    fullpath = File.join(@@path,dir)
    STDERR.puts "delete #{fullpath}"
    FileUtils.rm_rf fullpath
    if num > 1
      2.upto(num).each do |otherdir|
        fullpath = File.join(@@path,"#{dir}_#{otherdir}")
        STDERR.puts "delete #{fullpath}"
        FileUtils.rm_rf fullpath
      end
    end
  end
end

Instance Method Details

#create_question_files!Object



37
38
39
40
41
42
43
44
45
46
# File 'lib/ruql/prairielearn/question.rb', line 37

def create_question_files!
  begin
    @question_dir = dirname()
    Dir.mkdir(@question_dir) unless File.directory?(@question_dir)
    create_json!
    create_question_html!
  rescue RuntimeError => e
    raise Ruql::QuizContentError.new(e.message)
  end
end