Skip to content

Commit

Permalink
add question/choice order to export
Browse files Browse the repository at this point in the history
  • Loading branch information
kodvinci committed Dec 7, 2023
1 parent 2bcca41 commit 6623b01
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
2 changes: 1 addition & 1 deletion app/models/instrument.rb
Original file line number Diff line number Diff line change
Expand Up @@ -408,7 +408,7 @@ def create_loop_question(lq, variable_identifiers, question_identifier_variables

def wide_headers
variable_identifiers = []
question_identifier_variables = %w[_short_qid _question_type _label _other_text _special
question_identifier_variables = %w[_number _short_qid _question_type _label _option_order _other_text _special
_other _version _text _start_time _end_time]
iqs = Rails.cache.fetch("instrument-questions-#{id}-#{instrument_questions.with_deleted.maximum('updated_at')}",
expires_in: 30.minutes) do
Expand Down
19 changes: 17 additions & 2 deletions app/models/survey.rb
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,17 @@ def survey_duration
end_time - start_time if end_time && start_time
end

def question_order
question_order = []
s_display_order = metadata['display_order']&.split(',')
s_display_order ||= instrument.displays.pluck(:title)
s_display_order.each do |display_title|
display = instrument.displays.where(title: display_title).first
question_order << display.instrument_questions.map(&:identifier)
end
question_order.flatten!
end

def write_wide_row
headers =
Rails.cache.fetch("w_w_r_h-#{instrument_id}-#{instrument_version_number}", expires_in: 30.minutes) do
Expand All @@ -265,15 +276,17 @@ def write_wide_row
end
row = [id, uuid, device.identifier, device_label || device.label, latitude, longitude,
instrument_id, instrument_version_number, instrument_title, start_time&.to_s, end_time&.to_s, survey_duration]

metadata&.each do |k, v|
row[headers[k]] = v
end

order = question_order
responses.each do |response|
iq = find_instrument_question(response)
identifier_index = headers["q_#{response.question_identifier}"] unless response.empty?
row[identifier_index] = response.text if identifier_index
question_number_index = headers["q_#{response.question_identifier}_number"]
qid_index = order.index(response.question_identifier)
row[question_number_index] = (qid_index ? (qid_index + 1) : -1) if question_number_index
short_qid_index = headers["q_#{response.question_identifier}_short_qid"]
row[short_qid_index] = response.question_id if short_qid_index
question_type_index = headers["q_#{response.question_identifier}_question_type"]
Expand All @@ -286,6 +299,8 @@ def write_wide_row
row[other_identifier_index] = response.other_response if other_identifier_index
label_index = headers["q_#{response.question_identifier}_label"]
row[label_index] = option_labels(response) if label_index && !response.empty?
label_order_index = headers["q_#{response.question_identifier}_option_order"]
row[label_order_index] = response.randomized_data if label_order_index && response.randomized_data.present?
question_version_index = headers["q_#{response.question_identifier}_version"]
row[question_version_index] = response.question_version if question_version_index
question_text_index = headers["q_#{response.question_identifier}_text"]
Expand Down

0 comments on commit 6623b01

Please sign in to comment.