Skip to content

Commit

Permalink
Use vanilla method to clean up question names. Fix issue with unpubli…
Browse files Browse the repository at this point in the history
…shed forms throwing an error due to no version number'
  • Loading branch information
Alex committed Feb 6, 2024
1 parent 94db0a9 commit 74a368b
Showing 1 changed file with 22 additions and 5 deletions.
27 changes: 22 additions & 5 deletions app/models/forms/export.rb
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ def to_xls
end

if q.group? # is this a group?
group_name = q.code.tr(" ", "_")
group_name = vanillify(q.code)

if q.repeatable?
questions.row(row_index).push("begin repeat", group_name, q.code)
Expand All @@ -117,6 +117,14 @@ def to_xls
# convert question types to ODK style
qtype_converted = QTYPE_TO_XLS[q.qtype_name]

# TODO: "Constraints" in NEMO have a different definition than in XLSForm
# NEMO allows multiple constraint rules to be in effect that refer to different
# questions in addition to that question itself.
# In XLSForm, constraints are usually described in terms of the form response to
# that particular question, which is denoted by a period "."
# Also, if there are multiple constraint rules, it appears that they should be
# placed in parentheses () and separated by "and"
# https://docs.getodk.org/form-logic/#validating-and-restricting-responses
constraints_to_push = ""
# if we have any relevant conditions or constraints, save them now
conditions_to_push = conditions_to_xls(q.display_conditions, q.display_if)
Expand All @@ -135,7 +143,7 @@ def to_xls
# include leading space to respect XLSForm format
# question name should be followed by the option set name (if applicable) separated by a space
# replace any spaces in the option set name with underscores to ensure the form is parsed correctly
os_name = os.name.tr(" ", "_")
os_name = vanillify(os.name)

# is the option set multilevel?
if os.level_names.present?
Expand All @@ -151,7 +159,6 @@ def to_xls
# Modify question label
# NOTE: the question "label" (what NEMO calls "name") will have to be manually edited
# in the exported XLSForm by the user so that it makes grammatical sense.
# The below line simply makes the label unique.
label_to_push = "#{q.name}_#{level_name}"

# push a row for each level
Expand Down Expand Up @@ -194,7 +201,12 @@ def to_xls

## Settings
lang = @form.mission.setting.preferred_locales[0].to_s
settings.row(1).push(@form.name, @form.id, @form.current_version.decorate.name, lang)
version = if @form.current_version.present?
@form.current_version.decorate.name
else
"1"
end
settings.row(1).push(@form.name, @form.id, version, lang)

## Write
file = StringIO.new
Expand Down Expand Up @@ -307,7 +319,7 @@ def options_to_xls(option_sets)
level_to_push += node.ancestors[1..].map(&:name)
end
else
listname_to_push = os.name.tr(" ", "_")
listname_to_push = vanillify(os.name)
end

if node.option.present? # rubocop:disable Style/Next
Expand Down Expand Up @@ -336,5 +348,10 @@ def options_to_xls(option_sets)
# return os_matrix with prepended header_row
os_matrix.insert(0, header_row)
end

def vanillify(input)
out = input.vanilla # remove extra characters
out.tr(" ", "_") # replace spaces with underscores
end
end
end

0 comments on commit 74a368b

Please sign in to comment.