diff --git a/app/controllers/plans_controller.rb b/app/controllers/plans_controller.rb index 3661701..22760a5 100644 --- a/app/controllers/plans_controller.rb +++ b/app/controllers/plans_controller.rb @@ -269,7 +269,7 @@ respond_to do |format| format.html { render layout: false } - format.csv { send_data @plan.as_csv(@show_sections_questions, true, @selected_phase), filename: "#{file_name}.csv" } + format.csv { send_data @plan.as_csv(@show_sections_questions, @show_unanswered, @selected_phase, @show_custom_sections), filename: "#{file_name}.csv" } format.text { send_data render_to_string(partial: 'shared/export/plan_txt'), filename: "#{file_name}.txt" } format.docx { render docx: "#{file_name}.docx", content: render_to_string(partial: 'shared/export/plan') } format.pdf do diff --git a/app/helpers/plans_helper.rb b/app/helpers/plans_helper.rb index b5b3b45..f761b11 100644 --- a/app/helpers/plans_helper.rb +++ b/app/helpers/plans_helper.rb @@ -45,6 +45,12 @@ def download_plan_page_title(plan, phase, hash) # If there is more than one phase show the plan title and phase title - return hash[:phases].length > 1 ? "#{plan.title} - #{phase[:title]}" : plan.title + return hash[:phases].many? ? "#{plan.title} - #{phase[:title]}" : plan.title + end + + def display_questions_and_section_headings(section, show_sections_questions, show_custom_sections) + # Return true if show_sections_questions is true and either section not customised, or section is customised + # and show_custom_sections is true + return show_sections_questions && (!section[:modifiable] || (show_custom_sections && section[:modifiable])) end end diff --git a/app/models/concerns/exportable_plan.rb b/app/models/concerns/exportable_plan.rb index 9748ca2..6a6fdde 100644 --- a/app/models/concerns/exportable_plan.rb +++ b/app/models/concerns/exportable_plan.rb @@ -4,11 +4,11 @@ prepare(coversheet) end - def as_csv(headings = true, unanswered = true, selected_phase = nil) + def as_csv(headings = true, unanswered = true, selected_phase = nil, show_custom_sections = true) hash = prepare(false) CSV.generate do |csv| - hdrs = (hash[:phases].length > 1 ? [_('Phase')] : []) + hdrs = (hash[:phases].many? ? [_('Phase')] : []) if headings hdrs << [_('Section'),_('Question'),_('Answer')] else @@ -19,21 +19,27 @@ hash[:phases].each do |phase| if selected_phase.nil? || phase[:title] == selected_phase.title phase[:sections].each do |section| - section[:questions].each do |question| - answer = self.answer(question[:id], false) - answer_text = answer.present? ? answer.text : (unanswered ? _('Not Answered') : '') - flds = (hash[:phases].length > 1 ? [phase[:title]] : []) - if headings - if question[:text].is_a? String - question_text = question[:text] - else - question_text = (question[:text].length > 1 ? question[:text].join(', ') : question[:text][0]) + # Return true if either section not customised, or section is customised + # and unanswered is true + if !section[:modifiable] || (show_custom_sections && section[:modifiable]) + section[:questions].each do |question| + answer = self.answer(question[:id], false) + if answer.present? || (answer.blank? && unanswered) + answer_text = answer.present? ? answer.text : (unanswered ? _('Not Answered') : '') + flds = (hash[:phases].many? ? [phase[:title]] : []) + if headings + if question[:text].is_a? String + question_text = question[:text] + else + question_text = (question[:text].many? ? question[:text].join(', ') : question[:text][0]) + end + flds << [ section[:title], sanitize_text(question_text), sanitize_text(answer_text) ] + else + flds << [ sanitize_text(answer_text) ] + end + csv << flds.flatten end - flds << [ section[:title], sanitize_text(question_text), sanitize_text(answer_text) ] - else - flds << [ sanitize_text(answer_text) ] end - csv << flds.flatten end end end diff --git a/app/views/shared/export/_plan.erb b/app/views/shared/export/_plan.erb index 1bda070..b830166 100644 --- a/app/views/shared/export/_plan.erb +++ b/app/views/shared/export/_plan.erb @@ -38,50 +38,48 @@

<%= download_plan_page_title(@plan, phase, @hash) %>


<% phase[:sections].each do |section| %> - <%# Only render if @show_sections_questions is true and either section not customised or section is customised and @show_custom_sections is true %> - <% if @show_sections_questions && (!section[:modifiable] || (@show_custom_sections && section[:modifiable])) %> + <% if display_questions_and_section_headings(section, @show_sections_questions, @show_custom_sections) %>

<%= section[:title] %>

- <% end %> - <% section[:questions].each do |question| %> -
- <%# Only render if @show_sections_questions is true and either section not customised or section is customised and @show_custom_sections is true %> - <% if @show_sections_questions && (!section[:modifiable] || (@show_custom_sections && section[:modifiable])) && !@public_plan %> -

<%= raw question[:text].gsub(/(\s||<\/td>| )*(<\/tr>|)/,"") if question[:text].present?%>

-
- <% end %> - <% answer = @plan.answer(question[:id], false) %> - <% blank = answer.present? ? answer.is_blank? : true %> - <% options = answer.present? ? answer.question_options : [] %> - <%# case where question has not been answered sufficiently to display%> - <% if @show_unanswered && (answer.blank? || (options.blank? && blank))%> -

<%= _('Question not answered.') -%>

- <% else %> - <%# case where Question has options %> - <% if options.present?%> - + <% section[:questions].each do |question| %> +
+ <% if !@public_plan %> +

<%= raw question[:text].gsub(/(\s||<\/td>| )*(<\/tr>|)/,"") if question[:text].present?%>

+
<% end %> - <%# case for RDA answer display %> - <% if question[:format].rda_metadata? && !blank %> - <% ah = answer.answer_hash %> - <% if ah['standards'].present? %> + <% answer = @plan.answer(question[:id], false) %> + <% blank = answer.present? ? answer.is_blank? : true %> + <% options = answer.present? ? answer.question_options : [] %> + <%# case where question has not been answered sufficiently to display%> + <% if @show_unanswered && (answer.blank? || (options.blank? && blank))%> +

<%= _('Question not answered.') -%>

+ <% else %> + <%# case where Question has options %> + <% if options.any? %> <% end %> -

<%= raw ah['text'] %>

- <%# case for displaying comments OR text %> - <% elsif !blank %> -

<%= raw answer.text %>

+ <%# case for RDA answer display %> + <% if question[:format].rda_metadata? && !blank %> + <% ah = answer.answer_hash %> + <% if ah['standards'].present? %> + + <% end %> +

<%= raw ah['text'] %>

+ <%# case for displaying comments OR text %> + <% elsif !blank %> +

<%= raw answer.text %>

+ <% end %> <% end %> - <% end %> -
+
+ <% end %> <% end %> <% end %> <% end %> diff --git a/app/views/shared/export/_plan_txt.erb b/app/views/shared/export/_plan_txt.erb index b2a9fb8..d2be1c2 100644 --- a/app/views/shared/export/_plan_txt.erb +++ b/app/views/shared/export/_plan_txt.erb @@ -24,15 +24,12 @@ <% @hash[:phases].each do |phase| %> <%# Only render selected phase %> <% if phase[:title] == @selected_phase.title %> -<%= (@hash[:phases].length > 1 ? "#{phase[:title]}" : "") %> + <%= (@hash[:phases].length > 1 ? "#{phase[:title]}" : "") %> <% phase[:sections].each do |section| %> - <%# Only render if @show_sections_questions is true and either section not customised or section is customised and @show_custom_sections is true %> - <% if @show_sections_questions && (!section[:modifiable] || (@show_custom_sections && section[:modifiable])) %> + <% if display_questions_and_section_headings(section, @show_sections_questions, @show_custom_sections) %> <%= "#{section[:title]}\n" %> - <% end %> - <% section[:questions].each do |question| %> - <%# Only render if @show_sections_questions is true and either section not customised or section is customised and @show_custom_sections is true %> - <% if @show_sections_questions && (!section[:modifiable] || (@show_custom_sections && section[:modifiable])) %> + + <% section[:questions].each do |question| %> <%# text in this case is an array to accomodate for option_based %> <% if question[:text].respond_to?(:each) %> <% question[:text].each do |txt| %> @@ -41,22 +38,22 @@ <% else %> <%= "#{strip_tags(question[:text][0].gsub(/(\s||<\/td>| )*(<\/tr>|)/,""))}\n" if question[:text].present? && question[:text][0].present? %> <% end %> - <% end %> - <% answer = @plan.answer(question[:id], false) %> - <% blank = (answer.present? && answer.is_valid?) ? answer.text.gsub(/<\/?p>/, '').gsub(//, '\n').chomp.blank? : true %> - <% if blank && @show_unanswered %> + <% answer = @plan.answer(question[:id], false) %> + <% blank = (answer.present? && answer.is_valid?) ? answer.text.gsub(/<\/?p>/, '').gsub(//, '\n').chomp.blank? : true %> + <% if blank && @show_unanswered %> <%= " #{_("Question not answered.")}\n\n" %> - <% elsif !blank %> - <% if answer.question_options.length > 0 %> - <% answer.question_options.each do |opt| %> + <% elsif !blank %> + <% if answer.question_options.length > 0 %> + <% answer.question_options.each do |opt| %> <%= " #{opt.text}\n" %> + <% end %> + <% end %> +<%= " #{strip_tags(answer.text.gsub(/<\/?p>/, '').gsub(//, '\n').chomp)}\n\n" if answer.text.present? %> <% end %> <% end %> -<%= " #{strip_tags(answer.text.gsub(/<\/?p>/, '').gsub(//, '\n').chomp)}\n\n" if answer.text.present? %> <% end %> <% end %> <% end %> <% end %> -<% end %> <%= "----------------------------------------------------------" %> <%= _("A Data Management Plan created using %{application_name}") % { application_name: Rails.configuration.branding[:application][:name] } %> \ No newline at end of file