![]() Server : Apache System : Linux server2.corals.io 4.18.0-348.2.1.el8_5.x86_64 #1 SMP Mon Nov 15 09:17:08 EST 2021 x86_64 User : corals ( 1002) PHP Version : 7.4.33 Disable Function : exec,passthru,shell_exec,system Directory : /home/corals/dceprojects.corals.io/Corals/modules/Survey/Classes/ |
<?php namespace Corals\Modules\Survey\Classes; use Corals\Modules\Survey\Contracts\ModelSurveyContract; use Corals\Modules\Survey\Models\Question; use Corals\Modules\Survey\Models\Survey; use Illuminate\Support\Arr; class Surveys { /** * @var */ protected $surveyResponses; /** * @param bool $active * @return mixed */ public function getSurveysList(bool $active = true) { return Survey::query()->when($active, function ($query) { $query->active(); })->pluck('name', 'id') ->toArray(); } /** * @param array $except * @param bool $hashedId * @return mixed */ public function getQuestionsList($except = [], bool $hashedId = false) { $except = array_filter(Arr::wrap($except)); $questions = Question::query() ->where('status', 'active') ->when($except, function ($query) use ($except) { $query->whereNotIn('id', $except); })->pluck('title', 'id'); if ($hashedId) { $questions = $questions->mapWithKeys(function ($v, $id) { return [ hashids_encode($id) => $v ]; }); } return $questions->toArray(); } /** * @param Survey $survey * @param ModelSurveyContract|null $model * @return array */ public function getSurveyQuestions(Survey $survey, ModelSurveyContract $model = null) { $questions = collect(); $this->surveyResponses = null; if ($model) { $this->setSurveyResponses($survey, $model); } $survey->questions()->active() ->each(function (Question $question) use (&$questions) { $questions->push($this->getQuestionArray($question)); }); return $questions->toArray(); } /** * @param Survey $survey * @param ModelSurveyContract $model */ protected function setSurveyResponses(Survey $survey, ModelSurveyContract $model): void { $this->surveyResponses = $model->surveyResponses() ->with('answer') ->where('survey_id', $survey->id) ->get(); } /** * @param $question * @return array */ public function getQuestionAnswers($question) { $answers = []; foreach ($question->answers()->active()->get() as $answer) { $nextQuestionArray = null; if ($answer->next_question_id) { //means we're out of the sequence //no need to send survey $nextQuestionArray = $this->getQuestionArray( $answer->nextQuestion, $isRouting = true ); } $answers[] = [ 'hashed_id' => $answer->hashed_id, 'text' => $answer->text, 'score' => $answer->score, 'next_question' => $nextQuestionArray ?? null ]; } return $answers; } /** * @param $question * @return array */ public function getQuestion($question) { foreach ($question->answers()->active()->get() as $answer) { $answers[] = [ 'hashed_id' => $answer->hashed_id, 'text' => $answer->text, 'score' => $answer->score, ]; } return [ 'hashed_id' => $question->hashed_id, 'title' => $question->title, 'description' => $question->description, 'answers' => $answers ?? [], 'type' => $question->type ]; } /** * @param $question * @param bool $isRouting * @return array */ protected function getQuestionArray($question, bool $isRouting = false) { $answers = $this->getQuestionAnswers($question); if (!$answers && $question->next_question_id) { $nextQuestion = $this->getQuestionArray($question->nextQuestion, true); } if ($this->surveyResponses) { $responseAnswer = $this->surveyResponses->where('question_id', $question->id)->first(); $responseAnswerText = data_get($responseAnswer, 'answer_text'); //in case of radio btn. the value is the hashed id of the answer. $responseAnswerHashedId = data_get($responseAnswer, 'answer.hashed_id'); } return [ 'hashed_id' => $question->hashed_id, 'title' => $question->title, 'description' => $question->description, 'type' => $question->type, 'is_routing' => $isRouting, 'next_question' => $nextQuestion ?? null, 'response_answer' => $responseAnswerText ?? null, 'response_answer_hashed_id' => $responseAnswerHashedId ?? null, 'answers' => $answers ]; } /** * @param $currentQuestionId * @param $survey * @return mixed|null */ protected function getSurveyNextQuestion($currentQuestionId, $survey) { $getNextAndBreak = false; foreach ($survey->questions()->active()->get() as $question) { if ($getNextAndBreak) { $nextQuestion = $question; break; } if ($question->id == $currentQuestionId) { $getNextAndBreak = true; } } return $nextQuestion ?? null; } /** * @return \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Eloquent\Model|object|null */ public function getDefaultSurvey() { return Survey::query()->active()->orderBy('created_at')->first(); } public function getSurveyAsTree($survey) { $questions = $survey->questions()->with('answers.nextQuestion')->get(); $surveyData = []; foreach ($questions as $question) { $surveyData[] = $this->formatQuestion($question); } return $surveyData; } private function formatQuestion($question) { $children = []; if ($question->answers->count()) { foreach ($question->answers as $answer) { $nextQuestion = $answer->nextQuestion; $children[] = [ 'name' => $answer->text, 'children' => $nextQuestion ? [$this->formatQuestion($nextQuestion)] : [] ]; } } else { if ($question->nextQuestion) { $nextQuestion = $question->nextQuestion; $children = [$this->formatQuestion($nextQuestion)]; } } return [ 'name' => $question->title, 'children' => $children ]; } }