require "devise" require_dependency "think_feel_do_engine/application_controller" module ThinkFeelDoEngine # Uses the Navigator to direct a participant"s flow through the site. class NavigatorController < ApplicationController include Concerns::NavigatorEnabled before_action :authenticate_participant!, :instantiate_navigator URL_GENERATION_ALERT = "We're sorry, the content you were looking "\ " for couldn't be found." FIRST_CONTENT_POSITION = 1 layout "tool" def show_context context_name = params[:context_name] || home_tool.try(:title) @navigator.initialize_context(context_name) render "show_content" end def show_location @navigator.initialize_location( module_id: params[:module_id], provider_id: params[:provider_id], content_position: params[:content_position] ) complete_task_status(params[:module_id], params[:provider_id], params[:content_position]) render "show_content" rescue ActiveRecord::RecordNotFound, ActionView::Template::Error @navigator.initialize_context(home_tool.title) flash[:alert] = "Unable to find that module." render "show_content" end def show_next_content mark_engagement_completed @navigator.fetch_next_content if @navigator.current_module redirect_to navigator_location_path( module_id: @navigator.current_module.try(:id), provider_id: @navigator.current_content_provider .try(:id), content_position: @navigator.content_position) else next_content_not_found end end def show_previous_content @navigator.fetch_previous_content redirect_to navigator_location_path( module_id: @navigator.current_module.try(:id), provider_id: @navigator.current_content_provider.try(:id), content_position: @navigator.content_position ) end # Seek and redirect to the next content provider in order. def show_next_provider current_provider = @navigator.current_content_provider next_provider = current_provider while next_provider == current_provider @navigator.fetch_next_content next_provider = @navigator.current_content_provider end redirect_to navigator_location_path( module_id: @navigator.current_module.try(:id), provider_id: @navigator.current_content_provider.try(:id), content_position: @navigator.content_position ) end private def last_engagement current_participant .active_membership .task_statuses .for_content_module(navigator_content_module) .try(:last) .try(:engagements) .try(:last) end def complete_task_status(module_id, provider_id, content_id) content_module = BitCore::ContentModule.find(module_id) membership = current_participant .active_membership @task_status = membership .task_statuses .most_recent_for_content_module(content_module, membership.day_in_study) .first if @task_status && (!content_specified?(provider_id, content_id) || first_content_slide?(content_module, provider_id, content_id)) @task_status .engagements .build end @task_status.mark_complete unless @task_status.nil? end def first_content_slide?(content_module, provider_id, content_id) first_content_provider = content_module .content_providers .order(:position) .first provider_id == first_content_provider.id.to_s && content_id == FIRST_CONTENT_POSITION.to_s end def mark_engagement_completed if navigator_content_module && last_engagement && !@navigator.current_content_provider.exists?( @navigator.content_position + 1) last_engagement.update_attributes(completed_at: DateTime.current) end end def content_specified?(provider_id, content_id) !provider_id.blank? && !content_id.blank? end def module_exist? @navigator .current_module .provider_exists?(@navigator.provider_position + 1) end def navigator_content_module @navigator.current_module || @navigator .current_content_provider .content_module end def provider_exist? @navigator .current_content_provider .exists?(@navigator.content_position + 1) end def home_tool current_participant.current_group .arm .bit_core_tools.find_by_type("Tools::Home") end def next_content_not_found Raven.capture_message "URL Generation error in NavigatorController", extra: { participant_id: current_participant.try(:id), params: params } if defined?(Raven) redirect_to main_app.root_path, alert: URL_GENERATION_ALERT end end end