diff --git a/app/controllers/templates_controller.rb b/app/controllers/templates_controller.rb index 9108ba9..205b96c 100644 --- a/app/controllers/templates_controller.rb +++ b/app/controllers/templates_controller.rb @@ -52,7 +52,7 @@ # GET /dmptemplates/1 def admin_template @template = Template.includes(:org, phases: [sections: [questions: [:question_options, :question_format, - :suggested_answers]]]).find(params[:id]) + :annotations]]]).find(params[:id]) # check to see if this is a funder template needing customized authorize @template @@ -75,7 +75,7 @@ end end end - customizations = Template.includes(:org, phases: [sections: [questions: :suggested_answers ]]).where(org_id: current_user.org_id, customization_of: @template.dmptemplate_id).order(version: :desc) + customizations = Template.includes(:org, phases: [sections: [questions: :annotations ]]).where(org_id: current_user.org_id, customization_of: @template.dmptemplate_id).order(version: :desc) if customizations.present? # existing customization to port over max_version = customizations.first @@ -105,7 +105,7 @@ section_copy.save! else # not a customized section, iterate over questions - customization_phase = new_customization.phases.includes(sections: [questions: :suggested_answers]).where(number: phase.number).first + customization_phase = new_customization.phases.includes(sections: [questions: :annotations]).where(number: phase.number).first customization_section = customization_phase.sections.where(number: section.number).first section.questions.each do |question| # find corresponding question in new template diff --git a/app/models/annotation.rb b/app/models/annotation.rb index c6c79b7..66b58e6 100644 --- a/app/models/annotation.rb +++ b/app/models/annotation.rb @@ -1,5 +1,5 @@ class Annotation < ActiveRecord::Base - enum type: [ :guidance, :example_answer,] + enum type: [ :guidance, :example_answer] ## # Associations belongs_to :org diff --git a/app/models/question.rb b/app/models/question.rb index f5eabfc..93ae151 100644 --- a/app/models/question.rb +++ b/app/models/question.rb @@ -103,13 +103,24 @@ end ## - # get suggested answer belonging to the currents user for this question + # get example answer belonging to the currents user for this question # # @param org_id [Integer] the id for the organisation - # @return [String] the suggested_answer for this question for the specified organisation - def get_suggested_answer(org_id) - suggested_answer = Annotaion.find_by(org_id: org_id).where("type <> ?", Annotation.types[:example_answer]) - return suggested_answer + # @return [String] the example answer for this question for the specified org + def get_example_answer(org_id) + example_answer = Annotation.where(org_id: org_id).where("type <> ?", Annotation.types[:example_answer]) + return example_answer.first end + ## + # get guidance belonging to the current user's org for this question(need org + # to distinguish customizations) + # + # @param org_id [Integer] the id for the organisation + # @return [String] the annotation guidance for this question for the specified org + def get_guidance_annotation(org_id) + guidance = Annotation.where(org_id: org_id).where("type <> ?", Annotation.types[:guidance]) + return guidance.first + end + end diff --git a/app/models/template.rb b/app/models/template.rb index de44b10..0a78b22 100644 --- a/app/models/template.rb +++ b/app/models/template.rb @@ -72,10 +72,10 @@ section.questions.each do |question| phases[phase.number][:sections][section.number][:questions][question.number] = {} phases[phase.number][:sections][section.number][:questions][question.number][:data] = question - phases[phase.number][:sections][section.number][:questions][question.number][:suggested_answers] = {} - question.suggested_answers.each do |suggested_answer| - phases[phase.number][:sections][section.number][:questions][question.number][:suggested_answers][suggested_answer.id] = {} - phases[phase.number][:sections][section.number][:questions][question.number][:suggested_answers][suggested_answer.id][:data] = suggested_answer + phases[phase.number][:sections][section.number][:questions][question.number][:annotations] = {} + question.annotations.each do |annotation| + phases[phase.number][:sections][section.number][:questions][question.number][:annotations][annotation.id] = {} + phases[phase.number][:sections][section.number][:questions][question.number][:annotations][annotation.id][:data] = annotation end phases[phase.number][:sections][section.number][:questions][question.number][:question_options] = {} question.question_options.each do |question_option| diff --git a/app/views/questions/_add_question.html.erb b/app/views/questions/_add_question.html.erb index cbd0207..4dac18c 100644 --- a/app/views/questions/_add_question.html.erb +++ b/app/views/questions/_add_question.html.erb @@ -114,20 +114,20 @@ - <%= _('Suggested answer/ Example')%> + <%= _('Example Answer')%>
- <% suggested_answer = @new_question.suggested_answers.build %> - <%= f.fields_for :suggested_answers, suggested_answer do |s|%> + <% example_answer = @new_question.annotations.build %> + <% example_answer.type = :example_answer %> + <%= f.fields_for :annotations, example_answer do |s|%> <%= s.hidden_field :org_id, value: current_user.org_id %> <% end %>
- <%= link_to( image_tag("help_button.png"), "#", class: "suggested_answer_popover", rel: "popover", "data-html" => "true", "data-content" => _('You can add an example or suggested answer to help users respond. These will be presented above the answer box and can be copied/ pasted.'))%> + <%= link_to( image_tag("help_button.png"), "#", class: "suggested_answer_popover", rel: "popover", "data-html" => "true", "data-content" => _('You can add an example answer to help users respond. These will be presented above the answer box and can be copied/ pasted.'))%>
diff --git a/app/views/questions/_edit_question.html.erb b/app/views/questions/_edit_question.html.erb index d416548..4d2d72d 100644 --- a/app/views/questions/_edit_question.html.erb +++ b/app/views/questions/_edit_question.html.erb @@ -106,16 +106,16 @@ - <%= _('Suggested answer/ Example')%> + <%= _('Example Answer')%>
- <% suggested_answer = question.suggested_answers.find_by_org_id(current_user.org.id) %> - <% if suggested_answer.nil? %> - <% suggested_answer = question.suggested_answers.build %> + <% example_answer = question.get_example_answer(current_user.org.id) %> + <% if example_answer.nil? %> + <% example_answer = question.annotaions.build %> + <% example_answer.type = :example_answer %> <% end %> - <%= f.fields_for :suggested_answers, suggested_answer do |s|%> + <%= f.fields_for :annotations, example_answer do |s|%> <%= s.hidden_field :org_id, value: current_user.org.id %> diff --git a/app/views/questions/_show_question.html.erb b/app/views/questions/_show_question.html.erb index 687600e..c0fce59 100644 --- a/app/views/questions/_show_question.html.erb +++ b/app/views/questions/_show_question.html.erb @@ -63,25 +63,22 @@ <% if !question.section.phase.template.org.funder? %> - <% suggested_answer = question.get_suggested_answer(current_user.org_id) %> - <% if !suggested_answer.nil? && suggested_answer.text != "" %> + <% example_answer = question.get_example_answer(current_user.org_id) %> + <% if example_answer.present? && example_answer.text.present? %> - <% if suggested_answer.is_example? %> - <%= _('Example of answer')%> - <% else %> - <%= _('Suggested answer')%> - <% end %> + <%= _('Example of answer')%> - <%= raw suggested_answer.text %> + <%= raw example_answer.text %> <% end %> <% end %> - <% if !question.guidance.nil? %> + <% guidance = question.get_guidance_annotation(current_user.org_id) %> + <% if guidance.present? %> <%= _('Guidance')%> - <%= raw question.guidance %> + <%= raw guidance.text %> <% end %> @@ -104,17 +101,17 @@
<% if !question.modifiable %> - <% suggested_answer = question.get_suggested_answer(current_user.org_id) %> - <% if !suggested_answer.nil? && suggested_answer.text != "" %> + <% example_answer = question.get_example_answer(current_user.org_id) %> + <% if example_answer.present? && example_answer.text.present? %>
- <%= render partial: 'suggested_answers/show_suggested_answer', locals: {suggested_answer: suggested_answer, question: question} %> + <%= render partial: 'annotations/show_annotation', locals: {annotation: example_answer, question: question} %>
<% end %> <% end %> @@ -130,12 +127,12 @@ <% end %>
<% elsif !@edit && question.modifiable %> - <% suggested_answer = question.get_suggested_answer(current_user.org_id) %> - <% if suggested_answer.nil? %> + <% example_answer = question.get_example_answer(current_user.org_id) %> + <% if example_answer.nil? %>
<%= hidden_field_tag :question_id, question.id, class: "question_id" %> - <%= link_to _('Add suggested answer/ example'), '# ', class: "btn btn-primary add_suggested_answer_button"%> + <%= link_to _('Add example answer'), '# ', class: "btn btn-primary add_suggested_answer_button"%>
<% end %>