Skip to main content

Overview

The EnrollmentController manages student enrollments in courses, including creation, listing, and deletion with duplicate enrollment prevention. File Location: app/controllers/EnrollmentController.php Dependencies:
  • Enrollment model
  • Student model
  • Course model
  • Auth helper

Methods

index()

Displays list of all enrollments. Authorization: Admin only Behavior:
  • Retrieves all enrollment records with student and course details
  • Renders enrollment listing view
Code Example:
// Admin: List enrollments
public function index()
{
    Auth::admin();

    $enrollments = $this->enrollmentModel->getAll();
    require __DIR__ . '/../views/enrollments/index.php';
}
View Location: app/views/enrollments/index.php

create()

Displays the enrollment creation form. Authorization: Admin only Behavior:
  • Loads all students and courses for dropdown selection
  • Renders enrollment creation form
Code Example:
// Admin: Create enrollment
public function create()
{
    Auth::admin();

    $students = $this->studentModel->getAll();
    $courses = $this->courseModel->getAll();

    require __DIR__ . '/../views/enrollments/create.php';
}
View Location: app/views/enrollments/create.php

store()

Processes enrollment creation with duplicate prevention. Authorization: Admin only
student_id
integer
required
ID of the student to enroll (POST request)
course_id
integer
required
ID of the course to enroll in (POST request)
Behavior:
  1. Checks if student is already enrolled in the course
  2. If duplicate: Redirects with error parameter
  3. If valid: Creates enrollment and redirects to listing
Code Example:
// Admin: Store enrollment
public function store()
{
    Auth::admin();

    $student_id = $_POST['student_id'];
    $course_id = $_POST['course_id'];

    // 🔹 Check if already enrolled
    if ($this->enrollmentModel->isAlreadyEnrolled($student_id, $course_id)) {
        header("Location: /school_management/public/enrollments/create?error=duplicate");
        exit;
    }

    // 🔹 Otherwise insert normally
    $this->enrollmentModel->create($student_id, $course_id);

    header("Location: /school_management/public/enrollments");
    exit;
}
Response:
  • Success: Redirects to /enrollments
  • Duplicate: Redirects to /enrollments/create?error=duplicate

delete()

Removes a student’s enrollment from a course. Authorization: Admin only
student_id
integer
required
Student ID (from GET query parameter)
course_id
integer
required
Course ID (from GET query parameter)
Behavior:
  1. Validates admin authorization
  2. Deletes enrollment using composite key (student_id + course_id)
  3. Redirects to enrollment listing
Code Example:
// Admin: Delete enrollment
public function delete()
{
    Auth::admin();

    $this->enrollmentModel->delete(
        $_GET['student_id'],
        $_GET['course_id']
    );

    header("Location: /school_management/public/enrollments");
    exit;
}
Response: Redirects to /enrollments

Usage Examples

Creating an Enrollment

// POST /enrollments/store
$_POST = [
    'student_id' => 15,
    'course_id' => 8
];

Deleting an Enrollment

// GET /enrollments/delete?student_id=15&course_id=8
$_GET = [
    'student_id' => 15,
    'course_id' => 8
];

Business Logic

Duplicate Prevention

The controller prevents duplicate enrollments by checking if a student is already enrolled in a course before creating the record:
if ($this->enrollmentModel->isAlreadyEnrolled($student_id, $course_id)) {
    // Redirect with error
}

Composite Key

Enrollments use a composite key of student_id and course_id, making each enrollment unique per student-course pair.

Build docs developers (and LLMs) love