52 lines
1.6 KiB
PHP
52 lines
1.6 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use Illuminate\Http\Request;
|
|
use App\Models\Timetable;
|
|
use Carbon\Carbon;
|
|
|
|
class TimetableController extends Controller
|
|
{
|
|
public function index()
|
|
{
|
|
|
|
$studentLogId = session('student_log_id') ?? session('student_id');
|
|
|
|
if (!$studentLogId) {
|
|
return redirect('/signin')->with('error', 'Please login first!');
|
|
}
|
|
|
|
|
|
$rawTimetables = Timetable::whereNull('student_log_id')
|
|
->orWhere('student_log_id', $studentLogId)
|
|
->orderBy('sort_order', 'asc')
|
|
->get();
|
|
|
|
|
|
$schedule = [];
|
|
$timeSlots = [];
|
|
|
|
foreach ($rawTimetables as $item) {
|
|
if (!in_array($item->time_slot, $timeSlots)) {
|
|
$timeSlots[] = $item->time_slot;
|
|
}
|
|
$schedule[$item->time_slot][$item->day] = $item;
|
|
}
|
|
|
|
|
|
$todayName = Carbon::now()->format('l');
|
|
$todaySchedule = Timetable::where('day', $todayName)
|
|
->where(function($q) use ($studentLogId) {
|
|
$q->whereNull('student_log_id')
|
|
->orWhere('student_log_id', $studentLogId);
|
|
})
|
|
->where('type', '!=', 'Break')
|
|
->orderBy('sort_order', 'asc')
|
|
->get();
|
|
|
|
$days = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday'];
|
|
|
|
return view('timetable', compact('schedule', 'timeSlots', 'days', 'todaySchedule', 'todayName'));
|
|
}
|
|
} |