From 2bcca41b42b6b58dc3566e08e3032a862d5c0599 Mon Sep 17 00:00:00 2001 From: Leonard Ng'eno Date: Wed, 6 Dec 2023 11:32:51 -0500 Subject: [PATCH] correctly label choice task and pairwise comparison options --- app/models/survey.rb | 41 ++++++++++++++++++++++++++++- app/workers/survey_export_worker.rb | 2 ++ 2 files changed, 42 insertions(+), 1 deletion(-) diff --git a/app/models/survey.rb b/app/models/survey.rb index 444fcc3d..e2cabae9 100644 --- a/app/models/survey.rb +++ b/app/models/survey.rb @@ -186,7 +186,11 @@ def option_labels(response) labels << vq.options.map { |o| sanitize o.text } else response.text.split(Settings.list_delimiter).each do |option_index| - labels << if vq.other? && option_index.to_i == vq.other_index + labels << if vq.question_type == 'CHOICE_TASK' + choice_task_labels(vq, option_index.to_i) + elsif vq.question_type == 'PAIRWISE_COMPARISON' + pairwise_comparison_labels(vq, option_index) + elsif vq.other? && option_index.to_i == vq.other_index 'Other' else sanitize(label_text(vq, option_index)) @@ -196,6 +200,41 @@ def option_labels(response) labels.join(Settings.list_delimiter) end + def choice_task_labels(question, index) + label = '' + oios = question.option_set.option_in_option_sets[index] + oios.option_collages.each do |oc| + label += ' || ' if label.present? + oc.collage.diagrams.each do |d| + label += d.option.identifier + label += ' & ' unless d == oc.collage.diagrams.last + end + end + "(#{label})" + end + + def pairwise_comparison_labels(question, index) + option_identifiers = [] + question.option_in_option_sets.each do |oios| + oios.option_collages.each do |oc| + oc.collage.diagrams.each do |d| + option_identifiers << d.option.identifier + end + end + end + if index == '1.0' + "Strongly prefer #{option_identifiers[0]}" + elsif index == '2.0' + "Somewhat prefer #{option_identifiers[0]}" + elsif index == '3.0' + "No preference between #{option_identifiers[0]} and #{option_identifiers[1]}" + elsif index == '4.0' + "Somewhat prefer #{option_identifiers[1]}" + elsif index == '5.0' + "Strongly prefer #{option_identifiers[1]}" + end + end + def label_text(versioned_question, option_index) versioned_question.options[option_index.to_i].try(:text) end diff --git a/app/workers/survey_export_worker.rb b/app/workers/survey_export_worker.rb index aecdd74f..a4ea463a 100644 --- a/app/workers/survey_export_worker.rb +++ b/app/workers/survey_export_worker.rb @@ -6,6 +6,8 @@ class SurveyExportWorker def perform(survey_uuid) survey = Survey.includes(:responses).where(uuid: survey_uuid).try(:first) + return unless survey + SurveyExport.create(survey_id: survey.id) unless survey.survey_export survey.reload