Class: Senkyoshi::Matching

Inherits:
Question show all
Defined in:
lib/senkyoshi/models/questions/matching.rb

Constant Summary

Constants inherited from Question

Question::ITEM_FUNCTION, Question::QUESTION_TYPE

Instance Attribute Summary

Attributes inherited from Question

#answers

Instance Method Summary collapse

Methods inherited from Question

from, #get_fraction, #iterate_item, #set_answers, #set_correct_answers, #set_feedback, #set_incorrect_answers, #set_material, #set_max_score

Methods inherited from Resource

#_search_and_replace, #cleanup, #fix_html, #matches_xid?

Constructor Details

#initializeMatching

Returns a new instance of Matching.



5
6
7
8
9
10
# File 'lib/senkyoshi/models/questions/matching.rb', line 5

def initialize
  super
  @matches = []
  @matching_answers = {}
  @distractors = []
end

Instance Method Details

#canvas_conversion(assessment, _resources = nil) ⇒ Object



43
44
45
46
47
# File 'lib/senkyoshi/models/questions/matching.rb', line 43

def canvas_conversion(assessment, _resources = nil)
  @question.matches = @matches
  @question.distractors = @distractors
  super
end

#iterate_xml(data) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/senkyoshi/models/questions/matching.rb', line 12

def iterate_xml(data)
  super
  resprocessing = data.at("resprocessing")
  @matching_answers = set_matching_answers(resprocessing)
  matches_array = []
  answers = []
  if match_block = data.at("flow[@class=RIGHT_MATCH_BLOCK]")
    matches_array = match_block.
      search("flow[@class=FORMATTED_TEXT_BLOCK]").
      map(&:text)
  end
  if response_block = data.at("flow[@class=RESPONSE_BLOCK]")
    response_block.children.each do |response|
      id = response.at("response_lid").attributes["ident"].value
      question = response.at("mat_formattedtext").text
      answer_id = @matching_answers[id]
      answer = ""
      flow_label = response.at("flow_label")
      flow_label.children.each_with_index do |label, index|
        if label.attributes["ident"].value == answer_id
          answer = matches_array[index]
        end
      end
      answers << answer
      @matches << { id: id, question_text: question, answer_text: answer }
    end
  end
  @distractors = matches_array.reject { |i| answers.include?(i) }
  self
end

#set_matching_answers(resprocessing) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/senkyoshi/models/questions/matching.rb', line 49

def set_matching_answers(resprocessing)
  matching_answers = {}
  respcondition = resprocessing.css("respcondition")
  respcondition.each do |condition|
    if condition.attributes["title"] != "incorrect"
      varequal = condition.at("varequal")
      if varequal
        id = varequal.attributes["respident"].value
        matching_answers[id] = varequal.text
      end
    end
  end
  matching_answers
end