/home/techb158/workloadmatch.com/workloadmatch.com/Manager
Edit: /home/techb158/workloadmatch.com/workloadmatch.com/Manager/delete_course_from_teacher.php (4990B)
"error", "message" => "Invalid assignment ID."]));
}
// Begin a transaction to ensure both deletion and update succeed together.
$mysqli->begin_transaction();
try {
/* // 1. Retrieve the Schedule_ID for the assignment so we know which schedule to update.
//$stmt = $mysqli->prepare("SELECT Schedule_ID FROM Teacher_Course_Assignments WHERE Assignment_ID = ?");
if (!$stmt) {
throw new Exception("Error preparing retrieval query: " . $mysqli->error);
}
$stmt->bind_param("i", $assignmentID);
$stmt->execute();
$result = $stmt->get_result();
if ($result->num_rows === 0) {
throw new Exception("No assignment found with the provided ID.");
}
$row = $result->fetch_assoc();
$scheduleID = $row['Schedule_ID'];
$stmt->close();
// 2. Delete the teacher's course assignment.
$stmt = $mysqli->prepare("DELETE FROM Teacher_Course_Assignments WHERE Assignment_ID = ?");
if (!$stmt) {
throw new Exception("Error preparing deletion query: " . $mysqli->error);
}
$stmt->bind_param("i", $assignmentID);
if (!$stmt->execute()) {
throw new Exception("Error executing deletion query: " . $stmt->error);
}
$stmt->close();
// 3. Update the Course_Group_Schedule table to mark the schedule as not assigned.
$stmt = $mysqli->prepare("UPDATE Course_Group_Schedule_Main SET Assigned = 0 WHERE Schedule_ID = ?");
if (!$stmt) {
throw new Exception("Error preparing update query: " . $mysqli->error);
}
$stmt->bind_param("i", $scheduleID);
if (!$stmt->execute()) {
throw new Exception("Error executing update query: " . $stmt->error);
}
$stmt->close();
// 3. Update the Course_Group_Schedule table to mark the schedule as not assigned.
$stmt = $mysqli->prepare("UPDATE Schedule_Course_for_Group SET Assigned = 0 WHERE Schedule_ID = ?");
if (!$stmt) {
throw new Exception("Error preparing update query: " . $mysqli->error);
}
$stmt->bind_param("i", $scheduleID);
if (!$stmt->execute()) {
throw new Exception("Error executing update query: " . $stmt->error);
}
$stmt->close();
// Commit the transaction if all queries succeeded.
$mysqli->commit(); */
// 1. Get full context
$stmt = $mysqli->prepare("SELECT Teacher_ID, Course_ID, Group_ID, Program_ID
FROM Teacher_Course_Assignments WHERE Assignment_ID = ?");
$stmt->bind_param("i", $assignmentID);
$stmt->execute();
$row = $stmt->get_result()->fetch_assoc();
$teacherID = $row['Teacher_ID'];
$courseID = $row['Course_ID'];
$groupID = $row['Group_ID'];
$programID = $row['Program_ID'];
$stmt->close();
// 2. Get all related assignments
$stmt = $mysqli->prepare("SELECT Assignment_ID, Schedule_ID
FROM Teacher_Course_Assignments
WHERE Teacher_ID = ? AND Course_ID = ? AND Group_ID = ? AND Program_ID = ?");
$stmt->bind_param("iiii", $teacherID, $courseID, $groupID, $programID);
$stmt->execute();
$result = $stmt->get_result();
$assignments = [];
$schedules = [];
while ($row = $result->fetch_assoc()) {
$assignments[] = $row['Assignment_ID'];
$schedules[] = $row['Schedule_ID'];
}
$stmt->close();
// 3. Delete all assignments
foreach ($assignments as $aID) {
$stmt = $mysqli->prepare("DELETE FROM Teacher_Course_Assignments WHERE Assignment_ID = ?");
if (!$stmt) {
throw new Exception("Failed to prepare delete statement for Assignment_ID $aID: " . $mysqli->error);
}
$stmt->bind_param("i", $aID);
if (!$stmt->execute()) {
throw new Exception("Failed to execute delete for Assignment_ID $aID: " . $stmt->error);
}
$stmt->close();
}
// 4. Unassign all related schedules
foreach ($schedules as $sID) {
$stmt = $mysqli->prepare("UPDATE Course_Group_Schedule_Main SET Assigned = 0 WHERE Schedule_ID = ?");
$stmt->bind_param("i", $sID);
$stmt->execute();
$stmt->close();
$stmt = $mysqli->prepare("UPDATE Schedule_Course_for_Group SET Assigned = 0 WHERE Schedule_ID = ?");
$stmt->bind_param("i", $sID);
$stmt->execute();
$stmt->close();
}
$mysqli->commit();
echo json_encode(["status" => "success", "message" => "Course fully unassigned from teacher."]);
} catch (Exception $e) {
$mysqli->rollback();
echo json_encode(["status" => "error", "message" => "Unassignment failed: " . $e->getMessage()]);
}
}
?>