Manage Subjects
Existing Subjects
No subjects have been created yet.
| Name | Lessons / Week | Double Lesson | Is Elective | Elective Group | Actions |
|---|---|---|---|---|---|
| Edit |
prepare("DELETE FROM subjects WHERE id = ? AND school_id = ?"); $stmt->execute([$delete_id, $school_id]); $message = "Subject deleted successfully."; } catch (PDOException $e) { if ($e->getCode() == '23000') { // Integrity constraint violation $error = "Cannot delete this subject because it is currently used in workloads or schedules. Please remove those associations before deleting."; } else { $error = "Error deleting subject: " . $e->getMessage(); } } } // Handle POST request to add or update a subject if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['subject_name'])) { $subjectName = trim($_POST['subject_name']); $lessons_per_week = filter_input(INPUT_POST, 'lessons_per_week', FILTER_VALIDATE_INT, ['options' => ['default' => 1, 'min_range' => 1]]); $has_double_lesson = isset($_POST['has_double_lesson']) ? 1 : 0; $is_elective = isset($_POST['is_elective']) ? 1 : 0; $elective_group_id = !empty($_POST['elective_group_id']) ? $_POST['elective_group_id'] : null; $subject_id = $_POST['subject_id'] ?? null; if (empty($subjectName)) { $error = 'Subject name cannot be empty.'; } else { // Check for duplicates before inserting/updating $stmt = $pdo->prepare("SELECT id FROM subjects WHERE name = ? AND school_id = ? AND id != ?"); $stmt->execute([$subjectName, $school_id, $subject_id ?? 0]); if ($stmt->fetch()) { $error = "Error: Subject '" . htmlspecialchars($subjectName) . "' already exists."; } else { try { if ($subject_id) { // Update existing subject $stmt = $pdo->prepare("UPDATE subjects SET name = ?, lessons_per_week = ?, has_double_lesson = ?, is_elective = ?, elective_group_id = ? WHERE id = ? AND school_id = ?"); $stmt->execute([$subjectName, $lessons_per_week, $has_double_lesson, $is_elective, $elective_group_id, $subject_id, $school_id]); $message = "Subject updated successfully!"; } else { // Insert new subject $stmt = $pdo->prepare("INSERT INTO subjects (name, lessons_per_week, has_double_lesson, is_elective, elective_group_id, school_id) VALUES (?, ?, ?, ?, ?, ?)"); $stmt->execute([$subjectName, $lessons_per_week, $has_double_lesson, $is_elective, $elective_group_id, $school_id]); $message = "Subject created successfully!"; } } catch (PDOException $e) { $error = 'Database error: ' . $e->getMessage(); } } } } // Handle Edit request if (isset($_GET['edit_id'])) { try { $edit_id = $_GET['edit_id']; $stmt = $pdo->prepare("SELECT * FROM subjects WHERE id = ? AND school_id = ?"); $stmt->execute([$edit_id, $school_id]); $editing_subject = $stmt->fetch(PDO::FETCH_ASSOC); } catch (PDOException $e) { $error = "Error fetching subject: " . $e->getMessage(); } } // Fetch all subjects to display $subjects = []; try { $subjects_stmt = $pdo->prepare("SELECT s.*, eg.name as elective_group_name FROM subjects s LEFT JOIN elective_groups eg ON s.elective_group_id = eg.id WHERE s.school_id = ? ORDER BY s.name ASC"); $subjects_stmt->execute([$school_id]); $subjects = $subjects_stmt->fetchAll(PDO::FETCH_ASSOC); } catch (PDOException $e) { $error = 'Database error: ' . $e->getMessage(); } // Fetch all elective groups for the dropdown $elective_groups = []; try { $stmt = $pdo->prepare("SELECT * FROM elective_groups WHERE school_id = ? ORDER BY name ASC"); $stmt->execute([$school_id]); $elective_groups = $stmt->fetchAll(PDO::FETCH_ASSOC); } catch (PDOException $e) { $error = 'Database error while fetching elective groups: ' . $e->getMessage(); } ?>
No subjects have been created yet.
| Name | Lessons / Week | Double Lesson | Is Elective | Elective Group | Actions |
|---|---|---|---|---|---|
| Edit |