Skip to main content

Overview

The Timetable model manages class scheduling, including schedule creation, conflict detection, and personalized timetable retrieval for students and teachers. File: app/models/Timetable.php

Methods

getAll

Retrieves all timetable entries with course, subject, and teacher information. SQL Query:
SELECT 
    timetable.id,
    subjects.name AS subject,
    users.name AS teacher,
    timetable.day,
    timetable.start_time,
    timetable.end_time
FROM timetable
JOIN courses ON courses.id = timetable.course_id
JOIN subjects ON subjects.id = courses.subject_id
JOIN teachers ON teachers.id = courses.teacher_id
JOIN users ON users.id = teachers.id
ORDER BY timetable.day, timetable.start_time
result
array
Returns array of all timetable entries ordered by day and time
Example:
$timetable = new Timetable();
$schedule = $timetable->getAll();

create

Creates a new timetable entry for a course.
course_id
int
required
Course ID to schedule
day
string
required
Day of week (e.g., “Monday”, “Tuesday”)
start
string
required
Start time in HH:MM format (e.g., “09:00”)
end
string
required
End time in HH:MM format (e.g., “10:30”)
SQL Query:
INSERT INTO timetable (course_id, day, start_time, end_time)
VALUES (?, ?, ?, ?)
result
boolean
Returns true if creation was successful, false otherwise
Example:
$timetable = new Timetable();
$success = $timetable->create(12, 'Monday', '09:00', '10:30');
Use teacherHasConflict() before creating to prevent scheduling conflicts.

teacherHasConflict

Checks if a teacher has a scheduling conflict for the specified time slot.
teacher_id
int
required
Teacher ID to check
day
string
required
Day of week
start
string
required
Start time in HH:MM format
end
string
required
End time in HH:MM format
SQL Query:
SELECT COUNT(*) 
FROM timetable t
JOIN courses c ON c.id = t.course_id
WHERE c.teacher_id = ?
  AND t.day = ?
  AND (? < t.end_time AND ? > t.start_time)
result
boolean
Returns true if there is a conflict, false if the time slot is available
Example:
$timetable = new Timetable();
$hasConflict = $timetable->teacherHasConflict(7, 'Monday', '09:00', '10:30');
if (!$hasConflict) {
    // Safe to schedule
}
The conflict detection checks for overlapping time periods, not just exact matches.

delete

Deletes a timetable entry by ID.
id
int
required
Timetable entry ID to delete
SQL Query:
DELETE FROM timetable WHERE id = ?
result
boolean
Returns true if deletion was successful, false otherwise
Example:
$timetable = new Timetable();
$success = $timetable->delete(25);

getForStudent

Retrieves a personalized timetable for a specific student based on their enrollments.
student_id
int
required
Student ID to get timetable for
SQL Query:
SELECT 
    subjects.name AS subject,
    users.name AS teacher,
    timetable.day,
    timetable.start_time,
    timetable.end_time
FROM timetable
JOIN courses ON courses.id = timetable.course_id
JOIN enrollments ON enrollments.course_id = courses.id
JOIN subjects ON subjects.id = courses.subject_id
JOIN teachers ON teachers.id = courses.teacher_id
JOIN users ON users.id = teachers.id
WHERE enrollments.student_id = ?
ORDER BY timetable.day, timetable.start_time
result
array
Returns array of timetable entries for the student’s enrolled courses
Example:
$timetable = new Timetable();
$studentSchedule = $timetable->getForStudent(15);

getForTeacher

Retrieves a personalized timetable for a specific teacher.
teacher_id
int
required
Teacher ID to get timetable for
SQL Query:
SELECT 
    subjects.name AS subject,
    timetable.day,
    timetable.start_time,
    timetable.end_time
FROM timetable
JOIN courses ON courses.id = timetable.course_id
JOIN subjects ON subjects.id = courses.subject_id
WHERE courses.teacher_id = ?
ORDER BY timetable.day, timetable.start_time
result
array
Returns array of timetable entries for the teacher’s courses
Example:
$timetable = new Timetable();
$teacherSchedule = $timetable->getForTeacher(7);

Build docs developers (and LLMs) love