Skip to content
This repository has been archived by the owner on Oct 1, 2020. It is now read-only.

Commit

Permalink
Merge pull request #378 from CDCgov/redcap_surveys
Browse files Browse the repository at this point in the history
[SDPV-98] Redcap surveys
  • Loading branch information
kierk authored Oct 13, 2017
2 parents a0e9040 + 1d0ecb8 commit 759c37a
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 0 deletions.
8 changes: 8 additions & 0 deletions app/controllers/surveys_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,14 @@ def publish
end
end

# GET /surveys/1/redcap
def redcap
xml = render_to_string 'surveys/redcap.xml', layout: false
send_data(xml, filename: "#{@survey.name.underscore}_redcap.xml",
type: 'application/xml',
status: 200)
end

private

def can_survey_be_created?(survey)
Expand Down
60 changes: 60 additions & 0 deletions app/views/surveys/redcap.xml.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?xml version="1.0" encoding="UTF-8" ?>
<ODM xmlns="http://www.cdisc.org/ns/odm/v1.3" xmlns:ds="http://www.w3.org/2000/09/xmldsig#" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:redcap="https://projectredcap.org" xsi:schemaLocation="http://www.cdisc.org/ns/odm/v1.3 schema/odm/ODM1-3-1.xsd" ODMVersion="1.3.1" FileOID="000-00-0000" FileType="Snapshot" Description="test" AsOfDateTime="<%= Time.now %>" CreationDateTime="<%= @survey.created_at %>" SourceSystem="SDP-Vocabulary-Service" SourceSystemVersion="0.1">
<Study OID="Project.Test">
<GlobalVariables>
<StudyName><%=@survey.name%></StudyName> <!-- -->
<StudyDescription><%=@survey.description %></StudyDescription>
<ProtocolName>Test</ProtocolName>
<redcap:RecordAutonumberingEnabled>1</redcap:RecordAutonumberingEnabled>
<redcap:CustomRecordLabel></redcap:CustomRecordLabel>
<redcap:SecondaryUniqueField></redcap:SecondaryUniqueField>
<redcap:SchedulingEnabled>0</redcap:SchedulingEnabled>
<redcap:Purpose>0</redcap:Purpose>
<redcap:PurposeOther></redcap:PurposeOther>
<redcap:ProjectNotes></redcap:ProjectNotes>
</GlobalVariables>
<MetaDataVersion OID="Metadata.Test_2016-12-15_1938" Name="test" >
<!-- form defs -->
<% @survey.forms.each do |form| %>

<FormDef OID="Form.<%= form.name.gsub(' ', '_')%>" Name="<%= form.name %>" Repeating="No" redcap:FormName="<%= form.name.underscore %>">
<ItemGroupRef ItemGroupOID="<%= form.name.gsub(' ', '_')%>.single_section" Mandatory="No"/>
</FormDef>

<% end %>
<% @survey.forms.each do |form| %>
<!-- item groups -->
<ItemGroupDef OID="<%= form.name.gsub(' ', '_') %>.single_section" Name="<%= form.name %>" Repeating="No">
<% form.form_questions.each do |fq| %>
<ItemRef ItemOID="question_<%=fq.id%>" Mandatory="No" redcap:Variable="<%=fq.program_var || "question_#{fq.id}"%>"/>
<% end %>
</ItemGroupDef>
<% end %>
<% form_questions = @survey.forms.collect {|form| form.form_questions}.flatten %>

<!-- item defs -->
<% form_questions.each do |fq| %>
<ItemDef OID="question_<%=fq.id%>" Name="question_<%=fq.id%>" DataType="text" Length="999" redcap:Variable="<%=fq.program_var || "question_#{fq.id}"%>" redcap:FieldType="<%= fq.response_set ? 'select' : 'text' %>" >
<Question><TranslatedText><%= fq.question.content %></TranslatedText></Question>
<% if fq.response_set %>
<CodeListRef CodeListOID="question_<%=fq.id%>.choices"/>
<% end %>
</ItemDef>
<% end %>



<!-- code list defs -->
<% form_questions.each do |fq| %>
<% if fq.response_set %>
<CodeList OID="question_<%=fq.id%>.choices" Name="question_<%=fq.id%>" DataType="text" redcap:Variable="<%=fq.program_var || "question_#{fq.id}"%>">
<% fq.response_set.responses.each do |response| %>
<CodeListItem CodedValue="<%= response.value %>"><Decode><TranslatedText><%= response.display_name.blank? ? response.value : response.display_name%></TranslatedText></Decode></CodeListItem>
<% end %>
</CodeList>
<% end %>
<% end %>
</MetaDataVersion>
</Study>
</ODM>
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
resources :surveys, except: [:edit], defaults: { format: :json } do
get :revise, on: :member
put :publish, on: :member
get :redcap, on: :member
end
resources :questions, except: [:edit] do
get :revise, on: :member
Expand Down
21 changes: 21 additions & 0 deletions test/controllers/survey_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -84,4 +84,25 @@ class SurveysControllerTest < ActionDispatch::IntegrationTest
put publish_survey_path(surveys(:four), format: :json, params: { survey: surveys(:four) })
assert_response :forbidden
end

test 'should get redcap export ' do
get redcap_survey_url(@survey)
assert response.headers['Content-Disposition'].index("filename=\"#{@survey.name.underscore}_redcap.xml\"")
validate_redcap(response.body)
assert_response :success
end

def validate_redcap(xml)
doc = Nokogiri::XML::Document.parse(xml)
doc.root.add_namespace_definition('odm', 'http://www.cdisc.org/ns/odm/v1.3')
doc.root.add_namespace_definition('ds', 'http://www.w3.org/2000/09/xmldsig#')
doc.root.add_namespace_definition('xsi', 'http://www.w3.org/2001/XMLSchema-instance')
doc.root.add_namespace_definition('redcap', 'https://projectredcap.org')
question_count = @survey.survey_forms.collect { |sf| sf.form.form_questions.count }.sum
assert doc
assert doc.xpath('//odm:FormDef').length == @survey.survey_forms.length
assert doc.xpath('//odm:ItemGroupDef').length == @survey.survey_forms.length
assert doc.xpath('//odm:ItemDef').length == question_count
assert doc.xpath('//odm:ItemRef').length == question_count
end
end

0 comments on commit 759c37a

Please sign in to comment.