Menu
Coddy logo textTech

E-Learning Platform

Part of the Object Oriented Programming section of Coddy's PHP journey — lesson 88 of 91.

challenge icon

Challenge

Easy

Let's build an E-Learning Platform that manages courses, students, and instructors. This comprehensive challenge brings together inheritance, interfaces, encapsulation, and design patterns into a cohesive system.

You'll organize your code across six files:

  • User.php — Create an abstract User class that serves as the base for both students and instructors. Use constructor promotion for id (int) and name (string), both public readonly. Include an abstract method getRole(): string that child classes must implement.
  • Student.php — Include the User file. Create a Student class extending User. Students track their enrolled courses in a private array and their progress (percentage completed) for each course. Implement:
    • getRole(): string — returns "Student"
    • enrollInCourse(Course $course): string — enrolls the student if the course has capacity; returns success or failure message
    • getEnrolledCourses(): array — returns the array of enrolled courses
    • updateProgress(string $courseTitle, int $percent): void — sets progress for a course (0-100)
    • getProgress(string $courseTitle): int — returns progress percentage, or 0 if not enrolled
  • Instructor.php — Include the User file. Create an Instructor class extending User. Instructors can create and manage courses. Implement:
    • getRole(): string — returns "Instructor"
    • createCourse(string $title, int $capacity): Course — creates and returns a new Course with this instructor assigned
  • Course.php — Include the Instructor file. Create a Course class with constructor promotion for title (string, public readonly), instructor (Instructor, public readonly), and capacity (int, private). Track enrolled students in a private array. Implement:
    • getCapacity(): int — returns the maximum capacity
    • getEnrolledCount(): int — returns current enrollment count
    • hasCapacity(): bool — returns true if more students can enroll
    • addStudent(Student $student): bool — adds student if capacity allows, returns success status
    • getStudents(): array — returns enrolled students array
  • Platform.php — Include the Course and Student files. Create a Platform class that manages the entire system. Store courses and users in private arrays. Implement:
    • addCourse(Course $course): void — adds a course to the platform
    • findCourse(string $title): ?Course — finds a course by title
    • enrollStudent(Student $student, string $courseTitle): string — enrolls a student in a course. Return "Course not found", "Course is full", or "[name] enrolled in [title]"
    • getCourseStats(string $title): string — returns "[title]: [enrolled]/[capacity] students" or "Course not found"
  • main.php — Include the Platform file. You'll receive three inputs: instructor data (JSON), courses to create (JSON), and operations to perform (JSON).

    Instructor JSON format:

    {"id": 1, "name": "Dr. Smith"}

    Courses JSON format:

    [{"title": "PHP Basics", "capacity": 2}, {"title": "OOP Mastery", "capacity": 3}]

    Operations JSON format:

    [{"type": "enroll", "student_id": 1, "student_name": "Alice", "course": "PHP Basics"}, {"type": "progress", "student_id": 1, "course": "PHP Basics", "percent": 50}, {"type": "stats", "course": "PHP Basics"}]

    Create the platform, instructor, and all courses (using the instructor's createCourse method). Add courses to the platform. Then process each operation:

    • "enroll" — Create a new Student and enroll them; print the result from enrollStudent()
    • "progress" — Update student progress; print "[name] progress in [course]: [percent]%"
    • "stats" — Print the result from getCourseStats()

    Keep track of created students by their ID so you can update their progress later. Print each operation's result on a new line.

This challenge demonstrates how inheritance creates a clean user hierarchy, encapsulation protects enrollment data, and the platform acts as a coordinator managing the relationships between courses, students, and instructors.

Try it yourself

<?php

require_once 'Platform.php';

// Read inputs
$instructorData = (array)json_decode(trim(fgets(STDIN)), true);
$coursesData = (array)json_decode(trim(fgets(STDIN)), true);
$operations = (array)json_decode(trim(fgets(STDIN)), true);

// TODO: Create the platform

// TODO: Create the instructor from instructorData

// TODO: Create all courses using the instructor's createCourse method
// and add them to the platform

// TODO: Keep track of created students by their ID
$students = [];

// TODO: Process each operation
foreach ($operations as $op) {
    $operation = (array)$op;
    
    // TODO: Handle "enroll" operation
    // Create a new Student and enroll them
    // Print the result from enrollStudent()
    
    // TODO: Handle "progress" operation
    // Update student progress
    // Print "[name] progress in [course]: [percent]%"
    
    // TODO: Handle "stats" operation
    // Print the result from getCourseStats()
}

?>

All lessons in Object Oriented Programming