plan implementation 6, 7, 8, 9, 10
This commit is contained in:
@@ -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,
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user