Skip to main content
Manage classes, student enrollment, and bulk operations through Excel import/export.

Create Class

Endpoint: manage_classes.php Method: POST Authentication: Teacher session required

Request Parameters

action
string
required
Must be set to create_class
class_name
string
required
Name of the class to create

Database Operation

INSERT INTO classes (teacher_id, name) 
VALUES (?, ?)
Parameters:
  • teacher_id - From session ($_SESSION['teacher_id'])
  • name - Class name from request

Response

Success:
$_SESSION['success'] = "Class created successfully";
Redirects to manage_classes.php
Each class is owned by the teacher who created it. The teacher_id is automatically set from the session.

Add Student to Class

Endpoint: manage_classes.php Method: POST Authentication: Teacher session required

Request Parameters

action
string
required
Must be set to add_student
class_id
integer
required
ID of the class to add student to
student_id
string
required
Student identifier to add

Validation Process

  1. Verify Class Ownership:
    SELECT id FROM classes 
    WHERE id = ? AND teacher_id = ?
    
  2. Check Student Exists:
    SELECT * FROM students 
    WHERE student_id = ?
    
  3. Check Existing Enrollment:
    SELECT cs.class_id, c.name 
    FROM class_students cs
    JOIN classes c ON cs.class_id = c.id
    WHERE cs.student_id = ? AND c.teacher_id = ?
    
  4. Add to Class:
    INSERT INTO class_students (class_id, student_id) 
    VALUES (?, ?)
    

Enrollment Rules

Students can be enrolled in multiple classes from different teachers, but only ONE class per teacher.

Response Messages

Success:
$_SESSION['success'] = "Student added successfully";
Errors:
  • “Invalid class selected” - Class doesn’t belong to teacher
  • “Student ID not found” - Student doesn’t exist in database
  • “Student is already enrolled in this class”
  • “Student is already enrolled in your class: [class_name]. A student can only be enrolled in one class per teacher.”

Remove Student from Class

Endpoint: manage_classes.php Method: POST Authentication: Teacher session required

Request Parameters

action
string
required
Must be set to remove_student
class_id
integer
required
ID of the class
student_id
string
required
Student identifier to remove

Database Operation

DELETE FROM class_students 
WHERE class_id = ? AND student_id = ?

Response

Success:
$_SESSION['success'] = "Student removed successfully";
Removing a student from a class does NOT delete their attendance history.

Delete Class

Endpoint: manage_classes.php Method: POST Authentication: Teacher session required

Request Parameters

action
string
required
Must be set to delete_class
class_id
integer
required
ID of the class to delete

Deletion Process

Uses database transaction to ensure data integrity:
$pdo->beginTransaction();

try {
    // 1. Verify ownership
    SELECT * FROM classes WHERE id = ? AND teacher_id = ?
    
    // 2. Delete attendance records
    DELETE FROM attendance WHERE class_id = ?
    
    // 3. Delete schedules
    DELETE FROM class_schedules WHERE class_id = ?
    
    // 4. Delete student enrollments
    DELETE FROM class_students WHERE class_id = ?
    
    // 5. Delete class
    DELETE FROM classes WHERE id = ?
    
    $pdo->commit();
} catch (Exception $e) {
    $pdo->rollBack();
}

Cascade Deletions

Deleting a class removes:
  • All attendance records for the class
  • All class schedules
  • All student enrollments
  • The class itself

Response Messages

Success:
$_SESSION['success'] = "Class deleted successfully";
Errors:
  • “You don’t have permission to delete this class”
  • “Error deleting class: [exception message]”
Deleting a class is permanent and cannot be undone. All associated data (attendance, schedules, enrollments) will be deleted.

View Class Students

Query: Get all students enrolled in a class
SELECT s.* 
FROM students s
JOIN class_students cs ON s.student_id = cs.student_id
WHERE cs.class_id = ?
ORDER BY s.name

Response Fields

student_id
string
Unique student identifier
name
string
Student’s full name
email
string
Student’s email address

Excel Import

Endpoint: manage_classes.php Method: POST Authentication: Teacher session required Requirement: PhpSpreadsheet library must be installed

Request Parameters

action
string
required
Must be set to import
class_id
integer
required
ID of the class to import students into
excel_file
file
required
Excel or CSV file (.xlsx, .xls, .csv)
has_header
checkbox
Set to “1” if file has header row (default: checked)

File Format

Column Structure:
Column AColumn BColumn C
Student IDFull NameEmail
12345John Doe[email protected]
67890Jane Smith[email protected]
Supported Formats:
  • .xlsx - Excel 2007+
  • .xls - Excel 97-2003
  • .csv - Comma-separated values

Import Process

  1. Validate File:
    $file_extension = pathinfo($_FILES['excel_file']['name'], PATHINFO_EXTENSION);
    $allowed_extensions = ['xlsx', 'xls', 'csv'];
    
  2. Read File:
    $reader = new \PhpOffice\PhpSpreadsheet\Reader\Xlsx();
    $spreadsheet = $reader->load($_FILES['excel_file']['tmp_name']);
    $rows = $spreadsheet->getActiveSheet()->toArray();
    
  3. Process Each Row:
    • Skip header row if has_header is checked
    • Skip empty rows
    • Check if student exists
    • Create new student if data is complete
    • Check for existing enrollment with this teacher
    • Add to class

Auto-Create Students

If a student_id doesn’t exist but name and email are provided:
$default_password = password_hash('changeme123', PASSWORD_DEFAULT);

INSERT INTO students (student_id, name, email, password) 
VALUES (?, ?, ?, ?)
New students created during import get the default password: changeme123

Response Messages

Success:
$_SESSION['success'] = "Successfully imported {$imported} students.";
if ($skipped > 0) {
    $message .= " Skipped {$skipped} students.";
}
Import Errors:
$_SESSION['import_errors'] = [
    "Failed to create student: 12345",
    "Incomplete data for student: 67890",
    "Student 11111 is already enrolled in your class: Math 101"
];
File Errors:
  • “Error uploading file. Please try again.”
  • “Invalid class selected”
  • “Invalid file format. Please upload an Excel file (.xlsx, .xls) or CSV file.”
  • “Error processing file: [exception message]“

Excel Export

Endpoint: manage_classes.php Method: POST Authentication: Teacher session required Requirement: PhpSpreadsheet library must be installed

Request Parameters

action
string
required
Must be set to export
class_id
integer
required
ID of the class to export

Export Process

  1. Validate Class Ownership:
    SELECT * FROM classes 
    WHERE id = ? AND teacher_id = ?
    
  2. Get Students:
    SELECT s.student_id, s.name, s.email 
    FROM students s
    JOIN class_students cs ON s.student_id = cs.student_id
    WHERE cs.class_id = ?
    ORDER BY s.name
    
  3. Create Spreadsheet:
    $spreadsheet = new \PhpOffice\PhpSpreadsheet\Spreadsheet();
    $sheet = $spreadsheet->getActiveSheet();
    
    // Add headers
    $sheet->setCellValue('A1', 'Student ID');
    $sheet->setCellValue('B1', 'Name');
    $sheet->setCellValue('C1', 'Email');
    
    // Add data
    $row = 2;
    foreach ($students as $student) {
        $sheet->setCellValue('A' . $row, $student['student_id']);
        $sheet->setCellValue('B' . $row, $student['name']);
        $sheet->setCellValue('C' . $row, $student['email']);
        $row++;
    }
    
  4. Output File:
    header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
    header('Content-Disposition: attachment;filename="' . $class['name'] . '_roster.xlsx"');
    
    $writer = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($spreadsheet, 'Xlsx');
    $writer->save('php://output');
    

File Output

Filename: {class_name}_roster.xlsx Example: Computer_Science_101_roster.xlsx Format: Excel 2007+ (.xlsx) Columns:
  • A: Student ID
  • B: Name
  • C: Email
Features:
  • Auto-sized columns
  • Header row included
  • Sorted by student name

Response

File is downloaded directly to the browser. No page redirect. Error:
$_SESSION['error'] = "Invalid class selected";
PhpSpreadsheet library must be installed via Composer for import/export functionality.

Library Installation

Check Installation:
$phpspreadsheet_installed = class_exists('PhpOffice\PhpSpreadsheet\Spreadsheet');
Install via Composer:
composer require phpoffice/phpspreadsheet
Autoload:
require 'vendor/autoload.php';

use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
If PhpSpreadsheet is not installed, import/export buttons will be hidden and the functionality will be unavailable.

Build docs developers (and LLMs) love