plan implementation 6, 7, 8, 9, 10

This commit is contained in:
2026-02-03 10:50:56 +01:00
parent e8be239c32
commit 0b6c6736ef
16 changed files with 2665 additions and 44 deletions

View File

@@ -4,6 +4,7 @@
namespace App\Http\Controllers;
use App\Http\Requests\Screening\UpdateScreeningRequest;
use App\Models\Category;
use App\Models\Screening;
use Illuminate\Http\RedirectResponse;
@@ -32,14 +33,20 @@ public function show(Screening $screening): Response
{
return Inertia::render('Screening/Show', [
'screening' => $screening,
'questions' => array_values(config('screening.questions')),
]);
}
/**
* Save screening answers and redirect to result.
*/
public function update(Request $request, Screening $screening): RedirectResponse
public function update(UpdateScreeningRequest $request, Screening $screening): RedirectResponse
{
$validated = $request->validated();
$this->saveAnswers($screening, $validated['answers']);
$this->calculateAndUpdateScore($screening, $validated['answers']);
return redirect()->route('screening.result', $screening);
}
@@ -50,7 +57,58 @@ public function result(Screening $screening): Response
{
return Inertia::render('Screening/Result', [
'screening' => $screening,
'categories' => Category::orderBy('sort_order')->get(['id', 'name']),
'passed' => $screening->passed,
'score' => $screening->score,
'totalQuestions' => count(config('screening.questions')),
'categories' => $screening->passed ? Category::orderBy('sort_order')->get(['id', 'name']) : [],
]);
}
/**
* Save screening answers to the database using upsert pattern.
*/
private function saveAnswers(Screening $screening, array $answers): void
{
foreach ($answers as $questionNumber => $value) {
$screening->answers()->updateOrCreate(
[
'screening_id' => $screening->id,
'question_number' => (int) $questionNumber,
],
[
'value' => $value,
]
);
}
}
/**
* Calculate the score and update the screening record.
*/
private function calculateAndUpdateScore(Screening $screening, array $answers): void
{
$score = $this->calculateScore($answers);
$passed = $score >= config('screening.passing_score', 5);
$screening->update([
'score' => $score,
'passed' => $passed,
]);
}
/**
* Calculate the total score from the answers.
*/
private function calculateScore(array $answers): int
{
$score = 0;
foreach ($answers as $value) {
if ($value === 'yes') {
$score++;
}
}
return $score;
}
}

View File

@@ -4,7 +4,9 @@
namespace App\Http\Controllers;
use App\Http\Requests\Session\UpdateSessionRequest;
use App\Models\Session;
use App\Services\ScoringService;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Inertia\Inertia;
@@ -28,22 +30,89 @@ public function store(Request $request): RedirectResponse
}
/**
* Display the session questionnaire with category.
* Display the session questionnaire with category, question groups, questions, and existing answers.
*/
public function show(Session $session): Response
{
$session->load('category');
$questionGroups = $session->category
->questionGroups()
->with(['questions' => fn ($q) => $q->orderBy('sort_order')])
->orderBy('sort_order')
->get();
$answers = $session->answers()->get()->keyBy('question_id');
$scoringService = new ScoringService;
$score = $scoringService->calculateScore($session);
return Inertia::render('Session/Show', [
'session' => $session,
'questionGroups' => $questionGroups,
'answers' => $answers,
'score' => $score,
]);
}
/**
* Save session answers and redirect to result.
* Save session basic info, answers, and additional comments.
*/
public function update(Request $request, Session $session): RedirectResponse
public function update(UpdateSessionRequest $request, Session $session): RedirectResponse
{
$validated = $request->validated();
if (isset($validated['basic_info'])) {
$session->update(['basic_info' => $validated['basic_info']]);
}
if (isset($validated['answers'])) {
$this->saveAnswers($session, $validated['answers']);
}
if (isset($validated['additional_comments'])) {
$session->update(['additional_comments' => $validated['additional_comments']]);
}
if ($request->boolean('complete')) {
return $this->completeSession($session);
}
return back();
}
/**
* Save or update answers for the session using composite key upsert.
*/
private function saveAnswers(Session $session, array $answers): void
{
foreach ($answers as $questionId => $answer) {
$session->answers()->updateOrCreate(
['question_id' => (int) $questionId],
[
'value' => $answer['value'] ?? null,
'text_value' => $answer['text_value'] ?? null,
]
);
}
}
/**
* Complete the session by calculating final score and result.
*/
private function completeSession(Session $session): RedirectResponse
{
$scoringService = new ScoringService;
$score = $scoringService->calculateScore($session);
$result = $scoringService->determineResult($score);
$session->update([
'score' => $score,
'result' => $result,
'status' => 'completed',
'completed_at' => now(),
]);
return redirect()->route('sessions.result', $session);
}
@@ -52,8 +121,13 @@ public function update(Request $request, Session $session): RedirectResponse
*/
public function result(Session $session): Response
{
$session->load('category');
return Inertia::render('Session/Result', [
'session' => $session,
'score' => $session->score,
'result' => $session->result,
'categoryName' => $session->category->name,
]);
}
}