Class: Qti::V2::Models::Interactions::GapMatchInteraction

Inherits:
BaseInteraction show all
Defined in:
lib/qti/v2/models/interactions/gap_match_interaction.rb

Constant Summary

Constants inherited from Base

Base::BODY_ELEMENTS_CSS, Base::CHOICE_ELEMENTS_CSS, Base::INTERACTION_ELEMENTS_CSS

Instance Attribute Summary

Attributes inherited from BaseInteraction

#node

Attributes inherited from Models::Base

#doc, #manifest, #package_root, #path, #resource

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from BaseInteraction

#canvas_item_feedback, #initialize, #xpath_with_single_check

Methods inherited from Base

#qti_version

Methods inherited from Models::Base

#css_with_single_check, from_path!, #initialize, #parse_html, #parse_xml, #preprocess_xml_doc, #raise_unsupported, #remap_href_path, #sanitize_content!, #xpath_with_single_check

Constructor Details

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

Class Method Details

.matches(node, parent) ⇒ Object



6
7
8
9
10
# File 'lib/qti/v2/models/interactions/gap_match_interaction.rb', line 6

def self.matches(node, parent)
  matches = node.xpath('.//xmlns:gapMatchInteraction')
  return false if matches.count != 1
  new(node, parent)
end

Instance Method Details

#answersObject



83
84
85
86
87
# File 'lib/qti/v2/models/interactions/gap_match_interaction.rb', line 83

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

#blanksObject



77
78
79
80
81
# File 'lib/qti/v2/models/interactions/gap_match_interaction.rb', line 77

def blanks
  node.xpath('.//xmlns:gapText').map do |blank|
    { id: blank.attributes['identifier'].value }
  end
end

#choicesObject



105
106
107
# File 'lib/qti/v2/models/interactions/gap_match_interaction.rb', line 105

def choices
  @node.xpath('.//xmlns:gapText')
end

#clean_stem_itemsObject



16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/qti/v2/models/interactions/gap_match_interaction.rb', line 16

def clean_stem_items
  @clean_stem_items ||= begin
    node = @node.dup
    gap_node = node.xpath('.//xmlns:gapMatchInteraction')
    gap_node.children.filter('gapText').each(&:remove)

    gap_node.children.each do |child|
      child.remove if child.inner_text.strip.empty?
    end

    gap_node
  end
end

#correct_choice_value(node_id) ⇒ Object



89
90
91
# File 'lib/qti/v2/models/interactions/gap_match_interaction.rb', line 89

def correct_choice_value(node_id)
  answer_choice(choices, question_response_id_mapping[node_id]).content
end

#promptObject



30
31
32
# File 'lib/qti/v2/models/interactions/gap_match_interaction.rb', line 30

def prompt
  clean_stem_items.children.filter('prompt')
end

#prompt_hashObject



52
53
54
55
56
57
# File 'lib/qti/v2/models/interactions/gap_match_interaction.rb', line 52

def prompt_hash
  {
    type: 'text',
    value: prompt.first.text
  }
end

#scoring_data_structsObject



93
94
95
96
97
98
99
100
101
102
103
# File 'lib/qti/v2/models/interactions/gap_match_interaction.rb', line 93

def scoring_data_structs
  answer_nodes.map do |value_node|
    node_id = value_node.attributes['identifier']&.value
    ScoringData.new(
      correct_choice_value(node_id),
      'directedPair',
      id: node_id,
      case: false
    )
  end
end

#shuffled?Boolean

Returns:

  • (Boolean)


12
13
14
# File 'lib/qti/v2/models/interactions/gap_match_interaction.rb', line 12

def shuffled?
  @node.at_xpath('.//xmlns:gapMatchInteraction').attributes['shuffle']&.value.try(:downcase) == 'true'
end

#stem_itemsObject



34
35
36
37
38
39
40
41
# File 'lib/qti/v2/models/interactions/gap_match_interaction.rb', line 34

def stem_items
  if prompt.present?
    stem_text_with_prompt = stem_text.unshift(prompt_hash)
    stem_items_with_id_and_position(stem_text_with_prompt)
  else
    stem_items_with_id_and_position(stem_text)
  end
end

#stem_items_with_id_and_position(stem_text) ⇒ Object



43
44
45
46
47
48
49
50
# File 'lib/qti/v2/models/interactions/gap_match_interaction.rb', line 43

def stem_items_with_id_and_position(stem_text)
  stem_text.map.with_index do |stem_item, index|
    stem_item.merge(
      id: "stem_#{index}",
      position: index + 1
    )
  end
end

#stem_textObject



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/qti/v2/models/interactions/gap_match_interaction.rb', line 59

def stem_text
  clean_stem_items.search('p').children.map do |stem_item|
    if stem_item.name == 'gap'
      blank_id = stem_item.attributes['identifier'].value
      {
        type: 'blank',
        blank_id: blank_id,
        blank_name: correct_choice_value(blank_id)
      }
    else
      {
        type: 'text',
        value: stem_item.text.empty? ? ' ' : stem_item.text
      }
    end
  end
end