Class: Moodle2CC::Canvas::Question

Inherits:
Moodle2CC::CC::Question show all
Defined in:
lib/moodle2cc/canvas/question.rb

Constant Summary collapse

META_ATTRIBUTES =
[:question_type, :points_possible, :assessment_question_identifierref]
QUESTION_TYPE_MAP =
{
  'calculated' =>  'calculated_question',
  'description' => 'text_only_question',
  'essay' => 'essay_question',
  'match' => 'matching_question',
  'multianswer' => 'embedded_answers_question',
  'multichoice' => 'multiple_choice_question',
  'shortanswer' => 'short_answer_question',
  'numerical' => 'numerical_question',
  'truefalse' => 'true_false_question',
  'choice' => 'multiple_choice_question'
}
QUESTION_TYPE_ID_MAP =
{
  1 =>  'true_false_question', # yes/no question
  2 =>  'essay_question', # text box question
  3 =>  'essay_question', # essay box question
  4 =>  'multiple_choice_question', # radio buttons question
  5 =>  'multiple_answers_question', # check boxes question
  6 =>  'multiple_choice_question', # dropdown box question
  8 =>  'multiple_dropdowns_question', # rate 1..5 question
  9 =>  'essay_question', # date question
  10 =>  'numerical_question', # numeric question
  100 =>  'text_only_question', # label question
}

Constants included from Moodle2CC::CC::CCHelper

Moodle2CC::CC::CCHelper::ASSESSMENT_CC_QTI, Moodle2CC::CC::CCHelper::ASSESSMENT_META, Moodle2CC::CC::CCHelper::ASSESSMENT_NON_CC_FOLDER, Moodle2CC::CC::CCHelper::ASSESSMENT_TYPE, Moodle2CC::CC::CCHelper::ASSIGNMENT_GROUPS, Moodle2CC::CC::CCHelper::ASSIGNMENT_SETTINGS, Moodle2CC::CC::CCHelper::BASIC_LTI, Moodle2CC::CC::CCHelper::BLTI_NAMESPACE, Moodle2CC::CC::CCHelper::CANVAS_NAMESPACE, Moodle2CC::CC::CCHelper::CANVAS_PLATFORM, Moodle2CC::CC::CCHelper::CC_ASSIGNMENT_FOLDER, Moodle2CC::CC::CCHelper::CC_EXTENSION, Moodle2CC::CC::CCHelper::CC_WIKI_FOLDER, Moodle2CC::CC::CCHelper::COURSE_SETTINGS, Moodle2CC::CC::CCHelper::COURSE_SETTINGS_DIR, Moodle2CC::CC::CCHelper::COURSE_TOKEN, Moodle2CC::CC::CCHelper::DISCUSSION_TOPIC, Moodle2CC::CC::CCHelper::EVENTS, Moodle2CC::CC::CCHelper::EXTERNAL_FEEDS, Moodle2CC::CC::CCHelper::EXTERNAL_TOOLS, Moodle2CC::CC::CCHelper::FILES_META, Moodle2CC::CC::CCHelper::GRADING_STANDARDS, Moodle2CC::CC::CCHelper::IMS_DATE, Moodle2CC::CC::CCHelper::IMS_DATETIME, Moodle2CC::CC::CCHelper::LEARNING_OUTCOMES, Moodle2CC::CC::CCHelper::LOR, Moodle2CC::CC::CCHelper::MANIFEST, Moodle2CC::CC::CCHelper::MEDIA_OBJECTS_FOLDER, Moodle2CC::CC::CCHelper::MODULE_META, Moodle2CC::CC::CCHelper::MOODLE_FILEBASE_TOKEN, Moodle2CC::CC::CCHelper::MOODLE_SLASH_TOKEN, Moodle2CC::CC::CCHelper::OBJECT_TOKEN, Moodle2CC::CC::CCHelper::QTI_ASSESSMENT_TYPE, Moodle2CC::CC::CCHelper::QTI_EXTENSION, Moodle2CC::CC::CCHelper::QUESTION_BANK, Moodle2CC::CC::CCHelper::RUBRICS, Moodle2CC::CC::CCHelper::SYLLABUS, Moodle2CC::CC::CCHelper::WEBCONTENT, Moodle2CC::CC::CCHelper::WEB_CONTENT_TOKEN, Moodle2CC::CC::CCHelper::WEB_LINK, Moodle2CC::CC::CCHelper::WEB_RESOURCES_FOLDER, Moodle2CC::CC::CCHelper::WIKI_FOLDER, Moodle2CC::CC::CCHelper::WIKI_TOKEN, Moodle2CC::CC::CCHelper::XSD_URI

Instance Attribute Summary

Attributes inherited from Moodle2CC::CC::Question

#id, #identifier, #title

Instance Method Summary collapse

Methods included from Moodle2CC::CC::CCHelper

#convert_file_path_tokens, convert_file_path_tokens, #create_key, create_key, create_mod_key, #create_mod_key, #create_resource_key, create_resource_key, file_query_string, #file_slug, file_slug, #get_html_title_and_body, #get_html_title_and_body_and_id, #get_html_title_and_body_and_meta_fields, ims_date, #ims_date, ims_datetime, #ims_datetime, media_object_info

Constructor Details

#initialize(question, assessment = nil) ⇒ Question

Returns a new instance of Question.



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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/moodle2cc/canvas/question.rb', line 33

def initialize(question, assessment=nil)
  super
  @question_type = question.type ? QUESTION_TYPE_MAP[question.type] : QUESTION_TYPE_ID_MAP[question.type_id]
  @points_possible = question.grade
  @general_feedback = question.general_feedback
  @length = question.length

  @answers = []
  @matches = []

  unless question.answers.empty?
    @answers = question.answers.map do |answer|
      Moodle2CC::OpenStruct.new(
        :id => answer.id,
        :text => answer.text,
        :fraction => answer.fraction,
        :feedback => answer.feedback
      )
    end
  end

  unless question.choices.nil? || question.choices.empty?
    @answers = question.choices.map do |answer|
      Moodle2CC::OpenStruct.new(
        :id => answer.id,
        :text => answer.content
      )
    end
  end

  calculation = question.calculations.first unless question.calculations.nil?
  if calculation
    @answer_tolerance = calculation.tolerance
    @formula_decimal_places = calculation.correct_answer_format == 1 ? calculation.correct_answer_length : 0
    @formulas = question.answers.map { |a| a.text.gsub(/[\{\}\s]/, '') }
    @vars = calculation.dataset_definitions.map do |ds_def|
      name = ds_def.name
      type, min, max, scale = ds_def.options.split(':')
      {:name => name, :min => min, :max => max, :scale => scale}
    end
    @var_sets = []
    calculation.dataset_definitions.each do |ds_def|
      ds_def.dataset_items.sort_by(&:number).each_with_index do |ds_item, index|
        var_set = @var_sets[index] || {}
        vars = var_set[:vars] || {}
        vars[ds_def.name] = ds_item.value
        var_set[:vars] = vars
        @var_sets[index] = var_set
      end
    end
    @var_sets.map do |var_set|
      answer = 0
      var_set[:vars].each do |k, v|
        answer += v
      end
      var_set[:answer] = answer
    end
  end

  unless question.numericals.nil?
    @numericals = question.numericals.map do |n|
      Moodle2CC::OpenStruct.new(
        :answer => @answers.find { |a| a.id == n.answer_id },
        :tolerance => n.tolerance
      )
    end
  end

  if !question.matches.nil? && question.matches.length > 0
    answers = question.matches.inject({}) do |result, match|
      result[match.code] = match.answer_text
      result
    end
    @matches = question.matches.select do |match|
      match.question_text && match.question_text.strip.length > 0
    end.map do |match|
      Moodle2CC::OpenStruct.new(
        :question => match.question_text,
        :answers => answers,
        :answer => match.code
      )
    end
  end

  material = question.text
  material = question.content || '' if material.nil?
  material = material.gsub(/\{(.*?)\}/, '[\1]')
  material = material + image_html(question.image) unless question.image.nil? || question.image.strip == ''
  material = RDiscount.new(material).to_html if question.format == 4 # markdown
  material = convert_file_path_tokens(material)

  if @answers
    @answers.each do |answer|
      answer.text = convert_file_path_tokens(answer.text) if answer.text
    end
  end
  @material = material

  if @question_type == 'multiple_dropdowns_question' && !@answers.empty? && @length
    choices = @answers[0..(@length-1)]
    if @length >= @answers.length || !choices.all?{|a| !!a.text.match(/^\d/)}
      # the choices don't seem to be present, so construct them
      choices = (1..@length).map do |i|
        Moodle2CC::OpenStruct.new(
          :id => "#{i}",
          :text => "#{i}"
        )
      end
      @responses = @answers
    else
      @responses = @answers[@length..-1]
    end
    @answers = choices
    @responses.each_with_index do |response, index|
      @material << "\n#{response.text} [response#{index + 1}]"
    end
  end

  if question.instance_id
    @assessment_question_identifierref = create_key(question.id, "#{@identifier_prefix}question_")
  end
end

Instance Method Details

#create_additional_nodes(item_node) ⇒ Object



449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
# File 'lib/moodle2cc/canvas/question.rb', line 449

def create_additional_nodes(item_node)
  case @question_type
  when 'calculated_question'
    item_node.itemproc_extension do |extension_node|
      extension_node.calculated do |calculated_node|
        calculated_node.answer_tolerance @answer_tolerance
        calculated_node.formulas(:decimal_places => @formula_decimal_places) do |formulas_node|
          @formulas.each do |formula|
            formulas_node.formula formula
          end
        end
        calculated_node.vars do |vars_node|
          @vars.each do |var|
            vars_node.var(:name => var[:name], :scale => var[:scale]) do |var_node|
              var_node.min var[:min]
              var_node.max var[:max]
            end
          end
        end
        calculated_node.var_sets do |var_sets_node|
          @var_sets.each do |var_set|
            ident = var_set[:vars].sort.map { |k,v| v.to_s.split('.').join }.flatten.join
            var_sets_node.var_set(:ident => ident) do |var_set_node|
              var_set[:vars].each do |k, v|
                var_set_node.var(v, :name => k)
              end
              var_set_node.answer var_set[:answer]
            end
          end
        end
      end
    end
  end
end

#create_feedback_nodes(item_node) ⇒ Object



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
# File 'lib/moodle2cc/canvas/question.rb', line 422

def create_feedback_nodes(item_node)
  # Feeback
  if @general_feedback
    item_node.itemfeedback(:ident => 'general_fb') do |fb_node|
      fb_node.flow_mat do |flow_node|
        flow_node.material do |material_node|
          material_node.mattext(@general_feedback, :texttype => 'text/html')
        end
      end
    end
  end

  case @question_type
  when 'multiple_choice_question', 'numerical_question', 'short_answer_question', 'true_false_question'
    @answers.each do |answer|
      next unless answer.feedback && answer.feedback.strip.length > 0
      item_node.itemfeedback(:ident => "#{answer.id}_fb") do |feedback_node|
        feedback_node.flow_mat do |flow_node|
          flow_node.material do |material_node|
            material_node.mattext answer.feedback, :texttype => 'text/html'
          end
        end
      end
    end
  end
end

#create_item_xml(section_node) ⇒ Object



156
157
158
159
160
161
162
163
164
165
166
167
168
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
# File 'lib/moodle2cc/canvas/question.rb', line 156

def create_item_xml(section_node)
  section_node.item(:title => @title, :ident => @identifier) do |item_node|
    item_node. do |meta_node|
      meta_node. do |qtime_node|
        META_ATTRIBUTES.each do |attr|
          value = send(attr).to_s
          if value && value.length > 0
            qtime_node.qtimetadatafield do |field_node|
              field_node.fieldlabel attr.to_s
              field_node.fieldentry value
            end
          end
        end
      end
    end

    item_node.presentation do |presentation_node|
      presentation_node.material do |material_node|
        material_node.mattext(@material, :texttype => 'text/html')
      end
      create_responses(presentation_node)
    end

    item_node.resprocessing do |processing_node|
      processing_node.outcomes do |outcomes_node|
        outcomes_node.decvar(:maxvalue => '100', :minvalue => '0', :varname => 'SCORE', :vartype => 'Decimal')
      end
      if @general_feedback
        processing_node.respcondition(:continue => 'Yes') do |condition_node|
          condition_node.conditionvar do |var_node|
            var_node.other
          end
          condition_node.displayfeedback(:feedbacktype => 'Response', :linkrefid => 'general_fb')
        end
      end
      create_response_conditions(processing_node)
    end

    create_feedback_nodes(item_node)
    create_additional_nodes(item_node)
  end
end

#create_response_conditions(processing_node) ⇒ Object



293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
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
# File 'lib/moodle2cc/canvas/question.rb', line 293

def create_response_conditions(processing_node)
  case @question_type
  when 'calculated_question'
    processing_node.respcondition(:title => 'correct') do |condition|
      condition.conditionvar do |var_node|
        var_node.other
      end
      condition.setvar('100', :varname => 'SCORE', :action => 'Set')
    end
    processing_node.respcondition(:title => 'incorrect') do |condition|
      condition.conditionvar do |var_node|
        var_node.other
      end
      condition.setvar('0', :varname => 'SCORE', :action => 'Set')
    end
  when 'essay_question', 'embedded_answers_question'
    processing_node.respcondition(:continue => 'No') do |condition|
      condition.conditionvar do |var_node|
        var_node.other
      end
    end
  when 'matching_question'
    score = 100.0 / @matches.length.to_f
    @matches.each do |match|
      processing_node.respcondition do |condition_node|
        condition_node.conditionvar do |var_node|
          var_node.varequal match.answer, :respident => "response_#{match.answer}"
        end
        condition_node.setvar "%.2f" % score, :varname => 'SCORE', :action => 'Add'
      end
    end
  when 'multiple_choice_question'
    # Feeback
    @answers.each do |answer|
      next unless answer.feedback && answer.feedback.strip.length > 0
      processing_node.respcondition(:continue => 'Yes') do |condition_node|
        condition_node.conditionvar do |var_node|
          var_node.varequal answer.id, :respident => 'response1'
        end
        condition_node.displayfeedback(:feedbacktype => 'Response', :linkrefid => "#{answer.id}_fb")
      end
    end

    # Scores
    @answers.each do |answer|
      processing_node.respcondition(:continue => 'No') do |condition_node|
        condition_node.conditionvar do |var_node|
          var_node.varequal answer.id, :respident => 'response1'
        end
        condition_node.setvar(get_score(answer.fraction), :varname => 'SCORE', :action => 'Set')
      end
    end
  when 'multiple_answers_question'
    processing_node.respcondition(:continue => 'No') do |condition_node|
      first = @answers.first
      rest = @answers[1..-1]
      condition_node.conditionvar do |var_node|
        var_node.and do |and_node|
          and_node.varequal first.id, :respident => 'response1'
          rest.each do |answer|
            and_node.not do |not_node|
              not_node.varequal answer.id, :respident => 'response1'
            end
          end
        end
      end
      condition_node.setvar('100', :varname => 'SCORE', :action => 'Set')
    end
  when 'multiple_dropdowns_question'
    score = 100.0 / @responses.length.to_f
    @responses.each_with_index do |response, index|
      response_id = index + 1
      processing_node.respcondition do |condition_node|
        condition_node.conditionvar do |var_node|
          var_node.varequal "#{response_id}#{@answers.first.id}", :respident => "response_response#{response_id}"
        end
        condition_node.setvar "%.2f" % score, :varname => 'SCORE', :action => 'Add'
      end
    end
  when 'numerical_question'
    @numericals.each do |numerical|
      processing_node.respcondition(:continue => 'No') do |condition_node|
        condition_node.conditionvar do |var_node|
          var_node.or do |or_node|
            or_node.varequal numerical.answer.text, :respident => 'response1'
            or_node.and do |and_node|
              and_node.vargte numerical.answer.text.to_f - numerical.tolerance.to_f, :respident => 'response1'
              and_node.varlte numerical.answer.text.to_f + numerical.tolerance.to_f, :respident => 'response1'
            end
          end
        end
        condition_node.setvar(get_score(numerical.answer.fraction), :varname => "SCORE", :action => 'Set')
        condition_node.displayfeedback(:feedbacktype => 'Response', :linkrefid => "#{numerical.answer.id}_fb") if numerical.answer.feedback && numerical.answer.feedback.strip.length > 0
      end
    end
  when 'short_answer_question'
    # Feedback
    @answers.each do |answer|
      next unless answer.feedback && answer.feedback.strip.length > 0
      processing_node.respcondition(:continue => 'Yes') do |condition_node|
        condition_node.conditionvar do |var_node|
          var_node.varequal answer.text, :respident => 'response1'
        end
        condition_node.displayfeedback(:feedbacktype => 'Response', :linkrefid => "#{answer.id}_fb")
      end
    end

    # Conditions
    @answers.each do |answer|
      processing_node.respcondition(:continue => 'No') do |condition_node|
        condition_node.conditionvar do |var_node|
          var_node.varequal answer.text, :respident => 'response1'
        end
        condition_node.setvar(get_score(answer.fraction), :varname => 'SCORE', :action => "Set")
      end
    end
  when 'true_false_question'
    @answers.each do |answer|
      processing_node.respcondition(:continue => 'No') do |condition_node|
        condition_node.conditionvar do |var_node|
          var_node.varequal answer.id, :respident => 'response1'
        end
        condition_node.displayfeedback(:feedbacktype => 'Response', :linkrefid => "#{answer.id}_fb") if answer.feedback && answer.feedback.strip.length > 0
        condition_node.setvar(get_score(answer.fraction), :varname => 'SCORE', :action => "Set")
      end
    end
  end
end

#create_responses(presentation_node) ⇒ Object



199
200
201
202
203
204
205
206
207
208
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
252
253
254
255
256
257
258
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
# File 'lib/moodle2cc/canvas/question.rb', line 199

def create_responses(presentation_node)
  case @question_type
  when 'calculated_question'
    presentation_node.response_str(:rcardinality => 'Single', :ident => 'response1') do |response_node|
      response_node.render_fib(:fibtype => 'Decimal') do |render_node|
        render_node.response_label(:ident => 'answer1')
      end
    end
  when 'essay_question', 'short_answer_question', 'embedded_answers_question'
    presentation_node.response_str(:rcardinality => 'Single', :ident => 'response1') do |response_node|
      response_node.render_fib do |render_node|
        render_node.response_label(:ident => 'answer1', :rshuffle => 'No')
      end
    end
  when 'matching_question'
    @matches.each do |match|
      presentation_node.response_lid(:ident => "response_#{match.answer}") do |response_node|
        response_node.material do |material_node|
          material_node.mattext(match.question, :texttype => 'text/html')
        end
        response_node.render_choice do |choice_node|
          match.answers.each do |ident, text|
            choice_node.response_label(:ident => ident) do |label_node|
              label_node.material do |material_node|
                material_node.mattext text
              end
            end
          end
        end
      end
    end
  when 'multiple_choice_question'
    presentation_node.response_lid(:ident => 'response1', :rcardinality => 'Single') do |response_node|
      response_node.render_choice do |choice_node|
        @answers.each do |answer|
          choice_node.response_label(:ident => answer.id) do |label_node|
            label_node.material do |material_node|
              material_node.mattext answer.text, :texttype => 'text/html'
            end
          end
        end
      end
    end
  when 'multiple_answers_question'
    presentation_node.response_lid(:ident => 'response1', :rcardinality => 'Multiple') do |response_node|
      response_node.render_choice do |choice_node|
        @answers.each do |answer|
          choice_node.response_label(:ident => answer.id) do |label_node|
            label_node.material do |material_node|
              material_node.mattext answer.text, :texttype => 'text/html'
            end
          end
        end
      end
    end
  when 'multiple_dropdowns_question'
    @responses.each_with_index do |response, index|
      response_id = index + 1
      presentation_node.response_lid(:ident => "response_response#{response_id}") do |response_node|
        response_node.material do |material_node|
          material_node.mattext "response#{response_id}"
        end
        response_node.render_choice do |choice_node|
          @answers.each do |answer|
            choice_node.response_label(:ident => "#{response_id}#{answer.id}") do |label_node|
              label_node.material do |material_node|
                material_node.mattext answer.text, :texttype => 'text/html'
              end
            end
          end
        end
      end
    end
  when 'numerical_question'
    presentation_node.response_str(:rcardinality => 'Single', :ident => 'response1') do |response_node|
      response_node.render_fib(:fibtype => 'Decimal') do |render_fib_node|
        render_fib_node.response_label(:ident => 'answer1')
      end
    end
  when 'true_false_question'
    presentation_node.response_lid(:rcardinality => 'Single', :ident => 'response1') do |response_node|
      response_node.render_choice do |render_choice_node|
        @answers.each do |answer|
          render_choice_node.response_label(:ident => answer.id) do |response_label_node|
            response_label_node.material do |material_node|
              material_node.mattext(answer.text, :texttype => 'text/html')
            end
          end
        end
      end
    end
  end
end

#get_score(fraction) ⇒ Object



484
485
486
487
# File 'lib/moodle2cc/canvas/question.rb', line 484

def get_score(fraction)
  return 100 if fraction.nil?
  (100 * fraction).to_i
end

#image_html(image) ⇒ Object



489
490
491
492
# File 'lib/moodle2cc/canvas/question.rb', line 489

def image_html(image)
  image = image.strip
  %{<p><img src="#{WEB_CONTENT_TOKEN}/#{image}" alt="#{image}"/></p>}
end