improvements
This commit is contained in:
@@ -8,12 +8,14 @@
|
||||
use App\Models\Session;
|
||||
use App\Services\ActivityLogger;
|
||||
use App\Services\ScoringService;
|
||||
use Barryvdh\DomPDF\Facade\Pdf;
|
||||
use Illuminate\Http\RedirectResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Http\Response;
|
||||
use Illuminate\Support\Arr;
|
||||
use Illuminate\Validation\ValidationException;
|
||||
use Inertia\Inertia;
|
||||
use Inertia\Response;
|
||||
use Inertia\Response as InertiaResponse;
|
||||
|
||||
final class SessionController extends Controller
|
||||
{
|
||||
@@ -37,7 +39,7 @@ public function store(Request $request): RedirectResponse
|
||||
/**
|
||||
* Display the session questionnaire with category, question groups, questions, and existing answers.
|
||||
*/
|
||||
public function show(Session $session): Response
|
||||
public function show(Session $session): InertiaResponse
|
||||
{
|
||||
$session->load('category', 'user');
|
||||
|
||||
@@ -51,14 +53,10 @@ public function show(Session $session): Response
|
||||
|
||||
$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,
|
||||
]);
|
||||
}
|
||||
|
||||
@@ -201,7 +199,7 @@ private function validateDetailsAnswer($question, $answer, array &$errors): void
|
||||
/**
|
||||
* Display the final session result.
|
||||
*/
|
||||
public function result(Session $session): Response
|
||||
public function result(Session $session): InertiaResponse
|
||||
{
|
||||
$session->load('category');
|
||||
|
||||
@@ -212,4 +210,30 @@ public function result(Session $session): Response
|
||||
'categoryName' => $session->category->name,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate and download a PDF export of the completed session result.
|
||||
*/
|
||||
public function pdf(Session $session): Response
|
||||
{
|
||||
abort_unless($session->user_id === auth()->id(), 403);
|
||||
abort_unless($session->status === 'completed', 403);
|
||||
|
||||
$session->load(['user', 'category', 'answers.question']);
|
||||
|
||||
$questionGroups = $session->category
|
||||
->questionGroups()
|
||||
->with(['questions' => fn ($q) => $q->orderBy('sort_order')])
|
||||
->orderBy('sort_order')
|
||||
->get();
|
||||
|
||||
ActivityLogger::log('session_pdf_downloaded', $session->user_id, sessionId: $session->id, categoryId: $session->category_id);
|
||||
|
||||
$pdf = Pdf::loadView('pdf.session-result', [
|
||||
'session' => $session,
|
||||
'questionGroups' => $questionGroups,
|
||||
]);
|
||||
|
||||
return $pdf->download("go-no-go-{$session->id}.pdf");
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user