From 83fdeb8365178f8d8316b8967cd78fd25edb1997 Mon Sep 17 00:00:00 2001 From: Flatlogic Bot Date: Sat, 6 Dec 2025 03:24:34 +0000 Subject: [PATCH] All data has been deleted, now I want to see --- admin_data_management.php | 115 ++++++++++++++++++++++++++++++++++++++ includes/navbar.php | 2 + 2 files changed, 117 insertions(+) create mode 100644 admin_data_management.php diff --git a/admin_data_management.php b/admin_data_management.php new file mode 100644 index 0000000..847714c --- /dev/null +++ b/admin_data_management.php @@ -0,0 +1,115 @@ +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.

+
+ +
+
+
+ +
+ + + \ No newline at end of file diff --git a/includes/navbar.php b/includes/navbar.php index 842197e..4ce32cd 100644 --- a/includes/navbar.php +++ b/includes/navbar.php @@ -25,6 +25,8 @@ $role = $_SESSION['role'] ?? '';
  • Workloads
  • Timeslots
  • Elective Groups
  • +
  • +
  • Data Management