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| %> - > +<% allow_visibility = @plan.visibility_allowed? %> +<%= form_for(@plan, url: visibility_plan_path, method: :post, html: { id: 'set_visibility'}) do |f| %> + >
- <%= f.label :visibility, raw("#{f.radio_button :visibility, :privately_visible} #{_('Private: restricted to me and my collaborators')}") %> + <%= f.label :visibility_privately_visible, raw("#{f.radio_button :visibility, :privately_visible} #{_('Private: restricted to me and my collaborators')}") %>
- <%= f.label :visibility, raw("#{f.radio_button :visibility, :organisationally_visible} #{_('Organisation: anyone at my organisation can view')}") %> + <%= f.label :visibility_organisationally_visible, raw("#{f.radio_button :visibility, :organisationally_visible} #{_('Organisation: anyone at my organisation can view')}") %>
- <%= f.label :visibility, raw("#{f.radio_button :visibility, :publicly_visible} #{_('Public: anyone can view')}") %> + <%= f.label :visibility_publicly_visible, raw("#{f.radio_button :visibility, :publicly_visible} #{_('Public: anyone can view')}") %>
diff --git a/app/views/user_mailer/plan_visibility.html.erb b/app/views/user_mailer/plan_visibility.html.erb new file mode 100644 index 0000000..951fcfc --- /dev/null +++ b/app/views/user_mailer/plan_visibility.html.erb @@ -0,0 +1,36 @@ +<% + tool_name = Rails.configuration.branding[:application][:name] + username = @user.name + plan_title = @plan.title + plan_visibility = Plan.visibility_message(@plan.visibility.to_sym) + helpdesk_email = Rails.configuration.branding[:organisation][:helpdesk_email] + contact_us_url = Rails.configuration.branding[:organisation][:contact_us_url] + email_subject = _('Query or feedback related to %{tool_name}') %{ :tool_name => tool_name } +%> +<% FastGettext.with_locale FastGettext.default_locale do %> +

+ <%= _('Hello %{username}') %{ :username => username } %> +

+

+ <%= _('The plan %{plan_title} had its visibility changed to %{plan_visibility}.') %{ :plan_title => plan_title, :plan_visibility => plan_visibility } %> +

+

+ <%= _('Visibility definitions:') %> +

+

+

+ <%= _('If you have questions pertaining to this action, please visit the My Dashboard page in %{tool_name}' %{ :tool_name => tool_name }) %> +

+

+ <%= _('All the best') %> +
+ <%= _('The %{tool_name} team') %{ :tool_name => tool_name } %> +

+

+ <%= _('You may change your notification preferences on your profile page.') %> <%= _('Please do not reply to this email.') %> <%= raw(_('If you have any questions or need help, please contact us at %{helpdesk_email} or visit %{contact_us_url}') %{ :helpdesk_email => mail_to(helpdesk_email, helpdesk_email, subject: email_subject), :contact_us_url => link_to(contact_us_url, contact_us_url) }) %> +

+<% end %> \ No newline at end of file diff --git a/config/application.rb b/config/application.rb index d4fac8e..a498903 100644 --- a/config/application.rb +++ b/config/application.rb @@ -92,6 +92,6 @@ config.default_plan_visibility = 'privately_visible' # The percentage of answered questions needed to enable the plan visibility section of the Share plan page - config.default_plan_percentage_answered = 50.00 + config.default_plan_percentage_answered = 50 end end diff --git a/lib/assets/javascripts/views/plans/share.js b/lib/assets/javascripts/views/plans/share.js index 0bf0d75..6b10b8b 100644 --- a/lib/assets/javascripts/views/plans/share.js +++ b/lib/assets/javascripts/views/plans/share.js @@ -1,19 +1,37 @@ import * as notifier from '../../utils/notificationHelper'; import ariatiseForm from '../../utils/ariatiseForm'; +import { isObject, isString } from '../../utils/isType'; $(() => { // Invite Collaborators form on the Share page ariatiseForm({ selector: '#new_role' }); - const xhrRequest = (el) => { + const request = (el) => { const form = $(el).closest('form'); - - $.ajax({ + return $.ajax({ method: $(form).attr('method'), url: $(form).attr('action'), data: $(form).serializeArray(), dataType: 'json', - }).done((data) => { + }); + }; + + $('#set_visibility [name="plan[visibility]"]').click((e) => { + request(e.target).then((data) => { + if (isObject(data) && isString(data.msg)) { + notifier.renderNotice(data.msg); + } + }, (jqXHR, textStatus) => { + if (isObject(jqXHR.responseJSON)) { + notifier.renderAlert(jqXHR.responseJSON.msg); + } else { + notifier.renderAlert(textStatus); + } + }); + }); + + $('.change_plan_role select').change((e) => { + request(e.target).done((data) => { if (data.code === 1 && data.msg && data.msg !== '') { notifier.renderNotice(data.msg); } else { @@ -22,13 +40,5 @@ }, () => { // TODO adequate error handling for network error }); - }; - - $('#set_visibility [name="plan[visibility]"]').click((e) => { - xhrRequest(e.currentTarget); - }); - - $('.change_plan_role select').change((e) => { - xhrRequest(e.currentTarget); }); }); diff --git a/test/mailers/previews/user_mailer_preview.rb b/test/mailers/previews/user_mailer_preview.rb index a55b3be..39e41aa 100644 --- a/test/mailers/previews/user_mailer_preview.rb +++ b/test/mailers/previews/user_mailer_preview.rb @@ -14,4 +14,8 @@ user = User.find_by(email: 'super_admin@example.com') UserMailer.api_token_granted_notification(user) end + def plan_visibility + user = User.find_by(email: 'super_admin@example.com') + UserMailer.plan_visibility(user, user.plans.first) + end end \ No newline at end of file