Class: Qti::V1::Models::Interactions::FillBlankInteraction

Inherits:
BaseInteraction show all
Defined in:
lib/qti/v1/models/interactions/fill_blank_interaction.rb

Constant Summary collapse

CANVAS_REGEX =
/(\[.+?\])/.freeze

Constants inherited from Models::Base

Models::Base::ELEMENTS_REMAP

Instance Attribute Summary

Attributes inherited from BaseInteraction

#node

Attributes inherited from Models::Base

#doc, #manifest, #package_root, #path

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from BaseInteraction

canvas_multiple_fib?, #initialize, #shuffled?

Methods inherited from Base

#qti_version, #return_inner_content!

Methods inherited from Models::Base

#css_with_single_check, from_path!, #initialize, #object_tag_transformer, #parse_html, #parse_xml, #remap_href_path, #remap_unknown_tags_transformer, #sanitize_config, #sanitize_content!, #xpath_with_single_check

Constructor Details

This class inherits a constructor from Qti::V1::Models::Interactions::BaseInteraction

Class Method Details

.matches(node, parent) ⇒ Object

This will know if a class matches



9
10
11
12
13
14
15
16
17
18
19
# File 'lib/qti/v1/models/interactions/fill_blank_interaction.rb', line 9

def self.matches(node, parent)
  return false if node.at_xpath('.//xmlns:other').present?
  match = if BaseInteraction.canvas_multiple_fib?(node)
      node.at_xpath('.//xmlns:response_lid')
    else
      node.at_xpath('.//xmlns:render_fib')
    end
  return false if match.blank? ||
    match.attributes['fibtype']&.value == 'Decimal'
  new(node, parent)
end

Instance Method Details

#answersObject



108
109
110
111
112
# File 'lib/qti/v1/models/interactions/fill_blank_interaction.rb', line 108

def answers
  @answers ||= answer_nodes.map do |node|
    V1::Models::Choices::FillBlankChoice.new(node, self)
  end
end

#blanksObject



88
89
90
91
92
93
94
# File 'lib/qti/v1/models/interactions/fill_blank_interaction.rb', line 88

def blanks
  if node.at_xpath('.//xmlns:render_choice').present?
    canvas_blanks
  else
    qti_standard_blanks
  end
end

#canvas_blanksObject



96
97
98
99
100
# File 'lib/qti/v1/models/interactions/fill_blank_interaction.rb', line 96

def canvas_blanks
  node.xpath('.//xmlns:response_label').map do |blank|
    { id: blank.attributes['ident']&.value }
  end
end

#canvas_multiple_fib?Boolean

Returns:

  • (Boolean)


21
22
23
# File 'lib/qti/v1/models/interactions/fill_blank_interaction.rb', line 21

def canvas_multiple_fib?
  BaseInteraction.canvas_multiple_fib?(@node)
end

#canvas_stem_itemsObject



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/qti/v1/models/interactions/fill_blank_interaction.rb', line 33

def canvas_stem_items
  @response_lid_nodes = node.xpath('.//xmlns:response_lid')
  item_prompt = node.at_xpath('.//xmlns:mattext').text
  item_prompt_words = item_prompt.split(CANVAS_REGEX)
  item_prompt_words.map.with_index do |stem_item, index|
    if stem_item.match CANVAS_REGEX
      @response_lid_nodes.children.map do |response_lid_node|
        if stem_item.include?(response_lid_node.text)
          @blank_id = response_lid_node.parent.attributes['ident']&.value
        end
      end

      {
        id: "stem_#{index}",
        position: index + 1,
        type: 'blank',
        blank_id: @blank_id
      }
    else
      {
        id: "stem_#{index}",
        position: index + 1,
        type: 'text',
        value: stem_item
      }
    end
  end
end

#qti_standard_blanksObject



102
103
104
105
106
# File 'lib/qti/v1/models/interactions/fill_blank_interaction.rb', line 102

def qti_standard_blanks
  node.xpath('.//xmlns:response_str').map do |blank|
    { id: blank.attributes['ident']&.value }
  end
end

#qti_stem_itemsObject



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/qti/v1/models/interactions/fill_blank_interaction.rb', line 63

def qti_stem_items
  stem_item_nodes = node.xpath('.//xmlns:presentation').children
  stem_item_nodes.map.with_index do |stem_item, index|
    if stem_item.xpath('./xmlns:render_fib').present?
      {
        id: "stem_#{index}",
        position: index + 1,
        type: 'blank',
        blank_id: stem_item.attributes['ident'].value
      }
    else
      {
        id: "stem_#{index}",
        position: index + 1,
        type: 'text',
        value: stem_item.children.text
      }
    end
  end
end

#scoring_data_structsObject



114
115
116
117
118
119
120
121
122
123
124
# File 'lib/qti/v1/models/interactions/fill_blank_interaction.rb', line 114

def scoring_data_structs
  answer_nodes.map do |value_node|
    ScoringData.new(
      value_node.content,
      rcardinality,
      id: value_node.attributes['respident']&.value || value_node.attributes['ident']&.value,
      case: value_node.attributes['case']&.value || 'no',
      parent_identifier: value_node.parent.parent.attributes['ident']&.value,
    )
  end
end

#single_fill_in_blank?Boolean

Returns:

  • (Boolean)


84
85
86
# File 'lib/qti/v1/models/interactions/fill_blank_interaction.rb', line 84

def single_fill_in_blank?
  blanks.count == 1
end

#stem_itemsObject



25
26
27
28
29
30
31
# File 'lib/qti/v1/models/interactions/fill_blank_interaction.rb', line 25

def stem_items
  if canvas_multiple_fib?
    canvas_stem_items
  else
    qti_stem_items
  end
end