Skip to content

Commit

Permalink
Merge pull request #433 from CLOSER-Cohorts/feature/question-grid-imp…
Browse files Browse the repository at this point in the history
…ort-and-export-issues-with-rdsqs-312

RdsQs should now be imported and exported with the correct value #312
  • Loading branch information
spuddybike committed Jun 16, 2020
2 parents 0a3c691 + ee9ddba commit 2b9e205
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 13 deletions.
15 changes: 11 additions & 4 deletions app/models/question_grid.rb
Expand Up @@ -55,19 +55,26 @@ def pretty_corner_label
#
# @return [Array] All response domains
def response_domains
grid_rds_qs.map { |x| x.response_domain }
end

# Returns all grid rds_qs using horizontal_code_list_id
#
# @return [Array] All RdsQs
def grid_rds_qs
sql = <<~SQL
SELECT rds_qs.*
FROM rds_qs
INNER JOIN question_grids
ON question_grids.id = rds_qs.question_id
ON question_grids.id = rds_qs.question_id
AND rds_qs.question_type = 'QuestionGrid'
INNER JOIN codes
ON codes.code_list_id = question_grids.horizontal_code_list_id
ON codes.code_list_id = question_grids.horizontal_code_list_id
AND codes.value::int = rds_qs.code_id
WHERE question_grids.id = ?
ORDER BY codes.order
SQL
rds_qs = RdsQs.find_by_sql ([sql, self.id])
rds_qs.map { |x| x.response_domain }
RdsQs.find_by_sql ([sql, self.id])
end

# Exports as an XML fragment
Expand Down
24 changes: 16 additions & 8 deletions lib/exporters/xml/ddi/question_grid.rb
Expand Up @@ -63,30 +63,38 @@ def V3_2(qgrid_id)
end

rd_wrapper = qg
if qgrid.response_domains.count > 1

response_domains_count = qgrid.response_domains.count

if qgrid.response_domains.count > 0
rd_wrapper = Nokogiri::XML::Node.new 'd:StructuredMixedGridResponseDomain', @doc
qg.add_child rd_wrapper
end

qgrid.response_domains.each_with_index do |rd, i|
qgrid.grid_rds_qs.each do |rds_qs|
rd = rds_qs.response_domain
case rd
when ResponseDomainCode
cd = Nokogiri::XML::Node.new 'd:CodeDomain', @doc
cd.add_child create_reference_string 'r:CodeListReference', rd.code_list
cd.add_child '<r:ResponseCardinality minimumResponses="%{min}" maximumResponses="%{max}"></r:ResponseCardinality>' % {
min: rd.min_responses,
max: rd.max_responses
}

rd_wrapper.add_child wrap_grid_response_domain cd, qgrid.response_domains.count, i + 1
rd_wrapper.add_child wrap_grid_response_domain cd, response_domains_count, rds_qs.code_id

when ResponseDomainText
td = build_response_domain_text(rd)
rd_wrapper.add_child wrap_grid_response_domain td, qgrid.response_domains.count, i + 1
rd_wrapper.add_child wrap_grid_response_domain td, response_domains_count, rds_qs.code_id

when ResponseDomainDatetime
dd = build_response_domain_datetime(rd)
rd_wrapper.add_child wrap_grid_response_domain dd, qgrid.response_domains.count, i + 1
rd_wrapper.add_child wrap_grid_response_domain dd, response_domains_count, rds_qs.code_id

when ResponseDomainNumeric
nd = build_response_domain_numeric rd
rd_wrapper.add_child wrap_grid_response_domain nd, qgrid.response_domains.count, i + 1
rd_wrapper.add_child wrap_grid_response_domain nd, response_domains_count, rds_qs.code_id
end
end

Expand All @@ -110,7 +118,7 @@ def V3_2(qgrid_id)
# @param [Integer] col Column to attach the response domain to
# @return [Nokogiri::XML::Node] Wrapped response domain node
def wrap_grid_response_domain(node, rd_count, col)
if rd_count > 1
if rd_count > 0
wrapper = Nokogiri::XML::Node.new 'd:GridResponseDomain', @doc
wrapper.add_child node
wrapper.add_child <<~XML.delete("\n")
Expand All @@ -127,4 +135,4 @@ def wrap_grid_response_domain(node, rd_count, col)
end
end
end
end
end
2 changes: 1 addition & 1 deletion lib/importers/xml/ddi/question.rb
Expand Up @@ -95,7 +95,7 @@ def read_response_domain(node)
extract_urn_identifier(node.at_xpath('./CodeListReference'))
)
response_cardinality = node.at_xpath('./ResponseCardinality')
if response_cardinality
if response_cardinality && cl.response_domain.nil?
min_responses = response_cardinality['minimumResponses']
max_responses = response_cardinality['maximumResponses']
cl.create_response_domain_code(min_responses: min_responses, max_responses: max_responses)
Expand Down

0 comments on commit 2b9e205

Please sign in to comment.