diff --git a/app/controllers/annotations_controller.rb b/app/controllers/annotations_controller.rb index a262cb3..e83366b 100644 --- a/app/controllers/annotations_controller.rb +++ b/app/controllers/annotations_controller.rb @@ -4,18 +4,36 @@ #create annotations def admin_create - @example_answer = Annotation.new(params[:annotation]) - authorize @example_answer - if @example_answer.save - redirect_to admin_show_phase_path(id: @example_answer.question.section.phase_id, section_id: @example_answer.question.section_id, question_id: @example_answer.question.id, edit: 'true'), notice: _('Information was successfully created.') + # authorize the question (includes to reduce queries) + @question = Question.includes(section: { phase: :template}).find(params[:question_id]) + authorize @question + if params[:example_answer_text].present? + example_answer = init_annotation(params[:example_answer_text], @question, current_user.org, Annotation.types[:example_answer]) + end + if params[:guidance_text].present? + guidance = init_annotation(params[:guidance_text], @question, current_user.org, Annotation.types[:guidance]) + end + # if they dont exist, no requirement for them to be saved + ex_save = example_answer.present? ? example_answer.save : true + guid_save = guidance.present? ? guidance.save : true + + if ex_save && guid_save + redirect_to admin_show_phase_path(id: @question.section.phase_id, section_id: @question.section_id, question_id: @question.id, edit: 'true'), notice: _('Information was successfully created.') else - @section = @example_answer.question.section + @section = @question.section @phase = @section.phase @open = true @sections = @phase.sections @section_id = @section.id @question_id = @example_answer.question - flash[:notice] = failed_create_error(@example_answer, _('example answer')) + if !ex_save && !guid_save + flash[:notice] = failed_create_error(example_answer, _('example answer')) + '\n' + + failed_create_error(gudiance, _('guidance')) + elsif !guid_save + flash[:notice] = failed_create_error(gudiance, _('guidance')) + elsif !ex_save + flash[:notice] = failed_create_error(example_answer, _('example answer')) + end render "phases/admin_show" end end @@ -23,15 +41,49 @@ #update a example answer of a template def admin_update - @example_answer = Annotation.includes(question: { section: {phase: :template}}).find(params[:id]) - authorize @example_answer #.question.section.phase.template - @question = @example_answer.question + @question = Question.includes(section: { phase: :template}).find(params[:question_id]) + if params[:guidance_id].present? + guidance = Annotation.includes(question: {section: {phase: :template}}).find(params[:guidance_id]) + authorize guidance + end + if params[:example_answer_id].present? + example_answer = Annotation.includes(question: {section: {phase: :template}}).find(params[:example_answer_id]) + authorize example_answer + end + verify_authorized + # if guidance present, update + if params[:guidance_text].present? + if guidance.present? + guidance.text = params[:guidance_text] + else + guidance = init_annotation(params[:guidance_text], @question, current_user.org, Annotation.types[:guidance]) + end + end + # if example answer present, update + if params[:example_answer_text].present? + if example_answer.present? + example_answer.text = params[:example_answer_text] + else + example_answer = init_annotation(params[:example_answer_text], @question, current_user.org, Annotation.types[:example_answer]) + end + end + # only required to save if we updated/created one + ex_save = example_answer.present? ? example_answer.save : true + guid_save = guidance.present? ? guidance.save : true + @section = @question.section @phase = @section.phase - if @example_answer.update_attributes(params[:annotation]) + if ex_save && guid_save redirect_to admin_show_phase_path(id: @phase.id, section_id: @section.id, question_id: @question.id, edit: 'true'), notice: _('Information was successfully updated.') else - flash[:notice] = failed_update_error(@example_answer, _('example answer')) + if !ex_save && !guid_save + flash[:notice] = failed_create_error(example_answer, _('example answer')) + '\n' + + failed_create_error(gudiance, _('guidance')) + elsif !guid_save + flash[:notice] = failed_create_error(gudiance, _('guidance')) + elsif !ex_save + flash[:notice] = failed_create_error(example_answer, _('example answer')) + end render action: "phases/admin_show" end end @@ -50,4 +102,15 @@ end end + private + + def init_annotation(text, question, org, type) + annotation = Annotation.new + annotation.org = org + annotation.question = question + annotation.text = text + annotation.type = type + return annotation + end + end \ No newline at end of file diff --git a/app/controllers/templates_controller.rb b/app/controllers/templates_controller.rb index 76d55a5..c1ea7e8 100644 --- a/app/controllers/templates_controller.rb +++ b/app/controllers/templates_controller.rb @@ -50,7 +50,7 @@ } end - # PUT /org/admin/templates/:id/admin_customize + # GET /org/admin/templates/:id/admin_customize # ----------------------------------------------------- def admin_customize @template = Template.find(params[:id]) diff --git a/app/models/phase.rb b/app/models/phase.rb index a316416..5c2323f 100644 --- a/app/models/phase.rb +++ b/app/models/phase.rb @@ -4,6 +4,12 @@ # [+Created:+] 03/09/2014 # [+Copyright:+] Digital Curation Centre and University of California Curation Center class Phase < ActiveRecord::Base + + ## + # Sort order: Number ASC + default_scope { order(number: :asc) } + + # extend FriendlyId ## # Associations diff --git a/app/models/question.rb b/app/models/question.rb index 10efc8b..1d45c38 100644 --- a/app/models/question.rb +++ b/app/models/question.rb @@ -1,4 +1,9 @@ class Question < ActiveRecord::Base + + ## + # Sort order: Number ASC + default_scope { order(number: :asc) } + ## # Associations has_many :answers, :dependent => :destroy diff --git a/app/models/user.rb b/app/models/user.rb index 8ea0608..c1350f6 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -263,6 +263,14 @@ end end + ## + # Override devise_invitable email title + # -------------------------------------------------------------- + def deliver_invitation(options = {}) + super(options.merge(subject: _('A Data Management Plan in %{application_name} has been shared with you') % {application_name: Rails.configuration.branding[:application][:name]})) + end + + # TODO: Remove this, its never called. # this generates a reset password link for a given user # which can then be sent to them with the appropriate host diff --git a/app/policies/annotation_policy.rb b/app/policies/annotation_policy.rb index cf63f42..a426546 100644 --- a/app/policies/annotation_policy.rb +++ b/app/policies/annotation_policy.rb @@ -14,11 +14,12 @@ ## def admin_create? - user.can_modify_templates? && (annotation.question.section.phase.template.org_id == user.org_id) + # here we pass through a question instead of an annotation object + user.can_modify_templates? && (annotation.section.phase.template.org_id == user.org_id) end def admin_update? - user.can_modify_templates? && (annotation.question.section.phase.template.org_id == user.org_id) + user.can_modify_templates? && (annotation.question.section.phase.template.org_id == user.org_id) && annotation.org_id == user.org_id end def admin_destroy? diff --git a/app/views/annotations/_add_annotation.html.erb b/app/views/annotations/_add_annotation.html.erb index 8cf9113..51e62d4 100644 --- a/app/views/annotations/_add_annotation.html.erb +++ b/app/views/annotations/_add_annotation.html.erb @@ -1,15 +1,21 @@ -<%= form_for :annotation, url: admin_create_annotation_path do |f| %> - <%= f.hidden_field :org_id, value: current_user.org_id %> - <%= f.hidden_field :question_id, value: question.id %> - <%= f.hidden_field :type, value: Annotation.types[:example_answer] %> - +

<%= _('Add Annotations') %>

+<%= form_tag admin_create_annotation_path , class: 'add_annotation_form' do %> + + + + @@ -19,7 +25,8 @@
- <%= f.submit _('Save'), class: "btn btn-primary" %> - <%= link_to _('Cancel'), "#", id: "cancel_suugested_answer", class: "btn cancel" %> + <%= submit_tag _('Save'), class: "btn btn-primary" %> + <%= hidden_field_tag :question_id, question.id %> + <%= link_to _('Cancel'), "#", id: "cancel_add_annotations", class: "btn cancel" %>
<%end%> diff --git a/app/views/annotations/_edit_annotation.html.erb b/app/views/annotations/_edit_annotation.html.erb index dd1b5b7..3a59362 100644 --- a/app/views/annotations/_edit_annotation.html.erb +++ b/app/views/annotations/_edit_annotation.html.erb @@ -1,25 +1,43 @@ -<%= form_for( annotation, url: admin_update_annotation_path(annotation.id), html: { method: :put}) do |f| %> - <%= f.hidden_field :org_id, value: current_user.org_id %> +<%= form_tag admin_update_annotation_path, method: :put do %> + <% example_answer_text = example_answer.present? ? example_answer.text : '' %> + <% guidance_text = guidance.present? ? guidance.text : '' %> + <%= hidden_field_tag :example_answer_id, example_answer.present? ? example_answer.id : nil %> + <%= hidden_field_tag :guidance_id, guidance.present? ? guidance.id : nil %>
<%= _('Example Answer')%>
    -
  • <%= f.text_area :text, rows: 5 %> +
  • <%= text_area_tag :example_answer_text, nil, rows: 5 %> +
  • +
+
<%= _('Guidance')%> +
    +
  • <%= text_area_tag :guidance_text, nil, rows: 5 %>
- + + + + +
<%= _('example answer')%><%= _('Example Answer')%>
    -
  • <%= f.text_area :text, rows: 5 %>
  • +
  • <%= text_area_tag :example_answer_text, example_answer_text, rows: 5 %>
  • +
+
<%= _('Guidance')%> +
    +
  • <%= text_area_tag :guidance_text, guidance_text, rows: 5 %>

- +
- <%= f.submit _('Save'), class: 'btn btn-primary' %> - <%= link_to _('Delete'), admin_destroy_annotation_path(id: annotation.id), - confirm: _("You are about to delete an example answer for '%{question_text}'. Are you sure?") % { :question_text => question.text }, method: :delete, class: "btn btn-primary"%> + <%= submit_tag _('Save'), class: 'btn btn-primary' %> + <% if example_answer.present? %> + <%= link_to _('Delete Example Answer'), admin_destroy_annotation_path(id: example_answer.id), + confirm: _("You are about to delete an example answer for '%{question_text}'. Are you sure?") % { :question_text => question.text }, method: :delete, class: "btn btn-primary"%> + <% end %> + <% if guidance.present? %> + <%= link_to _('Delete Example Answer'), admin_destroy_annotation_path(id: guidance.id), + confirm: _("You are about to delete a guidance for '%{question_text}'. Are you sure?") % { :question_text => question.text }, method: :delete, class: "btn btn-primary"%> + <% end %> + <%= hidden_field_tag :question_id, question.id, class: "question_id" %> - <%= link_to _('Cancel'), '#', class: 'btn cancel cancel_edit_suggested_answer' %> + <%= link_to _('Cancel'), '#', class: 'btn cancel cancel_edit_annotations' %>
<% end %> diff --git a/app/views/annotations/_show_annotation.html.erb b/app/views/annotations/_show_annotation.html.erb index 3c95ace..a790d70 100644 --- a/app/views/annotations/_show_annotation.html.erb +++ b/app/views/annotations/_show_annotation.html.erb @@ -1,15 +1,26 @@ +

<%= _('Annotations') %>

- - - - + <% if example_answer.present? %> + + + + + <% end %> + <% if guidance.present? %> + + + + + <% end %>
- <%= _('Example Answer')%> - <%= raw annotation.text %>
+ <%= _('Example Answer')%> + <%= raw example_answer.text %>
+ <%= _('Guidance')%> + <%= raw guidance.text %>

<%= hidden_field_tag :question_id, question.id, class: "question_id" %> - <%= link_to _('Edit Example Answer'), '# ', class: "btn btn-primary edit_form_for_suggested_answer"%> + <%= link_to _('Edit Annotations'), '# ', class: "btn btn-primary edit_form_for_annotations"%>
diff --git a/app/views/guidance_groups/admin_edit.html.erb b/app/views/guidance_groups/admin_edit.html.erb index 006318b..77146d5 100644 --- a/app/views/guidance_groups/admin_edit.html.erb +++ b/app/views/guidance_groups/admin_edit.html.erb @@ -29,7 +29,7 @@
- <%= link_to(image_tag('help_button.png'), '#', class: 'guidance_group_title_popover', rel: "popover", 'data-html' => "true", 'data-content' => _("Add an appropriate name for your guidance group e.g. Glasgow guidance. This name will be used to tell the end user where the guidance has come from e.g. 'Glasgow Guidance on Metadata'")) %> + <%= link_to(image_tag('help_button.png'), '#', class: 'guidance_group_title_popover', rel: "popover", 'data-html' => "true", 'data-content' => _('Add an appropriate name for your guidance group. This name will be used to tell the end user where the guidance has come from. It will be appended to text identifying the theme e.g. "[guidance group name]: guidance on data sharing" so we suggest you just use the institution or department name.')) %>
@@ -75,4 +75,4 @@
<% end %> - \ No newline at end of file + diff --git a/app/views/guidance_groups/admin_new.html.erb b/app/views/guidance_groups/admin_new.html.erb index faf81f8..8ea92b8 100644 --- a/app/views/guidance_groups/admin_new.html.erb +++ b/app/views/guidance_groups/admin_new.html.erb @@ -25,7 +25,7 @@ <%= f.text_field :name, as: :string, class: "text_field" %>
- <%= link_to( image_tag("help_button.png"), "#", class: 'guidance_group_title_popover', rel: "popover", 'data-html' => "true", 'data-content' => _("Add an appropriate name for your guidance group e.g. Glasgow guidance. This name will be used to tell the end user where the guidance has come from e.g. 'Glasgow Guidance on Metadata'"))%> + <%= link_to( image_tag("help_button.png"), "#", class: 'guidance_group_title_popover', rel: "popover", 'data-html' => "true", 'data-content' => _('Add an appropriate name for your guidance group. This name will be used to tell the end user where the guidance has come from. It will be appended to text identifying the theme e.g. "[guidance group name]: guidance on data sharing" so we suggest you just use the institution or department name.'))%>
diff --git a/app/views/phases/_answer_form.html.erb b/app/views/phases/_answer_form.html.erb index 4a1028f..334c057 100644 --- a/app/views/phases/_answer_form.html.erb +++ b/app/views/phases/_answer_form.html.erb @@ -12,6 +12,9 @@ answer = answers.first else answer = Answer.new({ plan_id: plan.id, question_id: question.id, user_id: current_user.id }) + if question.default_value.present? + answer.text = question.default_value + end end %>
@@ -70,6 +73,7 @@
+ <% num_annotations = 0 %> <% if annotations.present? %> <% annotations.each do |annotation| %> @@ -77,7 +81,9 @@ -
+
<%= raw annotation.text %>
- + <% num_annotations += 1%>
<% end %> <% end %> - <% guidance_accordion_id = 0 %> + <% guidance_accordion_id = num_annotations %> <% question_guidances.each_pair do |theme, group| %> <% group.each do |gobj| %>
diff --git a/app/views/questions/_show_question.html.erb b/app/views/questions/_show_question.html.erb index 0518453..43aa3d3 100644 --- a/app/views/questions/_show_question.html.erb +++ b/app/views/questions/_show_question.html.erb @@ -100,17 +100,18 @@
+ <% example_answer = question.get_example_answer(current_user.org_id) %> + <% guidance = question.get_guidance_annotation(current_user.org_id) %> <% if !question.modifiable %> - <% example_answer = question.get_example_answer(current_user.org_id) %> - <% if example_answer.present? && example_answer.text.present? %> -
- <%= render partial: 'annotations/show_annotation', locals: {annotation: example_answer, question: question} %> + <% if example_answer.present? || guidance.present? %> +
+ <%= render partial: 'annotations/show_annotation', locals: {example_answer: example_answer, guidance: guidance, question: question} %>
-