79 lines
2.2 KiB
PHP
79 lines
2.2 KiB
PHP
<?php
|
|
|
|
namespace App\Modules\Academic\Services;
|
|
|
|
use App\Modules\Academic\Models\ScheduleModel;
|
|
use App\Modules\Academic\Models\StudentModel;
|
|
|
|
/**
|
|
* Schedule Resolver Service
|
|
*
|
|
* Handles schedule resolution logic for students.
|
|
*/
|
|
class ScheduleResolverService
|
|
{
|
|
protected StudentModel $studentModel;
|
|
protected ScheduleModel $scheduleModel;
|
|
|
|
public function __construct()
|
|
{
|
|
$this->studentModel = new StudentModel();
|
|
$this->scheduleModel = new ScheduleModel();
|
|
}
|
|
|
|
/**
|
|
* Get active schedule for a student at a specific datetime
|
|
*
|
|
* Rules:
|
|
* - Find student's class_id
|
|
* - Determine day_of_week from datetime (1=Monday, 7=Sunday)
|
|
* - Find schedule where start_time <= time < end_time
|
|
*
|
|
* @param int $studentId Student ID
|
|
* @param string $datetime Datetime string (Y-m-d H:i:s format)
|
|
* @return array|null Returns schedule detail or null if not found
|
|
*/
|
|
public function getActiveSchedule(int $studentId, string $datetime): ?array
|
|
{
|
|
// Find student
|
|
$student = $this->studentModel->find($studentId);
|
|
|
|
if (!$student) {
|
|
return null;
|
|
}
|
|
|
|
// Get class_id from student
|
|
$classId = $student->class_id;
|
|
|
|
// Parse datetime to get day of week and time
|
|
$timestamp = strtotime($datetime);
|
|
if ($timestamp === false) {
|
|
return null;
|
|
}
|
|
|
|
// Get day of week (1=Monday, 7=Sunday)
|
|
// PHP date('N') returns 1-7 (Monday-Sunday)
|
|
$dayOfWeek = (int) date('N', $timestamp);
|
|
|
|
// Get time in H:i:s format
|
|
$time = date('H:i:s', $timestamp);
|
|
|
|
// Find active schedule (lesson_slots/users as source of truth with fallback to schedule columns)
|
|
$row = $this->scheduleModel->getActiveScheduleRow($classId, $dayOfWeek, $time);
|
|
|
|
if ($row === null) {
|
|
return null;
|
|
}
|
|
|
|
return [
|
|
'schedule_id' => $row['id'],
|
|
'class_id' => $row['class_id'],
|
|
'subject_id' => $row['subject_id'],
|
|
'teacher_name' => $row['teacher_name'],
|
|
'room' => $row['room'],
|
|
'start_time' => $row['start_time'],
|
|
'end_time' => $row['end_time'],
|
|
];
|
|
}
|
|
}
|