prepare("SELECT Time_Slot FROM teacher_profile WHERE Teacher_ID = ?");
$teacherSlotStmt->bind_param("i", $teacherID);
$teacherSlotStmt->execute();
$teacherSlotResult = $teacherSlotStmt->get_result();
$teacherSlotRow = $teacherSlotResult->fetch_assoc();
$teacherSlots = $teacherSlotRow ? trim($teacherSlotRow['Time_Slot']) : '';
$teacherSlotStmt->close();
// Query to get courses for this program
$query = "SELECT Course_ID, Course_Name FROM Courses WHERE Program_ID = ? ORDER BY Course_ID";
$stmt = $mysqli->prepare($query);
$stmt->bind_param("i", $programID);
$stmt->execute();
$result = $stmt->get_result();
while ($row = $result->fetch_assoc()) {
$courseID = $row["Course_ID"];
// Check if an unassigned schedule exists for this course and group
$checkStmt = $mysqli->prepare("SELECT COUNT(*) FROM course_group_schedule_main
WHERE Program_ID = ? AND Course_ID = ? AND Group_ID = ? AND Assigned = 0");
if (!$checkStmt) {
die("Error preparing assignment check query: " . $mysqli->error);
}
$checkStmt->bind_param('iii', $programID, $courseID, $groupID);
$checkStmt->execute();
$checkStmt->bind_result($count);
$checkStmt->fetch();
$checkStmt->close();
if ($count > 0) {
// Check if teacher prefers this course
if ($teacherID > 0) {
$prefStmt = $mysqli->prepare("SELECT COUNT(*) FROM teacher_course_preferences
WHERE Teacher_ID = ? AND Course_ID = ?");
$prefStmt->bind_param("ii", $teacherID, $courseID);
$prefStmt->execute();
$prefStmt->bind_result($prefCount);
$prefStmt->fetch();
$prefStmt->close();
if ($prefCount == 0) {
continue; // Skip this course if not preferred
}
}
// Skip if this course's time slot doesn't match the teacher's available slots
$fetchStmt = $mysqli->prepare("SELECT Reserve_Course, Time_Slot, Start_Date, End_Date, Start_Time, End_Time
FROM course_group_schedule_main
WHERE Program_ID = ? AND Course_ID = ? AND Group_ID = ? AND Assigned = 0");
if (!$fetchStmt) {
echo "Error preparing schedule fetch query for Course ID $courseID: " . $mysqli->error . " ";
continue;
}
$fetchStmt->bind_param('iii', $programID, $courseID, $groupID);
$fetchStmt->execute();
$fetchStmt->bind_result($Reserve_Course, $Time_Slot, $Start_Date, $End_Date, $Start_Time, $End_Time);
if (!$fetchStmt->fetch()) {
$fetchStmt->close();
continue;
}
$fetchStmt->close();
// Skip if this course's time slot doesn't match the teacher's available slots
if ($teacherSlots !== '') {
$teacherSlotList = array_map('trim', explode(',', $teacherSlots));
if (!in_array($Time_Slot, $teacherSlotList)) {
continue;
}
}
$checkedReserve = ($Reserve_Course == 1) ? 'checked' : '';
// Check conflict with teacher's current schedule
if ($teacherID > 0) {
/* $conflictStmt = $mysqli->prepare("SELECT COUNT(*) FROM teacher_course_assignments
WHERE Teacher_ID = ?
AND Time_Slot = ?
AND (? <= End_Date AND ? >= Start_Date)"); */
// Revised overlap-checking query:
// CORRECT “overlap†statement:
$conflictStmt = $mysqli->prepare("
SELECT COUNT(*)
FROM teacher_course_assignments
WHERE Teacher_ID = ?
AND Time_Slot = ?
AND Start_Date <= ? -- (existing_start ≤ new_end)
AND End_Date >= ? -- (existing_end ≥ new_start)
");
/* if (!$conflictStmt) {
die("Error preparing conflict-check query: " . $mysqli->error);
} */
if ($conflictStmt) {
//$conflictStmt->bind_param("isss", $teacherID, $Time_Slot, $Start_Date, $End_Date);
// Bind: (teacherID, time_slot, new_end, new_start)
$conflictStmt->bind_param(
"isss",
$teacherID,
$Time_Slot,
$End_Date, // new schedule’s End_Date
$Start_Date // new schedule’s Start_Date
);
$conflictStmt->execute();
$conflictStmt->bind_result($conflictCount);
$conflictStmt->fetch();
$conflictStmt->close();
if ($conflictCount > 0) {
// There’s already an assignment for this teacher in the same time slot
// whose date-range overlaps [Start_Date … End_Date], so skip.
continue;
}
// Check if the teacher is unavailable during the given period
if ($teacherID > 0) {
// Prepare SQL statement to check teacher's unavailability
$availabilityStmt = $mysqli->prepare(
"SELECT COUNT(*) FROM teacher_unavailability
WHERE Teacher_ID = ?
AND Unavailable_From <= ?
AND Unavailable_To >= ?"
);
// Handle SQL statement preparation error
if (!$availabilityStmt) {
die("Error preparing teacher availability check query: " . $mysqli->error);
}
// Bind parameters to the SQL query
$availabilityStmt->bind_param(
"iss",
$teacherID, // Teacher ID to check
$End_Date, // End date of the course schedule
$Start_Date // Start date of the course schedule
);
// Execute the query
$availabilityStmt->execute();
// Fetch result to determine if the teacher is unavailable
$availabilityStmt->bind_result($unavailableCount);
$availabilityStmt->fetch();
// Close the statement
$availabilityStmt->close();
// If the teacher is unavailable during this period, skip this iteration
if ($unavailableCount > 0) {
continue;
}
}
}
}
$Start_Time = date("h:i A", strtotime($Start_Time));
$End_Time = date("h:i A", strtotime($End_Time));
echo "