Class: CanvasQtiToLearnosityConverter::QuizQuestion

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/canvas_qti_to_learnosity_converter/questions/question.rb

Instance Method Summary collapse

Constructor Details

#initialize(xml) ⇒ QuizQuestion

Returns a new instance of QuizQuestion.



8
9
10
# File 'lib/canvas_qti_to_learnosity_converter/questions/question.rb', line 8

def initialize(xml)
  @xml = xml
end

Instance Method Details

#convert(assets, path) ⇒ Object



59
60
61
62
# File 'lib/canvas_qti_to_learnosity_converter/questions/question.rb', line 59

def convert(assets, path)
  object = to_learnosity
  add_learnosity_assets(assets, path, object)
end

#dynamic_content_dataObject



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

def dynamic_content_data()
  {}
end

#extract_mattext(mattext_node) ⇒ Object



23
24
25
# File 'lib/canvas_qti_to_learnosity_converter/questions/question.rb', line 23

def extract_mattext(mattext_node)
  mattext_node.content
end

#extract_points_possibleObject



17
18
19
20
21
# File 'lib/canvas_qti_to_learnosity_converter/questions/question.rb', line 17

def extract_points_possible
  @xml.css(%{ item > itemmetadata > qtimetadata >
    qtimetadatafield > fieldlabel:contains("points_possible")})
    &.first&.next&.text&.to_f || 1.0
end

#extract_stimulusObject



12
13
14
15
# File 'lib/canvas_qti_to_learnosity_converter/questions/question.rb', line 12

def extract_stimulus()
  mattext = @xml.css("item > presentation > material > mattext").first
  extract_mattext(mattext)
end

#make_identifierObject



27
28
29
# File 'lib/canvas_qti_to_learnosity_converter/questions/question.rb', line 27

def make_identifier()
  SecureRandom.uuid
end

#process_assets!(assets, path, text) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/canvas_qti_to_learnosity_converter/questions/question.rb', line 35

def process_assets!(assets, path, text)
  doc = Nokogiri::XML.fragment(text)
  changed = false
  doc.css("img").each do |node|
    source = node["src"]
    next if !source

    source = URI::DEFAULT_PARSER.unescape(source)
    if /^\$IMS-CC-FILEBASE\$(.*)/.match(source) || /^((?!https?:).*)/.match(source)
      if source.start_with?("$IMS-CC-FILEBASE$")
        path = ''
      end
      asset_path = $1
      asset_path = asset_path.split("?").first.gsub(/^\//, '')
      asset_path = File.join(path, asset_path)
      clean_ext = File.extname(asset_path).gsub(/[^a-z0-9_.-]/i, '')
      assets[asset_path] ||= "#{SecureRandom.uuid}#{clean_ext}"
      node["src"] = "___EXPORT_ROOT___/assets/#{assets[asset_path]}"
      changed = true
    end
  end
  text.replace(doc.to_xml) if changed
end