Class: Senkyoshi::Matching

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

Constant Summary collapse

TAGS_PATTERN =
Regexp.union(
  /<\/?p[^>]*>/i,             # <p> tags
  /<\/?b[^>]*>/i,             # <b> tags
  /<\/?strong[^>]*>/i,        # <strong> tags
  /<\/?em[^>]*>/i,            # <em> tags
  /<\/?span[^>]*>/i,          # <span> tags
  /<\/?font[^>]*>/i,          # <font> tags
  /<\/?i(?!mg)[^>]*>/i,       # <i> tags, no <img>
  / style=("|')[^"']*("|')/i, # inline styles
)

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

#_find_directories, #_fix_path, #_matches_directory_xid?, #_search_and_replace, #cleanup, #fix_html, get_pre_data, #matches_xid?, #strip_xid

Constructor Details

#initializeMatching

Returns a new instance of Matching.



31
32
33
34
35
36
# File 'lib/senkyoshi/models/questions/matching.rb', line 31

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

Instance Method Details

#canvas_conversion(assessment, resources) ⇒ Object



80
81
82
83
84
85
86
87
88
89
# File 'lib/senkyoshi/models/questions/matching.rb', line 80

def canvas_conversion(assessment, resources)
  @matches.each do |match|
    match[:question_text] = fix_html(match[:question_text], resources)
    match[:answer_text] = fix_html(match[:answer_text], resources)
  end

  @question.matches = @matches
  @question.distractors = @distractors
  super
end

#iterate_xml(data) ⇒ Object



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
# File 'lib/senkyoshi/models/questions/matching.rb', line 42

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 { |match| strip_select_html(match.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: Senkyoshi.create_random_hex,
        question_text: question,
        answer_text: answer,
      }
    end
  end
  @distractors = matches_array.reject { |i| answers.include?(i) }
  self
end

#set_matching_answers(resprocessing) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/senkyoshi/models/questions/matching.rb', line 91

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

#strip_select_html(text) ⇒ Object



38
39
40
# File 'lib/senkyoshi/models/questions/matching.rb', line 38

def strip_select_html(text)
  text.gsub(TAGS_PATTERN, "")
end