Class: CanvasQtiToLearnosityConverter::MultipleChoiceQuestion

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

Direct Known Subclasses

MultipleAnswersQuestion

Instance Method Summary collapse

Methods inherited from QuizQuestion

#convert, #dynamic_content_data, #extract_feedback, #extract_mattext, #extract_points_possible, #extract_stimulus, 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



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/canvas_qti_to_learnosity_converter/questions/multiple_choice.rb', line 94

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

  learnosity[:options].each.with_index do |option, index|
    process_assets!(
      assets,
      path,
      option["label"]
    )
  end
  learnosity
end

#extract_optionsObject



5
6
7
8
9
10
11
12
13
14
# File 'lib/canvas_qti_to_learnosity_converter/questions/multiple_choice.rb', line 5

def extract_options()
  choices = @xml.css("item > presentation > response_lid > render_choice > response_label")
  choices.map do |choice|
    ident = choice.attribute("ident").value
    {
      "value" => ident,
      "label" => extract_mattext(choice.css("material > mattext").first),
    }
  end
end

#extract_response_idObject



16
17
18
# File 'lib/canvas_qti_to_learnosity_converter/questions/multiple_choice.rb', line 16

def extract_response_id()
  @xml.css("item > presentation > response_lid").attribute("ident").value
end

#extract_validationObject



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
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
71
72
73
74
75
76
77
78
79
# File 'lib/canvas_qti_to_learnosity_converter/questions/multiple_choice.rb', line 20

def extract_validation()
  resp_conditions = @xml.css("item > resprocessing > respcondition")
  
  # Extract all conditions that award points
  scored_conditions = resp_conditions.filter_map do |condition|
    setvar = condition.css("setvar").first
    next unless setvar
    
    score = setvar.text.to_f
    next if score <= 0
    
    answer_value = condition.css("varequal").text
    next if answer_value.empty?
    
    {
      value: answer_value,
      score: score
    }
  end.sort_by { |c| -c[:score] } # Sort by score descending
  
  return nil if scored_conditions.empty?
  
  # Get the highest scoring answer
  best_answer = scored_conditions.first
  points_possible = extract_points_possible
  
  # Helper method to scale score to points_possible
  scale_score = ->(score) { points_possible * (score / 100.0) }
  
  # If there's only one scoring option, use exactMatch
  if scored_conditions.length == 1
    {
      "scoring_type" => "exactMatch",
      "valid_response" => {
        "value" => [best_answer[:value]],
        "score" => scale_score.call(best_answer[:score]),
      },
    }
  else
    # Multiple scoring options, use partialMatch
    validation = {
      "scoring_type" => "partialMatch",
      "valid_response" => {
        "value" => [best_answer[:value]],
        "score" => scale_score.call(best_answer[:score]),
      },
    }
    
    # Add alternative responses with lower scores
    alt_responses = scored_conditions[1..].map do |condition|
      {
        "value" => [condition[:value]],
        "score" => scale_score.call(condition[:score]),
      }
    end
    
    validation["alt_responses"] = alt_responses if alt_responses.any?
    validation
  end
end

#to_learnosityObject



81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/canvas_qti_to_learnosity_converter/questions/multiple_choice.rb', line 81

def to_learnosity
  shuffle = @xml.css("item > presentation > response_lid > render_choice").first&.attribute("shuffle")&.value
  {
    stimulus: extract_stimulus(),
    options: extract_options(),
    multiple_responses: false,
    shuffle_options: shuffle == "Yes",
    response_id: extract_response_id(),
    type: "mcq",
    validation: extract_validation(),
  }
end