Class: Quby::Compiler::Entities::Questionnaire
- Inherits:
-
Object
- Object
- Quby::Compiler::Entities::Questionnaire
- 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
-
#abortable ⇒ Object
Returns the value of attribute abortable.
- #allow_hotkeys ⇒ Object
-
#allow_switch_to_bulk ⇒ Object
Returns the value of attribute allow_switch_to_bulk.
-
#anonymous_conditions ⇒ Object
Returns the value of attribute anonymous_conditions.
-
#charts ⇒ Object
Returns the value of attribute charts.
-
#check_key_clashes ⇒ Object
whether to check for clashes between question input keys (HTML form keys).
-
#check_score_keys_consistency ⇒ Object
whether to check consistency of score subkeys during seed generation.
-
#deactivate_answers_requested_at ⇒ Object
Returns the value of attribute deactivate_answers_requested_at.
-
#default_answer_value ⇒ Object
Returns the value of attribute default_answer_value.
-
#description ⇒ Object
Returns the value of attribute description.
-
#enable_previous_questionnaire_button ⇒ Object
Returns the value of attribute enable_previous_questionnaire_button.
-
#extra_css ⇒ Object
Returns the value of attribute extra_css.
-
#fields ⇒ Object
readonly
Returns the value of attribute fields.
-
#flags ⇒ Object
Returns the value of attribute flags.
-
#key ⇒ Object
Returns the value of attribute key.
-
#language ⇒ Object
Returns the value of attribute language.
-
#last_author ⇒ Object
Returns the value of attribute last_author.
- #leave_page_alert ⇒ Object
-
#license ⇒ Object
Returns the value of attribute license.
-
#licensor ⇒ Object
Returns the value of attribute licensor.
-
#lookup_tables ⇒ Object
Returns the value of attribute lookup_tables.
-
#outcome_description ⇒ Object
Returns the value of attribute outcome_description.
-
#outcome_regeneration_requested_at ⇒ Object
Returns the value of attribute outcome_regeneration_requested_at.
-
#outcome_tables ⇒ Object
Returns the value of attribute outcome_tables.
-
#panels ⇒ Object
Returns the value of attribute panels.
-
#renderer_version ⇒ Object
Returns the value of attribute renderer_version.
-
#respondent_types ⇒ Object
Returns the value of attribute respondent_types.
- #roqua_keys ⇒ Object
-
#sbg_domains ⇒ Object
Returns the value of attribute sbg_domains.
-
#sbg_key ⇒ Object
not required to be unique.
-
#score_calculations ⇒ Object
Returns the value of attribute score_calculations.
-
#score_schemas ⇒ Object
Returns the value of attribute score_schemas.
-
#seeds_patch ⇒ Object
a patch for the seeds, to define and fix changes to from-scratch generation.
-
#short_description ⇒ Object
Returns the value of attribute short_description.
-
#tags ⇒ Object
tags= is manually defined below.
-
#textvars ⇒ Object
Returns the value of attribute textvars.
-
#title ⇒ Object
Returns the value of attribute title.
-
#validate_html ⇒ Object
If false, we don’t check html for validity (for mate1 and mate1_pre).
-
#versions ⇒ Object
Returns the value of attribute versions.
Instance Method Summary collapse
- #actions ⇒ Object
- #add_chart(chart) ⇒ Object
- #add_flag(flag_options) ⇒ Object
- #add_outcome_table(outcome_table_options) ⇒ Object
- #add_panel(panel) ⇒ Object
- #add_score_calculation(builder) ⇒ Object
- #add_score_schema(score_schema) ⇒ Object
- #add_textvar(textvar_options) ⇒ Object
-
#answer_dsl_module ⇒ Object
rubocop:disable Metrics/MethodLength.
- #callback_after_dsl_enhance_on_questions ⇒ Object
- #completion ⇒ Object
- #default_textvars ⇒ Object
- #ensure_scores_have_schemas ⇒ Object
- #filter_flags(given_flags) ⇒ Object
- #filter_textvars(given_textvars) ⇒ Object
- #find_plottable(key) ⇒ Object
-
#initialize(key) ⇒ Questionnaire
constructor
A new instance of Questionnaire.
- #key_in_use?(key) ⇒ Boolean
- #questions ⇒ Object
- #questions_of_type(type) ⇒ Object
- #questions_tree ⇒ Object
- #register_question(question) ⇒ Object
- #scores ⇒ Object
-
#sorted_questions ⇒ Object
sorts parents before children, so showing makes more sense and visiblity rules are ordered correctly.
- #to_param ⇒ Object
- #validate_questions ⇒ Object
- #validations ⇒ Object
-
#visibility_rules ⇒ Object
Order is important quby2 follows them one by one and ignores rules by hidden question, so needs to be in order of interface.
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 |
# 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 @renderer_version = :v1 @extra_css = "" @allow_switch_to_bulk = false @panels = [] @flags = {}.with_indifferent_access @textvars = {}.with_indifferent_access @language = :nl @respondent_types = [] @tags = OpenStruct.new @check_key_clashes = true @validate_html = true @score_schemas = {}.with_indifferent_access @outcome_tables = [] @check_score_keys_consistency = true @lookup_tables = {} @versions = [] @seeds_patch = {} @anonymous_conditions = Entities::AnonymousConditions.new end |
Instance Attribute Details
#abortable ⇒ Object
Returns the value of attribute abortable.
69 70 71 |
# File 'lib/quby/compiler/entities/questionnaire.rb', line 69 def abortable @abortable end |
#allow_hotkeys ⇒ Object
120 121 122 |
# File 'lib/quby/compiler/entities/questionnaire.rb', line 120 def allow_hotkeys (@allow_hotkeys || :bulk).to_s end |
#allow_switch_to_bulk ⇒ Object
Returns the value of attribute allow_switch_to_bulk.
78 79 80 |
# File 'lib/quby/compiler/entities/questionnaire.rb', line 78 def allow_switch_to_bulk @allow_switch_to_bulk end |
#anonymous_conditions ⇒ Object
Returns the value of attribute anonymous_conditions.
105 106 107 |
# File 'lib/quby/compiler/entities/questionnaire.rb', line 105 def anonymous_conditions @anonymous_conditions end |
#charts ⇒ Object
Returns the value of attribute charts.
97 98 99 |
# File 'lib/quby/compiler/entities/questionnaire.rb', line 97 def charts @charts end |
#check_key_clashes ⇒ Object
whether to check for clashes between question input keys (HTML form keys)
88 89 90 |
# File 'lib/quby/compiler/entities/questionnaire.rb', line 88 def check_key_clashes @check_key_clashes end |
#check_score_keys_consistency ⇒ Object
whether to check consistency of score subkeys during seed generation
90 91 92 |
# File 'lib/quby/compiler/entities/questionnaire.rb', line 90 def check_score_keys_consistency @check_score_keys_consistency end |
#deactivate_answers_requested_at ⇒ Object
Returns the value of attribute deactivate_answers_requested_at.
85 86 87 |
# File 'lib/quby/compiler/entities/questionnaire.rb', line 85 def deactivate_answers_requested_at @deactivate_answers_requested_at end |
#default_answer_value ⇒ Object
Returns the value of attribute default_answer_value.
73 74 75 |
# File 'lib/quby/compiler/entities/questionnaire.rb', line 73 def default_answer_value @default_answer_value end |
#description ⇒ Object
Returns the value of attribute description.
62 63 64 |
# File 'lib/quby/compiler/entities/questionnaire.rb', line 62 def description @description end |
#enable_previous_questionnaire_button ⇒ Object
Returns the value of attribute enable_previous_questionnaire_button.
70 71 72 |
# File 'lib/quby/compiler/entities/questionnaire.rb', line 70 def @enable_previous_questionnaire_button end |
#extra_css ⇒ Object
Returns the value of attribute extra_css.
77 78 79 |
# File 'lib/quby/compiler/entities/questionnaire.rb', line 77 def extra_css @extra_css end |
#fields ⇒ Object (readonly)
Returns the value of attribute fields.
76 77 78 |
# File 'lib/quby/compiler/entities/questionnaire.rb', line 76 def fields @fields end |
#flags ⇒ Object
Returns the value of attribute flags.
99 100 101 |
# File 'lib/quby/compiler/entities/questionnaire.rb', line 99 def flags @flags end |
#key ⇒ Object
Returns the value of attribute key.
60 61 62 |
# File 'lib/quby/compiler/entities/questionnaire.rb', line 60 def key @key end |
#language ⇒ Object
Returns the value of attribute language.
81 82 83 |
# File 'lib/quby/compiler/entities/questionnaire.rb', line 81 def language @language end |
#last_author ⇒ Object
Returns the value of attribute last_author.
94 95 96 |
# File 'lib/quby/compiler/entities/questionnaire.rb', line 94 def @last_author end |
#leave_page_alert ⇒ Object
115 116 117 118 |
# File 'lib/quby/compiler/entities/questionnaire.rb', line 115 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 |
#license ⇒ Object
Returns the value of attribute license.
79 80 81 |
# File 'lib/quby/compiler/entities/questionnaire.rb', line 79 def license @license end |
#licensor ⇒ Object
Returns the value of attribute licensor.
80 81 82 |
# File 'lib/quby/compiler/entities/questionnaire.rb', line 80 def licensor @licensor end |
#lookup_tables ⇒ Object
Returns the value of attribute lookup_tables.
104 105 106 |
# File 'lib/quby/compiler/entities/questionnaire.rb', line 104 def lookup_tables @lookup_tables end |
#outcome_description ⇒ Object
Returns the value of attribute outcome_description.
63 64 65 |
# File 'lib/quby/compiler/entities/questionnaire.rb', line 63 def outcome_description @outcome_description end |
#outcome_regeneration_requested_at ⇒ Object
Returns the value of attribute outcome_regeneration_requested_at.
84 85 86 |
# File 'lib/quby/compiler/entities/questionnaire.rb', line 84 def outcome_regeneration_requested_at @outcome_regeneration_requested_at end |
#outcome_tables ⇒ Object
Returns the value of attribute outcome_tables.
102 103 104 |
# File 'lib/quby/compiler/entities/questionnaire.rb', line 102 def outcome_tables @outcome_tables end |
#panels ⇒ Object
Returns the value of attribute panels.
71 72 73 |
# File 'lib/quby/compiler/entities/questionnaire.rb', line 71 def panels @panels end |
#renderer_version ⇒ Object
Returns the value of attribute renderer_version.
74 75 76 |
# File 'lib/quby/compiler/entities/questionnaire.rb', line 74 def renderer_version @renderer_version end |
#respondent_types ⇒ Object
Returns the value of attribute respondent_types.
82 83 84 |
# File 'lib/quby/compiler/entities/questionnaire.rb', line 82 def respondent_types @respondent_types end |
#roqua_keys ⇒ Object
124 125 126 |
# File 'lib/quby/compiler/entities/questionnaire.rb', line 124 def roqua_keys @roqua_keys || [key] end |
#sbg_domains ⇒ Object
Returns the value of attribute sbg_domains.
67 68 69 |
# File 'lib/quby/compiler/entities/questionnaire.rb', line 67 def sbg_domains @sbg_domains end |
#sbg_key ⇒ Object
not required to be unique
66 67 68 |
# File 'lib/quby/compiler/entities/questionnaire.rb', line 66 def sbg_key @sbg_key end |
#score_calculations ⇒ Object
Returns the value of attribute score_calculations.
72 73 74 |
# File 'lib/quby/compiler/entities/questionnaire.rb', line 72 def score_calculations @score_calculations end |
#score_schemas ⇒ Object
Returns the value of attribute score_schemas.
103 104 105 |
# File 'lib/quby/compiler/entities/questionnaire.rb', line 103 def score_schemas @score_schemas end |
#seeds_patch ⇒ Object
a patch for the seeds, to define and fix changes to from-scratch generation
86 87 88 |
# File 'lib/quby/compiler/entities/questionnaire.rb', line 86 def seeds_patch @seeds_patch end |
#short_description ⇒ Object
Returns the value of attribute short_description.
64 65 66 |
# File 'lib/quby/compiler/entities/questionnaire.rb', line 64 def short_description @short_description end |
#tags ⇒ Object
tags= is manually defined below
83 84 85 |
# File 'lib/quby/compiler/entities/questionnaire.rb', line 83 def @tags end |
#textvars ⇒ Object
Returns the value of attribute textvars.
100 101 102 |
# File 'lib/quby/compiler/entities/questionnaire.rb', line 100 def textvars @textvars end |
#title ⇒ Object
Returns the value of attribute title.
61 62 63 |
# File 'lib/quby/compiler/entities/questionnaire.rb', line 61 def title @title end |
#validate_html ⇒ Object
If false, we don’t check html for validity (for mate1 and mate1_pre)
92 93 94 |
# File 'lib/quby/compiler/entities/questionnaire.rb', line 92 def validate_html @validate_html end |
#versions ⇒ Object
Returns the value of attribute versions.
68 69 70 |
# File 'lib/quby/compiler/entities/questionnaire.rb', line 68 def versions @versions end |
Instance Method Details
#actions ⇒ Object
227 228 229 |
# File 'lib/quby/compiler/entities/questionnaire.rb', line 227 def actions score_calculations.values.select(&:action) end |
#add_chart(chart) ⇒ Object
235 236 237 |
# File 'lib/quby/compiler/entities/questionnaire.rb', line 235 def add_chart(chart) charts.add chart end |
#add_flag(flag_options) ⇒ Object
239 240 241 242 243 244 245 246 247 248 |
# File 'lib/quby/compiler/entities/questionnaire.rb', line 239 def add_flag() if [:internal] flag_key = [:key].to_sym else flag_key = "#{key}_#{[:key]}".to_sym end [:key] = flag_key fail(ArgumentError, "Flag '#{flag_key}' already defined") if flags.key?(flag_key) flags[flag_key] = Flag.new(**) end |
#add_outcome_table(outcome_table_options) ⇒ Object
364 365 366 |
# File 'lib/quby/compiler/entities/questionnaire.rb', line 364 def add_outcome_table() outcome_tables << OutcomeTable.new(**, questionnaire: self) end |
#add_panel(panel) ⇒ Object
132 133 134 |
# File 'lib/quby/compiler/entities/questionnaire.rb', line 132 def add_panel(panel) @panels << panel end |
#add_score_calculation(builder) ⇒ Object
208 209 210 211 212 213 |
# File 'lib/quby/compiler/entities/questionnaire.rb', line 208 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
215 216 217 |
# File 'lib/quby/compiler/entities/questionnaire.rb', line 215 def add_score_schema(score_schema) score_schemas[score_schema.key] = score_schema end |
#add_textvar(textvar_options) ⇒ Object
256 257 258 259 260 261 262 |
# File 'lib/quby/compiler/entities/questionnaire.rb', line 256 def add_textvar() textvar_key = "#{key}_#{.fetch(:key)}".to_sym [:key] = textvar_key validate_textvar_keys_unique(textvar_key) validate_depends_on_flag(textvar_key, ) textvars[textvar_key] = Textvar.new(**) end |
#answer_dsl_module ⇒ Object
rubocop:disable Metrics/MethodLength
276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 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 |
# File 'lib/quby/compiler/entities/questionnaire.rb', line 276 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..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_questions ⇒ Object
144 145 146 147 148 149 |
# File 'lib/quby/compiler/entities/questionnaire.rb', line 144 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 |
#completion ⇒ Object
231 232 233 |
# File 'lib/quby/compiler/entities/questionnaire.rb', line 231 def completion score_calculations.values.select(&:completion).first end |
#default_textvars ⇒ Object
270 271 272 273 274 |
# File 'lib/quby/compiler/entities/questionnaire.rb', line 270 def default_textvars textvars.select { |key, textvar| textvar.default.present? } .map { |key, textvar| [key, textvar.default] } .to_h end |
#ensure_scores_have_schemas ⇒ Object
151 152 153 154 155 156 |
# File 'lib/quby/compiler/entities/questionnaire.rb', line 151 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
250 251 252 253 254 |
# File 'lib/quby/compiler/entities/questionnaire.rb', line 250 def filter_flags(given_flags) given_flags.select do |flag_key, _| flags.key? flag_key end end |
#filter_textvars(given_textvars) ⇒ Object
264 265 266 267 268 |
# File 'lib/quby/compiler/entities/questionnaire.rb', line 264 def filter_textvars(given_textvars) given_textvars.select do |textvar_key, _| textvars.key? textvar_key end end |
#find_plottable(key) ⇒ Object
223 224 225 |
# File 'lib/quby/compiler/entities/questionnaire.rb', line 223 def find_plottable(key) score_calculations[key] || question_hash.with_indifferent_access[key] end |
#key_in_use?(key) ⇒ Boolean
204 205 206 |
# File 'lib/quby/compiler/entities/questionnaire.rb', line 204 def key_in_use?(key) fields.key_in_use?(key) || score_calculations.key?(key) end |
#questions ⇒ Object
191 192 193 |
# File 'lib/quby/compiler/entities/questionnaire.rb', line 191 def questions question_hash.values end |
#questions_of_type(type) ⇒ Object
195 196 197 |
# File 'lib/quby/compiler/entities/questionnaire.rb', line 195 def questions_of_type(type) questions.select { |question| question.type == type } end |
#questions_tree ⇒ Object
166 167 168 169 170 171 172 173 174 175 176 |
# File 'lib/quby/compiler/entities/questionnaire.rb', line 166 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
136 137 138 139 140 141 142 |
# File 'lib/quby/compiler/entities/questionnaire.rb', line 136 def register_question(question) fields.add(question) if question.sets_textvar && !textvars.key?(question.sets_textvar) fail "Undefined textvar: #{question.sets_textvar}" end end |
#scores ⇒ Object
219 220 221 |
# File 'lib/quby/compiler/entities/questionnaire.rb', line 219 def scores score_calculations.values.select(&:score) end |
#sorted_questions ⇒ Object
sorts parents before children, so showing makes more sense and visiblity rules are ordered correctly
179 180 181 182 183 184 185 186 187 188 189 |
# File 'lib/quby/compiler/entities/questionnaire.rb', line 179 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_param ⇒ Object
128 129 130 |
# File 'lib/quby/compiler/entities/questionnaire.rb', line 128 def to_param key end |
#validate_questions ⇒ Object
158 159 160 161 162 163 164 |
# File 'lib/quby/compiler/entities/questionnaire.rb', line 158 def validate_questions question_hash.each_value do |q| unless q.valid? q.errors.each { |attr, err| errors.add(attr, err) } end end end |
#validations ⇒ Object
368 369 370 371 372 373 374 375 376 377 378 379 |
# File 'lib/quby/compiler/entities/questionnaire.rb', line 368 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 |
#visibility_rules ⇒ Object
Order is important quby2 follows them one by one and ignores rules by hidden question, so needs to be in order of interface. Flags don’t have this issue, so as long as they are first, their order doesn’t matter.
384 385 386 387 388 389 |
# File 'lib/quby/compiler/entities/questionnaire.rb', line 384 def visibility_rules @visibility_rules ||= [ *flags.values.flat_map { |flag| VisibilityRule.from_flag(flag) }, *sorted_questions.flat_map { |question| VisibilityRule.from(question) } ] end |