diff --git a/app/controllers/plans_controller.rb b/app/controllers/plans_controller.rb
index f98ff45..8bedcbc 100644
--- a/app/controllers/plans_controller.rb
+++ b/app/controllers/plans_controller.rb
@@ -6,7 +6,7 @@
def index
authorize Plan
- @plans = current_user.plans
+ @plans = current_user.active_plans
end
# GET /plans/public_index
@@ -292,49 +292,46 @@
# -------------------------------------------------------------
def public_export
@plan = Plan.find(params[:id])
-
# If the plan has multiple phases we should export each
- @plan.phases.each do |phase|
- @exported_plan = ExportedPlan.new.tap do |ep|
- ep.plan = @plan
- ep.phase_id = phase.id
- ep.format = :pdf
- plan_settings = @plan.settings(:export)
+ @exported_plan = ExportedPlan.new.tap do |ep|
+ ep.plan = @plan
+ ep.phase_id = @plan.phases.first.id
+ ep.format = :pdf
+ plan_settings = @plan.settings(:export)
- Settings::Template::DEFAULT_SETTINGS.each do |key, value|
- ep.settings(:export).send("#{key}=", plan_settings.send(key))
- end
- end
-
- begin
- @exported_plan.save!
- # If the template has multiple phases then append the phase title to the filename
- if @plan.phases.count > 1
- file_name = "#{@plan.title.gsub(/ /, "_")}-#{phase.title}"
- else
- file_name = @plan.title.gsub(/ /, "_")
- end
-
- respond_to do |format|
- format.pdf do
- @formatting = @plan.settings(:export).formatting
- render pdf: file_name,
- margin: @formatting[:margin],
- footer: {
- center: _('This document was generated by %{application_name}') % {application_name: Rails.configuration.branding[:application][:name]},
- font_size: 8,
- spacing: (@formatting[:margin][:bottom] / 2) - 4,
- right: '[page] of [topage]'
- }
- end
- end
- rescue ActiveRecord::RecordInvalid => e
- @phase_options = @plan.phases.order(:number).pluck(:title,:id)
- redirect_to show_export_plan_path(@plan), alert: _('Unable to download the DMP at this time.')
+ Settings::Template::DEFAULT_SETTINGS.each do |key, value|
+ ep.settings(:export).send("#{key}=", plan_settings.send(key))
end
end
+ # need to determine which phases to export
+ @a_q_ids = Answer.where(plan_id: @plan.id).pluck(:question_id).uniq
+ @a_s_ids = Question.where(id: @a_q_ids).pluck(:section_id).uniq
+ a_p_ids = Section.where(id: @a_s_ids).pluck(:phase_id).uniq
+ @phases = Phase.includes(sections: :questions).where(id: a_p_ids).order(:number)
+
+ begin
+ @exported_plan.save!
+ file_name = @plan.title.gsub(/ /, "_")
+
+ respond_to do |format|
+ format.pdf do
+ @formatting = @plan.settings(:export).formatting
+ render pdf: file_name,
+ margin: @formatting[:margin],
+ footer: {
+ center: _('This document was generated by %{application_name}') % {application_name: Rails.configuration.branding[:application][:name]},
+ font_size: 8,
+ spacing: (@formatting[:margin][:bottom] / 2) - 4,
+ right: '[page] of [topage]'
+ }
+ end
+ end
+ rescue ActiveRecord::RecordInvalid => e
+ @phase_options = @plan.phases.order(:number).pluck(:title,:id)
+ redirect_to show_export_plan_path(@plan), alert: _('Unable to download the DMP at this time.')
+ end
end
-
+
def duplicate
plan = Plan.find(params[:id])
authorize plan
diff --git a/app/controllers/roles_controller.rb b/app/controllers/roles_controller.rb
index abe77f2..4b79893 100644
--- a/app/controllers/roles_controller.rb
+++ b/app/controllers/roles_controller.rb
@@ -65,6 +65,22 @@
UserMailer.project_access_removed_notification(user, plan, current_user).deliver_now
redirect_to controller: 'plans', action: 'share', id: @role.plan.id
end
+
+ # This function makes user's role on a plan inactive - i.e. "removes" this from their plans
+ def deactivate
+ role = Role.find(params[:id])
+ authorize role
+ role.active = false
+ # if creator, remove from public plans list
+ if role.creator? && role.plan.publicly_visible?
+ role.plan.visibility = Plan.visibilities[:privately_visible]
+ role.plan.save
+ end
+ role.save
+ @plans = current_user.active_plans
+ flash[:notice] = _('Plan removed')
+ render "plans/index"
+ end
private
diff --git a/app/models/role.rb b/app/models/role.rb
index a51177d..588d55c 100644
--- a/app/models/role.rb
+++ b/app/models/role.rb
@@ -1,4 +1,5 @@
class Role < ActiveRecord::Base
+ after_initialize :set_defaults
include FlagShihTzu
##
@@ -37,6 +38,10 @@
end
end
+ def set_defaults
+ self.active = true if self.new_record?
+ end
+
end
# -----------------------------------------------------
diff --git a/app/models/user.rb b/app/models/user.rb
index 678c8dd..94918ec 100644
--- a/app/models/user.rb
+++ b/app/models/user.rb
@@ -82,6 +82,19 @@
end
##
+ # returns all active plans for a user
+ #
+ # @return [Plans]
+ def active_plans
+ plans = []
+ self.roles.includes(:plan).where(active: true).each do |r|
+ plans << r.plan
+ end
+ return plans
+ end
+
+
+ ##
# Returns the user's identifier for the specified scheme name
#
# @param the identifier scheme name (e.g. ORCID)
diff --git a/app/policies/plan_policy.rb b/app/policies/plan_policy.rb
index 3f8388c..99d9865 100644
--- a/app/policies/plan_policy.rb
+++ b/app/policies/plan_policy.rb
@@ -9,31 +9,31 @@
end
def show?
- @plan.readable_by?(@user.id)
+ @plan.readable_by?(@user.id) && Role.find_by(user_id: @user.id, plan_id: @plan.id).active
end
def share?
- @plan.readable_by?(@user.id)
+ @plan.readable_by?(@user.id) && Role.find_by(user_id: @user.id, plan_id: @plan.id).active
end
def export?
- @plan.readable_by?(@user.id)
+ @plan.readable_by?(@user.id) && Role.find_by(user_id: @user.id, plan_id: @plan.id).active
end
def show_export?
- @plan.readable_by?(@user.id)
+ @plan.readable_by?(@user.id) && Role.find_by(user_id: @user.id, plan_id: @plan.id).active
end
def update?
- @plan.editable_by?(@user.id)
+ @plan.editable_by?(@user.id) && Role.find_by(user_id: @user.id, plan_id: @plan.id).active
end
def destroy?
- @plan.editable_by?(@user.id)
+ @plan.editable_by?(@user.id) && Role.find_by(user_id: @user.id, plan_id: @plan.id).active
end
def status?
- @plan.readable_by?(@user.id)
+ @plan.readable_by?(@user.id) && Role.find_by(user_id: @user.id, plan_id: @plan.id).active
end
def possible_templates?
@@ -41,15 +41,15 @@
end
def duplicate?
- @plan.editable_by?(@user.id)
+ @plan.editable_by?(@user.id) && Role.find_by(user_id: @user.id, plan_id: @plan.id).active
end
def visibility?
- @plan.administerable_by?(@user.id)
+ @plan.administerable_by?(@user.id) && Role.find_by(user_id: @user.id, plan_id: @plan.id).active
end
def set_test?
- @plan.administerable_by?(@user.id)
+ @plan.administerable_by?(@user.id)&& Role.find_by(user_id: @user.id, plan_id: @plan.id).active
end
# TODO: These routes are no lonmger used
@@ -80,7 +80,7 @@
=end
def answer?
- @plan.readable_by?(@user.id)
+ @plan.readable_by?(@user.id) && Role.find_by(user_id: @user.id, plan_id: @plan.id).active
end
end
diff --git a/app/policies/role_policy.rb b/app/policies/role_policy.rb
index 00e2b79..c26d77c 100644
--- a/app/policies/role_policy.rb
+++ b/app/policies/role_policy.rb
@@ -19,4 +19,8 @@
def destroy?
@role.plan.owned_by?(@user.id)
end
+
+ def archive?
+ @role.user_id = @user.id
+ end
end
\ No newline at end of file
diff --git a/app/views/plans/index.html.erb b/app/views/plans/index.html.erb
index 6cd0dd6..9ead1b0 100644
--- a/app/views/plans/index.html.erb
+++ b/app/views/plans/index.html.erb
@@ -7,24 +7,24 @@
-<% if @plans.count > 0 %>
+<% if @plans.length > 0 %>
<%= _('The table below lists the plans that you have created, and any that have been shared with you by others.') %>
<%= _('These can be edited, shared, exported or deleted at anytime.')%>
<% else %>
- <%= _("Welcome.") %>
+ <%= _("Welcome.") %>
<%= _("You are now ready to create your first DMP.") %>
<%= _("Click the 'Create plan' button below to begin.")%>
<% end %>
-
+
- <% if @plans.count > 0 %>
+ <% if @plans.length > 0 %>
- <% if @plans.count > 10 %>
+ <% if @plans.length > 10 %>
-
- <% if plan.owned_by?(current_user.id) then %>
-
<%= link_to _('Delete'),
- plan_path(plan),
- method: :delete,
- data: {confirm: _('Are you sure you wish to delete this plan? If the plan is being shared with other users, by deleting it from your list, the plan will be deleted from their plan list as well')} %>
<% end %>
+ <% role = plan.roles.where(user_id: current_user.id).first %>
+ <% conf = (role.creator? && plan.publicly_visible?) ? _("Are you sure you wish to remove this public plan? This will remove it from the Public DMPs page but any collaborators will still be able to access it.") : _("Are you sure you wish to remove this plan? Any collaborators will still be able to access it.") %>
+
- <% @exported_plan.admin_details.each do |field|
- value = @exported_plan.send(field)
- if value.present? %>
-
<%= admin_field_t(field.to_s) -%> <%= value -%>
- <% else %>
-
<%= admin_field_t(field.to_s) -%> <%= _('-') %>
- <% end %>
- <% end %>
-
-
<%= _("Copyright information: The above plan creator(s) have agreed that others may use as much of the text of this plan as they would like in their own plans, and customise it as necessary. You do not need to credit the creator(s) as the source of the language used, but using any of the plan's text does not imply that the creator(s) endorse, or have any relationship to, your project or proposal") %>
+
+
+
+ <%= @plan.title %>
+
+
+
+
+
<%= @plan.title %>
- <% @exported_plan.sections.each do |section| %>
- <% questions = @exported_plan.questions_for_section(section.id)
- if questions.present?
- %>
-
<%= section.title %>
- <% questions.each do |question| %>
-
-
<%= raw question.text %>
- <% answer = @plan.answer(question.id, false) %>
- <% if answer.nil? then %>
-
- <% answer.question_options.each do |option| %>
-
<%= option.text %>
- <% end %>
-
-
- <% if question.option_comment_display == true then%>
- <% if !answer.text.nil? then %>
- <%= raw answer.text.gsub(/
(\s|
|<\/td>| )*(<\/tr>|
)/,"") %>
- <%end%>
- <%end%>
- <%else%>
-
- <% if !answer.text.nil? then %>
- <%= raw answer.text.gsub(/
(\s|
|<\/td>| )*(<\/tr>|
)/,"") %>
- <%end%>
- <% end %>
- <% end %>
-
+
<%= _("Copyright information: The above plan creator(s) have agreed that others may use as much of the text of this plan as they would like in their own plans, and customise it as necessary. You do not need to credit the creator(s) as the source of the language used, but using any of the plan's text does not imply that the creator(s) endorse, or have any relationship to, your project or proposal") %>
+ <% @phases.each do |phase| %>
+
+
<%= phase.title %>
+ <% Section.where(phase_id: phase.id, id: @a_s_ids).order(:number).each do |section| %>
+
<%= section.title %>
+ <% Question.where(section_id: section, id: @a_q_ids).order(:number).each do |question| %>
+
+ <% answer.question_options.each do |option| %>
+
<%= option.text %>
<% end %>
+
+
+ <% if question.option_comment_display == true %>
+ <% if !answer.text.nil? %>
+ <%= raw answer.text.gsub(/
(\s|
|<\/td>| )*(<\/tr>|
)/,"") %>
+ <% end %>
+ <% end %>
+ <% else %>
+
+ <% if !answer.text.nil? %>
+ <%= raw answer.text.gsub(/
(\s|
|<\/td>| )*(<\/tr>|
)/,"") %>
+ <% end %>
+ <% end %>
<% end %>
+
<% end %>
-
+ <% end %>
+ <% end %>
+
\ No newline at end of file
diff --git a/app/views/plans/share.html.erb b/app/views/plans/share.html.erb
index b331136..7347bb0 100644
--- a/app/views/plans/share.html.erb
+++ b/app/views/plans/share.html.erb
@@ -1,7 +1,7 @@
<% javascript('views/plans/share.js') %>
-