Files
fitness/app/Http/Controllers/DashboardController.php
rootandClaude Sonnet 5 a9923f72ce Add streaks and milestone badges
No gamification existed at all - nothing rewarded consistency or gave
users a reason to come back beyond the raw session stats already on
the dashboard. Adds a Gamification helper that derives (not stores)
a current/longest daily streak from completed WorkoutSessions and six
badges (first session, 10 and 50 sessions, 7- and 30-day streaks, a
100kg+ completed lift) from existing session/session-item data - no new
tables, since everything needed is already recorded.

Found and fixed a real Carbon 3 behavior change via testing: diffInDays()
no longer returns an absolute value by default (unlike Carbon 2) - it
returns a signed float. My longest-streak calculation used a strict
=== 1 check against the raw result, which silently always failed
because float(-1) or float(1) never strictly equals int 1. Fixed with
abs() plus an explicit (int) cast. Worth remembering for any other
diffIn*() usage in this codebase going forward.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-08-10 12:44:24 +03:00

75 lines
2.6 KiB
PHP

<?php
namespace App\Http\Controllers;
use App\Models\WorkoutSession;
use App\Support\Gamification;
use Illuminate\Support\Facades\Auth;
use Illuminate\View\View;
class DashboardController extends Controller
{
public function index(): View
{
$user = Auth::user();
// Basic counts
$workoutsCount = $user->workouts()->count();
$sessionsQuery = WorkoutSession::where('user_id', $user->id);
$sessionsCount = (clone $sessionsQuery)->count();
$completedSessions = (clone $sessionsQuery)->whereNotNull('completed_at')->count();
$completionRate = $sessionsCount > 0 ? round(($completedSessions / $sessionsCount) * 100) : 0;
// Recent sessions with progress
$recentSessions = (clone $sessionsQuery)
->latest('started_at')
->latest('id')
->with(['workout:id,title', 'items:id,session_id,done'])
->limit(5)
->get()
->map(function ($s) {
$total = $s->items->count();
$done = $s->items->where('done', true)->count();
$pct = $total > 0 ? (int) round(($done / $total) * 100) : 0;
return [
'id' => $s->id,
'workout_title' => optional($s->workout)->title ?? 'Workout',
'started_at' => $s->started_at,
'completed_at' => $s->completed_at,
'done' => $done,
'total' => $total,
'pct' => $pct,
];
});
// Activity in the last 30 days (number of sessions per day)
$since = now()->subDays(29)->startOfDay();
$dailyActivity = (clone $sessionsQuery)
->where('created_at', '>=', $since)
->get(['id', 'created_at'])
->groupBy(function ($s) { return $s->created_at->format('Y-m-d'); })
->map->count();
// Body weight trend
$weightLogs = $user->weightLogs()->orderBy('logged_at')->get(['id', 'weight_kg', 'logged_at']);
$weightChange = $weightLogs->count() >= 2
? round($weightLogs->last()->weight_kg - $weightLogs->first()->weight_kg, 2)
: null;
// Streaks and badges
$gamification = Gamification::forUser($user);
return view('dashboard', compact(
'workoutsCount',
'sessionsCount',
'completedSessions',
'completionRate',
'recentSessions',
'dailyActivity',
'weightLogs',
'weightChange',
'gamification'
));
}
}