Module: Surveyor::Models::ResponseSetMethods

Included in:
ResponseSet
Defined in:
lib/surveyor/models/response_set_methods.rb

Class Method Summary (collapse)

Instance Method Summary (collapse)

Class Method Details

+ (Object) included(base)



6
7
8
9
10
11
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
42
43
44
45
46
# File 'lib/surveyor/models/response_set_methods.rb', line 6

def self.included(base)
  # Associations
  base.send :belongs_to, :survey
  base.send :belongs_to, :user
  base.send :has_many, :responses, :dependent => :destroy
  base.send :accepts_nested_attributes_for, :responses, :allow_destroy => true
  
  @@validations_already_included ||= nil
  unless @@validations_already_included
    # Validations
    base.send :validates_presence_of, :survey_id
    base.send :validates_associated, :responses
    base.send :validates_uniqueness_of, :access_code
    
    @@validations_already_included = true
  end

  # Attributes
  base.send :attr_protected, :completed_at
  
  # Class methods
  base.instance_eval do
    def reject_or_destroy_blanks(hash_of_hashes)
      result = {}
      (hash_of_hashes || {}).each_pair do |k, hash|
        hash = Response.applicable_attributes(hash)
        if has_blank_value?(hash)
          result.merge!({k => hash.merge("_destroy" => "true")}) if hash.has_key?("id")
        else
          result.merge!({k => hash})
        end
      end
      result
    end
    def has_blank_value?(hash)
      return true if hash["answer_id"].blank?
      return false if (q = Question.find_by_id(hash["question_id"])) and q.pick == "one"
      hash.any?{|k,v| v.is_a?(Array) ? v.all?{|x| x.to_s.blank?} : v.to_s.blank?}
    end
  end
end

Instance Method Details

- (Object) access_code=(val)



59
60
61
62
63
64
# File 'lib/surveyor/models/response_set_methods.rb', line 59

def access_code=(val)
  while ResponseSet.find_by_access_code(val)
    val = Surveyor::Common.make_tiny_code
  end
  super
end

- (Object) all_dependencies(question_ids = nil)



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

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

- (Object) complete!



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

def complete!
  self.completed_at = Time.now
end

- (Boolean) complete?

Returns:

  • (Boolean)


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

def complete?
  !completed_at.nil?
end

- (Boolean) correct?

Returns:

  • (Boolean)


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

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

- (Object) correctness_hash



90
91
92
93
94
95
# File 'lib/surveyor/models/response_set_methods.rb', line 90

def correctness_hash
  { :questions => survey.sections_with_questions.map(&:questions).flatten.compact.size,
    :responses => responses.compact.size,
    :correct => responses.find_all(&:correct?).compact.size
  }
end

- (Object) count_group_responses(questions)

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



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

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

- (Object) default_args



54
55
56
57
# File 'lib/surveyor/models/response_set_methods.rb', line 54

def default_args
  self.started_at ||= Time.now
  self.access_code = Surveyor::Common.make_tiny_code
end

- (ResponseSetMethods) initialize(*args)

Instance methods

Returns:



49
50
51
52
# File 'lib/surveyor/models/response_set_methods.rb', line 49

def initialize(*args)
  super(*args)
  default_args
end

- (Boolean) is_answered?(question)

Returns:

  • (Boolean)


109
110
111
# File 'lib/surveyor/models/response_set_methods.rb', line 109

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

- (Boolean) is_group_unanswered?(group)

Returns:

  • (Boolean)


115
116
117
# File 'lib/surveyor/models/response_set_methods.rb', line 115

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

- (Boolean) is_unanswered?(question)

Returns:

  • (Boolean)


112
113
114
# File 'lib/surveyor/models/response_set_methods.rb', line 112

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

- (Boolean) mandatory_questions_complete?

Returns:

  • (Boolean)


96
97
98
# File 'lib/surveyor/models/response_set_methods.rb', line 96

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

- (Boolean) no_responses_for_section?(section)

Check existence of responses to questions from a given survey_section

Returns:

  • (Boolean)


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

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

- (Object) progress_hash



99
100
101
102
103
104
105
106
107
108
# File 'lib/surveyor/models/response_set_methods.rb', line 99

def progress_hash
  qs = survey.sections_with_questions.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

- (Object) to_csv(access_code = false, print_header = true)



66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/surveyor/models/response_set_methods.rb', line 66

def to_csv(access_code = false, print_header = true)
  qcols = Question.content_columns.map(&:name) - %w(created_at updated_at)
  acols = Answer.content_columns.map(&:name) - %w(created_at updated_at)
  rcols = Response.content_columns.map(&:name)
  csvlib = CSV.const_defined?(:Reader) ? FasterCSV : CSV
  result = csvlib.generate do |csv|
    csv << (access_code ? ["response set access code"] : []) + qcols.map{|qcol| "question.#{qcol}"} + acols.map{|acol| "answer.#{acol}"} + rcols.map{|rcol| "response.#{rcol}"} if print_header
    responses.each do |response|
      csv << (access_code ? [self.access_code] : []) + qcols.map{|qcol| response.question.send(qcol)} + acols.map{|acol| response.answer.send(acol)} + rcols.map{|rcol| response.send(rcol)}
    end
  end
  result
end

- (Object) unanswered_dependencies



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

def unanswered_dependencies
  unanswered_question_dependencies + unanswered_question_group_dependencies
end

- (Object) unanswered_question_dependencies



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

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

- (Object) unanswered_question_group_dependencies



132
133
134
# File 'lib/surveyor/models/response_set_methods.rb', line 132

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