Class: Missinglink::SurveyQuestion

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/missinglink/survey_question.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.first_or_create_by_sm_id(sm_id) ⇒ Object



28
29
30
31
32
33
34
# File 'app/models/missinglink/survey_question.rb', line 28

def self.first_or_create_by_sm_id(sm_id)
  if question = SurveyQuestion.find_by_sm_question_id(sm_id)
    return question
  else
    return SurveyQuestion.create(sm_question_id: sm_id)
  end
end

.parse(page, hash) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'app/models/missinglink/survey_question.rb', line 11

def self.parse(page, hash)
  question = SurveyQuestion.first_or_create_by_sm_id(hash['question_id'])

  question.update_attributes({ heading: hash['heading'],
                               position: hash['position'].to_i,
                               type_family: hash['type']['family'],
                               type_subtype: hash['type']['subtype'] })
  question.survey_pages = [page]
  question.save

  hash['answers'].each do |answer|
    SurveyAnswer.parse(question, answer)
  end

  return question.reload
end

Instance Method Details

#answer_strategyObject



118
119
120
# File 'app/models/missinglink/survey_question.rb', line 118

def answer_strategy
  Missinglink.answer_strategies[type_family][type_subtype]
end

#possible_responses(search_other = false) ⇒ Object

for reference, when searching, listing all possible responses is logical, but it is impossible to track all survey response answers that match the desired answer. therefore, we only track one example, and later find all similar response answers based on the question strategy



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
# File 'app/models/missinglink/survey_question.rb', line 41

def possible_responses(search_other = false)
  {}.tap do |hash|
    survey_response_answers.each do |sra|
      sa_row = (sra.row_survey_answer_id ? SurveyAnswer.find(sra.row_survey_answer_id) : nil)
      sa_col = (sra.col_survey_answer_id ? SurveyAnswer.find(sra.col_survey_answer_id) : nil)
      sa_col_choice = (sra.col_choice_survey_answer_id ? SurveyAnswer.find(sra.col_choice_survey_answer_id) : nil)

      case answer_strategy
      when "first_survey_response_answer_text"
        hash[sra.text] = sra.id unless (sra.text.nil? || hash[sra.text])
      when "answer_row_for_subquestion"
        other_text = (sra.text.nil? ? nil : "#{ (sa_row.try(:text) || "Other") }: #{ sra.text }")
        hash[other_text] = sra.id unless (other_text.nil? || hash[other_text])
      when "answer_row_for_response"
        other_text = ((!search_other || sra.text.nil?) ? nil : "#{ (sa_row.try(:text) || "Other") }: #{ sra.text }")
        hash[sa_row.text] = sra.id unless (sa_row.nil? || hash[sa_row.text])
        hash[other_text] = sra.id unless (other_text.nil? || hash[other_text])
      when "answer_row_and_column_for_response"
        main_text = "#{ sa_row.try(:text) }: #{ sa_col.try(:text) }"
        other_text = ((!search_other || sra.text.nil? || !sa_row.nil?) ? nil : "Other: #{ sra.text }")
        hash[main_text] = sra.id unless (sa_row.nil? || sa_col.nil? || hash[main_text])
        hash[other_text] = sra.id unless (other_text.nil? || hash[other_text])
      when "answer_row_column_choice_for_response"
        main_text = "#{ sa_row.try(:text) }, #{ sa_col.try(:text) }: #{ sa_col_choice.try(:text) }"
        other_text = ((!search_other || sra.text.nil? || !sa_row.nil?) ? nil : "Other: #{ sra.text }")
        hash[main_text] = sra.id unless (sa_row.nil? || sa_col.nil? || sa_col_choice.nil? || hash[main_text])
        hash[other_text] = sra.id unless (other_text.nil? || hash[other_text])
      end
    end
  end
end

#question_partsObject



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'app/models/missinglink/survey_question.rb', line 102

def question_parts
  return nil if survey_response_answers.empty?
  if answer_strategy == "answer_row_for_subquestion" ||
     answer_strategy == "answer_row_and_column_for_response" ||
     answer_strategy == "answer_row_column_choice_for_response"
    return {}.tap do |hash|
      survey_response_answers.each do |sra|
        sa_row = (sra.row_survey_answer_id ? SurveyAnswer.find(sra.row_survey_answer_id) : nil)
        hash[sa_row.text] = sra.row_survey_answer_id unless (sa_row.nil? || hash[sa_row.text])
      end
    end
  else
    return nil
  end
end

#similar_response_answers(response_answer, search_other = false) ⇒ Object



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
# File 'app/models/missinglink/survey_question.rb', line 73

def similar_response_answers(response_answer, search_other = false)
  if search_other || answer_strategy == "first_survey_response_answer_text"
    return survey_response_answers.select { |sra| sra.text == response_answer.text }.sort
  end

  case answer_strategy
  when "answer_row_for_subquestion"
    survey_response_answers.select do |sra|
      sra.row_survey_answer_id == response_answer.row_survey_answer_id &&
        sra.text == response_answer.text
    end.sort
  when "answer_row_for_response"
    survey_response_answers.select do |sra|
      sra.row_survey_answer_id == response_answer.row_survey_answer_id
    end.sort
  when "answer_row_and_column_for_response"
    survey_response_answers.select do |sra|
      sra.row_survey_answer_id == response_answer.row_survey_answer_id &&
        sra.col_survey_answer_id == response_answer.col_survey_answer_id
    end.sort
  when "answer_row_column_choice_for_response"
    survey_response_answers.select do |sra|
      sra.row_survey_answer_id == response_answer.row_survey_answer_id &&
        sra.col_survey_answer_id == response_answer.col_survey_answer_id &&
        sra.col_choice_survey_answer_id == response_answer.col_choice_survey_answer_id
    end.sort
  end
end