Class: CanvasQtiToLearnosityConverter::HotSpotQuestion

Inherits:
QuizQuestion
  • Object
show all
Defined in:
lib/canvas_qti_to_learnosity_converter/questions/hotspot.rb

Instance Method Summary collapse

Methods inherited from QuizQuestion

#convert, #dynamic_content_data, #extract_feedback, #extract_mattext, #extract_points_possible, for, #initialize, #make_identifier, #process_assets!

Constructor Details

This class inherits a constructor from CanvasQtiToLearnosityConverter::QuizQuestion

Instance Method Details

#add_learnosity_assets(assets, path, learnosity) ⇒ Object



110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/canvas_qti_to_learnosity_converter/questions/hotspot.rb', line 110

def add_learnosity_assets(assets, path, learnosity)
  process_assets!(
    assets,
    path,
    learnosity[:stimulus]
  )

  if learnosity[:image]
    process_image_asset!(assets, path, learnosity[:image])
  end

  learnosity
end

#convert_coords_to_points(rarea, coords_str) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/canvas_qti_to_learnosity_converter/questions/hotspot.rb', line 38

def convert_coords_to_points(rarea, coords_str)
  coords = coords_str.split(",").map(&:to_f)

  case rarea
  when "ellipse"
    x1, y1, x2, y2 = coords
    cx = ((x1 + x2) / 2.0) * 100
    cy = ((y1 + y2) / 2.0) * 100
    rx = ((x2 - x1).abs / 2.0) * 100
    ry = ((y2 - y1).abs / 2.0) * 100
    num_points = 12
    (0...num_points).map do |i|
      angle = 2 * Math::PI * i / num_points
      {
        "x" => cx + rx * Math.cos(angle),
        "y" => cy + ry * Math.sin(angle),
      }
    end
  when "polygon", "bounded"
    coords.each_slice(2).map do |x, y|
      { "x" => x * 100, "y" => y * 100 }
    end
  else
    # rectangle (default): x1,y1,x2,y2
    x1, y1, x2, y2 = coords
    [
      { "x" => x1 * 100, "y" => y1 * 100 },
      { "x" => x2 * 100, "y" => y1 * 100 },
      { "x" => x2 * 100, "y" => y2 * 100 },
      { "x" => x1 * 100, "y" => y2 * 100 },
    ]
  end
end

#extract_areas_and_attributesObject



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/canvas_qti_to_learnosity_converter/questions/hotspot.rb', line 72

def extract_areas_and_attributes()
  response_labels = @xml.css("item > presentation > flow > response_xy > render_hotspot > response_label")

  areas = []
  individual_attrs = []

  response_labels.each_with_index do |label, index|
    rarea = label.attribute("rarea")&.value || "rectangle"
    coords_str = label.text.strip
    ident = label.attribute("ident")&.value || index.to_s

    areas << convert_coords_to_points(rarea, coords_str)
    individual_attrs << { "area" => index.to_s, "label" => ident }
  end

  area_attributes = {
    "global" => {
      "fill" => "rgba(255,255,255,0)",
      "stroke" => "rgba(15,61,109,0.8)"
    },
    "individual" => individual_attrs
  }

  [areas, area_attributes]
end

#extract_imageObject



28
29
30
31
32
33
34
35
36
# File 'lib/canvas_qti_to_learnosity_converter/questions/hotspot.rb', line 28

def extract_image()
  matimage = @xml.css("item > presentation > flow > response_xy > render_hotspot > material > matimage").first
  return nil unless matimage

  uri = matimage.attribute("uri")&.value
  return nil unless uri

  { source: uri }
end

#extract_stimulusObject



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

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

#extract_validation(area_count) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
# File 'lib/canvas_qti_to_learnosity_converter/questions/hotspot.rb', line 98

def extract_validation(area_count)
  valid_indices = (0...area_count).map(&:to_s)

  {
    "scoring_type" => "exactMatch",
    "valid_response" => {
      "score" => extract_points_possible,
      "value" => valid_indices,
    }
  }
end

#to_learnosityObject



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/canvas_qti_to_learnosity_converter/questions/hotspot.rb', line 5

def to_learnosity
  areas, area_attributes = extract_areas_and_attributes()

  result = {
    type: "hotspot",
    stimulus: extract_stimulus(),
    areas: areas,
    area_attributes: area_attributes,
    multiple_responses: true,
    validation: extract_validation(areas.length),
  }

  image = extract_image()
  result[:image] = image if image

  result
end