Module: Surveyor::Models::ResponseSetMethods

Extended by:
ActiveSupport::Concern
Includes:
ActiveModel::ForbiddenAttributesProtection, ActiveModel::Validations
Included in:
ResponseSet
Defined in:
lib/surveyor/models/response_set_methods.rb

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#all_dependencies(question_ids = nil) ⇒ Object



134
135
136
137
138
139
140
# File 'lib/surveyor/models/response_set_methods.rb', line 134

def all_dependencies(question_ids = nil)
  arr = dependencies(question_ids).partition{|d| d.is_met?(self) }
  {
    :show => arr[0].map{|d| d.question_group_id.nil? ? "q_#{d.question_id}" : "g_#{d.question_group_id}"},
    :hide => arr[1].map{|d| d.question_group_id.nil? ? "q_#{d.question_id}" : "g_#{d.question_group_id}"}
  }
end

#as_json(options = nil) ⇒ Object



66
67
68
69
# File 'lib/surveyor/models/response_set_methods.rb', line 66

def as_json(options = nil)
  template_paths = ActionController::Base.view_paths.collect(&:to_path)
  Rabl.render(self, 'surveyor/show.json', :view_path => template_paths, :format => "hash")
end

#complete!Object



71
72
73
# File 'lib/surveyor/models/response_set_methods.rb', line 71

def complete!
  self.completed_at = Time.now
end

#complete?Boolean

Returns:

  • (Boolean)


75
76
77
# File 'lib/surveyor/models/response_set_methods.rb', line 75

def complete?
  !completed_at.nil?
end

#correct?Boolean

Returns:

  • (Boolean)


79
80
81
# File 'lib/surveyor/models/response_set_methods.rb', line 79

def correct?
  responses.all?(&:correct?)
end

#correctness_hashObject



82
83
84
85
86
87
# File 'lib/surveyor/models/response_set_methods.rb', line 82

def correctness_hash
  { :questions => Survey.where(id: self.survey_id).includes(sections: :questions).first.sections.map(&:questions).flatten.compact.size,
    :responses => responses.compact.size,
    :correct => responses.find_all(&:correct?).compact.size
  }
end

#count_group_responses(questions) ⇒ Object

Returns the number of response groups (count of group responses enterted) for this question group



112
113
114
115
116
117
118
# File 'lib/surveyor/models/response_set_methods.rb', line 112

def count_group_responses(questions)
  questions.map { |q|
    responses.select { |r|
      (r.question_id.to_i == q.id.to_i) && !r.response_group.nil?
    }.group_by(&:response_group).size
  }.max
end

#ensure_identifiersObject



38
39
40
41
# File 'lib/surveyor/models/response_set_methods.rb', line 38

def ensure_identifiers
  self.access_code ||= Surveyor::Common.make_tiny_code
  self.api_id ||= Surveyor::Common.generate_api_id
end

#ensure_start_timestampObject



34
35
36
# File 'lib/surveyor/models/response_set_methods.rb', line 34

def ensure_start_timestamp
  self.started_at ||= Time.now
end

#is_answered?(question) ⇒ Boolean

Returns:

  • (Boolean)


101
102
103
# File 'lib/surveyor/models/response_set_methods.rb', line 101

def is_answered?(question)
  %w(label image).include?(question.display_type) or !is_unanswered?(question)
end

#is_group_unanswered?(group) ⇒ Boolean

Returns:

  • (Boolean)


107
108
109
# File 'lib/surveyor/models/response_set_methods.rb', line 107

def is_group_unanswered?(group)
  group.questions.any?{|question| is_unanswered?(question)}
end

#is_unanswered?(question) ⇒ Boolean

Returns:

  • (Boolean)


104
105
106
# File 'lib/surveyor/models/response_set_methods.rb', line 104

def is_unanswered?(question)
  self.responses.detect{|r| r.question_id == question.id}.nil?
end

#mandatory_questions_complete?Boolean

Returns:

  • (Boolean)


88
89
90
# File 'lib/surveyor/models/response_set_methods.rb', line 88

def mandatory_questions_complete?
  progress_hash[:triggered_mandatory] == progress_hash[:triggered_mandatory_completed]
end

#no_responses_for_section?(section) ⇒ Boolean

Check existence of responses to questions from a given survey_section

Returns:

  • (Boolean)


143
144
145
# File 'lib/surveyor/models/response_set_methods.rb', line 143

def no_responses_for_section?(section)
  !responses.any?{|r| r.survey_section_id == section.id}
end

#progress_hashObject



91
92
93
94
95
96
97
98
99
100
# File 'lib/surveyor/models/response_set_methods.rb', line 91

def progress_hash
  qs = Survey.where(id: self.survey_id).includes(sections: :questions).first.sections.map(&:questions).flatten
  ds = dependencies(qs.map(&:id))
  triggered = qs - ds.select{|d| !d.is_met?(self)}.map(&:question)
  { :questions => qs.compact.size,
    :triggered => triggered.compact.size,
    :triggered_mandatory => triggered.select{|q| q.mandatory?}.compact.size,
    :triggered_mandatory_completed => triggered.select{|q| q.mandatory? and is_answered?(q)}.compact.size
  }
end

#to_csv(access_code = false, print_header = true) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/surveyor/models/response_set_methods.rb', line 43

def to_csv(access_code = false, print_header = true)
  result = Surveyor::Common.csv_impl.generate do |csv|
    if print_header
      csv << (access_code ? ["response set access code"] : []) +
        csv_question_columns.map{|qcol| "question.#{qcol}"} +
        csv_answer_columns.map{|acol| "answer.#{acol}"} +
        csv_response_columns.map{|rcol| "response.#{rcol}"}
    end
    responses.each do |response|
      csv << (access_code ? [self.access_code] : []) +
        csv_question_columns.map{|qcol| response.question.send(qcol)} +
        csv_answer_columns.map{|acol| response.answer.send(acol)} +
        csv_response_columns.map{|rcol| response.send(rcol)}
    end
  end
  result
end

#unanswered_dependenciesObject



120
121
122
# File 'lib/surveyor/models/response_set_methods.rb', line 120

def unanswered_dependencies
  unanswered_question_dependencies + unanswered_question_group_dependencies
end

#unanswered_question_dependenciesObject



124
125
126
# File 'lib/surveyor/models/response_set_methods.rb', line 124

def unanswered_question_dependencies
  dependencies.select{ |d| d.question && self.is_unanswered?(d.question) && d.is_met?(self) }.map(&:question)
end

#unanswered_question_group_dependenciesObject



128
129
130
131
132
# File 'lib/surveyor/models/response_set_methods.rb', line 128

def unanswered_question_group_dependencies
  dependencies.
    select{ |d| d.question_group && self.is_group_unanswered?(d.question_group) && d.is_met?(self) }.
    map(&:question_group)
end

#update_from_ui_hash(ui_hash) ⇒ Object



147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/surveyor/models/response_set_methods.rb', line 147

def update_from_ui_hash(ui_hash)
  transaction do
    ui_hash.each do |ord, response_hash|
      api_id = response_hash['api_id']
      fail "api_id missing from response #{ord}" unless api_id

      existing = Response.where(:api_id => api_id).first
      updateable_attributes = response_hash.reject { |k, v| k == 'api_id' }

      if self.class.has_blank_value?(response_hash)
        existing.destroy if existing
      elsif existing
        if existing.question_id.to_s != updateable_attributes['question_id']
          fail "Illegal attempt to change question for response #{api_id}."
        end

        existing.update_attributes(updateable_attributes)
      else
        responses.build(updateable_attributes).tap do |r|
          r.api_id = api_id
          r.save!
        end
      end

    end
  end
end