Class: Senkyoshi::Calculated

Inherits:
Question show all
Defined in:
lib/senkyoshi/models/questions/calculated.rb

Constant Summary

Constants inherited from Question

Question::ITEM_FUNCTION, Question::QUESTION_TYPE

Instance Attribute Summary collapse

Attributes inherited from Question

#answers

Instance Method Summary collapse

Methods inherited from Question

from, #get_fraction, #iterate_item, #set_answers, #set_correct_answers, #set_feedback, #set_incorrect_answers, #set_material, #set_max_score

Methods inherited from Resource

#_find_directories, #_fix_path, #_matches_directory_xid?, #_search_and_replace, #cleanup, #fix_html, get_pre_data, #matches_xid?, #strip_xid

Constructor Details

#initializeCalculated

Returns a new instance of Calculated.



22
23
24
25
26
27
28
29
# File 'lib/senkyoshi/models/questions/calculated.rb', line 22

def initialize
  @dataset_definitions = []
  @var_sets = []
  @correct_answer_length = 0
  @correct_answer_format = 0
  @tolerance = 0
  super
end

Instance Attribute Details

#dataset_definitionsObject (readonly)

Returns the value of attribute dataset_definitions.



20
21
22
# File 'lib/senkyoshi/models/questions/calculated.rb', line 20

def dataset_definitions
  @dataset_definitions
end

#var_setsObject (readonly)

Returns the value of attribute var_sets.



20
21
22
# File 'lib/senkyoshi/models/questions/calculated.rb', line 20

def var_sets
  @var_sets
end

Instance Method Details

#_parse_var_sets(parent_node) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/senkyoshi/models/questions/calculated.rb', line 75

def _parse_var_sets(parent_node)
  parent_node.search("var_set").map do |var_set|
    vars = var_set.search("var").each_with_object({}) do |var, hash|
      hash[var.attributes["name"].text] = var.text
    end

    {
      ident: var_set.attributes["ident"].text,
      answer: var_set.at("answer").text,
      vars: vars,
    }
  end
end

#_parse_vars(parent_node) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/senkyoshi/models/questions/calculated.rb', line 61

def _parse_vars(parent_node)
  parent_node.search("var").map do |var|
    scale = var.attributes["scale"]
    min = var.at("min").text
    max = var.at("max").text
    options = ":#{min}:#{max}:#{scale}"

    {
      name: var.attributes["name"].value,
      options: options,
    }
  end
end

#canvas_conversionObject



31
32
33
34
35
36
37
38
# File 'lib/senkyoshi/models/questions/calculated.rb', line 31

def canvas_conversion(*)
  @question.dataset_definitions = @dataset_definitions
  @question.var_sets = @var_sets
  @question.correct_answer_length = @correct_answer_length
  @question.correct_answer_format = @correct_answer_format
  @question.tolerance = @tolerance
  super
end

#iterate_xml(data) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/senkyoshi/models/questions/calculated.rb', line 40

def iterate_xml(data)
  super
  calculated_node = data.at("itemproc_extension > calculated")
  math_ml = CGI.unescapeHTML(calculated_node.at("formula").text)
  formula = Nokogiri::HTML(math_ml).text

  answer = Answer.new(formula)
  @answers.push(answer)

  @tolerance = calculated_node.at("answer_tolerance").text
  # canvas_cc only uses the correct_answer_length if the
  # correct_answer_format is 1. It is not known what 1 represents.
  @correct_answer_format = 1
  @correct_answer_length = calculated_node.at("answer_scale").text

  @dataset_definitions = _parse_vars(calculated_node.at("vars"))
  @var_sets = _parse_var_sets(calculated_node.at("var_sets"))

  self
end