Class: CanvasQtiToLearnosityConverter::CalculatedQuestion

Inherits:
TemplateQuestion show all
Defined in:
lib/canvas_qti_to_learnosity_converter/questions/calculated.rb

Instance Method Summary collapse

Methods inherited from TemplateQuestion

#extract_template, #get_template

Methods inherited from QuizQuestion

#convert, #extract_feedback, #extract_mattext, #extract_points_possible, for, #initialize, #make_identifier, #process_assets!

Constructor Details

This class inherits a constructor from CanvasQtiToLearnosityConverter::QuizQuestion

Instance Method Details

#add_learnosity_assets(assets, path, learnosity) ⇒ Object



51
52
53
54
55
56
57
58
# File 'lib/canvas_qti_to_learnosity_converter/questions/calculated.rb', line 51

def add_learnosity_assets(assets, path, learnosity)
  process_assets!(
    assets,
    path,
    learnosity[:stimulus]
  )
  learnosity
end

#dynamic_content_dataObject



60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/canvas_qti_to_learnosity_converter/questions/calculated.rb', line 60

def dynamic_content_data()
  values = extract_dynamic_content_data()

  columns = (0...(values.first.count - 1)).map{ |x| "val#{x}" }
  columns.push("answer")

  rows = Hash[values.map.with_index do |row, index|
    [make_identifier(), { values: row, index: index }]
  end]

  { cols: columns, rows: rows }
end

#extract_dynamic_content_dataObject



73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/canvas_qti_to_learnosity_converter/questions/calculated.rb', line 73

def extract_dynamic_content_data()
  template = get_template()
  vars = extract_template_values(template).map do |var_name|
    @xml.css(%{item > itemproc_extension > calculated > var_sets >
      var_set > var[name="#{var_name}"]}).map { |node| node.text }
  end

  answers = @xml.css("item > itemproc_extension var_sets answer").map do |node|
    node.text
  end

  vars.push(answers).transpose
end

#extract_stimulusObject



15
16
17
18
19
20
21
22
23
24
25
# File 'lib/canvas_qti_to_learnosity_converter/questions/calculated.rb', line 15

def extract_stimulus()
  template = get_template()
  uses_backtick = template.match?(/`[^`]+`/)

  extract_template_values(template).each.with_index do |var_name, index|
    pattern = uses_backtick ? "`#{var_name}`" : "[#{var_name}]"
    template.sub!(pattern, "{{var:val#{index}}}")
  end

  template
end

#extract_template_values(template) ⇒ Object

New Quizzes exports variables with backtick notation (e.g. ‘w`), while Classic Quizzes uses square bracket notation (e.g. [w]). Detect which format is in use and return plain variable names either way.



30
31
32
33
# File 'lib/canvas_qti_to_learnosity_converter/questions/calculated.rb', line 30

def extract_template_values(template)
  backtick_vars = template.scan(/`([^`]+)`/).map(&:first)
  backtick_vars.any? ? backtick_vars : super
end

#extract_validationObject



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/canvas_qti_to_learnosity_converter/questions/calculated.rb', line 35

def extract_validation()
  pm = @xml.css("item > itemproc_extension > calculated > answer_tolerance")
    .first.text

  {
    "scoring_type" => "exactMatch",
    "valid_response" => {
      "score" => extract_points_possible,
      "value" => [[{
        "method" => "equivValue",
        "value" => "{{var:answer}}\\pm#{pm}"
      }]]
    }
  }
end

#to_learnosityObject



5
6
7
8
9
10
11
12
13
# File 'lib/canvas_qti_to_learnosity_converter/questions/calculated.rb', line 5

def to_learnosity
  {
    type: "clozeformula",
    is_math: true,
    stimulus: extract_stimulus(),
    template: "{{response}}",
    validation: extract_validation(),
  }
end