diff --git a/app/controllers/plans_controller.rb b/app/controllers/plans_controller.rb
index b3d9499..cb28d3a 100644
--- a/app/controllers/plans_controller.rb
+++ b/app/controllers/plans_controller.rb
@@ -175,14 +175,11 @@
def share
@plan = Plan.find(params[:id])
- authorize @plan
- @visibility = @plan.visibility.present? ? @plan.visibility.to_s : Rails.application.config.default_plan_visibility
-
- min_percentage = Rails.application.config.default_plan_percentage_answered
- nanswers = @plan.num_answered_questions()
- nquestions = @plan.num_questions()
- value=(nanswers.to_f/nquestions*100).round(2)
- @allow_visibility = (value >= min_percentage && !@plan.is_test?)
+ if @plan.present?
+ authorize @plan
+ else
+ redirect_to(plans_path)
+ end
end
@@ -305,16 +302,26 @@
end
end
- # AJAX access to update the plan's visibility
- # POST /plans/:id
+ # POST /plans/:id/visibility
def visibility
plan = Plan.find(params[:id])
- authorize plan
- plan.visibility = "#{plan_params[:visibility]}"
- if plan.save
- render json: {msg: success_message(_('plan\'s visibility'), _('changed'))}
+ if plan.present?
+ authorize plan
+ if plan.visibility_allowed?
+ plan.visibility = plan_params[:visibility]
+ if plan.save
+ UserMailer.plan_visibility(User.find_by(email: "jose.lloret@ed.ac.uk"),plan).deliver_now()
+ render status: :ok, json: { msg: success_message(_('plan\'s visibility'), _('changed')) }
+ else
+ render status: :internal_server_error, json: { msg: _('Error raised while saving the visibility for plan id %{plan_id}') %{ :plan_id => params[:id]} }
+ end
+ else
+ render status: :forbidden, json: {
+ msg: _('Unable to change the plan\'s status since it is needed at least '\
+ '%{percentage} percentage responded') %{ :percentage => Rails.application.config.default_plan_percentage_answered } }
+ end
else
- render status: :bad_request, json: {msg: _("Unable to change the plan's status")}
+ render status: :not_found, json: { msg: _('Unable to find plan id %{plan_id}') %{ :plan_id => params[:id]} }
end
end
diff --git a/app/mailers/user_mailer.rb b/app/mailers/user_mailer.rb
index 47b02dd..40c0cbe 100644
--- a/app/mailers/user_mailer.rb
+++ b/app/mailers/user_mailer.rb
@@ -68,4 +68,13 @@
end
end
end
+
+ def plan_visibility(user, plan)
+ @user = user
+ @plan = plan
+ FastGettext.with_locale FastGettext.default_locale do
+ mail(to: @user.email,
+ subject: _('DMP Visibility Changed: %{plan_title}') %{ :plan_title => @plan.title })
+ end
+ end
end
diff --git a/app/models/plan.rb b/app/models/plan.rb
index 2645278..91e17a5 100644
--- a/app/models/plan.rb
+++ b/app/models/plan.rb
@@ -652,6 +652,23 @@
return plan_copy
end
+ # Returns visibility message given a Symbol type visibility passed, otherwise nil
+ def self.visibility_message(type)
+ message = {
+ :organisationally_visible => _('institutional'),
+ :publicly_visible => _('public'),
+ :is_test => _('test'),
+ :privately_visible => _('private')
+ }
+ message[type]
+ end
+
+ # Determines whether or not visibility changes are permitted according to the
+ # percentage of the plan answered in respect to a threshold defined at application.config
+ def visibility_allowed?
+ value=(self.num_answered_questions().to_f/self.num_questions()*100).round(2)
+ !self.is_test? && value >= Rails.application.config.default_plan_percentage_answered
+ end
private
@@ -787,5 +804,4 @@
self.title = "My plan (#{self.template.title})" if self.title.nil? && !self.template.nil?
end
end
-
end
diff --git a/app/views/plans/_share_form.html.erb b/app/views/plans/_share_form.html.erb
index 86f6638..c90e816 100644
--- a/app/views/plans/_share_form.html.erb
+++ b/app/views/plans/_share_form.html.erb
@@ -1,17 +1,17 @@
<%= _('Set plan visibility') %>
<%= _('Public or organisational visibility is intended for finished plans. You must answer at least one question to enable these options.') %>
-
-<%= form_for @plan, html: {method: :put, id: "set_visibility"} do |f| %>
-