Skip to main content

Overview

The Course model manages course operations, including CRUD operations and various query methods for retrieving course data with related information. File: app/models/Course.php

Methods

getAll

Retrieves all courses with subject and teacher information. SQL Query:
SELECT courses.id, subjects.name AS subject_name, users.name AS teacher_name
FROM courses
JOIN subjects ON courses.subject_id = subjects.id
JOIN teachers ON courses.teacher_id = teachers.id
JOIN users ON teachers.id = users.id
result
array
Returns array of all courses with related data
Example:
$course = new Course();
$allCourses = $course->getAll();

create

Creates a new course with a subject and teacher assignment.
subject_id
int
required
ID of the subject for this course
teacher_id
int
required
ID of the teacher assigned to this course
SQL Query:
INSERT INTO courses (subject_id, teacher_id) VALUES (?, ?)
result
void
No return value (throws exception on failure)
Example:
$course = new Course();
$course->create(3, 7); // Subject ID 3, Teacher ID 7

delete

Deletes a course by ID.
id
int
required
Course ID to delete
SQL Query:
DELETE FROM courses WHERE id = ?
result
void
No return value (throws exception on failure)
Example:
$course = new Course();
$course->delete(12);

getByTeacher

Retrieves all courses assigned to a specific teacher.
teacher_id
int
required
Teacher ID to filter by
SQL Query:
SELECT courses.id, subjects.name
FROM courses
JOIN subjects ON courses.subject_id = subjects.id
WHERE courses.teacher_id = ?
result
array
Returns array of courses for the specified teacher
Example:
$course = new Course();
$teacherCourses = $course->getByTeacher(7);

belongsToTeacher

Checks if a specific course belongs to a specific teacher.
course_id
int
required
Course ID to check
teacher_id
int
required
Teacher ID to verify ownership
SQL Query:
SELECT COUNT(*) FROM courses WHERE id = ? AND teacher_id = ?
result
boolean
Returns true if the course belongs to the teacher, false otherwise
Example:
$course = new Course();
$hasAccess = $course->belongsToTeacher(12, 7);
if ($hasAccess) {
    // Teacher can manage this course
}

getForTimetable

Retrieves all courses formatted for timetable display with subject and teacher names. SQL Query:
SELECT 
    courses.id,
    subjects.name AS subject,
    users.name AS teacher
FROM courses
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 subjects.name
result
array
Returns array of courses ordered by subject name
Example:
$course = new Course();
$timetableCourses = $course->getForTimetable();

find

Finds a specific course by ID.
id
int
required
Course ID to find
SQL Query:
SELECT * FROM courses WHERE id = ?
result
array|false
Returns course record as associative array, or false if not found
Example:
$course = new Course();
$courseData = $course->find(12);

Build docs developers (and LLMs) love