diff --git a/app/assets/javascripts/protocols/steps.js.erb b/app/assets/javascripts/protocols/steps.js.erb index 6b2b62f58..f56d96692 100644 --- a/app/assets/javascripts/protocols/steps.js.erb +++ b/app/assets/javascripts/protocols/steps.js.erb @@ -24,6 +24,38 @@ function applyCheckboxCallBack() { }); } +// Complete mymodule +function complete_my_module_actions() { + var modal = $('#completed-task-modal'); + + modal.find('[data-action="complete"]') + .off().on().click(function(event) { + event.stopPropagation(); + event.preventDefault(); + event.stopImmediatePropagation(); + $.ajax({ + url: modal.data('url'), + type: 'GET', + success: function(data) { + var task_button = $("[data-action='complete-task']"); + task_button.attr('data-action', 'uncomplete-task'); + task_button.find('.btn') + .removeClass('btn-primary').addClass('btn-greyed'); + $('.task-due-date').html(data.module_header_due_date_label); + $('.task-state-label').html(data.module_state_label); + task_button + .find('button') + .html(' ' + + data.task_button_title); + modal.modal('hide'); + }, + error: function() { + modal.modal('hide'); + } + }); + }); +} + // Sets callback for completing/uncompleting step function applyStepCompletedCallBack() { // First, remove old event handlers, as we use turbolinks @@ -46,14 +78,10 @@ function applyStepCompletedCallBack() { button = step.find("[data-action='complete-step']"); button.attr("data-action", "uncomplete-step"); button.find(".btn").removeClass("btn-primary").addClass("btn-default"); - if (data.task_completed) { - task_button = $("[data-action='complete-task']"); - task_button.attr('data-action', 'uncomplete-task'); - task_button.find('.btn') - .removeClass('btn-primary').addClass('btn-default'); - $('.task-due-date').html(data.module_header_due_date_label); - $('.task-state-label').html(data.module_state_label); - task_button.find('button').html(data.task_button_title); + + if (data.task_ready_to_complete) { + $('#completed-task-modal').modal('show'); + complete_my_module_actions(); } } else { diff --git a/app/controllers/my_modules_controller.rb b/app/controllers/my_modules_controller.rb index 290d3d42a..9aedef3a7 100644 --- a/app/controllers/my_modules_controller.rb +++ b/app/controllers/my_modules_controller.rb @@ -3,26 +3,27 @@ class MyModulesController < ApplicationController include TeamsHelper include InputSanitizeHelper - before_action :load_vars, only: [ - :show, :update, :destroy, - :description, :due_date, :protocols, :results, - :samples, :activities, :activities_tab, - :assign_samples, :unassign_samples, - :delete_samples, :toggle_task_state, - :samples_index, :archive] - before_action :load_vars_nested, only: [:new, :create] - before_action :check_edit_permissions, only: [ - :update, :description, :due_date - ] - before_action :check_destroy_permissions, only: [:destroy] - before_action :check_view_info_permissions, only: [:show] - before_action :check_view_activities_permissions, only: [:activities, :activities_tab] - before_action :check_view_protocols_permissions, only: [:protocols] - before_action :check_view_results_permissions, only: [:results] - before_action :check_view_samples_permissions, only: [:samples, :samples_index] - before_action :check_view_archive_permissions, only: [:archive] - before_action :check_assign_samples_permissions, only: [:assign_samples] - before_action :check_unassign_samples_permissions, only: [:unassign_samples] + before_action :load_vars, + only: %I[show update destroy description due_date protocols + results samples activities activities_tab + assign_samples unassign_samples delete_samples + toggle_task_state samples_index archive + complete_my_module] + before_action :load_vars_nested, only: %I[new create] + before_action :check_edit_permissions, + only: %I[update description due_date] + before_action :check_destroy_permissions, only: :destroy + before_action :check_view_info_permissions, only: :show + before_action :check_view_activities_permissions, + only: %I[activities activities_tab] + before_action :check_view_protocols_permissions, only: :protocols + before_action :check_view_results_permissions, only: :results + before_action :check_view_samples_permissions, + only: %I[samples samples_index] + before_action :check_view_archive_permissions, only: :archive + before_action :check_assign_samples_permissions, only: :assign_samples + before_action :check_unassign_samples_permissions, only: :unassign_samples + before_action :check_complete_my_module_perimission, only: :complete_my_module layout 'fluid'.freeze @@ -396,6 +397,31 @@ class MyModulesController < ApplicationController end end + def complete_my_module + respond_to do |format| + if @my_module.uncompleted? && @my_module.check_completness_status + @my_module.complete + @my_module.save + task_completion_activity + format.json do + render json: { + task_button_title: t('my_modules.buttons.uncomplete'), + module_header_due_date_label: render_to_string( + partial: 'my_modules/module_header_due_date_label.html.erb', + locals: { my_module: @my_module } + ), + module_state_label: render_to_string( + partial: 'my_modules/module_state_label.html.erb', + locals: { my_module: @my_module } + ) + }, status: :ok + end + else + format.json { render json: {}, status: :unprocessable_entity } + end + end + end + private def task_completion_activity @@ -485,6 +511,10 @@ class MyModulesController < ApplicationController end end + def check_complete_my_module_perimission + render_403 unless can_complete_module(@my_module) + end + def my_module_params params.require(:my_module).permit(:name, :description, :due_date, :archived) diff --git a/app/controllers/steps_controller.rb b/app/controllers/steps_controller.rb index d02ecf455..74f384a2d 100644 --- a/app/controllers/steps_controller.rb +++ b/app/controllers/steps_controller.rb @@ -330,10 +330,13 @@ class StepsController < ApplicationController respond_to do |format| if step - completed = params[:completed] == "true" + completed = params[:completed] == 'true' protocol = step.protocol - authorized = ((completed and can_complete_step_in_protocol(protocol)) or (!completed and can_uncomplete_step_in_protocol(protocol))) + authorized = ( + (completed and can_complete_step_in_protocol(protocol)) || + (!completed and can_uncomplete_step_in_protocol(protocol)) + ) if authorized changed = step.completed != completed @@ -346,15 +349,15 @@ class StepsController < ApplicationController if step.save if protocol.in_module? - task_completed = protocol.my_module.check_completness + ready_to_complete = protocol.my_module.check_completness_status end # Create activity if changed completed_steps = protocol.steps.where(completed: true).count all_steps = protocol.steps.count - str = completed ? "activities.complete_step" : - "activities.uncomplete_step" + str = 'activities.uncomplete_step' + str = 'activities.complete_step' if completed message = t( str, @@ -386,27 +389,14 @@ class StepsController < ApplicationController else t('protocols.steps.options.uncomplete_title') end - task_button_title = - t('my_modules.buttons.uncomplete') if task_completed format.json do - if task_completed + if ready_to_complete && protocol.my_module.uncompleted? render json: { - new_title: localized_title, - task_completed: task_completed, - task_button_title: task_button_title, - module_header_due_date_label: render_to_string( - partial: 'my_modules/module_header_due_date_label.html.erb', - locals: { my_module: step.protocol.my_module } - ), - module_state_label: render_to_string( - partial: 'my_modules/module_state_label.html.erb', - locals: { my_module: step.protocol.my_module } - ) - }, - status: :accepted + task_ready_to_complete: true, + new_title: localized_title + }, status: :ok else - render json: { new_title: localized_title }, - status: :accepted + render json: { new_title: localized_title }, status: :ok end end else diff --git a/app/models/my_module.rb b/app/models/my_module.rb index ffb2caa32..bc0861014 100644 --- a/app/models/my_module.rb +++ b/app/models/my_module.rb @@ -374,17 +374,14 @@ class MyModule < ActiveRecord::Base state == 'completed' end - # Mark task completed if all steps become completed - def check_completness + # Check if my_module is ready to become completed + def check_completness_status if protocol && protocol.steps.count > 0 completed = true protocol.steps.find_each do |step| completed = false unless step.completed end - if completed - update_attributes(state: 'completed', completed_on: DateTime.now) - return true - end + return true if completed end false end diff --git a/app/views/my_modules/_complete_task_modal.html.erb b/app/views/my_modules/_complete_task_modal.html.erb new file mode 100644 index 000000000..9c58d711a --- /dev/null +++ b/app/views/my_modules/_complete_task_modal.html.erb @@ -0,0 +1,28 @@ +