tutoring/app/Http/Controllers/Student/CalendarController.php

47 lines
1.4 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
namespace App\Http\Controllers\Student;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\Slot;
use Carbon\Carbon;
class CalendarController extends Controller
{
public function index(Request $request)
{
$now = Carbon::now('Europe/London');
// Default: this MonSun, but flip to next week after Fri 15:00
$start = $now->copy()->startOfWeek(Carbon::MONDAY);
$fridayCutoff = $start->copy()->addDays(4)->setTime(15, 0);
if ($now->greaterThanOrEqualTo($fridayCutoff)) {
$start->addWeek();
}
// Week navigation
$weekOffset = (int) $request->query('week', 0);
if ($weekOffset !== 0) {
$start->addWeeks($weekOffset);
}
$end = $start->copy()->addDays(6);
// Load slots + bookings for this visible week
$slots = Slot::with(['bookings' => function ($q) use ($start, $end) {
$q->whereBetween('date', [$start->toDateString(), $end->toDateString()]);
}])->get();
// Reuse the same view for now (we'll hide admin-only bits next)
return view('calendar.index', [
'start' => $start,
'end' => $end,
'weekOffset' => $weekOffset,
'slots' => $slots,
'now' => $now,
'viewer' => 'student',
]);
}
}