exec("SET FOREIGN_KEY_CHECKS=0;"); $pdo->beginTransaction(); if (isset($_POST['clear_timetable'])) { // Clearing timetable data only $pdo->prepare("DELETE FROM schedule_teachers WHERE schedule_id IN (SELECT id FROM schedules WHERE school_id = ?)")->execute([$school_id]); $pdo->prepare("DELETE FROM schedules WHERE school_id = ?")->execute([$school_id]); $message = "Timetable data has been successfully cleared."; } elseif (isset($_POST['clear_all_data'])) { // Deleting all school data. // 1. Junction tables $pdo->prepare("DELETE FROM schedule_teachers WHERE schedule_id IN (SELECT id FROM schedules WHERE school_id = ?)")->execute([$school_id]); $pdo->prepare("DELETE FROM elective_group_subjects WHERE elective_group_id IN (SELECT id FROM elective_groups WHERE school_id = ?)")->execute([$school_id]); // 2. Core data tables $pdo->prepare("DELETE FROM schedules WHERE school_id = ?")->execute([$school_id]); $pdo->prepare("DELETE FROM workloads WHERE school_id = ?")->execute([$school_id]); $pdo->prepare("DELETE FROM subjects WHERE school_id = ?")->execute([$school_id]); $pdo->prepare("DELETE FROM teachers WHERE school_id = ?")->execute([$school_id]); $pdo->prepare("DELETE FROM classes WHERE school_id = ?")->execute([$school_id]); $pdo->prepare("DELETE FROM elective_groups WHERE school_id = ?")->execute([$school_id]); // 3. Users (keep the logged-in admin) $current_user_id = $_SESSION['user_id'] ?? 0; $stmt = $pdo->prepare("DELETE FROM users WHERE school_id = ? AND id != ?"); $stmt->execute([$school_id, $current_user_id]); $message = "All school data has been successfully cleared."; } $pdo->commit(); $pdo->exec("SET FOREIGN_KEY_CHECKS=1;"); } catch (Exception $e) { if ($pdo->inTransaction()) { $pdo->rollBack(); } // Always re-enable foreign key checks, even on error $pdo->exec("SET FOREIGN_KEY_CHECKS=1;"); $error = "An error occurred: " . $e->getMessage(); } } $page_title = "Data Management"; ?> <?= htmlspecialchars($page_title) ?> - Haki Schedule

Clear Timetable Data

This action will delete all generated timetable schedules. This is useful if you want to regenerate the timetable after making changes to classes, subjects, or workloads. This will allow you to delete classes and subjects if they are currently locked by the timetable.

This action cannot be undone.

Clear All School Data

This action will permanently delete ALL data associated with your school, including classes, subjects, teachers, workloads, and the timetable. This is for starting completely fresh.

This is a destructive action and cannot be undone.