diff --git a/app/controllers/answers_controller.rb b/app/controllers/answers_controller.rb index 9c9f70e..abc63af 100644 --- a/app/controllers/answers_controller.rb +++ b/app/controllers/answers_controller.rb @@ -58,6 +58,7 @@ }).find(p_params[:plan_id]) @question = @answer.question @section = @plan.get_section(@question.section_id) + template = @section.phase.template render json: { "question" => { @@ -66,7 +67,7 @@ "locking" => @stale_answer ? render_to_string(partial: 'answers/locking', locals: { question: @question, answer: @stale_answer, user: @answer.user }, formats: [:html]) : nil, - "form" => render_to_string(partial: 'answers/new_edit', locals: { question: @question, answer: @answer, readonly: false, locking: false }, formats: [:html]), + "form" => render_to_string(partial: 'answers/new_edit', locals: { template: template, question: @question, answer: @answer, readonly: false, locking: false, base_template_org: template.base_org }, formats: [:html]), "answer_status" => render_to_string(partial: 'answers/status', locals: { answer: @answer}, formats: [:html]) }, "section" => { diff --git a/app/controllers/org_admin/templates_controller.rb b/app/controllers/org_admin/templates_controller.rb index 158016e..7440e04 100644 --- a/app/controllers/org_admin/templates_controller.rb +++ b/app/controllers/org_admin/templates_controller.rb @@ -341,7 +341,7 @@ template.save flash[:notice] = _("Your #{template.template_type} has been published and is now available to users.") - redirect_to "#{org_admin_templates_path}#{template.template_type == 'customisation' ? '#funder-templates' : '#organisation-templates'}" + redirect_to "#{org_admin_templates_path}#{template.template_type == _('customisation') ? '#funder-templates' : '#organisation-templates'}" end end @@ -359,7 +359,7 @@ flash[:notice] = _("Your #{template.template_type} is no longer published. Users will not be able to create new DMPs for this #{template.template_type} until you re-publish it") end - redirect_to "#{org_admin_templates_path}#{template.template_type == 'customisation' ? '#funder-templates' : '#organisation-templates'}" + redirect_to "#{org_admin_templates_path}#{template.template_type == _('customisation') ? '#funder-templates' : '#organisation-templates'}" end # PUT /org_admin/template_options (AJAX) diff --git a/app/controllers/phases_controller.rb b/app/controllers/phases_controller.rb index 14e3210..f60cec0 100644 --- a/app/controllers/phases_controller.rb +++ b/app/controllers/phases_controller.rb @@ -21,6 +21,7 @@ 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, question_guidance: plan.guidance_by_question_as_hash, guidance_groups: guidance_groups, @@ -80,6 +81,7 @@ authorize @phase @template = @phase.template @current_tab = params[:r] || 'all-templates' + @base_template_org = @phase.template.base_org end diff --git a/app/controllers/questions_controller.rb b/app/controllers/questions_controller.rb index 292d970..fbbd17f 100644 --- a/app/controllers/questions_controller.rb +++ b/app/controllers/questions_controller.rb @@ -65,7 +65,7 @@ # The user cleared out the guidance value so delete the record guidance.destroy! if guidance.present? end - example_answer = @question.get_example_answer(current_user.org_id) + example_answer = @question.get_example_answers(current_user.org_id).first if params["question"]["annotations_attributes"].present? && params["question"]["annotations_attributes"]["0"]["id"].present? unless example_answer.present? example_answer = Annotation.new(type: :example_answer, org_id: current_user.org_id, question_id: @question.id) diff --git a/app/models/question.rb b/app/models/question.rb index b92355d..8ca6c34 100644 --- a/app/models/question.rb +++ b/app/models/question.rb @@ -108,17 +108,17 @@ ## # get example answer belonging to the currents user for this question # - # @param org_id [Integer] the id for the organisation - # @return [String] the example answer for this question for the specified org - def get_example_answer(org_id) - example_answer = self.annotations.where(org_id: org_id).where(type: Annotation.types[:example_answer]).order(:created_at) - return example_answer.first + # @param org_ids [Array] the ids for the organisations + # @return [Array] the example answers for this question for the specified orgs + def get_example_answers(org_ids) + org_ids = [org_ids] unless org_ids.is_a?(Array) + self.annotations.where(org_id: [org_ids], type: Annotation.types[:example_answer]).order(:created_at) end def first_example_answer self.annotations.where(type: Annotation.types[:example_answer]).order(:created_at).first end - + ## # get guidance belonging to the current user's org for this question(need org # to distinguish customizations) diff --git a/app/models/template.rb b/app/models/template.rb index 21e5b9c..0cd9aae 100644 --- a/app/models/template.rb +++ b/app/models/template.rb @@ -70,12 +70,15 @@ } # Retrieves the maximum version for the array of customization_ofs passed. If customization_ofs is missing, every maximum # version for each different customization_of will be retrieved - scope :customization_ofs_with_max_version, -> (customization_ofs=nil) { - if customization_ofs.is_a?(Array) - select("MAX(version) AS version", :customization_of).where(customization_of: customization_ofs).group(:customization_of) - else - select("MAX(version) AS version", :customization_of).group(:customization_of) + scope :customization_ofs_with_max_version, -> (customization_ofs=nil, org_id=nil) { + chained_scope = select("MAX(version) AS version", :customization_of) + if customization_ofs.respond_to?(:each) + chained_scope = chained_scope.where(customization_of: customization_ofs) end + if org_id.present? + chained_scope = chained_scope.where(org_id: org_id) + end + chained_scope.group(:customization_of) } # Retrieves the latest template version, i.e. the one with maximum version for each dmptemplate_id scope :latest_version, -> (dmptemplate_ids=nil) { @@ -85,7 +88,7 @@ } # Retrieves the latest customized version, i.e. the one with maximum version for each customization_of=dmptemplate_id scope :latest_customization, -> (org_id, dmptemplate_ids=nil) { - from(customization_ofs_with_max_version(dmptemplate_ids), :current) + from(customization_ofs_with_max_version(dmptemplate_ids, org_id), :current) .joins("INNER JOIN templates ON current.version = templates.version"\ " AND current.customization_of = templates.customization_of") .where('templates.org_id = ?', org_id) @@ -259,6 +262,16 @@ self.customization_of.present? ? _('customisation') : _('template') end + # Retrieves the template's org or the org of the template this one is derived + # from of it is a customization + def base_org + if self.customization_of.present? + base_template_org = Template.where(dmptemplate_id: self.customization_of).first.org + else + base_template_org = self.org + end + end + # -------------------------------------------------------- private # Initialize the published and dirty flags for new templates @@ -268,11 +281,11 @@ self.published = false self.migrated = false self.dirty = false - self.visibility = 0 # Organisationally visible by default self.is_default = false if self.is_default.nil? self.version = 0 if self.version.nil? - self.visibility = Template.visibilities[:organisationally_visible] if self.visibility.nil? - + # Organisationally visible by default unless Org is only a funder + self.visibility = (self.org.present? && self.org.funder_only?) ? Template.visibilities[:publicly_visible] : Template.visibilities[:organisationally_visible] + # Generate a unique identifier for the dmptemplate_id if necessary if self.dmptemplate_id.nil? self.dmptemplate_id = loop do diff --git a/app/views/annotations/_show.html.erb b/app/views/annotations/_show.html.erb index b5085f4..8d52d14 100644 --- a/app/views/annotations/_show.html.erb +++ b/app/views/annotations/_show.html.erb @@ -1,13 +1,14 @@ -<% org = template.org.abbreviation.present? ? template.org.abbreviation : template.org.name %> <% if example_answer.present? || guidance.present? %> > <% if example_answer.present? %> -
<%= _('Example answer') %>
+ <% label = (for_plan && template.customization_of.present?) ? _('%{org} Example Answer') % { org: example_answer.org.short_name } : _('Example Answer') %> +
<%= label %>
<%= raw example_answer.text %>
<% end %> <% if guidance.present? %> -
<%= _('Guidance') %>
+ <% label = (for_plan && template.customization_of.present?) ? _('%{org} Guidance') % { org: guidance.org.short_name } : _('Guidance') %> +
<%= label %>
<%= raw guidance.text %>
<% end %> -<% end %> +<% end %> \ No newline at end of file diff --git a/app/views/answers/_locking.html.erb b/app/views/answers/_locking.html.erb index e47642b..5018ff9 100644 --- a/app/views/answers/_locking.html.erb +++ b/app/views/answers/_locking.html.erb @@ -1,5 +1,6 @@

<%= _('The following answer cannot be saved') %>

- <%= render partial: '/answers/new_edit', locals: { question: question, answer: answer, readonly: true, locking: true } %> + <%# We do not need to re-show example answers in this lock conflict section so leave template nil %> + <%= render partial: '/answers/new_edit', locals: { template: nil, question: question, answer: answer, readonly: true, locking: true } %>

<%= _('since %{name} saved the answer below while you were editing. Please, combine your changes and then save the answer again.') % { name: user.name} %>

\ No newline at end of file diff --git a/app/views/answers/_new_edit.html.erb b/app/views/answers/_new_edit.html.erb index 14e1513..64688ce 100644 --- a/app/views/answers/_new_edit.html.erb +++ b/app/views/answers/_new_edit.html.erb @@ -1,4 +1,4 @@ -<%# locals: { question, answer, readonly, locking } %> +<%# locals: { template, question, answer, readonly, locking } %> @@ -64,15 +64,18 @@ <%= f.button(_('Save'), class: "btn btn-default", type: "submit") %> - <% annotation = question.first_example_answer %> - <% if annotation.present? && annotation.text.present? %> -
- - <%="#{annotation.org.abbreviation} "%> <%=_('example answer')%> - -
- <%= raw annotation.text %> -
-
+ <% if template.present? && template.org.present? %> + <% question.get_example_answers([base_template_org.id, template.org.id]).each do |annotation| %> + <% if annotation.present? && annotation.org.present? && annotation.text.present? %> +
+ + <%="#{annotation.org.abbreviation} "%> <%=_('example answer')%> + +
+ <%= raw annotation.text %> +
+
+ <% end %> + <% end %> <% end %> <% end %> diff --git a/app/views/phases/_edit_plan_answers.html.erb b/app/views/phases/_edit_plan_answers.html.erb index 1c746c2..bad5182 100644 --- a/app/views/phases/_edit_plan_answers.html.erb +++ b/app/views/phases/_edit_plan_answers.html.erb @@ -63,7 +63,7 @@
" class="answer-locking">
" class="answer-form"> - <%= render(partial: '/answers/new_edit', locals: { question: question, answer: answer, readonly: readonly, locking: false }) %> + <%= render(partial: '/answers/new_edit', locals: { template: phase.template, question: question, answer: answer, readonly: readonly, locking: false, base_template_org: base_template_org }) %>
" class="mt-10"> <%= render(partial: '/answers/status', locals: { answer: answer }) %> @@ -72,7 +72,7 @@
- <%= render partial: '/phases/guidances_notes', locals: { plan: plan, template: phase.template, question: question, answer: answer, question_guidance: question_guidance, guidance_groups: guidance_groups } %> + <%= render partial: '/phases/guidances_notes', locals: { plan: plan, template: phase.template, question: question, answer: answer, question_guidance: question_guidance, guidance_groups: guidance_groups, base_template_org: base_template_org } %>
<%= raw('
') if i != section.questions.length - 1 %> diff --git a/app/views/phases/_guidances_notes.html.erb b/app/views/phases/_guidances_notes.html.erb index b23c6ea..7c89413 100644 --- a/app/views/phases/_guidances_notes.html.erb +++ b/app/views/phases/_guidances_notes.html.erb @@ -29,7 +29,7 @@ <% end %> diff --git a/app/views/phases/admin_preview.html.erb b/app/views/phases/admin_preview.html.erb index 8619c4c..338caeb 100644 --- a/app/views/phases/admin_preview.html.erb +++ b/app/views/phases/admin_preview.html.erb @@ -18,7 +18,7 @@
- <%= render partial: '/phases/edit_plan_answers', locals: { plan: nil, phase: @phase, readonly: true, question_guidance: {}, edit: false, guidance_groups: [] } %> + <%= render partial: '/phases/edit_plan_answers', locals: { plan: nil, phase: @phase, readonly: true, question_guidance: {}, edit: false, guidance_groups: [], base_template_org: @base_template_org } %>
diff --git a/app/views/questions/_edit_question.html.erb b/app/views/questions/_edit_question.html.erb index 3de20c4..8d452db 100644 --- a/app/views/questions/_edit_question.html.erb +++ b/app/views/questions/_edit_question.html.erb @@ -66,7 +66,7 @@
<%= f.label(:example_answer, _('Example Answer'), class: "control-label") %>
- <% example_answer = question.get_example_answer(current_user.org.id) %> + <% example_answer = question.get_example_answers(current_user.org.id).first %> <% if example_answer.nil? %> <% example_answer = question.annotations.build %> <% example_answer.type = :example_answer %> diff --git a/app/views/questions/_show_question.html.erb b/app/views/questions/_show_question.html.erb index 96bbb48..8a5cc30 100644 --- a/app/views/questions/_show_question.html.erb +++ b/app/views/questions/_show_question.html.erb @@ -36,7 +36,7 @@ <% if !question.section.phase.template.org.funder? %> - <% example_answer = question.get_example_answer(@original_org.id) %> + <% example_answer = question.get_example_answers(@original_org.id).first %> <% if example_answer.present? && example_answer.text.present? %>
<%= _('example answer')%>
<%= raw example_answer.text %>
@@ -60,7 +60,7 @@
- <% example_answer = question.get_example_answer(current_user.org_id) %> + <% example_answer = question.get_example_answers(current_user.org_id).first %> <% guidance = question.get_guidance_annotation(current_user.org_id) %> <% editing = (example_answer.present? || guidance.present?) %> <% if !question.modifiable %> diff --git a/test/unit/question_test.rb b/test/unit/question_test.rb index 9cd95b2..706a10d 100644 --- a/test/unit/question_test.rb +++ b/test/unit/question_test.rb @@ -64,7 +64,7 @@ assert_equal 'Test 2', @question.annotations.where(org_id: Org.first.id).first.text, "expected the correct annotation" org = Org.create(name: 'New One', links: {"org":[]}) - assert_equal nil, @question.get_example_answer(org.id), "expected no annotation for a new org" + assert_equal 0, @question.get_example_answers(org.id).length, "expected no annotation for a new org" end # ---------------------------------------------------