Overview
TheEnrollmentController manages student enrollments in courses, including creation, listing, and deletion with duplicate enrollment prevention.
File Location: app/controllers/EnrollmentController.php
Dependencies:
EnrollmentmodelStudentmodelCoursemodelAuthhelper
Methods
index()
Displays list of all enrollments. Authorization: Admin only Behavior:- Retrieves all enrollment records with student and course details
- Renders enrollment listing view
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
app/views/enrollments/create.php
store()
Processes enrollment creation with duplicate prevention. Authorization: Admin onlyID of the student to enroll (POST request)
ID of the course to enroll in (POST request)
- Checks if student is already enrolled in the course
- If duplicate: Redirects with error parameter
- If valid: Creates enrollment and redirects to listing
- Success: Redirects to
/enrollments - Duplicate: Redirects to
/enrollments/create?error=duplicate
delete()
Removes a student’s enrollment from a course. Authorization: Admin onlyStudent ID (from GET query parameter)
Course ID (from GET query parameter)
- Validates admin authorization
- Deletes enrollment using composite key (student_id + course_id)
- Redirects to enrollment listing
/enrollments
Usage Examples
Creating an Enrollment
Deleting an Enrollment
Business Logic
Duplicate Prevention
The controller prevents duplicate enrollments by checking if a student is already enrolled in a course before creating the record:Composite Key
Enrollments use a composite key ofstudent_id and course_id, making each enrollment unique per student-course pair.