diff --git a/app/controllers/plans_controller.rb b/app/controllers/plans_controller.rb index 1600e12..f98ff45 100644 --- a/app/controllers/plans_controller.rb +++ b/app/controllers/plans_controller.rb @@ -226,6 +226,8 @@ def show_export @plan = Plan.find(params[:id]) authorize @plan + @phase_options = @plan.phases.order(:number).pluck(:title,:id) + @export_settings = @plan.settings(:export) render 'show_export' end @@ -235,9 +237,8 @@ @plan = Plan.find(params[:id]) authorize @plan - # If no format is specified, default to PDF - params[:format] = 'pdf' if params[:format].nil? - + # We should re-work this into something more useful than creating a new one + # every time a plan gets exported @exported_plan = ExportedPlan.new.tap do |ep| ep.plan = @plan ep.phase_id = params[:phase_id] @@ -250,14 +251,23 @@ end end + # setup some variables we will need in the export views + # here, if custom sections are included, we want all sections, otherwise, + # we only want those which are not modifiable, as they are the original template + @sections = params[:export][:custom_sections].present? || @plan.template.customization_of.nil? ? @exported_plan.sections.order(:number) : Phase.find(params[:phase_id]).sections.where(modifiable: false) # prefetch questions? + @unanswered_questions = params[:export][:unanswered_questions].present? + @question_headings = params[:export][:question_headings].present? + @show_details = params[:export][:project_details].present? + + begin @exported_plan.save! file_name = @exported_plan.settings(:export)[:value]['title'].gsub(/ /, "_") respond_to do |format| format.html - format.csv { send_data @exported_plan.as_csv, filename: "#{file_name}.csv" } - format.text { send_data @exported_plan.as_txt, filename: "#{file_name}.txt" } + 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.docx { render docx: 'export', filename: "#{file_name}.docx" } format.pdf do @formatting = @plan.settings(:export).formatting @@ -272,6 +282,7 @@ end end rescue ActiveRecord::RecordInvalid => e + @phase_options = @plan.phases.order(:number).pluck(:title,:id) redirect_to show_export_plan_path(@plan), alert: _('%{format} is not a valid exporting format. Available formats to export are %{available_formats}.') % {format: params[:format], available_formats: ExportedPlan::VALID_FORMATS.to_s} end @@ -318,6 +329,7 @@ end end rescue ActiveRecord::RecordInvalid => e + @phase_options = @plan.phases.order(:number).pluck(:title,:id) redirect_to show_export_plan_path(@plan), alert: _('Unable to download the DMP at this time.') end end diff --git a/app/controllers/settings/plans_controller.rb b/app/controllers/settings/plans_controller.rb index c406401..07b2789 100644 --- a/app/controllers/settings/plans_controller.rb +++ b/app/controllers/settings/plans_controller.rb @@ -36,6 +36,7 @@ flash[:alert] = _('An error has occurred while saving/resetting your export settings.') end respond_to do |format| + @phase_options = @plan.phases.order(:number).pluck(:title,:id) format.html { redirect_to(show_export_plan_path(@plan.id)) } # format.json { render json: settings_json } end diff --git a/app/models/exported_plan.rb b/app/models/exported_plan.rb index 0bd9ef1..c14979f 100644 --- a/app/models/exported_plan.rb +++ b/app/models/exported_plan.rb @@ -99,24 +99,40 @@ # Export formats - def as_csv + def as_csv(sections, unanswered_questions, question_headings) CSV.generate do |csv| - csv << [_('Section'),_('Question'),_('Answer'),_('Selected option(s)'),_('Answered by'),_('Answered at')] - self.sections.each do |section| - questions = self.questions_for_section(section) - if questions.present? - questions.each do |question| - answer = self.plan.answer(question.id) - q_format = question.question_format - if q_format.option_based? - options_string = answer.question_options.collect {|o| o.text}.join('; ') - else - options_string = '' - end + if question_headings + csv << [_('Section'),_('Question'),_('Answer'),_('Selected option(s)'),_('Answered by'),_('Answered at')] + else + csv << [_('Section'),_('Answer'),_('Selected option(s)'),_('Answered by'),_('Answered at')] + end + sections.each do |section| + section.questions.each do |question| + answer = Answer.where(plan_id: self.plan_id, question_id: question.id).first + # skip unansewered questions + if answer.blank? && !unanswered_questions + next + end + answer_text = answer.present? ? answer.text : '' + q_format = question.question_format + if q_format.option_based? + options_string = answer.question_options.collect {|o| o.text}.join('; ') + else + options_string = '' + end + if question_headings csv << [ section.title, sanitize_text(question.text), - question.option_comment_display ? sanitize_text(answer.text) : '', + question.option_comment_display ? sanitize_text(answer_text) : '', + options_string, + user.name, + answer.updated_at + ] + else + csv << [ + section.title, + question.option_comment_display ? sanitize_text(answer_text) : '', options_string, user.name, answer.updated_at @@ -127,40 +143,42 @@ end end - def as_txt + def as_txt(sections, unanswered_questions, question_headings, details) output = "#{self.plan.title}\n\n#{self.plan.template.title}\n" output += "\n"+_('Details')+"\n\n" - - self.admin_details.each do |at| - value = self.send(at) - if value.present? - output += admin_field_t(at.to_s) + ": " + value + "\n" - else - output += admin_field_t(at.to_s) + ": " + _('-') + "\n" - end + if details + self.admin_details.each do |at| + value = self.send(at) + if value.present? + output += admin_field_t(at.to_s) + ": " + value + "\n" + else + output += admin_field_t(at.to_s) + ": " + _('-') + "\n" + end + end end - self.sections.each do |section| - questions = self.questions_for_section(section) - if questions.present? - output += "\n#{section.title}\n" - questions.each do |question| + sections.each do |section| + output += "\n#{section.title}\n" + section.questions.each do |question| + answer = self.plan.answer(question.id, false) + #skip if question un-answered + if answer.nil? && !unanswered_questions then next end + + if question_headings qtext = sanitize_text( question.text.gsub(/
<%= _('Question not answered') %>
- <% else %> - <% q_format = question.question_format %> - <% if q_format.option_based? %> -<%= _('Question not answered') %>
+ <% else %> + <% q_format = question.question_format %> + <% if q_format.option_based? %> +| <%= _('Title')%> | -<%= _('Description')%> | -
|---|---|
- <%= admin_field_t(field.to_s) -%> |
- <%= value.present? ? value : _('-') %> | -
| <%= _('Questions')%> | -<%= _('Answers')%> | -
|---|---|
|
- - <%= raw question.text %> - |
-
- <% answer = @plan.answer(question.id, false) %>
- <% if answer.nil? %>
- <%= _('Question not answered') %> - <% else %> - <% q_format = question.question_format %> - <% if q_format.option_based? %> -
|
-
| <%= _('Title')%> | +<%= _('Description')%> | +
|---|---|
- <%= admin_field_t(field.to_s) -%> |
+ <%= value.present? ? value : _('-') %> | +
| <%= _('Questions')%> | + <% end %> +<%= _('Answers')%> | +
|---|---|
|
+ - <%= raw question.text %> + |
+ <% end %>
+
+ <% if answer.nil? %>
+ <%= _('Question not answered') %> + <% else %> + <% q_format = question.question_format %> + <% if q_format.option_based? %> +
|
+
<%= admin_field_t(field.to_s) -%> <%= value -%>
- <% else %> -<%= admin_field_t(field.to_s) -%> <%= _('-') %>
+ <% if @show_details %> + <% @exported_plan.admin_details.each do |field| + value = @exported_plan.send(field) + if value.present? %> +<%= admin_field_t(field.to_s) -%> <%= value -%>
+ <% else %> +<%= admin_field_t(field.to_s) -%> <%= _('-') %>
+ <% end %> <% end %> <% end %> - <% @exported_plan.sections.each do |section| %> - <% questions = @exported_plan.questions_for_section(section.id) - if questions.present? - %> -<%= _('Question not answered.') -%>
- <% else %> - <% q_format = question.question_format %> - <% if q_format.option_based? %> -<%= _('Question not answered.') -%>
+ <% else %> + <% q_format = question.question_format %> + <% if q_format.option_based? %> +