diff --git a/app/controllers/plans_controller.rb b/app/controllers/plans_controller.rb index 589b7f8..39bf08f 100644 --- a/app/controllers/plans_controller.rb +++ b/app/controllers/plans_controller.rb @@ -246,9 +246,9 @@ file_name = @plan.title.gsub(/ /, "_") respond_to do |format| - format.html - format.csv { send_data @exported_plan.as_csv(@sections, @unanswered_question, @question_headings), filename: "#{file_name}.csv" } - format.text { send_data @exported_plan.as_txt(@sections, @unanswered_question, @question_headings, @show_details), filename: "#{file_name}.txt" } + format.html { render layout: false } + format.csv { send_data @plan.as_csv(@show_sections_questions), filename: "#{file_name}.csv" } + format.text { send_data render_to_string(partial: 'shared/export/plan_txt'), filename: "#{file_name}.txt" } format.docx { render docx: 'export', filename: "#{file_name}.docx" } format.pdf do render pdf: file_name, diff --git a/app/models/concerns/exportable_plan.rb b/app/models/concerns/exportable_plan.rb index d9a3a03..fe34e50 100644 --- a/app/models/concerns/exportable_plan.rb +++ b/app/models/concerns/exportable_plan.rb @@ -7,6 +7,38 @@ prepare(coversheet) end + def as_csv(headings = true, unanswered = true) + hash = prepare(false) + + CSV.generate do |csv| + hdrs = (hash[:phases].length > 1 ? [_('Phase')] : []) + if headings + hdrs << [_('Section'),_('Question'),_('Answer')] + else + csv << _('Answer') + end + + csv << hdrs.flatten + hash[:phases].each do |phase| + 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 + question_text = (question[:text].length > 1 ? question[:text].join(', ') : question[:text][0]) + flds << [ section[:title], sanitize_text(question_text), sanitize_text(answer_text) ] + else + flds << [ sanitize_text(answer_text) ] + end + + csv << flds.flatten + end + end + end + end + end + private def prepare(coversheet = false) hash = coversheet ? prepare_coversheet : {} @@ -86,5 +118,9 @@ end exported_plan.save end + + def sanitize_text(text) + if (!text.nil?) then ActionView::Base.full_sanitizer.sanitize(text.gsub(/ /i,"")) end + end end end \ No newline at end of file diff --git a/app/views/shared/export/_plan_txt.erb b/app/views/shared/export/_plan_txt.erb new file mode 100644 index 0000000..83eaaa4 --- /dev/null +++ b/app/views/shared/export/_plan_txt.erb @@ -0,0 +1,59 @@ +<%= "#{@plan.title}" %> +<%= "----------------------------------------------------------\n" %> +<% if @show_coversheet %> +<%= @hash[:attribution].length > 1 ? _("Creators: ") : _('Creator:') %> <%= @hash[:attribution].join(', ') %> +<%= _("Affiliation: ") + @hash[:affiliation] %> + <% if @hash[:funder].present? %> +<%= _("Template: ") + @hash[:funder] %> + <% else %> +<%= _("Template: ") + @hash[:template] + @hash[:customizer] %> + <% end %> + <% if @plan.grant_number.present? %> +<%= _("Grant number: ") + @plan.grant_number %> + <% end %> + <% if @plan.description.present? %> +<%= _("Project abstract: ") %> +<%= "\t" + @plan.description + "\n" %> + <% end %> +<%= _("Last modified: ") + l(@plan.updated_at.to_date, formats: :short) %> +<%= _("Copyright information:") %> +<%= "\t" + _(" The above plan creator(s) have agreed that others may use as much of the text of this plan as they would like in their own plans, and customise it as necessary. You do not need to credit the creator(s) as the source of the language used, but using any of the plan's text does not imply that the creator(s) endorse, or have any relationship to, your project or proposal") %> +<%= "----------------------------------------------------------\n" %> +<% end %> + +<% @hash[:phases].each do |phase| %> +<%= (@hash[:phases].length > 1 ? "#{phase[:title]}" : "") %> + <% phase[:sections].each do |section| %> + <% if @show_sections_questions %> +<%= "#{section[:title]}\n" %> + <% end %> + <% section[:questions].each do |question| %> + <% if @show_sections_questions %> + <%# text in this case is an array to accomodate for option_based %> + <% if question[:text].length > 1 %> + <% question[:text].each do |txt| %> +<%= txt + "\n" %> + <% end %> + <% else %> +<%= question[:text][0].gsub(/