Module: Qti::Models::ResourceGroup

Included in:
Manifest, V2::Models::NonAssessmentTest
Defined in:
lib/qti/models/resource.rb

Constant Summary collapse

RESOURCE_QTI_TYPES =
%w[imsqti_test_xmlv2p1
imsqti_test_xmlv2p2
imsqti_xmlv1p2].freeze
ASSESSMENT_CLASSES =
{
  'imsqti_xmlv1p2' => Qti::V1::Models::Assessment,
  'imsqti_test_xmlv2p1' => Qti::V2::Models::AssessmentTest,
  'imsqti_test_xmlv2p2' => Qti::V2::Models::AssessmentTest
}.freeze
EMBEDDED_QTI_TYPES =
%w[imsqti_item_xmlv2p1
imsqti_item_xmlv2p2].freeze
EMBEDDED_NON_ASSESSMENT_ID =
'@embedded_non_assessment'.freeze

Instance Method Summary collapse

Instance Method Details

#assessment_identifiers(embedded_as_assessment: true) ⇒ Object



94
95
96
97
98
# File 'lib/qti/models/resource.rb', line 94

def assessment_identifiers(embedded_as_assessment: true)
  id_list = identifier_list('/assessment')
  return id_list + [EMBEDDED_NON_ASSESSMENT_ID] if embedded_as_assessment && embedded_non_assessment?
  id_list
end

#asset_resource_for(rsc) ⇒ Object



64
65
66
# File 'lib/qti/models/resource.rb', line 64

def asset_resource_for(rsc)
  asset_resource_for_canvas(rsc) || asset_resource_for_ims(rsc)
end

#asset_resource_for_canvas(rsc) ⇒ Object



68
69
70
# File 'lib/qti/models/resource.rb', line 68

def asset_resource_for_canvas(rsc)
  rsc.canvas_extra_file('.xml.qti')
end

#asset_resource_for_ims(rsc) ⇒ Object



72
73
74
# File 'lib/qti/models/resource.rb', line 72

def asset_resource_for_ims(rsc)
  rsc.href
end

#associated_content_listObject



100
101
102
103
# File 'lib/qti/models/resource.rb', line 100

def associated_content_list
  predicate = rtype_predicate('associatedcontent', 'learning-application-resource')
  xmlns_resource_list("[#{predicate}]").map { |r| r[:identifier] }
end

#identifier_list(rsc_type) ⇒ Object



82
83
84
85
86
# File 'lib/qti/models/resource.rb', line 82

def identifier_list(rsc_type)
  RESOURCE_QTI_TYPES.map do |v|
    xmlns_resource_list("[#{rtype_predicate(v, rsc_type)}]").map { |r| r[:identifier] }
  end.flatten
end

#item_resources_v2Object



125
126
127
128
129
# File 'lib/qti/models/resource.rb', line 125

def item_resources_v2
  nodes = resources('[@type="imsqti_item_xmlv2p2"]')
  return nodes if nodes.count >= 1
  resources('[@type="imsqti_item_xmlv2p1"]')
end

#load_asset_resource(rsc_path) ⇒ Object

Raises:

  • (ArgumentError)


76
77
78
79
80
# File 'lib/qti/models/resource.rb', line 76

def load_asset_resource(rsc_path)
  doc = Nokogiri.XML(File.read(rsc_path), rsc_path.to_s, &:noblanks)
  raise ArgumentError unless doc
  doc
end

#load_associated_contentObject



105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/qti/models/resource.rb', line 105

def load_associated_content
  @_load_associated_content ||= associated_content_list.map do |ident|
    rsc = resource_for(ident)
    rsc_path = remap_href_path(asset_resource_for(rsc))
    doc = load_asset_resource(rsc_path)

    # There are other types, but all we support right now are object banks...
    unless doc.search('objectbank').empty?
      Qti::V1::Models::ObjectBank.from_path!(rsc_path, package_root: @package_root,
                                                       resource: rsc)
    end
  end.reject(&:nil?)
end

#objectbanksObject



119
120
121
122
123
# File 'lib/qti/models/resource.rb', line 119

def objectbanks
  load_associated_content.select do |c|
    c.instance_of?(Qti::V1::Models::ObjectBank)
  end
end

#resource_for(identifier, qti_type = nil) ⇒ Object



88
89
90
91
92
# File 'lib/qti/models/resource.rb', line 88

def resource_for(identifier, qti_type = nil)
  qti_type = " and starts-with(@type, '#{qti_type}')" if qti_type
  base_xpath = "[@identifier='#{identifier}'#{qti_type}]"
  Resource.new(resource_node(base_xpath), self)
end

#resource_node(type) ⇒ Object



131
132
133
# File 'lib/qti/models/resource.rb', line 131

def resource_node(type)
  xpath_with_single_check(xpath_resource(type))
end

#resources(type = '') ⇒ Object



60
61
62
# File 'lib/qti/models/resource.rb', line 60

def resources(type = '')
  @doc.xpath(xpath_resource(type))
end