Class: Question

Inherits:
ActiveRecord::Base
  • Object
show all
Includes:
Surveyor::Models::QuestionMethods
Defined in:
app/models/question.rb,
lib/surveyor/parser.rb,
lib/surveyor/unparser.rb,
lib/surveyor/redcap_parser.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Surveyor::Models::QuestionMethods

#css_class, #default_args, #dependent?, #display_type=, included, #initialize, #mandatory?, #part_of_group?, #pick=, #renderer, #solo?, #split_text, #triggered?

Instance Attribute Details

#context_referenceObject

attributes



126
127
128
# File 'lib/surveyor/parser.rb', line 126

def context_reference
  @context_reference
end

#correctObject

attributes



126
127
128
# File 'lib/surveyor/parser.rb', line 126

def correct
  @correct
end

Class Method Details

.build_and_set(context, r) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/surveyor/redcap_parser.rb', line 73

def self.build_and_set(context, r)
  if !r[:section_header].blank?
    context[:survey_section].questions.build({:display_type => "label", :text => r[:section_header]})
    print "label_ "
  end
  context[:question] = context[:survey_section].questions.build({
    :reference_identifier => r[:variable__field_name],
    :text => r[:field_label],
    :help_text => r[:field_note],
    :is_mandatory => (/^y/i.match r[:required_field]) ? true : false,
    :pick => pick_from_field_type(r[:field_type]),
    :display_type => display_type_from_field_type(r[:field_type])
  })
  unless context[:question].reference_identifier.blank?
    context[:lookup] ||= []
    context[:lookup] << [context[:question].reference_identifier, nil, context[:question]]
  end    
  print "question_#{context[:question].reference_identifier} "
end

.display_type_from_field_type(ft) ⇒ Object



95
96
97
# File 'lib/surveyor/redcap_parser.rb', line 95

def self.display_type_from_field_type(ft)
  {"text" => :string, "dropdown" => :dropdown, "notes" => :text}[ft]
end

.parse_and_build(context, args, original_method, reference_identifier) ⇒ Object



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/surveyor/parser.rb', line 129

def self.parse_and_build(context, args, original_method, reference_identifier)
  # clear context
  context.delete_if{|k,v| %w(question dependency dependency_condition answer validation validation_condition).map(&:to_sym).include? k}
  
  # build and set context
  text = args[0] || "Question"
  context[:question] = context[:survey_section].questions.build({
    :context_reference => context,
    :question_group => context[:question_group],
    :reference_identifier => reference_identifier,
    :text => text,
    :display_type => (original_method =~ /label|image/ ? original_method : "default")}.merge(args[1] || {}))
  
  # keep reference for dependencies
  context[:question_references][reference_identifier] = context[:question] unless reference_identifier.blank?
  
  # add grid answers
  if context[:question_group] && context[:question_group].display_type == "grid"
    (context[:grid_answers] || []).each do |grid_answer|
      a = context[:question].answers.build(grid_answer.attributes)
      context[:answer_references][reference_identifier] ||= {} unless reference_identifier.blank?
      context[:answer_references][reference_identifier][grid_answer.reference_identifier] = a unless reference_identifier.blank? or grid_answer.reference_identifier.blank?
    end
  end
end

.pick_from_field_type(ft) ⇒ Object



92
93
94
# File 'lib/surveyor/redcap_parser.rb', line 92

def self.pick_from_field_type(ft)
  {"checkbox" => :any, "radio" => :one}[ft] || :none
end

Instance Method Details

#resolve_correct_answersObject



155
156
157
158
159
160
161
# File 'lib/surveyor/parser.rb', line 155

def resolve_correct_answers
  unless correct.blank? or reference_identifier.blank? or context_reference.blank?
    # Looking up references for quiz answers
    context_reference[:answer_references][reference_identifier] ||= {}
    print (self.correct_answer = context_reference[:answer_references][reference_identifier][correct]) ? "found correct answer:#{correct} " : "lost! correct answer:#{correct} "
  end
end

#unparse(dsl) ⇒ Object

nonblock



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/surveyor/unparser.rb', line 64

def unparse(dsl)
  attrs = (self.attributes.diff Question.new(:text => text).attributes).delete_if{|k,v| %w(created_at updated_at reference_identifier id survey_section_id question_group_id api_id).include?(k) or (k == "display_type" && v == "label")}.symbolize_keys!
  dsl << (solo? ? "\n" : "  ")
  if display_type == "label"
    dsl << "    label"
  else
    dsl << "    q"
  end
  dsl << "_#{reference_identifier}" unless reference_identifier.blank?
  dsl << " \"#{text}\""
  dsl << (attrs.blank? ? "\n" : ", #{attrs.inspect.gsub(/\{|\}/, "")}\n")
  if solo? or question_group.display_type != "grid"
    answers.each{|answer| answer.unparse(dsl)}
  end
  dependency.unparse(dsl) if dependency
end