diff --git a/app/controllers/plans_controller.rb b/app/controllers/plans_controller.rb index 2a5514e..3661701 100644 --- a/app/controllers/plans_controller.rb +++ b/app/controllers/plans_controller.rb @@ -162,19 +162,9 @@ plan, phase = Plan.load_for_phase(params[:id], params[:phase_id]) - readonly = !plan.editable_by?(current_user.id) - guidance_groups = GuidanceGroup.where(published: true, id: plan.guidance_group_ids) - # Since the answers have been pre-fetched through plan (see Plan.load_for_phase) - # we create a hash whose keys are question id and value is the answer associated - answers = plan.answers.reduce({}){ |m, a| m[a.question_id] = a; m } - render('/phases/edit', locals: { - base_template_org: phase.template.base_org, - plan: plan, phase: phase, readonly: readonly, - guidance_groups: guidance_groups, - answers: answers, - guidance_service: GuidanceService.new(plan) }) + render_phases_edit(plan, phase, guidance_groups) end # PUT /plans/1 @@ -196,13 +186,13 @@ format.json {render json: {code: 1, msg: success_message(_('plan'), _('saved'))}} else flash[:alert] = failed_update_error(@plan, _('plan')) - format.html { render action: "edit" } + format.html { render_phases_edit(@plan, @plan.phases.first, @plan.guidance_groups) } format.json {render json: {code: 0, msg: flash[:alert]}} end rescue Exception flash[:alert] = failed_update_error(@plan, _('plan')) - format.html { render action: "edit" } + format.html { render_phases_edit(@plan, @plan.phases.first, @plan.guidance_groups) } format.json {render json: {code: 0, msg: flash[:alert]}} end end @@ -263,9 +253,13 @@ @plan = Plan.includes(:answers).find(params[:id]) authorize @plan + @selected_phase = @plan.phases.find(params[:phase_id]) + @show_coversheet = params[:export][:project_details].present? @show_sections_questions = params[:export][:question_headings].present? @show_unanswered = params[:export][:unanswered_questions].present? + @show_custom_sections = params[:export][:custom_sections].present? + @public_plan = false @hash = @plan.as_pdf(@show_coversheet) @@ -275,7 +269,7 @@ respond_to do |format| format.html { render layout: false } - format.csv { send_data @plan.as_csv(@show_sections_questions), filename: "#{file_name}.csv" } + format.csv { send_data @plan.as_csv(@show_sections_questions, true, @selected_phase), 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 @@ -439,4 +433,23 @@ feedback_confirmation_default_message feedback_constant_to_text(text, current_user, @plan, current_user.org) end + + private + + # ============================ + # = Private instance methods = + # ============================ + + def render_phases_edit(plan, phase, guidance_groups) + readonly = !plan.editable_by?(current_user.id) + # Since the answers have been pre-fetched through plan (see Plan.load_for_phase) + # we create a hash whose keys are question id and value is the answer associated + answers = plan.answers.reduce({}){ |m, a| m[a.question_id] = a; m } + render('/phases/edit', locals: { + base_template_org: phase.template.base_org, + plan: plan, phase: phase, readonly: readonly, + guidance_groups: guidance_groups, + answers: answers, + guidance_service: GuidanceService.new(plan) }) + end end diff --git a/app/helpers/plans_helper.rb b/app/helpers/plans_helper.rb index b726508..b5b3b45 100644 --- a/app/helpers/plans_helper.rb +++ b/app/helpers/plans_helper.rb @@ -42,4 +42,9 @@ return _('Private: restricted to me and people I invite.') end end + + 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 + end end diff --git a/app/models/concerns/exportable_plan.rb b/app/models/concerns/exportable_plan.rb index 37af160..9748ca2 100644 --- a/app/models/concerns/exportable_plan.rb +++ b/app/models/concerns/exportable_plan.rb @@ -4,7 +4,7 @@ prepare(coversheet) end - def as_csv(headings = true, unanswered = true) + def as_csv(headings = true, unanswered = true, selected_phase = nil) hash = prepare(false) CSV.generate do |csv| @@ -17,23 +17,24 @@ 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 - if question[:text].is_a? String - question_text = question[:text] + 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]) + end + flds << [ section[:title], sanitize_text(question_text), sanitize_text(answer_text) ] else - question_text = (question[:text].length > 1 ? question[:text].join(', ') : question[:text][0]) + flds << [ sanitize_text(answer_text) ] end - flds << [ section[:title], sanitize_text(question_text), sanitize_text(answer_text) ] - else - flds << [ sanitize_text(answer_text) ] + csv << flds.flatten end - - csv << flds.flatten end end end @@ -56,7 +57,7 @@ template.phases.each do |phase| phs = { title: phase.title, number: phase.number, sections: [] } phase.sections.each do |section| - sctn = { title: section.title, number: section.number, questions: [] } + sctn = { title: section.title, number: section.number, questions: [], modifiable: section.modifiable } section.questions.each do |question| txt = question.text sctn[:questions] << { id: question.id, text: txt, format: question.question_format } diff --git a/app/views/shared/export/_plan.erb b/app/views/shared/export/_plan.erb index d6db153..1bda070 100644 --- a/app/views/shared/export/_plan.erb +++ b/app/views/shared/export/_plan.erb @@ -32,55 +32,59 @@ <% end %> <% @hash[:phases].each do |phase| %> -
- -

<%= (@hash[:phases].length > 1 ? "#{@plan.title} - #{phase[:title]}" : @plan.title) %>

-
- <% phase[:sections].each do |section| %> - <% if @show_sections_questions %> -

<%= section[:title] %>

- <% end %> + <%# Only render selected phase %> + <% if phase[:title] == @selected_phase.title %> +
+

<%= 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])) %> +

<%= section[:title] %>

+ <% end %> - <% section[:questions].each do |question| %> -
- <% if @show_sections_questions && !@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| %> +
+ <%# 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 %> - <%# 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.present?%> <% 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 %> + <% end %> <% end %> diff --git a/app/views/shared/export/_plan_txt.erb b/app/views/shared/export/_plan_txt.erb index 6b06132..b2a9fb8 100644 --- a/app/views/shared/export/_plan_txt.erb +++ b/app/views/shared/export/_plan_txt.erb @@ -22,13 +22,17 @@ <% end %> <% @hash[:phases].each do |phase| %> +<%# Only render selected phase %> +<% if phase[:title] == @selected_phase.title %> <%= (@hash[:phases].length > 1 ? "#{phase[:title]}" : "") %> <% phase[:sections].each do |section| %> - <% if @show_sections_questions %> + <%# 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[:title]}\n" %> <% end %> <% section[:questions].each do |question| %> - <% if @show_sections_questions %> + <%# 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])) %> <%# text in this case is an array to accomodate for option_based %> <% if question[:text].respond_to?(:each) %> <% question[:text].each do |txt| %> @@ -53,6 +57,6 @@ <% 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