mirror of
https://gitlab.com/CIEFWorldwideSdnBhd/exchange-2.0.git
synced 2026-08-19 04:23:55 +00:00
173 lines
7.0 KiB
PHP
173 lines
7.0 KiB
PHP
<?php
|
|
|
|
namespace App\Classes\Modules\Questionnaires\ControllersLogic;
|
|
|
|
|
|
use App\Classes\General\Abstracts\AbstractControllerLogic;
|
|
use App\Classes\Modules\Questionnaires\Standards\Rules\CanFetchQuestion;
|
|
use App\Classes\Modules\Questionnaires\Processors\SaveAnswerV1AdminWFProcessor;
|
|
use App\Classes\Modules\Questionnaires\Processors\FetchNextQuestionV1AdminWFProcessor;
|
|
use App\Classes\Modules\Questionnaires\Processors\FetchFirstQuestionV1AdminWFProcessor;
|
|
use App\Classes\Modules\Questionnaires\Services\ListsQuestionUserAnswerSelected;
|
|
use App\Classes\Modules\Accounts\Services\CreatesKeyValuePair;
|
|
use App\Classes\Modules\Accounts\DataTransferObjects\KeyValuePairObject;
|
|
use Illuminate\Http\JsonResponse;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\Log;
|
|
use Illuminate\Support\Facades\Auth;
|
|
use App\Http\Resources\QuestionResource;
|
|
use App\Models\Booking;
|
|
|
|
class UpdateNextQuestionV1AdminWFLogic extends AbstractControllerLogic
|
|
{
|
|
|
|
/**
|
|
* @return array
|
|
*/
|
|
protected function notification():array {
|
|
return [
|
|
'title' => 'Retrieved Admin Workflow Next Question',
|
|
'message' => 'You have successfully retrieved a Admin Workflow Next Question'
|
|
];
|
|
}
|
|
|
|
/** @var CanFetchQuestion */
|
|
private $canFetchQuestion;
|
|
|
|
/** @var SaveAnswerV1AdminWFProcessor */
|
|
private $saveAnswerProcessor;
|
|
|
|
/** @var FetchNextQuestionV1AdminWFProcessor */
|
|
private $fetchNextQuestionQAProcessor;
|
|
|
|
/** @var FetchFirstQuestionV1AdminWFProcessor */
|
|
private $fetchFirstQuestionQAProcessor;
|
|
|
|
/** @var ListsQuestionUserAnswerSelected */
|
|
private $listsUserAnswerSelected;
|
|
|
|
/** @var CreatesKeyValuePair */
|
|
private $createsKeyValuePair;
|
|
|
|
/**
|
|
* UpdateNextQuestionV1AdminWFLogic constructor.
|
|
* @param CanFetchQuestion $canFetchQuestion
|
|
* @param SaveAnswerV1AdminWFProcessor $saveAnswerProcessor
|
|
* @param FetchNextQuestionV1AdminWFProcessor $fetchNextQuestionQAProcessor
|
|
* @param FetchFirstQuestionV1AdminWFProcessor $fetchFirstQuestionQAProcessor
|
|
* @param ListsQuestionUserAnswerSelected $listsUserAnswerSelected
|
|
* @param CreatesKeyValuePair $createsKeyValuePair
|
|
*/
|
|
public function __construct(CanFetchQuestion $canFetchQuestion, SaveAnswerV1AdminWFProcessor $saveAnswerProcessor, FetchNextQuestionV1AdminWFProcessor $fetchNextQuestionQAProcessor, FetchFirstQuestionV1AdminWFProcessor $fetchFirstQuestionQAProcessor, ListsQuestionUserAnswerSelected $listsUserAnswerSelected, CreatesKeyValuePair $createsKeyValuePair)
|
|
{
|
|
$this->canFetchQuestion = $canFetchQuestion;
|
|
$this->saveAnswerProcessor = $saveAnswerProcessor;
|
|
$this->fetchNextQuestionQAProcessor = $fetchNextQuestionQAProcessor;
|
|
$this->fetchFirstQuestionQAProcessor = $fetchFirstQuestionQAProcessor;
|
|
$this->listsUserAnswerSelected = $listsUserAnswerSelected;
|
|
$this->createsKeyValuePair = $createsKeyValuePair;
|
|
}
|
|
|
|
|
|
/**
|
|
* @param Request $request
|
|
* @return JsonResponse
|
|
* @throws MalformedRequestException
|
|
* @throws ResourceNotFoundException
|
|
*/
|
|
public function logic(Request $request) : JsonResponse
|
|
{
|
|
$questionId = 0;
|
|
$userId = Auth::user()->id;
|
|
$currentQuestion = $request->question;
|
|
$questionId = $currentQuestion['id'];
|
|
$questionType = $currentQuestion['question_type'];
|
|
$currentQuestionNumber = $currentQuestion['question_number'];
|
|
$answerNextQuestionNumber = null;
|
|
$answerValue = null;
|
|
$timeUsedSeconds = 0;
|
|
$reference = null;
|
|
$questionMetadata = null;
|
|
|
|
$this->canFetchQuestion->passes();
|
|
|
|
if ($request->has('answerObj')) {
|
|
$answer = $request->answerObj;
|
|
$answerNextQuestionNumber = $answer['next_question_number'];
|
|
$answerValue = $answer['value'];
|
|
Log::info('answerNextQuestionNumber: '. $answerNextQuestionNumber);
|
|
Log::info('answerValue: '. $answerValue);
|
|
}
|
|
|
|
if ($request->has('extra')) {
|
|
$extra = $request->extra;
|
|
$questionMetadata = $extra['questionMetadata'] ?? null;
|
|
$timeUsedSeconds = intval($extra['timeUsedSeconds']) ?? 0;
|
|
$reference = $extra['session_id'];
|
|
}
|
|
|
|
//Save answer given by user (both next and previous)
|
|
$filesUpload = $request->input('files');
|
|
$answerOptionId = 0;
|
|
if ($request->has('answerObj')) {
|
|
$answerOptionId = intval($request->answerObj['id']);
|
|
}
|
|
$answerInText = $request->answer;
|
|
if ($request->has('isPrevious')){
|
|
$answerInText = "go_back";
|
|
}
|
|
$answer = $this->saveAnswerProcessor->execute($userId, 0, $questionId, $questionType, $questionMetadata, $answerInText, $answerOptionId, $filesUpload, $timeUsedSeconds, $request->has('isPrevious'), $reference);
|
|
|
|
//Marked data that has already been processed so that it does not appear again
|
|
$isNoGoingBack = null;
|
|
if($currentQuestion && $currentQuestion['is_end'] === 1){
|
|
$booking = Booking::where('id', $questionMetadata['id'])->first();
|
|
|
|
$key1 = "admin_workflow_processed";
|
|
$key2 = "admin_workflow_processing";
|
|
if (strpos($currentQuestion['question_number'], '1688') === 0) {
|
|
$key1 = '1688_'.$key1;
|
|
$key2 = '1688_'.$key2;
|
|
}
|
|
if (strpos($currentQuestion['question_number'], 'fill_po') === 0) {
|
|
$key1 = 'fill_po_'.$key1;
|
|
$key2 = 'fill_po_'.$key2;
|
|
}
|
|
if (strpos($currentQuestion['question_number'], 'approve_po') === 0) {
|
|
$key1 = 'approve_po_'.$key1;
|
|
$key2 = 'approve_po_'.$key2;
|
|
}
|
|
|
|
$processingRecord = $booking->attributesKVP()->where('key', $key2)->first();
|
|
if ($processingRecord) {
|
|
$processingRecord->delete();
|
|
}
|
|
|
|
$kvp = $booking->attributesKVP()->where('key', $key1)->first();
|
|
|
|
if(!$kvp){
|
|
$keyValuePairObject = new KeyValuePairObject($key1, true);
|
|
$this->createsKeyValuePair->execute($booking, $keyValuePairObject);
|
|
}
|
|
// $isNoGoingBack = 1;
|
|
}
|
|
|
|
$previousAnswer = null;
|
|
if ($request->has('extra')) {
|
|
$extra = $request->extra;
|
|
$reference = $extra['session_id'];
|
|
$previousAnswer = $this->listsUserAnswerSelected->execute(['user_id' => $userId, 'reference' => $reference, 'is_previous' => 0, 'order_by' => (object)['column' => 'id','DESC' => true]])[0];
|
|
}
|
|
|
|
//Get returned question (previous or next)
|
|
$returnQuestion = $this->fetchNextQuestionQAProcessor->execute($request, $previousAnswer);
|
|
|
|
//When a questionnaire ended, return back the first question
|
|
if(is_null($returnQuestion)){
|
|
$returnQuestion = $this->fetchFirstQuestionQAProcessor->execute($request);
|
|
}
|
|
|
|
return $this->resourceResponse(new QuestionResource($returnQuestion, $userId, $request->has('isPrevious'), $isNoGoingBack, $previousAnswer));
|
|
}
|
|
}
|