require "study_engine/config" require "cat_engine/question" module StudyEngine class AssessmentsController < ::StudyEngine::ApplicationController expose :assessment, :welcome_content, :events, :enabled_application_methods def index @welcome_content = StudyEngine.welcome_content @assessment = BeginAssessmentForm.new assessment.coordinator = current_user end def create @assessment = BeginAssessmentForm.new(params[:assessment]) assessment.coordinator = current_user if assessment.save if assessment.by_url? flash.now.notice = "An assessment has been created." ::StudyEngine::Mailer.assessment_url_notification(current_user.email, assessment).deliver_now render :create elsif assessment.by_mail? flash.now.notice = "An assessment has been created." render :create else redirect_to assessment end elsif assessment.restartable? flash.now.alert = "Partial assessment data already exists for this ID number and event. Would you like to resume or restart?" render :restart else flash.now.alert = assessment.errors.full_messages.join(", ") render :index end end def show if assessment.complete? if assessment.completed_at.nil? assessment.touch :completed_at send_notification end render :complete else render :show end end def edit redirect_to assessment end def update assessment.save_answer Answer.new(question: CatEngine::Question.new(params[:cat_engine_question])) redirect_to assessment end def timeout @assessment = BeginAssessmentForm.new_from_model(assessment) flash.now.alert = "Assessment timed out due to inactivity. Would you like to resume or restart?" render :restart end def notification send_notification head :ok end private def send_notification if email = assessment.coordinator_email ::StudyEngine::Mailer.assessment_recorded_notification(email, assessment).deliver_now end end def assessment @assessment ||= Assessment.find(params[:id]) end def current_user_required? return true unless %w[show update notification].include?(params[:action]) && assessment.by_url? end def events StudyEngine.events end def enabled_application_methods StudyEngine.enabled_application_methods end end end