/home/techb158/workloadmatch.com/Manager/Inc
Edit: /home/techb158/workloadmatch.com/Manager/Inc/Lockup_to_Assign.php (6367B)
$courseID) {
$courseID = intval($courseID);
$timeSlotInput = !empty($_POST['Time_Slot'][$index]) ? mysqli_real_escape_string($mysqli, $_POST['Time_Slot'][$index]) : null;
$startDate = !empty($_POST['Start_Date'][$index]) ? mysqli_real_escape_string($mysqli, $_POST['Start_Date'][$index]) : null;
$endDate = !empty($_POST['End_Date'][$index]) ? mysqli_real_escape_string($mysqli, $_POST['End_Date'][$index]) : null;
$queryTime = "SELECT Time_Slot_ID, Time_Slot, Time_From, Time_To
FROM time_slot_programs
WHERE Time_Slot = ?";
$stmtTime = $mysqli->prepare($queryTime);
if (!$stmtTime) { continue; }
$stmtTime->bind_param("s", $timeSlotInput);
if (!$stmtTime->execute()) { $stmtTime->close(); continue; }
$resultTime = $stmtTime->get_result();
$timeRow = $resultTime->fetch_assoc();
$stmtTime->close();
if ($timeRow) {
$timeSlot = $timeRow["Time_Slot"];
$timeSlotID = $timeRow["Time_Slot_ID"];
$startTime = !empty($timeRow['Time_From']) ? $timeRow['Time_From'] : null;
$endTime = !empty($timeRow['Time_To']) ? $timeRow['Time_To'] : null;
} else {
continue;
}
$reserveCourse = isset($_POST['Reserve_Course'][$courseID]) ? 1 : 0;
if ($timeSlot && $startDate && $endDate && $startTime && $endTime) {
$checkQuery = "SELECT COUNT(*) FROM teacher_course_assignments
WHERE Program_ID = ? AND Course_ID = ? AND Group_ID = ? AND Teacher_ID = ?";
$stmtCheck = $mysqli->prepare($checkQuery);
if (!$stmtCheck) { continue; }
$stmtCheck->bind_param("iiii", $Program_ID, $courseID, $Group_ID, $Teacher_ID);
if (!$stmtCheck->execute()) { $stmtCheck->close(); continue; }
$stmtCheck->bind_result($count);
$stmtCheck->fetch();
$stmtCheck->close();
if ($count > 0) { continue; }
$conflictQuery = "SELECT COUNT(*) AS cnt FROM teacher_course_assignments
WHERE Teacher_ID = ?
AND (? <= End_Date AND ? >= Start_Date)
AND NOT (Course_ID = ? AND Group_ID = ? AND Program_ID = ?)";
$stmtConflict = $mysqli->prepare($conflictQuery);
if (!$stmtConflict) { continue; }
$stmtConflict->bind_param("issiii", $Teacher_ID, $startDate, $endDate, $courseID, $Group_ID, $Program_ID);
if (!$stmtConflict->execute()) { $stmtConflict->close(); continue; }
$resultConflict = $stmtConflict->get_result();
$conflictRow = $resultConflict->fetch_assoc();
$conflictCount = $conflictRow['cnt'];
$stmtConflict->close();
if ($conflictCount > 0) { continue; }
$fetchQuery = "SELECT Schedule_ID, 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";
$stmtFetch = $mysqli->prepare($fetchQuery);
if (!$stmtFetch) { continue; }
$stmtFetch->bind_param("iii", $Program_ID, $courseID, $Group_ID);
if (!$stmtFetch->execute()) { $stmtFetch->close(); continue; }
$stmtFetch->bind_result($Schedule_ID, $dbReserveCourse, $dbTime_Slot, $dbStart_Date, $dbEnd_Date, $dbStart_Time, $dbEnd_Time);
$schedules = [];
while ($stmtFetch->fetch()) {
$schedules[] = [
'Schedule_ID' => $Schedule_ID,
'Reserve_Course' => $dbReserveCourse,
'Time_Slot' => $dbTime_Slot,
'Start_Date' => $dbStart_Date,
'End_Date' => $dbEnd_Date,
'Start_Time' => $dbStart_Time,
'End_Time' => $dbEnd_Time,
];
}
$stmtFetch->close();
if (empty($schedules)) { continue; }
foreach ($schedules as $s) {
$assignedAt = date('Y-m-d H:i:s');
$stmtInsert = $mysqli->prepare("INSERT INTO teacher_course_assignments
(Teacher_ID, Course_ID, Group_ID, Program_ID, Schedule_ID, Time_Slot, Start_Date, End_Date, Assigned_At)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)");
if (!$stmtInsert) { continue; }
$stmtInsert->bind_param("iiiiissss",
$Teacher_ID, $courseID, $Group_ID, $Program_ID,
$s['Schedule_ID'], $s['Time_Slot'], $s['Start_Date'], $s['End_Date'], $assignedAt);
if ($stmtInsert->execute()) {
$stmtInsert->close();
$stmtUpdate1 = $mysqli->prepare("UPDATE course_group_schedule_main SET Assigned = 1 WHERE Schedule_ID = ?");
$stmtUpdate1->bind_param("i", $s['Schedule_ID']);
$stmtUpdate1->execute();
$stmtUpdate1->close();
$stmtUpdate2 = $mysqli->prepare("UPDATE schedule_course_for_group SET Assigned = 1 WHERE Schedule_ID = ? AND Program_ID = ? AND Group_ID = ?");
$stmtUpdate2->bind_param("iii", $s['Schedule_ID'], $Program_ID, $Group_ID);
$stmtUpdate2->execute();
$stmtUpdate2->close();
$assignedCount++;
} else {
$stmtInsert->close();
}
}
}
}
if ($assignedCount > 0) {
set_flash_msg("$assignedCount course(s) assigned successfully!", 'success');
} else {
set_flash_msg("No courses were assigned. They may conflict with existing assignments.", 'warning');
}
header("Location: Lockup_to_Assign");
exit();
}
?>