Class: CanvasQtiToLearnosityConverter::Converter

Inherits:
Object
  • Object
show all
Defined in:
lib/canvas_qti_to_learnosity_converter/convert.rb

Defined Under Namespace

Classes: CanvasEntryTypeNotSupportedError, CanvasQtiQuiz, CanvasQuestionTypeNotSupportedError

Constant Summary collapse

FEATURE_TYPES =
[ :text_only_question ]
QUESTION_TYPES =
[
  :multiple_choice_question,
  :true_false_question,
  :multiple_answers_question,
  :short_answer_question,
  :fill_in_multiple_blanks_question,
  :multiple_dropdowns_question,
  :matching_question,
  :essay_question,
  :file_upload_question,
  :ordering_question,
  :hot_spot_question,
  :categorization_question,
]
TYPE_MAP =
{
  multiple_choice_question: MultipleChoiceQuestion,
  true_false_question: MultipleChoiceQuestion,
  multiple_answers_question: MultipleAnswersQuestion,
  short_answer_question: ShortAnswerQuestion,
  fill_in_multiple_blanks_question: FillTheBlanksQuestion,
  multiple_dropdowns_question: MultipleDropdownsQuestion,
  matching_question: MatchingQuestion,
  essay_question: EssayQuestion,
  file_upload_question: FileUploadQuestion,
  ordering_question: OrderingQuestion,
  hot_spot_question: HotSpotQuestion,
  categorization_question: CategorizationQuestion,
  text_only_question: TextOnlyQuestion,
  numerical_question: NumericalQuestion,
  calculated_question: CalculatedQuestion,

  "cc.multiple_choice.v0p1": MultipleChoiceQuestion,
  "cc.multiple_response.v0p1": MultipleAnswersQuestion,
  "cc.fib.v0p1": ShortAnswerQuestion,
  "cc.true_false.v0p1": MultipleChoiceQuestion,
  "cc.essay.v0p1": EssayQuestion,
}
MAX_QUESTIONS_PER_ITEM =

Canvas lets you create questions that are associated with a stimulus. Authoring these in Learnosity is terrible if there are too many questions in the same item, so we limit the number of questions per item to 30.

30

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConverter

Returns a new instance of Converter.



75
76
77
78
79
80
81
82
83
# File 'lib/canvas_qti_to_learnosity_converter/convert.rb', line 75

def initialize
  @items = []
  @widgets = []
  @item_banks = []
  @assessments = []
  @assets = {}
  @errors = {}
  @namespace = SecureRandom.uuid
end

Instance Attribute Details

#assessmentsObject

Returns the value of attribute assessments.



73
74
75
# File 'lib/canvas_qti_to_learnosity_converter/convert.rb', line 73

def assessments
  @assessments
end

#assetsObject

Returns the value of attribute assets.



73
74
75
# File 'lib/canvas_qti_to_learnosity_converter/convert.rb', line 73

def assets
  @assets
end

#errorsObject

Returns the value of attribute errors.



73
74
75
# File 'lib/canvas_qti_to_learnosity_converter/convert.rb', line 73

def errors
  @errors
end

#item_banksObject

Returns the value of attribute item_banks.



73
74
75
# File 'lib/canvas_qti_to_learnosity_converter/convert.rb', line 73

def item_banks
  @item_banks
end

#itemsObject

Returns the value of attribute items.



73
74
75
# File 'lib/canvas_qti_to_learnosity_converter/convert.rb', line 73

def items
  @items
end

#widgetsObject

Returns the value of attribute widgets.



73
74
75
# File 'lib/canvas_qti_to_learnosity_converter/convert.rb', line 73

def widgets
  @widgets
end

Instance Method Details

#build_item_definition(item, learnosity_type, quiz_item, path, child_items) ⇒ Object

Canvas new quizzes can have a stimulus with associated questions. These have a material orientation attribute that specifies the orientation of the stimulus. We create a single Learnosity item with multiple widgets for these items.



209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
# File 'lib/canvas_qti_to_learnosity_converter/convert.rb', line 209

def build_item_definition(item, learnosity_type, quiz_item, path, child_items)
  ident = item.attribute("ident")&.value

  item_widgets = [{
    type: learnosity_type,
    data: quiz_item.convert(@assets, path),
    reference: build_reference("#{ident}_widget"),
    metadata: { original_item_ref: ident },
  }]

  definition = {}

  if item.css("presentation > material[orientation]").present?
    child_widgets = child_items.map do |child_item|
      convert_child_item(child_item:, path:, parent_ident: ident)
    end.flatten

    child_widgets = limit_child_questions(child_widgets, ident)

    item_widgets += child_widgets
  end

  if item.css("presentation > material[orientation='left']").present?
    definition[:regions] = [
      {
        widgets: [{ reference: item_widgets.first[:reference] }],
        width: 50,
        type: "column"
      },
      {
        widgets: child_widgets.map{ |w| { reference: w[:reference] } },
        width: 50,
        type: "column"
      }
    ]
    definition[:scroll] = { enabled: false }
    definition[:type] = "root"
  else
    definition[:widgets] = item_widgets.map{ |w| { reference: w[:reference] } }
  end

  [item_widgets, definition]
end

#build_quiz_from_file(path) ⇒ Object



114
115
116
117
118
119
120
# File 'lib/canvas_qti_to_learnosity_converter/convert.rb', line 114

def build_quiz_from_file(path)
  qti_file = File.new path
  qti_string = qti_file.read
  CanvasQtiQuiz.new(qti_string: qti_string)
ensure
  qti_file.close
end

#build_quiz_from_qti_string(qti_string) ⇒ Object



110
111
112
# File 'lib/canvas_qti_to_learnosity_converter/convert.rb', line 110

def build_quiz_from_qti_string(qti_string)
  CanvasQtiQuiz.new(qti_string: qti_string)
end

#build_reference(ident = nil) ⇒ Object



370
371
372
373
374
375
376
# File 'lib/canvas_qti_to_learnosity_converter/convert.rb', line 370

def build_reference(ident = nil)
  if ident.present?
    Digest::UUID.uuid_v5(@namespace, ident)
  else
    SecureRandom.uuid
  end
end

#clean_title(title) ⇒ Object



321
322
323
# File 'lib/canvas_qti_to_learnosity_converter/convert.rb', line 321

def clean_title(title)
  title&.gsub(/["']/, "")
end

#clone_bank_item(parent_item, child_items, path) ⇒ Object

We need to create a new item for stimuluses that are from a question bank, as the item in the assessment will need to have multiple widgets in it, and the item from the bank only has the stimulus. We don’t want to modify the original item in the bank as it could be used in multiple assessments and it’s convenient to have the original to clone. We also create new widgets, so that the item can be modified without affecting any other items that use the same widgets.



259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
# File 'lib/canvas_qti_to_learnosity_converter/convert.rb', line 259

def clone_bank_item(parent_item, child_items, path)
  item_ref = parent_item.attribute("item_ref").value
  bank_item = @items.find { |i| i[:metadata][:original_item_ref] == item_ref }
  new_item = bank_item.deep_dup

  if new_item[:definition][:regions].blank?
    raise "Trying to add a child item to a stimulus from a question bank that wasn't converted with regions"
  end

  # We need new references throughout because we can't consistently generate
  # references when this stimulus/questions could be used in multiple assessments with
  # different questions for the same bank stimulus
  new_item[:reference] = build_reference

  # Use the bank item reference in the metadata so we don't accidentally
  # find this cloned one if the original is used elsewhere
  new_item[:metadata][:original_item_ref] = bank_item[:reference]

  cloned_stimulus = @widgets.find { |w| w[:metadata][:original_item_ref] == item_ref }.deep_dup
  cloned_stimulus[:reference] = build_reference
  @widgets.push(cloned_stimulus)

  stimulus_region = new_item[:definition][:regions].find { |r| r[:widgets].present? }
  stimulus_region[:widgets] = [{ reference: cloned_stimulus[:reference] }]

  empty_region = new_item[:definition][:regions].find { |r| r[:widgets].blank? }
  child_widgets = child_items.map do |child_item|
    convert_child_item(child_item:, path:, parent_ident: item_ref, new_references: true)
  end.flatten

  child_widgets = limit_child_questions(child_widgets, item_ref)

  empty_region[:widgets] = child_widgets.map{ |w| { reference: w[:reference] } }
  new_item[:questions] = child_widgets.select{ |w| w[:type] == "question" }.map{ |w| w[:reference] }
  new_item[:features] = [{ reference: cloned_stimulus[:reference] }]

  @widgets += child_widgets
  @items.push(new_item)
  new_item
end

#clone_bank_widget(parent_ident, child_item) ⇒ Object



153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/canvas_qti_to_learnosity_converter/convert.rb', line 153

def clone_bank_widget(parent_ident, child_item)
  # If the item is from a question bank we want to create a new copy of the widget
  # as Learnosity doesn't really share widgets between items
  child_item_ref = child_item.attribute("item_ref")&.value
  converted_widget = @widgets.find { |w| w[:metadata][:original_item_ref] == child_item_ref }

  if converted_widget.blank?
    raise "Could not find converted widget for item_ref #{child_item_ref}"
  end

  new_widget = converted_widget.deep_dup
  new_widget[:reference] = build_reference("#{parent_ident}_#{child_item_ref}")
  new_widget[:metadata][:original_item_ref] = "#{parent_ident}_#{child_item_ref}"
  new_widget
end

#convert_assessment(qti, path) ⇒ Object



325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
# File 'lib/canvas_qti_to_learnosity_converter/convert.rb', line 325

def convert_assessment(qti, path)
  quiz = CanvasQtiQuiz.new(qti_string: qti)
  assessment = quiz.at_css("assessment")
  return nil unless assessment

  ident = assessment.attribute("ident")&.value
  reference = build_reference(ident)
  title = clean_title(assessment.attribute("title").value)

  item_refs = convert_items(quiz, path)
  @assessments <<
    {
      reference:,
      title:,
      data: {
        items: item_refs.uniq.map { |ref| { reference: ref } },
        config: { title: },
      },
      status: "published",
      tags: {},
    }
end

#convert_child_item(child_item:, path:, parent_ident:, new_references: false) ⇒ Object



169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
# File 'lib/canvas_qti_to_learnosity_converter/convert.rb', line 169

def convert_child_item(child_item:, path:, parent_ident:, new_references: false)
  if child_item.name == "section"
    item_refs = child_item.css("sourcebank_ref").map do |sourcebank_ref|
      @items.select do |i|
        (
          i.dig(:metadata, :original_item_bank_ref) == sourcebank_ref.text &&
          !(i.dig(:definition, :regions)&.any? { |r| r[:widgets].blank? }) # Exclude empty stimulus items
        )
      end.map { |i| i[:metadata][:original_item_ref] }
    end.flatten

    bank_widgets = item_refs.map { |ref| @widgets.find { |w| w[:metadata][:original_item_ref] == ref } }

    bank_widgets.map do |widget|
      new_widget = widget.deep_dup
      new_widget[:reference] = build_reference
      new_widget
    end
  elsif child_item.name == "bankentry_item"
    new_widget = clone_bank_widget(parent_ident, child_item)
    if new_references
      new_widget[:reference] = build_reference
    end
    new_widget
  else
    child_ident = child_item.attribute("ident")&.value
    child_learnosity_type, child_quiz_item = convert_item(qti_string: child_item.to_html)

    {
      type: child_learnosity_type,
      data: child_quiz_item.convert(@assets, path),
      reference: build_reference("#{parent_ident}_#{child_ident}"),
      metadata: { original_item_ref: child_ident },
    }
  end
end

#convert_imscc_export(path) ⇒ Object



499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
# File 'lib/canvas_qti_to_learnosity_converter/convert.rb', line 499

def convert_imscc_export(path)
  Zip::File.open(path) do |zip_file|
    entry = zip_file.find_entry("imsmanifest.xml")
    manifest = entry.get_input_stream.read
    parsed_manifest = Nokogiri.XML(manifest, &:noblanks)

    item_bank_paths = imscc_item_bank_paths(parsed_manifest)
    item_bank_paths.each do |item_bank_path|
      qti = zip_file.find_entry(item_bank_path).get_input_stream.read
      convert_item_bank(qti, File.dirname(item_bank_path))
    end

    assessment_paths = imscc_quiz_paths(parsed_manifest)
    assessment_paths.each do |qti_path|
      qti = zip_file.find_entry(qti_path).get_input_stream.read
      convert_assessment(qti, File.dirname(qti_path))
    end

    {
      errors: @errors,
    }
  end
end

#convert_item(qti_string:) ⇒ Object



300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
# File 'lib/canvas_qti_to_learnosity_converter/convert.rb', line 300

def convert_item(qti_string:)
  xml = Nokogiri.XML(qti_string, &:noblanks)
  type = extract_type(xml)

  if FEATURE_TYPES.include?(type)
    learnosity_type = "feature"
  else
    learnosity_type = "question"
  end

  question_class = TYPE_MAP[type]

  if question_class
    question = question_class.for(xml)
  else
    raise CanvasQuestionTypeNotSupportedError.new(type)
  end

  [learnosity_type, question]
end

#convert_item_bank(qti_string, path) ⇒ Object



348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
# File 'lib/canvas_qti_to_learnosity_converter/convert.rb', line 348

def convert_item_bank(qti_string, path)
  qti = CanvasQtiQuiz.new(qti_string:)
  item_bank = qti.at_css("objectbank")
  return nil unless item_bank

  ident = item_bank.attribute("ident")&.value
  title = clean_title(qti.css(%{ objectbank > qtimetadata >
    qtimetadatafield > fieldlabel:contains("bank_title")})
    &.first&.next&.text || '')

  meta = {
    original_item_bank_ref: ident,
  }
  item_refs = convert_items(qti, path, meta:, tags: { "Item Bank" => [title] })
  @item_banks <<
    {
      title: title,
      ident: ident,
      item_refs: item_refs,
    }
end

#convert_items(qti, path, meta: {}, tags: {}) ⇒ Object



378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
# File 'lib/canvas_qti_to_learnosity_converter/convert.rb', line 378

def convert_items(qti, path, meta: {}, tags: {})
  converted_item_refs = []

  items_by_parent_stimulus = {}
  qti.css("item,bankentry_item,section").each do |item|
    ident = item.attribute("ident")&.value
    next if ident == "root_section"

    parent_ident = parent_stimulus_ident(item)
    if parent_ident.present?
      items_by_parent_stimulus[parent_ident] ||= []
      items_by_parent_stimulus[parent_ident].push(item)
    end
  end

  qti.css("item,bankentry_item,section").each.with_index do |item, index|
    begin
      # Skip items that have a parent, as we'll convert them when we convert the parent
      next if parent_stimulus_ident(item).present?

      ident = item.attribute("ident")&.value
      item_ref = item.attribute("item_ref")&.value

      if item.name == "section"
        next if ident == "root_section"

        item.css("sourcebank_ref").each do |sourcebank_ref|
          item_refs = @items.select { |i| i.dig(:metadata, :original_item_bank_ref) == sourcebank_ref.text }.map { |i| i[:reference] }
          converted_item_refs += item_refs
        end
      elsif item.name == "bankentry_item" && item_ref.present? && items_by_parent_stimulus[item_ref].present?
        new_item = clone_bank_item(item, items_by_parent_stimulus[item_ref], path)
        converted_item_refs.push(new_item[:reference])
      elsif item.name == "bankentry_item" && item_ref.present?
        converted_item_refs.push(build_reference(item_ref))
      elsif item.name == "item"
        reference = build_reference(ident)
        item_title = item.attribute("title")&.value || ''
        learnosity_type, quiz_item = convert_item(qti_string: item.to_html)

        item_widgets, definition = build_item_definition(
          item,
          learnosity_type,
          quiz_item,
          path,
          items_by_parent_stimulus.fetch(ident, [])
        )

        @widgets += item_widgets

        @items << {
          title: item_title,
          reference:,
          metadata: meta.merge({ original_item_ref: ident }),
          definition:,
          questions: item_widgets.select{ |w| w[:type] == "question" }.map{ |w| w[:reference] },
          features: item_widgets.select{ |w| w[:type] == "feature" }.map{ |w| w[:reference] },
          status: "published",
          tags: tags,
          type: learnosity_type,
          dynamic_content_data: quiz_item.dynamic_content_data()
        }

        converted_item_refs.push(reference)
      end

    rescue CanvasQuestionTypeNotSupportedError => e
      @errors[ident] ||= []
      @errors[ident].push({
        index: index,
        error_type: "unsupported_question",
        question_type: e.question_type.to_s,
        message: e.message,
      })
    rescue StandardError => e
      @errors[ident || item_ref] ||= []
      @errors[ident || item_ref].push({
        index: index,
        error_type: e.class.name.split("::").last,
        message: e.message,
      })
    end
  end
  converted_item_refs
end

#convert_qti_file(path) ⇒ Object



464
465
466
467
468
469
470
471
# File 'lib/canvas_qti_to_learnosity_converter/convert.rb', line 464

def convert_qti_file(path)
  file = File.new(path)
  qti_string = file.read
  convert(qti_string)
ensure
  file.close
  file.unlink
end

#extract_type(xml) ⇒ Object



122
123
124
125
126
127
128
129
# File 'lib/canvas_qti_to_learnosity_converter/convert.rb', line 122

def extract_type(xml)
  xml.css(%{ item > itemmetadata > qtimetadata >
    qtimetadatafield > fieldlabel:contains("question_type")})
    &.first&.next&.text&.to_sym ||
    xml.css(%{ item > itemmetadata > qtimetadata >
      qtimetadatafield > fieldlabel:contains("cc_profile")})
      &.first&.next&.text&.to_sym
end

#generate_learnosity_export(input_path, output_path) ⇒ Object



523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
# File 'lib/canvas_qti_to_learnosity_converter/convert.rb', line 523

def generate_learnosity_export(input_path, output_path)
  result = convert_imscc_export(input_path)

  export_writer = ExportWriter.new(output_path)
  export_writer.write_to_zip("export.json", { version: 2.0 })

  @assessments.each do |activity|
    export_writer.write_to_zip("activities/#{activity[:reference]}.json", activity)
  end
  @items.each do |item|
    export_writer.write_to_zip("items/#{item[:reference]}.json", item)
  end
  @widgets.each do |widget|
    export_writer.write_to_zip("#{widget[:type]}s/#{widget[:reference]}.json", widget)
  end

  Zip::File.open(input_path) do |input|
    @assets.each do |source, destination|
      source = source.gsub(/^\//, '')
      asset = input.find_entry(source) || input.find_entry("web_resources/#{source}")
      if asset
        export_writer.write_asset_to_zip("assets/#{destination}", input.read(asset))
      end
    end
  end
  export_writer.close

  result
end

#imscc_item_bank_paths(parsed_manifest) ⇒ Object



480
481
482
483
484
485
# File 'lib/canvas_qti_to_learnosity_converter/convert.rb', line 480

def imscc_item_bank_paths(parsed_manifest)
  resources = parsed_manifest.css("resources > resource[type='associatedcontent/imscc_xmlv1p1/learning-application-resource']")
  resources.map do |entry|
    resource_path(parsed_manifest, entry)
  end
end

#imscc_quiz_paths(parsed_manifest) ⇒ Object



473
474
475
476
477
478
# File 'lib/canvas_qti_to_learnosity_converter/convert.rb', line 473

def imscc_quiz_paths(parsed_manifest)
  resources = parsed_manifest.css("resources > resource[type^='imsqti_xmlv1p2']")
  resources.map do |entry|
    resource_path(parsed_manifest, entry)
  end
end

#limit_child_questions(child_widgets, parent_ident) ⇒ Object



139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/canvas_qti_to_learnosity_converter/convert.rb', line 139

def limit_child_questions(child_widgets, parent_ident)
  if child_widgets.count > MAX_QUESTIONS_PER_ITEM
    # We only want to include the first MAX_QUESTIONS_PER_ITEM questions
    child_widgets = child_widgets.first(MAX_QUESTIONS_PER_ITEM)
    @errors[parent_ident] ||= []
    @errors[parent_ident].push({
      error_type: "too_many_questions",
      message: "Too many questions for item, only the first #{MAX_QUESTIONS_PER_ITEM} will be included",
    })
  end

  child_widgets
end

#parent_stimulus_ident(item) ⇒ Object



131
132
133
134
135
136
137
# File 'lib/canvas_qti_to_learnosity_converter/convert.rb', line 131

def parent_stimulus_ident(item)
  if item.name == "bankentry_item" || item.name == "section"
    item.attribute("parent_stimulus_item_ident")&.value
  else
    item.css("itemmetadata > qtimetadata > qtimetadatafield > fieldlabel:contains('parent_stimulus_item_ident')").first&.next&.text
  end
end

#resource_path(parsed_manifest, entry) ⇒ Object



487
488
489
490
491
492
493
494
495
496
497
# File 'lib/canvas_qti_to_learnosity_converter/convert.rb', line 487

def resource_path(parsed_manifest, entry)
  # Use the Canvas non_cc_assignment qti path when possible.  This works for both classic and new quizzes
  entry.css("dependency").each do |dependency|
    ref = dependency.attribute("identifierref").value
    parsed_manifest.css(%{resources > resource[identifier="#{ref}"] > file}).each do |file|
      path = file.attribute("href").value
      return path if path.match?(/^non_cc_assessments/)
    end
  end
  entry.css("file").first&.attribute("href")&.value
end