Overview
The TimetableController manages course schedules with teacher conflict detection. Provides different views for admins, teachers, and students.
File Location: app/controllers/TimetableController.php
Dependencies:
Timetable model
Course model
Auth helper
Admin Methods
index()
Displays all timetable entries for administrative management.
Authorization: Admin only
Behavior:
- Retrieves all timetable entries with course details
- Renders admin timetable view
Code Example:
// ADMIN
public function index()
{
Auth::admin();
$timetables = $this->model->getAll();
require __DIR__ . '/../views/timetable/index.php';
}
View Location: app/views/timetable/index.php
create()
Displays timetable creation form and handles schedule creation with conflict detection.
Authorization: Admin only
Course ID to schedule (POST request)
Day of the week (e.g., ‘Monday’, ‘Tuesday’)
Start time in HH:MM format (e.g., ‘09:00’)
End time in HH:MM format (e.g., ‘11:00’)
Conflict Detection:
- Checks if the course’s teacher already has another course during the specified time
- Prevents double-booking of teachers
Behavior:
- GET request: Loads available courses and renders form
- POST request: Validates no conflicts, creates schedule entry
Code Example:
public function create()
{
Auth::admin();
$courseModel = new Course();
$courses = $courseModel->getForTimetable();
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$course_id = $_POST['course_id'];
$day = $_POST['day'];
$start = $_POST['start_time'];
$end = $_POST['end_time'];
// get teacher id from selected course
$course = $courseModel->find($course_id);
$teacher_id = $course['teacher_id'];
// check conflict
if ($this->model->teacherHasConflict($teacher_id, $day, $start, $end)) {
$_SESSION['error'] = "❌ This teacher already has another course during this time.";
header("Location: /school_management/public/timetable/create");
exit;
}
// no conflict → insert
$this->model->create($course_id, $day, $start, $end);
header("Location: /school_management/public/timetable");
exit;
}
require __DIR__ . '/../views/timetable/create.php';
}
Response:
- GET: Renders creation form
- POST Success: Redirects to
/timetable
- POST Conflict: Redirects to
/timetable/create with error message
delete()
Deletes a timetable entry.
Authorization: Admin only
Timetable entry ID to delete (from GET query parameter)
Code Example:
public function delete()
{
Auth::admin();
$this->model->delete($_GET['id']);
header("Location: /school_management/public/timetable");
}
Response: Redirects to /timetable
Student Methods
student()
Displays the logged-in student’s personal timetable based on their enrollments.
Authorization: Student only
Behavior:
- Retrieves timetable for courses the student is enrolled in
- Renders student timetable view
Code Example:
// STUDENT
public function student()
{
Auth::student();
$data = $this->model->getForStudent($_SESSION['user']['id']);
require __DIR__ . '/../views/timetable/student.php';
}
View Location: app/views/timetable/student.php
Teacher Methods
teacher()
Displays the logged-in teacher’s personal timetable for courses they teach.
Authorization: Teacher only
Behavior:
- Retrieves timetable for courses taught by the teacher
- Renders teacher timetable view
Code Example:
// TEACHER
public function teacher()
{
Auth::teacher();
$data = $this->model->getForTeacher($_SESSION['user']['id']);
require __DIR__ . '/../views/timetable/teacher.php';
}
View Location: app/views/timetable/teacher.php
Usage Examples
Creating a Timetable Entry
// POST /timetable/create
$_POST = [
'course_id' => 12,
'day' => 'Monday',
'start_time' => '09:00',
'end_time' => '11:00'
];
// GET /timetable/student
// Session:
$_SESSION['user'] = [
'id' => 25,
'role' => 'student'
];
// GET /timetable/teacher
// Session:
$_SESSION['user'] = [
'id' => 3,
'role' => 'teacher'
];
Conflict Detection Logic
The system prevents teacher double-booking:
if ($this->model->teacherHasConflict($teacher_id, $day, $start, $end)) {
$_SESSION['error'] = "❌ This teacher already has another course during this time.";
// Redirect with error
}
Checks:
- Same teacher
- Same day of week
- Overlapping time ranges
Time Overlap Logic:
Two time slots overlap if:
- New start time is before existing end time AND
- New end time is after existing start time
Role-Based Views
| Role | Method | View | Data Scope |
|---|
| Admin | index() | All entries | Entire timetable |
| Student | student() | Enrolled courses | Student’s schedule |
| Teacher | teacher() | Taught courses | Teacher’s schedule |
Error Handling
Conflict Error
Stored in session and displayed on redirect:
$_SESSION['error'] = "❌ This teacher already has another course during this time.";
The view should check and display:
if (isset($_SESSION['error'])) {
echo $_SESSION['error'];
unset($_SESSION['error']);
}