diff --git a/admin_data_management.php b/admin_data_management.php new file mode 100644 index 0000000..e2c4e36 --- /dev/null +++ b/admin_data_management.php @@ -0,0 +1,156 @@ +beginTransaction(); + + $stmt = $pdo->prepare("DELETE FROM schedule_teachers WHERE schedule_id IN (SELECT id FROM schedules WHERE school_id = ?)"); + $stmt->execute([$school_id]); + + $stmt = $pdo->prepare("DELETE FROM schedules WHERE school_id = ?"); + $stmt->execute([$school_id]); + + $pdo->commit(); + $message = "Timetable data has been successfully cleared."; + } catch (Exception $e) { + $pdo->rollBack(); + $error = "An error occurred while clearing timetable data: " . $e->getMessage(); + } + } + + if (isset($_POST['clear_all_data'])) { + if (isset($_POST['confirm_delete'])) { + try { + $pdo->beginTransaction(); + + // Order of deletion is important to avoid foreign key constraint errors + $tables_to_clear = [ + 'schedule_teachers', + 'schedules', + 'workloads', + 'elective_group_subjects', + 'teachers', // Teachers might be referenced by workloads + 'subjects', // Subjects are referenced by many tables + 'elective_groups', + 'classes' + ]; + + // First, delete from linking tables based on schedule_id + $stmt = $pdo->prepare("DELETE FROM schedule_teachers WHERE schedule_id IN (SELECT id FROM schedules WHERE school_id = ?)"); + $stmt->execute([$school_id]); + + // Now delete from the main tables with a school_id + $stmt = $pdo->prepare("DELETE FROM schedules WHERE school_id = ?"); + $stmt->execute([$school_id]); + $stmt = $pdo->prepare("DELETE FROM workloads WHERE school_id = ?"); + $stmt->execute([$school_id]); + + // elective_group_subjects links subjects and elective_groups + $stmt = $pdo->prepare("DELETE egs FROM elective_group_subjects egs JOIN subjects s ON egs.subject_id = s.id WHERE s.school_id = ?"); + $stmt->execute([$school_id]); + + $stmt = $pdo->prepare("DELETE FROM teachers WHERE school_id = ?"); + $stmt->execute([$school_id]); + $stmt = $pdo->prepare("DELETE FROM subjects WHERE school_id = ?"); + $stmt->execute([$school_id]); + $stmt = $pdo->prepare("DELETE FROM elective_groups WHERE class_id IN (SELECT id FROM classes WHERE school_id = ?)"); + $stmt->execute([$school_id]); + $stmt = $pdo->prepare("DELETE FROM classes WHERE school_id = ?"); + $stmt->execute([$school_id]); + + $pdo->commit(); + $message = "All school data has been successfully cleared."; + } catch (Exception $e) { + $pdo->rollBack(); + $error = "An error occurred while clearing all data: " . $e->getMessage(); + } + } else { + $error = "Please check the confirmation box to proceed with deleting all data."; + } + } +} + +?> + + +
+ + +This action will delete all generated timetable entries (`schedules` and `schedule_teachers`) for your school. This is useful if you want to regenerate the timetable from scratch or if you are unable to delete other items like classes or subjects due to database constraints.
+This will not delete your classes, subjects, teachers, or workloads.
+ +WARNING: This is a destructive action.
+This action will permanently delete all data associated with your school, including:
+Your user account and school registration will not be affected. Use this option only if you want to start over completely.
+ +