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

Constants inherited from Models::Base

Models::Base::ELEMENTS_REMAP

Instance Attribute Summary

Attributes inherited from BaseInteraction

#node

Attributes inherited from Models::Base

#doc

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from BaseInteraction

#xpath_with_single_check

Methods inherited from Base

#qti_version

Methods inherited from Models::Base

#css_with_single_check, from_path!, #parse_xml, #remap_href_path, #remap_unknown_tags_transformer, #sanitize_config, #sanitize_content!, #xpath_with_single_check

Constructor Details

#initialize(node) ⇒ GapMatchInteraction

Returns a new instance of GapMatchInteraction.



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

def initialize(node)
  @node = node
end

Class Method Details

.matches(node) ⇒ Object



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

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

Instance Method Details

#answersObject



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

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

#blanksObject



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

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

#choicesObject



113
114
115
# File 'lib/qti/v2/models/interactions/gap_match_interaction.rb', line 113

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

#clean_stem_itemsObject



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/qti/v2/models/interactions/gap_match_interaction.rb', line 20

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|
      if child.inner_text.strip.empty?
        child.remove
      end
    end

    gap_node
  end
end

#promptObject



36
37
38
# File 'lib/qti/v2/models/interactions/gap_match_interaction.rb', line 36

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

#prompt_hashObject



58
59
60
61
62
63
# File 'lib/qti/v2/models/interactions/gap_match_interaction.rb', line 58

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

#scoring_data_structsObject



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/qti/v2/models/interactions/gap_match_interaction.rb', line 93

def scoring_data_structs
  question_response_pairs = node.xpath('.//xmlns:correctResponse//xmlns:value').map do |value|
    value.content.split
  end
  question_response_pairs.map!{ |qrp| qrp.reverse }
  question_response_id_mapping = Hash[question_response_pairs]
  answer_nodes.map { |value_node|
    node_id = value_node.attributes['identifier']&.value
    answer_choice = choices.find{ |choice| choice.attributes['identifier']&.value == question_response_id_mapping[node_id] }
    ScoringData.new(
      answer_choice.content,
      'directedPair',
      {
        id: node_id,
        case: false
      }
    )
  }
end

#shuffled?Boolean

Returns:

  • (Boolean)


16
17
18
# File 'lib/qti/v2/models/interactions/gap_match_interaction.rb', line 16

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

#stem_itemsObject



40
41
42
43
44
45
46
47
# File 'lib/qti/v2/models/interactions/gap_match_interaction.rb', line 40

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



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

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



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/qti/v2/models/interactions/gap_match_interaction.rb', line 65

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