Class: Quby::Compiler::Entities::Questionnaire

Inherits:
Object
  • Object
show all
Extended by:
ActiveModel::Naming
Includes:
ActiveModel::Validations
Defined in:
lib/quby/compiler/entities/questionnaire.rb

Defined Under Namespace

Classes: InputKeyAlreadyDefined, UnknownInputKey, ValidationError

Constant Summary collapse

VALID_LICENSES =
[:unknown,
:free,               # freely available without license costs,
:pay_per_completion, # costs associated with each completed questionnaire,
:private,            # not a publicly available questionnaire
:deprecated]
RESPONDENT_TYPES =
%i( profess patient parent second_parent teacher caregiver )

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key) ⇒ Questionnaire

Returns a new instance of Questionnaire.



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/quby/compiler/entities/questionnaire.rb', line 33

def initialize(key)
  @key = key
  @sbg_domains = []
  @score_calculations = {}.with_indifferent_access
  @charts = Charting::Charts.new
  @fields = Fields.new(self)
  @license = :unknown
  @layout_version = nil
  @extra_css = ""
  @css_vars = nil
  @panels = []
  @flags = {}.with_indifferent_access
  @textvars = {}.with_indifferent_access
  @language = 'nl'
  @translatable_into = nil
  @respondent_types = []
  @tags = []
  @check_key_clashes = true
  @validate_html = true
  @score_schemas = {}.with_indifferent_access
  @outcome_tables = []
  @check_score_keys_consistency = true
  @lookup_tables = {}
  @sexp_variables = {}
  @extra_visibility_rules = []
  @versions = []
  @editable_from_version = nil
  @seeds_patch = {}
  @anonymous_conditions = Entities::AnonymousConditions.new
  @check_checkbox_options_start_with_question_key = true
end

Instance Attribute Details

#abortableObject

Returns the value of attribute abortable.



75
76
77
# File 'lib/quby/compiler/entities/questionnaire.rb', line 75

def abortable
  @abortable
end

#allow_hotkeysObject



131
132
133
# File 'lib/quby/compiler/entities/questionnaire.rb', line 131

def allow_hotkeys
  (@allow_hotkeys || :bulk).to_s
end

#anonymous_conditionsObject

Returns the value of attribute anonymous_conditions.



116
117
118
# File 'lib/quby/compiler/entities/questionnaire.rb', line 116

def anonymous_conditions
  @anonymous_conditions
end

#chartsObject

Returns the value of attribute charts.



106
107
108
# File 'lib/quby/compiler/entities/questionnaire.rb', line 106

def charts
  @charts
end

#check_checkbox_options_start_with_question_keyObject

Returns the value of attribute check_checkbox_options_start_with_question_key.



118
119
120
# File 'lib/quby/compiler/entities/questionnaire.rb', line 118

def check_checkbox_options_start_with_question_key
  @check_checkbox_options_start_with_question_key
end

#check_key_clashesObject

whether to check for clashes between question input keys (HTML form keys)



98
99
100
# File 'lib/quby/compiler/entities/questionnaire.rb', line 98

def check_key_clashes
  @check_key_clashes
end

#check_score_keys_consistencyObject

whether to check consistency of score subkeys during seed generation



100
101
102
# File 'lib/quby/compiler/entities/questionnaire.rb', line 100

def check_score_keys_consistency
  @check_score_keys_consistency
end

#css_varsObject

Returns the value of attribute css_vars.



87
88
89
# File 'lib/quby/compiler/entities/questionnaire.rb', line 87

def css_vars
  @css_vars
end

#deactivate_answers_requested_atObject

Returns the value of attribute deactivate_answers_requested_at.



95
96
97
# File 'lib/quby/compiler/entities/questionnaire.rb', line 95

def deactivate_answers_requested_at
  @deactivate_answers_requested_at
end

#default_answer_valueObject

Returns the value of attribute default_answer_value.



82
83
84
# File 'lib/quby/compiler/entities/questionnaire.rb', line 82

def default_answer_value
  @default_answer_value
end

#descriptionObject

Returns the value of attribute description.



67
68
69
# File 'lib/quby/compiler/entities/questionnaire.rb', line 67

def description
  @description
end

#editable_by_professionalsObject

Returns the value of attribute editable_by_professionals.



76
77
78
# File 'lib/quby/compiler/entities/questionnaire.rb', line 76

def editable_by_professionals
  @editable_by_professionals
end

#editable_from_versionObject

Returns the value of attribute editable_from_version.



74
75
76
# File 'lib/quby/compiler/entities/questionnaire.rb', line 74

def editable_from_version
  @editable_from_version
end

#enable_previous_questionnaire_buttonObject

Returns the value of attribute enable_previous_questionnaire_button.



77
78
79
# File 'lib/quby/compiler/entities/questionnaire.rb', line 77

def enable_previous_questionnaire_button
  @enable_previous_questionnaire_button
end

#environmentsObject

Returns the value of attribute environments.



78
79
80
# File 'lib/quby/compiler/entities/questionnaire.rb', line 78

def environments
  @environments
end

#extra_cssObject

Returns the value of attribute extra_css.



86
87
88
# File 'lib/quby/compiler/entities/questionnaire.rb', line 86

def extra_css
  @extra_css
end

#extra_visibility_rulesObject (readonly)

Returns the value of attribute extra_visibility_rules.



114
115
116
# File 'lib/quby/compiler/entities/questionnaire.rb', line 114

def extra_visibility_rules
  @extra_visibility_rules
end

#fieldsObject (readonly)

Returns the value of attribute fields.



85
86
87
# File 'lib/quby/compiler/entities/questionnaire.rb', line 85

def fields
  @fields
end

#flagsObject

Returns the value of attribute flags.



108
109
110
# File 'lib/quby/compiler/entities/questionnaire.rb', line 108

def flags
  @flags
end

Returns the value of attribute footer.



70
71
72
# File 'lib/quby/compiler/entities/questionnaire.rb', line 70

def footer
  @footer
end

#keyObject

Returns the value of attribute key.



65
66
67
# File 'lib/quby/compiler/entities/questionnaire.rb', line 65

def key
  @key
end

#languageObject

Returns the value of attribute language.



90
91
92
# File 'lib/quby/compiler/entities/questionnaire.rb', line 90

def language
  @language
end

#layout_versionObject

Returns the value of attribute layout_version.



83
84
85
# File 'lib/quby/compiler/entities/questionnaire.rb', line 83

def layout_version
  @layout_version
end

#leave_page_alertObject



126
127
128
129
# File 'lib/quby/compiler/entities/questionnaire.rb', line 126

def leave_page_alert
  return nil unless Settings.enable_leave_page_alert
  @leave_page_alert || "Als u de pagina verlaat worden uw antwoorden niet opgeslagen."
end

#licenseObject

Returns the value of attribute license.



88
89
90
# File 'lib/quby/compiler/entities/questionnaire.rb', line 88

def license
  @license
end

#licensorObject

Returns the value of attribute licensor.



89
90
91
# File 'lib/quby/compiler/entities/questionnaire.rb', line 89

def licensor
  @licensor
end

#lookup_tablesObject

Returns the value of attribute lookup_tables.



115
116
117
# File 'lib/quby/compiler/entities/questionnaire.rb', line 115

def lookup_tables
  @lookup_tables
end

#originalObject

Returns the value of attribute original.



79
80
81
# File 'lib/quby/compiler/entities/questionnaire.rb', line 79

def original
  @original
end

#outcome_descriptionObject

Returns the value of attribute outcome_description.



68
69
70
# File 'lib/quby/compiler/entities/questionnaire.rb', line 68

def outcome_description
  @outcome_description
end

#outcome_regeneration_requested_atObject

Returns the value of attribute outcome_regeneration_requested_at.



94
95
96
# File 'lib/quby/compiler/entities/questionnaire.rb', line 94

def outcome_regeneration_requested_at
  @outcome_regeneration_requested_at
end

#outcome_tablesObject

Returns the value of attribute outcome_tables.



111
112
113
# File 'lib/quby/compiler/entities/questionnaire.rb', line 111

def outcome_tables
  @outcome_tables
end

#panelsObject

Returns the value of attribute panels.



80
81
82
# File 'lib/quby/compiler/entities/questionnaire.rb', line 80

def panels
  @panels
end

#respondent_typesObject

Returns the value of attribute respondent_types.



92
93
94
# File 'lib/quby/compiler/entities/questionnaire.rb', line 92

def respondent_types
  @respondent_types
end

#roqua_keysObject



135
136
137
# File 'lib/quby/compiler/entities/questionnaire.rb', line 135

def roqua_keys
  @roqua_keys || [key]
end

#sbg_domainsObject

Returns the value of attribute sbg_domains.



73
74
75
# File 'lib/quby/compiler/entities/questionnaire.rb', line 73

def sbg_domains
  @sbg_domains
end

#sbg_keyObject

not required to be unique



72
73
74
# File 'lib/quby/compiler/entities/questionnaire.rb', line 72

def sbg_key
  @sbg_key
end

#score_calculationsObject

Returns the value of attribute score_calculations.



81
82
83
# File 'lib/quby/compiler/entities/questionnaire.rb', line 81

def score_calculations
  @score_calculations
end

#score_schemasObject

Returns the value of attribute score_schemas.



112
113
114
# File 'lib/quby/compiler/entities/questionnaire.rb', line 112

def score_schemas
  @score_schemas
end

#seeds_patchObject

a patch for the seeds, to define and fix changes to from-scratch generation



96
97
98
# File 'lib/quby/compiler/entities/questionnaire.rb', line 96

def seeds_patch
  @seeds_patch
end

#sexp_variablesObject

Returns the value of attribute sexp_variables.



113
114
115
# File 'lib/quby/compiler/entities/questionnaire.rb', line 113

def sexp_variables
  @sexp_variables
end

#short_descriptionObject

Returns the value of attribute short_description.



69
70
71
# File 'lib/quby/compiler/entities/questionnaire.rb', line 69

def short_description
  @short_description
end

#tagsObject

tags= is manually defined below



93
94
95
# File 'lib/quby/compiler/entities/questionnaire.rb', line 93

def tags
  @tags
end

#textvarsObject

Returns the value of attribute textvars.



109
110
111
# File 'lib/quby/compiler/entities/questionnaire.rb', line 109

def textvars
  @textvars
end

#titleObject

Returns the value of attribute title.



66
67
68
# File 'lib/quby/compiler/entities/questionnaire.rb', line 66

def title
  @title
end

#translatable_intoObject

Returns the value of attribute translatable_into.



91
92
93
# File 'lib/quby/compiler/entities/questionnaire.rb', line 91

def translatable_into
  @translatable_into
end

#validate_htmlObject

If false, we don’t check html for validity (for mate1 and mate1_pre)



102
103
104
# File 'lib/quby/compiler/entities/questionnaire.rb', line 102

def validate_html
  @validate_html
end

#versionsObject

Returns the value of attribute versions.



74
75
76
# File 'lib/quby/compiler/entities/questionnaire.rb', line 74

def versions
  @versions
end

Instance Method Details

#actionsObject



246
247
248
# File 'lib/quby/compiler/entities/questionnaire.rb', line 246

def actions
  score_calculations.values.select(&:action)
end

#add_chart(chart) ⇒ Object



254
255
256
# File 'lib/quby/compiler/entities/questionnaire.rb', line 254

def add_chart(chart)
  charts.add chart
end

#add_flag(flag_options) ⇒ Object



258
259
260
261
262
263
264
265
266
267
# File 'lib/quby/compiler/entities/questionnaire.rb', line 258

def add_flag(flag_options)
  if flag_options[:internal]
    flag_key = flag_options[:key].to_sym
  else
    flag_key = "#{key}_#{flag_options[:key]}".to_sym
  end
  flag_options[:key] = flag_key
  fail(ArgumentError, "Flag '#{flag_key}' already defined") if flags.key?(flag_key)
  flags[flag_key] = Flag.new(**flag_options)
end

#add_outcome_table(outcome_table_options) ⇒ Object



387
388
389
# File 'lib/quby/compiler/entities/questionnaire.rb', line 387

def add_outcome_table(outcome_table_options)
  outcome_tables << OutcomeTable.new(**outcome_table_options, questionnaire: self)
end

#add_panel(panel) ⇒ Object



147
148
149
# File 'lib/quby/compiler/entities/questionnaire.rb', line 147

def add_panel(panel)
  @panels << panel
end

#add_score_calculation(builder) ⇒ Object



227
228
229
230
231
232
# File 'lib/quby/compiler/entities/questionnaire.rb', line 227

def add_score_calculation(builder)
  if score_calculations.key?(builder.key)
    fail InputKeyAlreadyDefined, "Score key `#{builder.key}` already defined."
  end
  score_calculations[builder.key] = builder
end

#add_score_schema(score_schema) ⇒ Object



234
235
236
# File 'lib/quby/compiler/entities/questionnaire.rb', line 234

def add_score_schema(score_schema)
  score_schemas[score_schema.key] = score_schema
end

#add_sexp_variable(key, sexp_variable) ⇒ Object



383
384
385
# File 'lib/quby/compiler/entities/questionnaire.rb', line 383

def add_sexp_variable(key, sexp_variable)
  sexp_variables[key] = sexp_variable
end

#add_textvar(textvar_options) ⇒ Object



275
276
277
278
279
280
281
# File 'lib/quby/compiler/entities/questionnaire.rb', line 275

def add_textvar(textvar_options)
  textvar_key = "#{key}_#{textvar_options.fetch(:key)}".to_sym
  textvar_options[:key] = textvar_key
  validate_textvar_keys_unique(textvar_key)
  validate_depends_on_flag(textvar_key, textvar_options)
  textvars[textvar_key] = Textvar.new(**textvar_options)
end

#answer_dsl_moduleObject

rubocop:disable Metrics/MethodLength



295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
# File 'lib/quby/compiler/entities/questionnaire.rb', line 295

def answer_dsl_module # rubocop:disable Metrics/MethodLength
  # Have to put this in a local variable so the module definition block can access it
  questions_in_var = questions

  @answer_dsl_cache ||= Module.new do
    questions_in_var.each do |question|
      next if question&.key.blank?
      case question.type
      when :date
        question.components.each do |component|
          # assignment to 'value' hash must be done under string keys
          key = question.send("#{component}_key").to_s
          define_method(key) do
            self.value ||= Hash.new
            self.value[key]
          end

          define_method("#{key}=") do |v|
            self.value ||= Hash.new
            self.value[key] = v&.strip
          end
        end

        define_method(question.key) do
          self.value ||= Hash.new

          components = question.components.sort
          component_values = components.map do |component|
            value_key = question.send("#{component}_key").to_s
            self.value[value_key]
          end
          case components
          when [:day, :month, :year]
            component_values.reverse.take_while { |p| p.present? }.reverse.join('-')
          when [:month, :year]
            component_values.reject(&:blank?).join('-')
          when [:hour, :minute]
            component_values.all?(&:blank?) ? '' : component_values.join(':')
          end
        end

      when :check_box

        define_method(question.key) do
          self.value ||= Hash.new
          self.value[question.key.to_s] ||= Hash.new
        end

        question.options.each do |opt|
          next if opt&.key.blank?
          define_method("#{opt.key}") do
            self.value ||= Hash.new
            self.value[question.key.to_s] ||= Hash.new
            self.value[opt.key.to_s] ||= 0
          end

          define_method("#{opt.key}=") do |v|
            v = v.to_i
            self.value ||= Hash.new
            self.value[question.key.to_s] ||= Hash.new
            self.value[question.key.to_s][opt.key.to_s] = v
            self.value[opt.key.to_s] = v
          end
        end
      else
        # Includes:
        # question.type == :radio
        # question.type == :scale
        # question.type == :select
        # question.type == :string
        # question.type == :textarea
        # question.type == :integer
        # question.type == :float

        define_method(question.key) do
          self.value ||= Hash.new
          self.value[question.key.to_s]
        end

        define_method(question.key.to_s + "=") do |v|
          self.value ||= Hash.new
          self.value[question.key.to_s] = v
        end
      end rescue nil
    end
  end
end

#callback_after_dsl_enhance_on_questionsObject



159
160
161
162
163
164
# File 'lib/quby/compiler/entities/questionnaire.rb', line 159

def callback_after_dsl_enhance_on_questions
  question_hash.each_value do |q|
    q.run_callbacks :after_dsl_enhance
  end
  ensure_scores_have_schemas if Quby::Settings.require_score_schemas
end

#completionObject



250
251
252
# File 'lib/quby/compiler/entities/questionnaire.rb', line 250

def completion
  score_calculations.values.select(&:completion).first
end

#default_textvarsObject



289
290
291
292
293
# File 'lib/quby/compiler/entities/questionnaire.rb', line 289

def default_textvars
  textvars.select { |key, textvar| textvar.default.present? }
          .map    { |key, textvar| [key, textvar.default] }
          .to_h
end

#ensure_scores_have_schemasObject



166
167
168
169
170
171
# File 'lib/quby/compiler/entities/questionnaire.rb', line 166

def ensure_scores_have_schemas
  missing_schemas = scores.map(&:key).map(&:to_s) - score_schemas.keys
  missing_schemas.each do |key|
    errors.add "Score #{key}", 'is missing a score schema'
  end
end

#filter_flags(given_flags) ⇒ Object



269
270
271
272
273
# File 'lib/quby/compiler/entities/questionnaire.rb', line 269

def filter_flags(given_flags)
  given_flags.select do |flag_key, _|
    flags.key? flag_key
  end
end

#filter_textvars(given_textvars) ⇒ Object



283
284
285
286
287
# File 'lib/quby/compiler/entities/questionnaire.rb', line 283

def filter_textvars(given_textvars)
  given_textvars.select do |textvar_key, _|
    textvars.key? textvar_key
  end
end

#find_plottable(key) ⇒ Object



242
243
244
# File 'lib/quby/compiler/entities/questionnaire.rb', line 242

def find_plottable(key)
  score_calculations[key] || question_hash.with_indifferent_access[key]
end

#key_in_use?(key) ⇒ Boolean

Returns:

  • (Boolean)


219
220
221
# File 'lib/quby/compiler/entities/questionnaire.rb', line 219

def key_in_use?(key)
  fields.key_in_use?(key) || score_calculations.key?(key)
end

#questionsObject



206
207
208
# File 'lib/quby/compiler/entities/questionnaire.rb', line 206

def questions
  question_hash.values
end

#questions_of_type(type) ⇒ Object



210
211
212
# File 'lib/quby/compiler/entities/questionnaire.rb', line 210

def questions_of_type(type)
  questions.select { |question| question.type == type }
end

#questions_treeObject



181
182
183
184
185
186
187
188
189
190
191
# File 'lib/quby/compiler/entities/questionnaire.rb', line 181

def questions_tree
  return @questions_tree_cache if @questions_tree_cache

  recurse = lambda do |question|
    [question, question.subquestions.map(&recurse)]
  end

  @questions_tree_cache = (@panels && @panels.map do |panel|
    panel.items.map { |item| recurse.call(item) if item.is_a?(Quby::Compiler::Entities::Question) }
  end)
end

#register_question(question) ⇒ Object



151
152
153
154
155
156
157
# File 'lib/quby/compiler/entities/questionnaire.rb', line 151

def register_question(question)
  fields.add(question)

  if question.sets_textvar && !textvars.key?(question.sets_textvar)
    fail "Undefined textvar: #{question.sets_textvar}"
  end
end

#scoresObject



238
239
240
# File 'lib/quby/compiler/entities/questionnaire.rb', line 238

def scores
  score_calculations.values.select(&:score)
end

#sorted_questionsObject

sorts parents before children, so showing makes more sense and visiblity rules are ordered correctly



194
195
196
197
198
199
200
201
202
203
204
# File 'lib/quby/compiler/entities/questionnaire.rb', line 194

def sorted_questions
  return @sorted_questions if @sorted_questions

  key_to_order_by = questions.map.with_index.to_h { [_1.key, [_2]] }
  questions.each do |question|
    if question.parent
      key_to_order_by[question.key].unshift(key_to_order_by[question.parent.key].first)
    end
  end
  @sorted_questions = questions.sort_by { key_to_order_by[_1.key] }
end

#to_paramObject



143
144
145
# File 'lib/quby/compiler/entities/questionnaire.rb', line 143

def to_param
  key
end

#translatable?Boolean

Returns:

  • (Boolean)


223
224
225
# File 'lib/quby/compiler/entities/questionnaire.rb', line 223

def translatable?
  !translatable_into.nil?
end

#validate_questionsObject



173
174
175
176
177
178
179
# File 'lib/quby/compiler/entities/questionnaire.rb', line 173

def validate_questions
  question_hash.each_value do |q|
    unless q.valid?
      q.errors.each { |attr, err| errors.add(attr, err) }
    end
  end
end

#validationsObject



391
392
393
394
395
396
397
398
399
400
401
402
# File 'lib/quby/compiler/entities/questionnaire.rb', line 391

def validations
  @validations ||= fields.question_hash.values.flat_map do |question|
    question.validations.map do |validation|
      case validation[:type]
      when :answer_group_minimum, :answer_group_maximum
        Validation.new(validation.merge(field_keys: questions.select {|q| q.question_group == validation[:group]}.map(&:key)))
      else
        Validation.new(validation.merge(field_key: question.key))
      end
    end
  end.uniq(&:config)
end

#version_numberObject



139
140
141
# File 'lib/quby/compiler/entities/questionnaire.rb', line 139

def version_number
  versions.last&.number || 0
end

#visibility_rulesObject

Order is important We sort by order of the question being shown/hidden with showing winning from hiding, since showing wins from hiding. This is important since a shown question can’t be hidden, so each question is only ever changed once while looping over the rules.



407
408
409
410
411
412
413
414
415
416
417
# File 'lib/quby/compiler/entities/questionnaire.rb', line 407

def visibility_rules
  return @visibility_rules if @visibility_rules
  question_order = sorted_questions.map.with_index{ [_1.key, _2] }.to_h
  @visibility_rules ||= [
    *flags.values.flat_map { |flag| VisibilityRule.from_flag(flag) },
    *sorted_questions.flat_map { |question| VisibilityRule.from(question) },
    *extra_visibility_rules
  ].sort_by.with_index do |rule, idx|
    [question_order[rule.action[:field_key]], rule.action[:type] == 'show_question' ? 0 : 1, idx]
  end
end