diff --git a/app/controllers/plans_controller.rb b/app/controllers/plans_controller.rb
index 8878657..e568f10 100644
--- a/app/controllers/plans_controller.rb
+++ b/app/controllers/plans_controller.rb
@@ -153,6 +153,7 @@
end
# GET /plans/show
+ # SEE MODULE
def show
@plan = Plan.includes(
template: { phases: { sections: { questions: :answers } } },
diff --git a/app/models/madmp_fragment.rb b/app/models/madmp_fragment.rb
index b278ee1..f4373b5 100644
--- a/app/models/madmp_fragment.rb
+++ b/app/models/madmp_fragment.rb
@@ -100,7 +100,7 @@
end
def get_dmp_fragments
- MadmpFragment.where(dmp_id: dmp_id)
+ MadmpFragment.where(dmp_id: dmp.id)
end
# Returns a human readable version of the structured answer
diff --git a/app/models/madmp_schema.rb b/app/models/madmp_schema.rb
index 735dc99..05dd958 100644
--- a/app/models/madmp_schema.rb
+++ b/app/models/madmp_schema.rb
@@ -80,7 +80,7 @@
sub_schema = MadmpSchema.find(prop["schema_id"])
parameters.append(key => sub_schema.generate_strong_params(false))
elsif prop["type"] == "array" && !flat
- parameters.append({key => []})
+ parameters.append({ key => [] })
else
parameters.append(key)
end
diff --git a/app/models/plan.rb b/app/models/plan.rb
index 9d519a5..1f43819 100644
--- a/app/models/plan.rb
+++ b/app/models/plan.rb
@@ -40,13 +40,10 @@
include ValidationValues
prepend Dmpopidor::Models::Plan
- after_create :create_plan_fragments
-
# =============
# = Constants =
# =============
-
# Returns visibility message given a Symbol type visibility passed, otherwise
# nil
VISIBILITY_MESSAGE = {
diff --git a/app/views/branded/plans/_edit_details.html.erb b/app/views/branded/plans/_edit_details.html.erb
index b23551b..38e9110 100644
--- a/app/views/branded/plans/_edit_details.html.erb
+++ b/app/views/branded/plans/_edit_details.html.erb
@@ -10,24 +10,40 @@
<%= _("Project Details") %>
- <%= render(partial: 'plans/plan_details/plan_project', locals: {
- f: f,
- persons: persons,
- project_fragment: project_fragment
- } ) %>
+
+ <%= render(partial: 'shared/dynamic_form/form', locals: {
+ f: f,
+ fragment: project_fragment,
+ schema: MadmpSchema.find_by(name: "ProjectStandard"),
+ question_id: nil,
+ readonly: false,
+ classname: "project",
+ fragment_id: project_fragment.id,
+ template_locale: @plan.template.locale
+ }) %>
+
+ <%= f.button(_('Save'), class: "btn btn-default", type: "submit") %>
- <%= render(partial: 'plans/plan_details/guidance_configuration', locals: { f: f } ) %>
+ <%= render(partial: 'plans/guidance_configuration', locals: { f: f } ) %>
diff --git a/app/views/branded/plans/_guidance_configuration.html.erb b/app/views/branded/plans/_guidance_configuration.html.erb
new file mode 100644
index 0000000..5fefc84
--- /dev/null
+++ b/app/views/branded/plans/_guidance_configuration.html.erb
@@ -0,0 +1,54 @@
+<%= _('Plan Guidance Configuration') %>
+
+<% if @all_guidance_groups.length > 0 %>
+ <%= _('To help you write your plan, %{application_name} can show you guidance from a variety of organisations.') %
+ {application_name: Rails.configuration.branding[:application][:name]} %>
+
+
+ <%= _('Select up to 6 organisations to see their guidance.') %>
+
+ <%= render partial: "guidance_choices",
+ locals: {choices: @important_ggs, form: f,
+ current_selections: @selected_guidance_groups} %>
+
+
+
+ <% if @all_guidance_groups.length > @important_ggs.length %>
+ <%= _('Find guidance from additional organisations below') %>
+ <%= link_to _('See the full list'), '#', 'data-toggle' => 'modal', 'data-target' => '#modal-full-guidances', class: 'modal-guidances-window' %>
+ <% end %>
+
+
+ <%= f.hidden_field(:default_visibility, value: @is_test ? 'is_test' : Rails.application.config.default_plan_visibility) %>
+ <%= f.button(_('Save'), class: "btn btn-default", type: "submit") %>
+
+<% else %>
+ <%= _("There is no additional guidance for this template.") %>
+<% end %>
+
+<% if @all_guidance_groups.length > @important_ggs.length %>
+
+
+
+
+
+
+
+
+
<%= _('Select Guidance') %>
+
+ <%= _('To help you write your plan, %{application_name} can show you guidance from a variety of organisations. Please choose up to 6 organisations of the following organisations who offer guidance relevant to your plan.') %
+ {application_name: Rails.configuration.branding[:application][:name]} %>
+
+
<%= _("Don't forget to save your changes after making your selections.") %>
+
+ <%= render partial: "guidance_choices",
+ locals: {choices: @all_ggs_grouped_by_org, form: f,
+ current_selections: @selected_guidance_groups} %>
+
+
+
+
+
+
+<% end %>
\ No newline at end of file
diff --git a/app/views/branded/plans/plan_details/_guidance_configuration.html.erb b/app/views/branded/plans/plan_details/_guidance_configuration.html.erb
deleted file mode 100644
index 5fefc84..0000000
--- a/app/views/branded/plans/plan_details/_guidance_configuration.html.erb
+++ /dev/null
@@ -1,54 +0,0 @@
-<%= _('Plan Guidance Configuration') %>
-
-<% if @all_guidance_groups.length > 0 %>
- <%= _('To help you write your plan, %{application_name} can show you guidance from a variety of organisations.') %
- {application_name: Rails.configuration.branding[:application][:name]} %>
-
-
- <%= _('Select up to 6 organisations to see their guidance.') %>
-
- <%= render partial: "guidance_choices",
- locals: {choices: @important_ggs, form: f,
- current_selections: @selected_guidance_groups} %>
-
-
-
- <% if @all_guidance_groups.length > @important_ggs.length %>
- <%= _('Find guidance from additional organisations below') %>
- <%= link_to _('See the full list'), '#', 'data-toggle' => 'modal', 'data-target' => '#modal-full-guidances', class: 'modal-guidances-window' %>
- <% end %>
-
-
- <%= f.hidden_field(:default_visibility, value: @is_test ? 'is_test' : Rails.application.config.default_plan_visibility) %>
- <%= f.button(_('Save'), class: "btn btn-default", type: "submit") %>
-
-<% else %>
- <%= _("There is no additional guidance for this template.") %>
-<% end %>
-
-<% if @all_guidance_groups.length > @important_ggs.length %>
-
-
-
-
-
-
-
-
-
<%= _('Select Guidance') %>
-
- <%= _('To help you write your plan, %{application_name} can show you guidance from a variety of organisations. Please choose up to 6 organisations of the following organisations who offer guidance relevant to your plan.') %
- {application_name: Rails.configuration.branding[:application][:name]} %>
-
-
<%= _("Don't forget to save your changes after making your selections.") %>
-
- <%= render partial: "guidance_choices",
- locals: {choices: @all_ggs_grouped_by_org, form: f,
- current_selections: @selected_guidance_groups} %>
-
-
-
-
-
-
-<% end %>
\ No newline at end of file
diff --git a/app/views/branded/plans/plan_details/_plan_metadata.html.erb b/app/views/branded/plans/plan_details/_plan_metadata.html.erb
deleted file mode 100644
index b733010..0000000
--- a/app/views/branded/plans/plan_details/_plan_metadata.html.erb
+++ /dev/null
@@ -1,168 +0,0 @@
-<%# locals: { persons } %>
-<% meta = meta_fragment.data %>
-<% contact = meta_fragment.contact %>
-<% dmpID = meta["dmpID"] %>
-<% licence = meta["licence"] %>
-
-
-
-
- <%= create_text_field(
- f,
- @plan.template.locale,
- "dmpLanguage",
- 'DMP language code',
- "dmp_language",
- readonly: true
- )
- %>
-
-
-<%= render(partial: 'shared/fragments/person_fragment_form', locals: {
- f: f,
- legend: 'DMP coordinator',
- person: contact ? contact.data : nil,
- person_id: contact ? contact.id : nil,
- person_list: persons,
- plan: @plan,
- fragment_name: "contact",
- tooltip: "Person who coordinates data management planning, also called DMP author or manager. By default, the person who creates the DMP with DMP OPIDoR"
- }) %>
-
-
- <%= create_text_field(
- f,
- meta["creationDate"],
- "creationDate",
- 'Creation date',
- "creation_date",
- readonly: true
- )
- %>
-
-
-
- <%= create_text_field(
- f,
- meta["lastModifiedDate"],
- "lastModifiedDate",
- 'Last modification date',
- "last_modified_date",
- readonly: true
- )
- %>
-
-
-
- <%= create_select_field(
- f,
- dmpID ? dmpID["idType"] : "DOI",
- "plan[dmpID[idType]]",
- 'Identifier Type',
- "dmp_id_type",
- ["DOI", "HANDLE", "ARK", "URL"]
- )
- %>
-
-
- <%= create_text_field(
- f,
- dmpID ? dmpID["value"] : nil,
- "dmpID[value]",
- 'DMP persistent identifier',
- "dmp_id_value",
- ttip: "Provide the plan ID after possible publishing or deposit in an archive"
- )
- %>
-
-
-
-
- <%= 'DMP Licence' %>
-
- <%= create_text_field(
- f,
- licence ? licence["licenseName"] : nil,
- "licence[licenseName]",
- 'Licence name',
- "licence_name"
- )
- %>
-
-
- <%= create_url_field(
- f,
- licence ? licence["licenseUrl"] : nil,
- "licence[licenseUrl]",
- 'Licence URL',
- "licence_url"
- )
- %>
-
-
- <%= create_date_field(
- f,
- licence ? licence["licenceStartDate"] : nil,
- "licence[licenceStartDate]",
- 'License start date',
- "licence_start_date",
- ttip: "Date from which the DMP is shared publicly and can be reused"
- )
- %>
-
-
-
-
- <%= render(partial: 'shared/dynamic_form/fields/simple_multiple_field', locals: {
- f: f,
- field_values: meta["relatedDoc"],
- readonly: false,
- field_label: 'Related Documentation',
- field_properties: {
- "type" => "string",
- "format" => "uri"
- },
- field_name: "relatedDoc", ttip: "Provide the documents associated with the DMP", answer_id: nil
- })
- %>
-
-
- <%= render(partial: 'shared/dynamic_form/fields/simple_multiple_field', locals: {
- f: f,
- field_values: meta["associatedDmp"],
- readonly: false,
- field_label: 'Associated DMPs reference',
- field_properties: {
- "type" => "string"
- },
- field_name: "associatedDmp", ttip: "Provide the other DMPs associated with the project", answer_id: nil
- })
- %>
-
-
-<%= f.button(_('Save'), class: "btn btn-default", type: "submit") %>
\ No newline at end of file
diff --git a/app/views/branded/plans/plan_details/_plan_project.html.erb b/app/views/branded/plans/plan_details/_plan_project.html.erb
deleted file mode 100644
index 928dec9..0000000
--- a/app/views/branded/plans/plan_details/_plan_project.html.erb
+++ /dev/null
@@ -1,106 +0,0 @@
-<%# locals: { persons } %>
-<% project_abstract_tooltip = _("Briefly summarise your research project to help others understand the purposes for which the data are being collected or created.") %>
-
-<% project = project_fragment.data %>
-<% fundings = project_fragment.fundings %>
-<% partners = project_fragment.partners %>
-<% principalInvestigator = project_fragment.principalInvestigator %>
-
-
- <%= create_text_field(
- f,
- project["title"],
- "project_title",
- 'Project title',
- "project_title"
- )
- %>
-
-
- <%= create_text_field(
- f,
- project["acronym"],
- "project_acronym",
- 'Project acronym',
- "project_acronym"
- )
- %>
-
-
-
-
-
-<%= render(partial: 'shared/dynamic_form/fields/complex_multiple_field', locals: {
- field_values: fundings,
- parent_id: project_fragment.id,
- schema: MadmpSchema.find_by(name: "FundingStandard"),
- readonly: false,
- template_locale: @plan.template.locale,
- field_label: "Funding", answer_id: nil
- }
-) %>
-
-
- <%= create_date_field(
- f,
- project["startDate"],
- "project_start_date",
- 'Project start date',
- "project_start_date"
- )
- %>
-
-
- <%= create_date_field(
- f,
- project["endDate"],
- "project_end_date",
- 'Project end date',
- "project_end_date"
- )
- %>
-
-
-<%= render(partial: 'shared/dynamic_form/fields/complex_multiple_field', locals: {
- field_values: partners,
- parent_id: project_fragment.id,
- schema: MadmpSchema.find_by(name: "PartnerStandard"),
- readonly: false,
- template_locale: @plan.template.locale,
- field_label: "Project partner", answer_id: nil
- }
-) %>
-
-
- <%= create_url_field(
- f,
- project["experimentalPlanUrl"],
- "experimental_plan_url",
- 'Experimental plan reference',
- "experimental_plan_url"
- )
- %>
-
-
-
-<%= render(partial: 'shared/fragments/person_fragment_form', locals: {
- f: f,
- legend: 'Project coordinator',
- person: principalInvestigator ? principalInvestigator.data : nil,
- person_id: principalInvestigator ? principalInvestigator.id : nil,
- person_list: persons,
- plan: @plan,
- fragment_name: "principalInvestigator", tooltip: "also called Principal investigator"
- }) %>
-
-<%= f.button(_('Save'), class: "btn btn-default", type: "submit") %>
\ No newline at end of file
diff --git a/config/locale/dmpopidor.pot b/config/locale/dmpopidor.pot
index 165ea1d..4dca277 100644
--- a/config/locale/dmpopidor.pot
+++ b/config/locale/dmpopidor.pot
@@ -479,3 +479,6 @@
msgid "General informations"
msgstr ""
+
+msgid "\"%{project_title}\" project DMP"
+msgstr ""
diff --git a/config/locale/en_GB/dmpopidor.po b/config/locale/en_GB/dmpopidor.po
index 075513a..7bbf6cb 100644
--- a/config/locale/en_GB/dmpopidor.po
+++ b/config/locale/en_GB/dmpopidor.po
@@ -474,4 +474,7 @@
msgstr "Create"
msgid "General informations"
-msgstr "General informations"
\ No newline at end of file
+msgstr "General informations"
+
+msgid "\"%{project_title}\" project DMP"
+msgstr "\"%{project_title}\" project DMP"
\ No newline at end of file
diff --git a/config/locale/fr_FR/dmpopidor.po b/config/locale/fr_FR/dmpopidor.po
index ac5ba11..342981d 100644
--- a/config/locale/fr_FR/dmpopidor.po
+++ b/config/locale/fr_FR/dmpopidor.po
@@ -476,4 +476,7 @@
msgstr "Créer"
msgid "General informations"
-msgstr "Informations générales"
\ No newline at end of file
+msgstr "Informations générales"
+
+msgid "\"%{project_title}\" project DMP"
+msgstr "DMP du projet \"%{project_title}\""
diff --git a/lib/dmpopidor/controllers/plans.rb b/lib/dmpopidor/controllers/plans.rb
index b3f5af1..3af0402 100644
--- a/lib/dmpopidor/controllers/plans.rb
+++ b/lib/dmpopidor/controllers/plans.rb
@@ -1,318 +1,372 @@
+# frozen_string_literal: true
+
module Dmpopidor
- module Controllers
- module Plans
- # CHANGES:
- # Added Active Flag on Org
- def new
- @plan = Plan.new
- authorize @plan
+ module Controllers
- # Get all of the available funders and non-funder orgs
- @funders = Org.funder
- .joins(:templates)
- .where(templates: { published: true }).uniq.sort_by(&:name)
- @orgs = (Org.organisation + Org.institution + Org.managing_orgs + Org.where(is_other: true)).flatten
- .select { |org| org.active == true }
- .uniq.sort_by(&:name)
+ module Plans
- # Get the current user's org
- @default_org = current_user.org if @orgs.include?(current_user.org) || @funders.include?(current_user.org)
+ # CHANGES:
+ # Added Active Flag on Org
+ def new
+ @plan = Plan.new
+ authorize @plan
- # Get the default template
- @default_template = Template.default
+ # Get all of the available funders and non-funder orgs
+ @funders = Org.funder
+ .joins(:templates)
+ .where(templates: { published: true }).uniq.sort_by(&:name)
+ @orgs = (Org.organisation + Org.institution + Org.managing_orgs + Org.where(is_other: true)).flatten
+ .select { |org| org.active == true }
+ .uniq.sort_by(&:name)
- if params.key?(:test)
- flash[:notice] = "#{_('This is a')} #{_('test plan')} "
- end
- @is_test = params[:test] ||= false
- respond_to :html
+ # Get the current user's org
+ @default_org = current_user.org if @orgs.include?(current_user.org) || @funders.include?(current_user.org)
+
+ # Get the default template
+ @default_template = Template.default
+
+ if params.key?(:test)
+ flash[:notice] = "#{_('This is a')} #{_('test plan')} "
end
+ @is_test = params[:test] ||= false
+ respond_to :html
+ end
+ # CHANGES:
+ # Added Research Output Support
+ def create
+ @plan = Plan.new
+ authorize @plan
+ # We set these ids to -1 on the page to trick ariatiseForm into allowing the
+ # autocomplete to be blank if the no org/funder checkboxes are checked off
+ org_id = (plan_params[:org_id] == "-1" ? "" : plan_params[:org_id])
- # CHANGES:
- # Added Research Output Support
- def create
- @plan = Plan.new
- authorize @plan
-
- # We set these ids to -1 on the page to trick ariatiseForm into allowing the
- # autocomplete to be blank if the no org/funder checkboxes are checked off
- org_id = (plan_params[:org_id] == "-1" ? "" : plan_params[:org_id])
- funder_id = (plan_params[:funder_id] == "-1" ? "" : plan_params[:funder_id])
-
- # If the template_id is blank then we need to look up the available templates and
- # return JSON
- if plan_params[:template_id].blank?
- # Something went wrong there should always be a template id
- respond_to do |format|
- flash[:alert] = _("Unable to identify a suitable template for your plan.")
- format.html { redirect_to new_plan_path }
- end
- else
- # Otherwise create the plan
- if current_user.surname.blank?
- @plan.principal_investigator = nil
- else
- @plan.principal_investigator = current_user.name(false)
- end
-
- @plan.principal_investigator_email = current_user.email
-
- orcid = current_user.identifier_for(IdentifierScheme.find_by(name: "orcid"))
- @plan.principal_investigator_identifier = orcid.identifier unless orcid.nil?
-
- @plan.funder_name = plan_params[:funder_name]
-
- @plan.visibility = if plan_params["visibility"].blank?
- Rails.application.config.default_plan_visibility
- else
- plan_params[:visibility]
- end
-
- @plan.template = Template.find(plan_params[:template_id])
-
- if plan_params[:title].blank?
- @plan.title = if current_user.firstname.blank?
- _("My Plan") + "(" + @plan.template.title + ")"
- else
- current_user.firstname + "'s" + _(" Plan")
- end
- else
- @plan.title = plan_params[:title]
- end
-
- if @plan.save
- # pre-select org's guidance and the default org's guidance
- ids = (Org.managing_orgs << current_user.org_id << org_id).flatten.uniq
- ggs = GuidanceGroup.where(org_id: ids, optional_subset: false, published: true)
-
- if !ggs.blank? then @plan.guidance_groups << ggs end
-
- default = Template.default
-
- msg = "#{success_message(@plan, _('created'))} "
-
- if !default.nil? && default == @plan.template
- # We used the generic/default template
- msg += " #{_('This plan is based on the default template.')}"
-
- elsif !@plan.template.customization_of.nil?
- # rubocop:disable Metrics/LineLength
- # We used a customized version of the the funder template
- # rubocop:disable Metrics/LineLength
- msg += " #{d_('dmpopidor', 'This plan is based on the %{funder_name}: %{template_name} template with customisations by the %{org_name}') % {
- funder_name: plan_params[:funder_name],
- template_name: @plan.template.title,
- org_name: plan_params[:org_name]
- } }"# rubocop:enable Metrics/LineLength
- else
- # rubocop:disable Metrics/LineLength
- # We used the specified org's or funder's template
- # rubocop:disable Metrics/LineLength
- msg += " #{d_('dmpopidor', 'This plan is based on the %{org_name}: %{template_name} template') % { org_name: @plan.template.org.name, template_name: @plan.template.title} }"
- # rubocop:enable Metrics/LineLength
- end
-
- @plan.add_user!(current_user.id, :creator)
-
- # Add default research output if possible
- @plan.research_outputs.create(
- abbreviation: 'Default',
- fullname: 'Default research output',
- is_default: true,
- type: ResearchOutputType.find_by(label: "Dataset"),
- order: 1
- )
-
- respond_to do |format|
- flash[:notice] = msg
- format.html { redirect_to plan_path(@plan) }
- end
-
- else
- # Something went wrong so report the issue to the user
- respond_to do |format|
- flash[:alert] = failure_message(@plan, _("create"))
- format.html { redirect_to new_plan_path }
- end
- end
- end
- end
-
- # GET /plans/:plan_id/phases/:id/edit
- # CHANGES :
- # Added Research Output Support
- def edit
- plan = Plan.includes(:research_outputs).find(params[:id])
- authorize plan
- plan, phase = Plan.load_for_phase(params[:id], params[:phase_id])
- guidance_groups = GuidanceGroup.where(published: true, id: plan.guidance_group_ids)
- render_phases_edit(plan, phase, guidance_groups)
- end
-
- # PUT /plans/1
- # PUT /plans/1.json
- # CHANGES :
- # Added Research Output Support
- # Added DMP, Meta & Project update
- def update
- @plan = Plan.find(params[:id])
- authorize @plan
- attrs = plan_params
- # rubocop:disable Metrics/BlockLength
+ # If the template_id is blank then we need to look up the available templates and
+ # return JSON
+ if plan_params[:template_id].blank?
+ # Something went wrong there should always be a template id
respond_to do |format|
- begin
- # Save the guidance group selections
- guidance_group_ids = if params[:guidance_group_ids].blank?
- []
- else
- params[:guidance_group_ids].map(&:to_i).uniq
- end
- @plan.guidance_groups = GuidanceGroup.where(id: guidance_group_ids)
- @plan.save
- if @plan.update_attributes(attrs)
- @plan.update_plan_fragments(plan_meta_params, plan_project_params)
-
- format.html do
- redirect_to plan_path(@plan),
- notice: success_message(@plan, _("saved"))
- end
- format.json do
- render json: { code: 1, msg: success_message(@plan, _("saved")) }
- end
- else
- format.html do
- # TODO: Should do a `render :show` here instead but show defines too many
- # instance variables in the controller
- redirect_to "#{plan_path(@plan)}", alert: failure_message(@plan, _("save"))
- end
- format.json do
- render json: { code: 0, msg: failure_message(@plan, _("save")) }
- end
- end
-
- rescue Exception
- flash[:alert] = failure_message(@plan, _("save"))
- format.html do
- render_phases_edit(@plan, @plan.phases.first, @plan.guidance_groups)
- end
- format.json do
- render json: { code: 0, msg: flash[:alert] }
- end
- end
+ flash[:alert] = _("Unable to identify a suitable template for your plan.")
+ format.html { redirect_to new_plan_path }
end
- # rubocop:enable Metrics/BlockLength
- end
+ else
+ # Otherwise create the plan
+ if current_user.surname.blank?
+ @plan.principal_investigator = nil
+ else
+ @plan.principal_investigator = current_user.name(false)
+ end
- # POST /plans/:id/visibility
- def visibility
- plan = Plan.find(params[:id])
- if plan.present?
- authorize plan
- if plan.visibility_allowed?
- plan.visibility = plan_params[:visibility]
- if plan.save
- deliver_if(recipients: plan.owner_and_coowners,
- key: "owners_and_coowners.visibility_changed") do |r|
- UserMailer.plan_visibility(r, plan).deliver_now()
- end
- redirect_to :back, notice: success_message(plan, _("updated"))
- else
- redirect_to :back, notice: failure_message(plan, _("update"))
- end
- else
+ @plan.principal_investigator_email = current_user.email
+
+ orcid = current_user.identifier_for(IdentifierScheme.find_by(name: "orcid"))
+ @plan.principal_investigator_identifier = orcid.identifier unless orcid.nil?
+
+ @plan.funder_name = plan_params[:funder_name]
+
+ @plan.visibility = if plan_params["visibility"].blank?
+ Rails.application.config.default_plan_visibility
+ else
+ plan_params[:visibility]
+ end
+
+ @plan.template = Template.find(plan_params[:template_id])
+
+ if plan_params[:title].blank?
+ @plan.title = if current_user.firstname.blank?
+ _("My Plan") + "(" + @plan.template.title + ")"
+ else
+ current_user.firstname + "'s" + _(" Plan")
+ end
+ else
+ @plan.title = plan_params[:title]
+ end
+
+ if @plan.save
+ # pre-select org's guidance and the default org's guidance
+ ids = (Org.managing_orgs << current_user.org_id << org_id).flatten.uniq
+ ggs = GuidanceGroup.where(org_id: ids, optional_subset: false, published: true)
+
+ if !ggs.blank? then @plan.guidance_groups << ggs end
+
+ default = Template.default
+
+ msg = "#{success_message(@plan, _('created'))} "
+
+ if !default.nil? && default == @plan.template
+ # We used the generic/default template
+ msg += " #{_('This plan is based on the default template.')}"
+
+ elsif !@plan.template.customization_of.nil?
+ # We used a customized version of the the funder template
# rubocop:disable Metrics/LineLength
- redirect_to :back, notice: failure_message(
- _("Unable to change the plan's status since it is needed at least %{percentage} percentage responded") % {
- percentage: Rails.application.config.default_plan_percentage_answered
- }
- )
+ msg += " #{d_('dmpopidor', 'This plan is based on the %{funder_name}: %{template_name} template with customisations by the %{org_name}') % {
+ funder_name: plan_params[:funder_name],
+ template_name: @plan.template.title,
+ org_name: plan_params[:org_name]
+ } }"
+ # rubocop:enable Metrics/LineLength
+ else
+ # We used the specified org's or funder's template
+ # rubocop:disable Metrics/LineLength
+ msg += " #{d_('dmpopidor', 'This plan is based on the %{org_name}: %{template_name} template') % { org_name: @plan.template.org.name, template_name: @plan.template.title} }"
# rubocop:enable Metrics/LineLength
end
- else
- redirect_to :back, notice: failure_message(
- _("Unable to find plan id %{plan_id}") % { plan_id: params[:id] }
+
+ @plan.add_user!(current_user.id, :creator)
+
+ @plan.create_plan_fragments
+
+ # Add default research output if possible
+ @plan.research_outputs.create(
+ abbreviation: "Default",
+ fullname: "Default research output",
+ is_default: true,
+ type: ResearchOutputType.find_by(label: "Dataset"),
+ order: 1
)
- end
- end
- # Removing test flag now put the plan in privately visibility
- def set_test
- plan = Plan.find(params[:id])
- authorize plan
- plan.visibility = (params[:is_test] === "1" ? :is_test : :privately_visible)
- # rubocop:disable Metrics/LineLength
- if plan.save
- render json: {
- code: 1,
- msg: (plan.is_test? ? _("Your project is now a test.") : _("Your project is no longer a test."))
- }
+ respond_to do |format|
+ flash[:notice] = msg
+ format.html { redirect_to plan_path(@plan) }
+ end
+
else
- render status: :bad_request, json: {
- code: 0, msg: _("Unable to change the plan's test status")
- }
+ # Something went wrong so report the issue to the user
+ respond_to do |format|
+ flash[:alert] = failure_message(@plan, _("create"))
+ format.html { redirect_to new_plan_path }
+ end
end
- # rubocop:enable Metrics/LineLength
- end
-
- # CHANGES : Research Outputs support
- def download
- @plan = Plan.find(params[:id])
- authorize @plan
- @research_outputs = @plan.research_outputs
- @phase_options = @plan.phases.order(:number).pluck(:title, :id)
- @export_settings = @plan.settings(:export)
- render "download"
- end
-
- private
- # CHANGES : Research Outputs support
- def plan_params
- params.require(:plan)
- .permit(:org_id, :org_name, :funder_id, :funder_name, :template_id,
- :title, :visibility, :grant_number, :description, :identifier,
- :principal_investigator_phone, :principal_investigator,
- :principal_investigator_email, :data_contact,
- :principal_investigator_identifier, :data_contact_email,
- :data_contact_phone, :guidance_group_ids,
- research_outputs_attributes: %i[_destroy])
- end
-
- def plan_meta_params
- params.require(:plan)
- .permit(:title, :descriptione, :associatedDMPId, :versionNumber, :versionNotes,
- relatedDocUrl: [],
- contact: [:lastName, :firstName, :mbox, :identifier],
- licence: [ :licenseName, :licenseUrl, :licenceStartDate],
- dmpID: [:value, :idType])
- end
-
- def plan_project_params
- params.require(:plan)
- .permit(:project_title, :project_acronym, :project_id, :project_description,
- :project_start_date, :project_end_date, :experimental_plan_url,
- principalInvestigator: [:lastName, :firstName, :mbox, :identifier] )
- end
-
- # CHANGES : maDMP Fragments SUPPORT
- def render_phases_edit(plan, phase, guidance_groups)
- readonly = !plan.editable_by?(current_user.id)
- @schemas = MadmpSchema.all
- # 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.includes(:madmp_fragment).reduce({}) { |m, a| m["#{a.question_id}_#{a.research_output_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_presenter: GuidancePresenter.new(plan)
- })
end
end
+
+ # GET /plans/show
+ def show
+ @plan = Plan.includes(
+ template: { phases: { sections: { questions: :answers } } },
+ plans_guidance_groups: { guidance_group: :guidances }
+ ).find(params[:id])
+ @schemas = MadmpSchema.all
+ authorize @plan
+
+ @research_outputs = @plan.research_outputs.order(:order)
+
+ @visibility = if @plan.visibility.present?
+ @plan.visibility.to_s
+ else
+ Rails.application.config.default_plan_visibility
+ end
+
+ @editing = (!params[:editing].nil? && @plan.administerable_by?(current_user.id))
+
+ # Get all Guidance Groups applicable for the plan and group them by org
+ @all_guidance_groups = @plan.guidance_group_options
+ @all_ggs_grouped_by_org = @all_guidance_groups.sort.group_by(&:org)
+ @selected_guidance_groups = @plan.guidance_groups
+
+ # Important ones come first on the page - we grab the user's org's GGs and
+ # "Organisation" org type GGs
+ @important_ggs = []
+
+ if @all_ggs_grouped_by_org.include?(current_user.org)
+ @important_ggs << [current_user.org, @all_ggs_grouped_by_org[current_user.org]]
+ end
+ @all_ggs_grouped_by_org.each do |org, ggs|
+ if org.organisation?
+ @important_ggs << [org, ggs]
+ end
+
+ # If this is one of the already selected guidance groups its important!
+ if !(ggs & @selected_guidance_groups).empty?
+ @important_ggs << [org, ggs] unless @important_ggs.include?([org, ggs])
+ end
+ end
+
+ # Sort the rest by org name for the accordion
+ @important_ggs = @important_ggs.sort_by { |org, gg| (org.nil? ? "" : org.name) }
+ @all_ggs_grouped_by_org = @all_ggs_grouped_by_org.sort_by do |org, gg|
+ (org.nil? ? "" : org.name)
+ end
+ @selected_guidance_groups = @selected_guidance_groups.ids
+
+ @based_on = if @plan.template.customization_of.nil?
+ @plan.template
+ else
+ Template.where(family_id: @plan.template.customization_of).first
+ end
+ respond_to :html
+ end
+
+ # GET /plans/:plan_id/phases/:id/edit
+ # CHANGES :
+ # Added Research Output Support
+ def edit
+ plan = Plan.includes(:research_outputs).find(params[:id])
+ authorize plan
+ plan, phase = Plan.load_for_phase(params[:id], params[:phase_id])
+ guidance_groups = GuidanceGroup.where(published: true, id: plan.guidance_group_ids)
+ render_phases_edit(plan, phase, guidance_groups)
+ end
+
+ # PUT /plans/1
+ # PUT /plans/1.json
+ # CHANGES :
+ # Added Research Output Support
+ # Added DMP, Meta & Project update
+ def update
+ @plan = Plan.find(params[:id])
+ authorize @plan
+ attrs = plan_params
+ # rubocop:disable Metrics/BlockLength
+ respond_to do |format|
+ begin
+ # Save the guidance group selections
+ guidance_group_ids = if params[:guidance_group_ids].blank?
+ []
+ else
+ params[:guidance_group_ids].map(&:to_i).uniq
+ end
+ @plan.guidance_groups = GuidanceGroup.where(id: guidance_group_ids)
+ @plan.save
+ if @plan.update_attributes(attrs)
+ @plan.update_plan_fragments(
+ schema_params(MadmpSchema.find_by(classname: "meta")),
+ schema_params(MadmpSchema.find_by(classname: "project"))
+ )
+
+ format.html do
+ redirect_to plan_path(@plan),
+ notice: success_message(@plan, _("saved"))
+ end
+ format.json do
+ render json: { code: 1, msg: success_message(@plan, _("saved")) }
+ end
+ else
+ format.html do
+ # TODO: Should do a `render :show` here instead but show defines too many
+ # instance variables in the controller
+ redirect_to "#{plan_path(@plan)}", alert: failure_message(@plan, _("save"))
+ end
+ format.json do
+ render json: { code: 0, msg: failure_message(@plan, _("save")) }
+ end
+ end
+ rescue Exception
+ flash[:alert] = failure_message(@plan, _("save"))
+ format.html do
+ render_phases_edit(@plan, @plan.phases.first, @plan.guidance_groups)
+ end
+ format.json do
+ render json: { code: 0, msg: flash[:alert] }
+ end
+ end
+ end
+ # rubocop:enable Metrics/BlockLength
+ end
+
+ # POST /plans/:id/visibility
+ def visibility
+ plan = Plan.find(params[:id])
+ if plan.present?
+ authorize plan
+ if plan.visibility_allowed?
+ plan.visibility = plan_params[:visibility]
+ if plan.save
+ deliver_if(recipients: plan.owner_and_coowners,
+ key: "owners_and_coowners.visibility_changed") do |r|
+ UserMailer.plan_visibility(r, plan).deliver_now()
+ end
+ redirect_to :back, notice: success_message(plan, _("updated"))
+ else
+ redirect_to :back, notice: failure_message(plan, _("update"))
+ end
+ else
+ # rubocop:disable Metrics/LineLength
+ redirect_to :back, notice: failure_message(
+ _("Unable to change the plan's status since it is needed at least %{percentage} percentage responded") % {
+ percentage: Rails.application.config.default_plan_percentage_answered
+ }
+ )
+ # rubocop:enable Metrics/LineLength
+ end
+ else
+ redirect_to :back, notice: failure_message(
+ _("Unable to find plan id %{plan_id}") % { plan_id: params[:id] }
+ )
+ end
+ end
+
+ # Removing test flag now put the plan in privately visibility
+ def set_test
+ plan = Plan.find(params[:id])
+ authorize plan
+ plan.visibility = (params[:is_test] === "1" ? :is_test : :privately_visible)
+ # rubocop:disable Metrics/LineLength
+ if plan.save
+ render json: {
+ code: 1,
+ msg: (plan.is_test? ? _("Your project is now a test.") : _("Your project is no longer a test."))
+ }
+ else
+ render status: :bad_request, json: {
+ code: 0, msg: _("Unable to change the plan's test status")
+ }
+ end
+ # rubocop:enable Metrics/LineLength
+ end
+
+ # CHANGES : Research Outputs support
+ def download
+ @plan = Plan.find(params[:id])
+ authorize @plan
+ @research_outputs = @plan.research_outputs
+ @phase_options = @plan.phases.order(:number).pluck(:title, :id)
+ @export_settings = @plan.settings(:export)
+ render "download"
+ end
+
+ private
+ # CHANGES : Research Outputs support
+ def plan_params
+ params.require(:plan)
+ .permit(:org_id, :org_name, :funder_id, :funder_name, :template_id,
+ :title, :visibility, :grant_number, :description, :identifier,
+ :principal_investigator_phone, :principal_investigator,
+ :principal_investigator_email, :data_contact,
+ :principal_investigator_identifier, :data_contact_email,
+ :data_contact_phone, :guidance_group_ids,
+ research_outputs_attributes: %i[_destroy])
+ end
+
+ # Get the parameters corresponding to the schema
+ def schema_params(schema, flat = false)
+ s_params = schema.generate_strong_params(flat)
+ params.require(:plan).permit(s_params)
+ end
+
+ # CHANGES : maDMP Fragments SUPPORT
+ def render_phases_edit(plan, phase, guidance_groups)
+ readonly = !plan.editable_by?(current_user.id)
+ @schemas = MadmpSchema.all
+ # 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.includes(:madmp_fragment).reduce({}) { |m, a| m["#{a.question_id}_#{a.research_output_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_presenter: GuidancePresenter.new(plan)
+ })
+ end
+
end
- end
\ No newline at end of file
+
+ end
+
+end
diff --git a/lib/dmpopidor/models/plan.rb b/lib/dmpopidor/models/plan.rb
index 1407646..92cb74c 100644
--- a/lib/dmpopidor/models/plan.rb
+++ b/lib/dmpopidor/models/plan.rb
@@ -1,15 +1,17 @@
+# frozen_string_literal: true
+
module Dmpopidor
+
module Models
+
module Plan
-
-
# CHANGE : Fix to creator display
def owner
usr_id = ::Role.where(plan_id: id, active: true)
- .administrator
- .order(:created_at)
- .pluck(:user_id).first
+ .administrator
+ .order(:created_at)
+ .pluck(:user_id).first
usr_id.present? ? ::User.find(usr_id) : nil
end
@@ -90,7 +92,7 @@
# 'reviewer' Role for the Plan.
# CHANGES : Added feedback_requestor & request_date columns
def complete_feedback(org_admin)
- ::Plan.transaction do
+ ::Plan.transaction do
begin
self.feedback_requested = false
self.feedback_requestor = nil
@@ -122,7 +124,6 @@
research_outputs.count
end
-
# Return the JSON Fragment linked to the Plan
#
# Returns JSON
@@ -130,100 +131,54 @@
Fragment::Dmp.where("(data->>'plan_id')::int = ?", id).first
end
- # Create the Project JSON Fragment
- #
- # Returns JSON
- def create_project_json(project = nil)
- fragment = nil
- if project.nil?
- fragment = {
- "title" => self.title,
- "description" => self.description
- }
-
- else
- fragment = {
- "title" => project["project_title"],
- "acronym" => project["project_acronym"],
- "description" => project["project_description"],
- "projectId" => project["project_id"],
- "startDate" => project["project_start_date"],
- "endDate" => project["project_end_date"],
- "experimentalPlanUrl" => project["experimental_plan_url"],
- "principalInvestigator" => project["principalInvestigator"]
- }
-
- end
- fragment
- end
-
- # Create the Meta JSON Fragment
- #
- # Returns JSON
- def create_meta_json(meta = nil)
- fragment = nil
- if meta.nil?
- fragment = {
- "creationDate" => self.created_at,
- "lastModifiedDate" => self.updated_at
- }
- else
- fragment = meta.merge({
- "creationDate" => self.created_at,
- "lastModifiedDate" => self.updated_at
- })
- end
- fragment
- end
-
- # Create a Person JSON Fragment if it doesn't exist
- #
- # Returns JSON
- def create_or_update_person_fragment(person)
- dmp_fragment = self.json_fragment()
- person_fragment = nil
- ## TODO : Permettre la mise à jour d'une personne
- unless person[:mbox].empty?
- person_fragment = dmp_fragment.persons.where(
- "data->>'mbox' = ?", person[:mbox]
- ).first
- if person_fragment.nil?
- person_fragment = dmp_fragment.persons.create(
- data: person,
- madmp_schema_id: MadmpSchema.find_by(classname: "person").id
- )
- else
- person_fragment.update(
- data: person,
- madmp_schema_id: MadmpSchema.find_by(classname: "person").id
- )
- person_fragment.save!
- end
- end
- person_fragment
- end
-
def create_plan_fragments
dmp_fragment = Fragment::Dmp.create(
data: {
"plan_id" => id
},
- madmp_schema: MadmpSchema.find_by(classname: "dmp")
+ madmp_schema: MadmpSchema.find_by(name: "DMPStandard")
)
Fragment::Project.create(
- data: create_project_json,
+ data: {
+ "title" => title
+ },
dmp_id: dmp_fragment.id,
parent_id: dmp_fragment.id,
madmp_schema: MadmpSchema.find_by(name: "ProjectStandard")
)
- Fragment::Meta.create(
- data: create_meta_json,
+ meta = Fragment::Meta.create(
+ data: {
+ "title" => d_("dmpopidor", "\"%{project_title}\" project DMP") % { project_title: title },
+ "creationDate" => created_at.strftime("%F"),
+ "lastModifiedDate" => updated_at.strftime("%F"),
+ "dmpLanguage" => template.locale
+ },
dmp_id: dmp_fragment.id,
parent_id: dmp_fragment.id,
madmp_schema: MadmpSchema.find_by(name: "MetaStandard")
)
+
+ person = Fragment::Person.create(
+ data: {
+ "lastName" => owner.surname,
+ "firstName" => owner.firstname,
+ "mbox" => owner.email
+ },
+ dmp_id: dmp_fragment.id,
+ madmp_schema: MadmpSchema.find_by(name: "PersonStandard")
+ )
+
+ Fragment::Contributor.create(
+ data: {
+ "person" => { "dbid" => person.id },
+ "role" => "DMP contact"
+ },
+ dmp_id: dmp_fragment.id,
+ parent_id: meta.id,
+ madmp_schema: MadmpSchema.find_by(name: "ContributorStandard")
+ )
end
def update_plan_fragments(meta, project)
@@ -240,11 +195,11 @@
}
dmp_fragment.meta.update(
- data: create_meta_json(meta)
+ data: dmp_fragment.meta.data.merge(meta)
)
dmp_fragment.project.update(
- data: create_project_json(project)
+ data: dmp_fragment.project.data.merge(project)
)
end