diff --git a/app/controllers/plans_controller.rb b/app/controllers/plans_controller.rb
index 3710ff5..f844cbd 100644
--- a/app/controllers/plans_controller.rb
+++ b/app/controllers/plans_controller.rb
@@ -38,9 +38,13 @@
def create
@plan = Plan.new
authorize @plan
-
+
@plan.principal_investigator = current_user.surname.blank? ? nil : "#{current_user.firstname} #{current_user.surname}"
- @plan.data_contact = current_user.email
+ @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 = (plan_params['visibility'].blank? ? Rails.application.config.default_plan_visibility :
@@ -81,15 +85,15 @@
if !default.nil? && default == @plan.template
# We used the generic/default template
- msg += _('This plan is based on the 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
- msg += "#{_('This plan is based on the')} #{plan_params[:funder_name]} #{_('template with customisations by the')} #{plan_params[:org_name]}"
+ msg += " #{_('This plan is based on the')} #{plan_params[:funder_name]} #{_('template with customisations by the')} #{plan_params[:org_name]}"
else
# We used the specified org's or funder's template
- msg += "#{_('This plan is based on the')} #{@plan.template.org.name} template."
+ msg += " #{_('This plan is based on the')} #{@plan.template.org.name} template."
end
flash[:notice] = msg
@@ -122,7 +126,8 @@
# Get all Guidance Groups applicable for the plan and group them by org
@all_guidance_groups = @plan.get_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 = []
@important_ggs << [current_user.org, @all_ggs_grouped_by_org.delete(current_user.org)]
@@ -131,12 +136,19 @@
@important_ggs << [org,ggs]
@all_ggs_grouped_by_org.delete(org)
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])
+ @all_ggs_grouped_by_org.delete(org)
+ end
end
# Sort the rest by org name for the accordion
+ @important_ggs = @important_ggs.sort_by{|org,gg| org.name}
@all_ggs_grouped_by_org = @all_ggs_grouped_by_org.sort_by {|org,gg| org.name}
-
- @selected_guidance_groups = @plan.guidance_groups.pluck(:id)
+ @selected_guidance_groups = @selected_guidance_groups.collect{|gg| gg.id}
+
@based_on = (@plan.template.customization_of.nil? ? @plan.template : Template.where(dmptemplate: @plan.template.customization_of).first)
respond_to :html
@@ -152,43 +164,42 @@
# if we have a phase then we are editing that phase.
#
# GET /plans/1/edit
- def edit
- @plan = Plan.find(params[:id])
- authorize @plan
-
- @visibility = @plan.visibility.present? ? @plan.visibility.to_s : Rails.application.config.default_plan_visibility
-
- # If there was no phase specified use the template's 1st phase
- @phase = (params[:phase].nil? ? @plan.template.phases.first : Phase.find(params[:phase]))
- @show_phase_tab = params[:phase]
- @readonly = !@plan.editable_by?(current_user.id)
-
- # Get all Guidance Groups applicable for the plan and group them by org
- @all_guidance_groups = @plan.get_guidance_group_options
- @all_ggs_grouped_by_org = @all_guidance_groups.sort.group_by(&:org)
-
- # Important ones come first on the page - we grab the user's org's GGs and "Organisation" org type GGs
- @important_ggs = []
- @important_ggs << [current_user.org, @all_ggs_grouped_by_org.delete(current_user.org)]
- @all_ggs_grouped_by_org.each do |org, ggs|
- if org.organisation?
- @important_ggs << [org,ggs]
- @all_ggs_grouped_by_org.delete(org)
- end
- end
-
- # Sort the rest by org name for the accordion
- @all_ggs_grouped_by_org = @all_ggs_grouped_by_org.sort_by {|org,gg| org.name}
-
- @selected_guidance_groups = @plan.guidance_groups.pluck(:id)
- @based_on = (@plan.template.customization_of.nil? ? @plan.template : Template.where(dmptemplate: @plan.template.customization_of).first)
-
- flash[:notice] = "#{_('This is a')} #{_('test plan')}" if params[:test]
- @is_test = params[:test] ||= false
-
- respond_to :html
- end
-
+# def edit
+# @plan = Plan.find(params[:id])
+# authorize @plan
+#
+# @visibility = @plan.visibility.present? ? @plan.visibility.to_s : Rails.application.config.default_plan_visibility
+#
+# # If there was no phase specified use the template's 1st phase
+# @phase = (params[:phase].nil? ? @plan.template.phases.first : Phase.find(params[:phase]))
+# @show_phase_tab = params[:phase]
+# @readonly = !@plan.editable_by?(current_user.id)
+#
+# # Get all Guidance Groups applicable for the plan and group them by org
+# @all_guidance_groups = @plan.get_guidance_group_options
+# @all_ggs_grouped_by_org = @all_guidance_groups.sort.group_by(&:org)
+#
+# # Important ones come first on the page - we grab the user's org's GGs and "Organisation" org type GGs
+# @important_ggs = []
+# @important_ggs << [current_user.org, @all_ggs_grouped_by_org.delete(current_user.org)]
+# @all_ggs_grouped_by_org.each do |org, ggs|
+# if org.organisation?
+# @important_ggs << [org,ggs]
+# @all_ggs_grouped_by_org.delete(org)
+# end
+# end
+#
+# # Sort the rest by org name for the accordion
+# @all_ggs_grouped_by_org = @all_ggs_grouped_by_org.sort_by {|org,gg| org.name}
+#
+# @selected_guidance_groups = @plan.guidance_groups.pluck(:id)
+# @based_on = (@plan.template.customization_of.nil? ? @plan.template : Template.where(dmptemplate: @plan.template.customization_of).first)
+#
+# flash[:notice] = "#{_('This is a')} #{_('test plan')}" if params[:test]
+# @is_test = params[:test] ||= false
+#
+# respond_to :html
+# end
# PUT /plans/1
# PUT /plans/1.json
@@ -197,6 +208,12 @@
authorize @plan
attrs = plan_params
+# attrs[:visibility] = attrs[:visibility].to_sym
+
+ # Save the guidance group selections
+ guidance_group_ids = params[:guidance_group_ids].blank? ? [] : params[:guidance_group_ids].map(&:to_i)
+ save_guidance_selections(guidance_group_ids)
+
respond_to do |format|
if @plan.update_attributes(attrs)
format.html { redirect_to @plan, :editing => false, notice: success_message(_('plan'), _('saved')) }
@@ -209,35 +226,22 @@
end
-
+# TODO: Do we need this is selections are saved with rest of form?
def update_guidance_choices
@plan = Plan.find(params[:id])
authorize @plan
guidance_group_ids = params[:guidance_group_ids].blank? ? [] : params[:guidance_group_ids].map(&:to_i)
- all_guidance_groups = @plan.get_guidance_group_options
- plan_groups = @plan.guidance_groups
- guidance_groups = GuidanceGroup.where( id: guidance_group_ids)
- all_guidance_groups.each do |group|
- # case where plan group exists but not in selection
- if plan_groups.include?(group) && ! guidance_groups.include?(group)
- # remove from plan groups
- @plan.guidance_groups.delete(group)
- end
- # case where plan group dosent exist and in selection
- if !plan_groups.include?(group) && guidance_groups.include?(group)
- # add to plan groups
- @plan.guidance_groups << group
- end
- end
+ save_guidance_selections(guidance_group_ids)
@plan.save
flash[:notice] = success_message(_('guidance choices'), _('saved'))
redirect_to action: "show"
end
-
+
def share
@plan = Plan.find(params[:id])
authorize @plan
- #@plan_data = @plan.to_hash
+ @visibility = @plan.visibility.present? ? @plan.visibility.to_s : Rails.application.config.default_plan_visibility
+ @allow_visibility = (@plan.num_answered_questions >= 1 && !@plan.is_test?)
end
@@ -405,9 +409,20 @@
authorize plan
plan.visibility = "#{plan_params[:visibility]}"
if plan.save
- render json: {code: 1, msg: ''}
+ render json: {code: 1, msg: success_message(_('plan\'s visibility'), _('changed'))}
else
- render json: {code: 0, msg: _("Unable to change the plan's Test status")}
+ render json: {code: 0, msg: _("Unable to change the plan's status")}
+ end
+ end
+
+ def set_test
+ plan = Plan.find(params[:id])
+ authorize plan
+ plan.visibility = "#{plan_params[:visibility]}"
+ 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 json: {code: 0, msg: _("Unable to change the plan's test status")}
end
end
@@ -415,9 +430,32 @@
private
def plan_params
- params.require(:plan).permit(:org_id, :org_name, :funder_id, :funder_name, :template_id, :title, :visibility)
+ params.require(:plan).permit(:org_id, :org_name, :funder_id, :funder_name, :template_id, :title, :visibility,
+ :grant_number, :description, :identifier, :principal_investigator,
+ :principal_investigator_email, :principal_investigator_identifier,
+ :data_contact, :data_contact_email, :guidance_group_ids)
end
+ def save_guidance_selections(guidance_group_ids)
+ all_guidance_groups = @plan.get_guidance_group_options
+ plan_groups = @plan.guidance_groups
+ guidance_groups = GuidanceGroup.where(id: guidance_group_ids)
+ all_guidance_groups.each do |group|
+ # case where plan group exists but not in selection
+ if plan_groups.include?(group) && ! guidance_groups.include?(group)
+ # remove from plan groups
+ @plan.guidance_groups.delete(group)
+ end
+ # case where plan group dosent exist and in selection
+ if !plan_groups.include?(group) && guidance_groups.include?(group)
+ # add to plan groups
+ @plan.guidance_groups << group
+ end
+ end
+ @plan.save
+ end
+
+
# different versions of the same template have the same dmptemplate_id
# but different version numbers so for each set of templates with the
# same dmptemplate_id choose the highest version number.
diff --git a/app/controllers/roles_controller.rb b/app/controllers/roles_controller.rb
index ef91cca..abe77f2 100644
--- a/app/controllers/roles_controller.rb
+++ b/app/controllers/roles_controller.rb
@@ -6,6 +6,7 @@
registered = true
@role = Role.new(role_params)
authorize @role
+
access_level = params[:role][:access_level].to_i
set_access_level(access_level)
if params[:user].present?
@@ -45,12 +46,12 @@
access_level = params[:role][:access_level].to_i
set_access_level(access_level)
if @role.update_attributes(role_params)
- flash[:notice] = success_message(_('sharing details'), _('saved'))
UserMailer.permissions_change_notification(@role, current_user).deliver_now
- redirect_to controller: 'plans', action: 'share', id: @role.plan.id
+ render json: {code: 1, msg: "Successfully changed the permissions for #{@role.user.email}. They have been notified via email."}
else
- flash[:alert] = failed_create_error(@role, _('role'))
- render action: "edit"
+# flash[:alert] = failed_create_error(@role, _('role'))
+ #format.html{ render action: "edit" }
+ render json: {code: 1, msg: flash[:alert]}
end
end
diff --git a/app/helpers/plans_helper.rb b/app/helpers/plans_helper.rb
index c4f3af4..d61258d 100644
--- a/app/helpers/plans_helper.rb
+++ b/app/helpers/plans_helper.rb
@@ -42,8 +42,10 @@
return "#{_('Institution')}"
when 'publicly_visible'
return "#{_('Public')}"
+ when 'privately_visible'
+ return "#{_('Private')}"
else
- return "#{_('Private')}" # Both Test and Private
+ return "#{_('N/A')}" # Test Plans
end
end
diff --git a/app/models/plan.rb b/app/models/plan.rb
index ab84aca..6579f97 100644
--- a/app/models/plan.rb
+++ b/app/models/plan.rb
@@ -32,7 +32,7 @@
:exported_plans, :project, :title, :template, :grant_number,
:identifier, :principal_investigator, :principal_investigator_identifier,
:description, :data_contact, :funder_name, :visibility, :exported_plans,
- :roles, :users, :org, :data_contact_email, :data_contact_phone,
+ :roles, :users, :org, :data_contact_email, :principal_investigator_email,
:as => [:default, :admin]
accepts_nested_attributes_for :roles
diff --git a/app/policies/plan_policy.rb b/app/policies/plan_policy.rb
index e6d6d15..448a3c4 100644
--- a/app/policies/plan_policy.rb
+++ b/app/policies/plan_policy.rb
@@ -56,6 +56,10 @@
@plan.administerable_by?(@user.id)
end
+ def set_test?
+ @plan.administerable_by?(@user.id)
+ end
+
# TODO: These routes are no lonmger used
=begin
def section_answers?
diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb
index db53c0d..86bdedf 100644
--- a/app/views/layouts/application.html.erb
+++ b/app/views/layouts/application.html.erb
@@ -63,14 +63,17 @@
<%= render "layouts/header" %>
-