adds validation
This commit is contained in:
@@ -11,6 +11,7 @@
|
||||
use Illuminate\Http\RedirectResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Arr;
|
||||
use Illuminate\Validation\ValidationException;
|
||||
use Inertia\Inertia;
|
||||
use Inertia\Response;
|
||||
|
||||
@@ -109,6 +110,8 @@ private function saveAnswers(Session $session, array $answers): void
|
||||
*/
|
||||
private function completeSession(Session $session): RedirectResponse
|
||||
{
|
||||
$this->validateSessionCompletion($session);
|
||||
|
||||
$scoringService = new ScoringService;
|
||||
$score = $scoringService->calculateScore($session);
|
||||
$result = $scoringService->determineResult($score);
|
||||
@@ -125,6 +128,76 @@ private function completeSession(Session $session): RedirectResponse
|
||||
return redirect()->route('sessions.result', $session);
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate that all required fields are answered before session completion.
|
||||
*/
|
||||
private function validateSessionCompletion(Session $session): void
|
||||
{
|
||||
$session->load(['category.questionGroups.questions', 'answers']);
|
||||
|
||||
$errors = [];
|
||||
|
||||
foreach ($session->category->questionGroups as $questionGroup) {
|
||||
foreach ($questionGroup->questions as $question) {
|
||||
$answer = $session->answers->firstWhere('question_id', $question->id);
|
||||
|
||||
$this->validateRadioAnswer($question, $answer, $errors);
|
||||
$this->validateDetailsAnswer($question, $answer, $errors);
|
||||
}
|
||||
}
|
||||
|
||||
if (Arr::exists($errors, 0)) {
|
||||
throw ValidationException::withMessages([
|
||||
'complete' => $errors,
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate that radio button questions have an answer selected.
|
||||
*/
|
||||
private function validateRadioAnswer($question, $answer, array &$errors): void
|
||||
{
|
||||
$hasRadioButtons = $question->has_yes || $question->has_no || $question->has_na;
|
||||
|
||||
if ($hasRadioButtons && (! $answer || $answer->value === null)) {
|
||||
$errors[] = "Question '{$question->text}' requires an answer.";
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate that questions with required details have text values provided.
|
||||
*/
|
||||
private function validateDetailsAnswer($question, $answer, array &$errors): void
|
||||
{
|
||||
$details = $question->details;
|
||||
$hasRadioButtons = $question->has_yes || $question->has_no || $question->has_na;
|
||||
|
||||
if ($details === 'required') {
|
||||
if (! $answer || empty(trim(Arr::get($answer->toArray(), 'text_value', '')))) {
|
||||
$errors[] = "Question '{$question->text}' requires details to be provided.";
|
||||
}
|
||||
}
|
||||
|
||||
if ($details === 'req_on_yes' && $answer && $answer->value === 'yes') {
|
||||
if (empty(trim(Arr::get($answer->toArray(), 'text_value', '')))) {
|
||||
$errors[] = "Question '{$question->text}' requires details when answered 'Yes'.";
|
||||
}
|
||||
}
|
||||
|
||||
if ($details === 'req_on_no' && $answer && $answer->value === 'no') {
|
||||
if (empty(trim(Arr::get($answer->toArray(), 'text_value', '')))) {
|
||||
$errors[] = "Question '{$question->text}' requires details when answered 'No'.";
|
||||
}
|
||||
}
|
||||
|
||||
if (! $hasRadioButtons && $details !== null && $details !== '') {
|
||||
if (! $answer || empty(trim(Arr::get($answer->toArray(), 'text_value', '')))) {
|
||||
$errors[] = "Question '{$question->text}' requires a text response.";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Display the final session result.
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user