prepare("SELECT id FROM teachers WHERE user_id = ?"); $stmt->execute([$user_id]); $teacher = $stmt->fetch(PDO::FETCH_ASSOC); $teacher_id = $teacher['id']; // Handle Delete request if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['delete_id'])) { try { $stmt = $pdo->prepare("DELETE FROM workloads WHERE id = ? AND teacher_id = ? AND school_id = ?"); if ($stmt->execute([$_POST['delete_id'], $teacher_id, $school_id])) { $message = 'Workload deleted successfully!'; } else { $error = 'Failed to delete workload.'; } } catch (PDOException $e) { $error = 'Database error: ' . $e->getMessage(); } } // Handle POST request to add or update a workload elseif ($_SERVER['REQUEST_METHOD'] === 'POST') { $workload_id = $_POST['workload_id'] ?? null; $class_id = $_POST['class_id'] ?? null; $subject_id = $_POST['subject_id'] ?? null; $lessons_per_week = $_POST['lessons_per_week'] ?? null; if (empty($class_id) || empty($subject_id) || empty($lessons_per_week)) { $error = 'All fields are required.'; } elseif (!filter_var($lessons_per_week, FILTER_VALIDATE_INT, ["options" => ["min_range" => 1]])) { $error = 'Lessons per week must be a positive number.'; } else { try { if ($workload_id) { // Update $stmt = $pdo->prepare("UPDATE workloads SET class_id = ?, subject_id = ?, lessons_per_week = ? WHERE id = ? AND teacher_id = ? AND school_id = ?"); if ($stmt->execute([$class_id, $subject_id, $lessons_per_week, $workload_id, $teacher_id, $school_id])) { $message = 'Workload updated successfully!'; } else { $error = 'Failed to update workload.'; } } else { // Insert $stmt = $pdo->prepare("INSERT INTO workloads (class_id, subject_id, teacher_id, lessons_per_week, school_id) VALUES (?, ?, ?, ?, ?)"); if ($stmt->execute([$class_id, $subject_id, $teacher_id, $lessons_per_week, $school_id])) { $message = 'Workload created successfully!'; } else { $error = 'Failed to create workload.'; } } } catch (PDOException $e) { if ($e->errorInfo[1] == 1062) { $error = 'Error: This workload assignment (Class, Subject) already exists for you.'; } else { $error = 'Database error: ' . $e->getMessage(); } } } } // Handle Edit request (fetch workload to edit) if (isset($_GET['edit_id'])) { try { $stmt = $pdo->prepare("SELECT * FROM workloads WHERE id = ? AND teacher_id = ? AND school_id = ?"); $stmt->execute([$_GET['edit_id'], $teacher_id, $school_id]); $edit_workload = $stmt->fetch(PDO::FETCH_ASSOC); } catch (PDOException $e) { $error = 'Database error: ' . $e->getMessage(); } } // Fetch related data for dropdowns try { $classes_stmt = $pdo->prepare("SELECT id, name FROM classes WHERE school_id = ? ORDER BY name"); $classes_stmt->execute([$school_id]); $classes = $classes_stmt->fetchAll(PDO::FETCH_ASSOC); $subjects_stmt = $pdo->prepare("SELECT id, name FROM subjects WHERE school_id = ? ORDER BY name"); $subjects_stmt->execute([$school_id]); $subjects = $subjects_stmt->fetchAll(PDO::FETCH_ASSOC); } catch (PDOException $e) { $error = 'Database error while fetching data: ' . $e->getMessage(); } // Fetch all workloads for this teacher to display $workloads = []; try { $workloads_stmt = $pdo->prepare(" SELECT w.id, c.name as class_name, s.name as subject_name, w.lessons_per_week, w.created_at, w.class_id, w.subject_id FROM workloads w JOIN classes c ON w.class_id = c.id JOIN subjects s ON w.subject_id = s.id WHERE w.teacher_id = ? AND w.school_id = ? ORDER BY w.created_at DESC "); $workloads_stmt->execute([$teacher_id, $school_id]); $workloads = $workloads_stmt->fetchAll(PDO::FETCH_ASSOC); } catch (PDOException $e) { $error = 'Database error while fetching workloads: ' . $e->getMessage(); } ?> My Workload - Haki Schedule

My Workload

Cancel Edit
My Existing Workloads
ClassSubjectLessons/WeekActions