Dashboard: add top 10 candidates and update global scores

This commit is contained in:
jeremy bayse
2026-03-22 18:44:10 +01:00
parent 898b56c535
commit 7d03d52ae9
2 changed files with 105 additions and 18 deletions

View File

@@ -20,13 +20,32 @@ use App\Models\Attempt;
Route::get('/dashboard', function () {
$stats = [];
$quizzes = [];
$topCandidates = [];
if (auth()->user()->isAdmin()) {
$allCandidates = Candidate::with(['attempts'])->get();
$stats = [
'total_candidates' => Candidate::count(),
'finished_tests' => Attempt::whereNotNull('finished_at')->count(),
'average_score' => round(Attempt::whereNotNull('finished_at')->avg('score') ?? 0, 1),
'best_score' => Attempt::whereNotNull('finished_at')->max('score') ?? 0,
'average_score' => round($allCandidates->avg('weighted_score') ?? 0, 1),
'best_score' => round($allCandidates->max('weighted_score') ?? 0, 1),
];
$topCandidates = Candidate::with(['user', 'attempts'])
->get()
->sortByDesc('weighted_score')
->take(10)
->map(function($candidate) {
return [
'id' => $candidate->id,
'name' => $candidate->user->name,
'email' => $candidate->user->email,
'status' => $candidate->status,
'weighted_score' => $candidate->weighted_score
];
})
->values()
->all();
} else {
$candidate = auth()->user()->candidate;
$quizzes = \App\Models\Quiz::all()->map(function($quiz) use ($candidate) {
@@ -39,7 +58,8 @@ Route::get('/dashboard', function () {
return Inertia::render('Dashboard', [
'stats' => $stats,
'quizzes' => $quizzes
'quizzes' => $quizzes,
'top_candidates' => $topCandidates
]);
})->middleware(['auth', 'verified'])->name('dashboard');