/home/techb158/workloadmatch.com/workloadmatch.com/BackUp/Manager
Edit: /home/techb158/workloadmatch.com/workloadmatch.com/BackUp/Manager/do_remove_saturday.php (6170B)
prepare("
SELECT class_days, Weekend_Class
FROM Manager_Group_Name
WHERE Group_ID = ?
");
$stmt->bind_param("i", $Group_ID);
$stmt->execute();
return $stmt->get_result()->fetch_assoc() ?: [];
}
/**
* Build list of holidays in that calendar year
*/
function get_holidays($mysqli, $Start_Date) {
$holidays = [];
$sql = "
SELECT Event_Start, Event_End
FROM Events
WHERE Calendar_Year = YEAR(?)
";
$stmt = $mysqli->prepare($sql);
$stmt->bind_param('s', $Start_Date);
$stmt->execute();
$res = $stmt->get_result();
while ($r = $res->fetch_assoc()) {
$cur = new DateTime($r['Event_Start']);
$end = new DateTime($r['Event_End']);
while ($cur <= $end) {
$holidays[$cur->format('Y-m-d')] = true;
$cur->modify('+1 day');
}
}
return $holidays;
}
/**
* Build list of retake dates
*/
function get_retake_dates($mysqli, $Program_ID, $Group_ID) {
$rd = [];
$stmt = $mysqli->prepare("
SELECT Retake_Date
FROM Retake_Records
WHERE Program_ID = ? AND Group_ID = ?
");
$stmt->bind_param("ii", $Program_ID, $Group_ID);
$stmt->execute();
foreach ($stmt->get_result() as $row) {
$rd[$row['Retake_Date']] = true;
}
return $rd;
}
/**
* Generate next ~400 valid days after $start
* (skip July, holidays, retakes, non-class days)
*/
function generate_valid_dates($start, $classDays, $holidays, $retakes) {
$out = [];
$cur = new DateTime($start);
for ($i = 0; $i < 400; $i++, $cur->modify('+1 day')) {
$d = $cur->format('Y-m-d');
$dayName = $cur->format('l');
$month = (int)$cur->format('m');
if ($month === 7) continue;
if (!in_array($dayName, $classDays)) continue;
if (isset($holidays[$d]) || isset($retakes[$d])) continue;
$out[] = $d;
}
return $out;
}
// 1) Validate
if (empty($_POST['Schedule_ID'])) {
http_response_code(400);
echo json_encode(['error'=>'Missing Schedule_ID']);
exit;
}
$scheduleID = (int)$_POST['Schedule_ID'];
// 2) Fetch the session to delete (reserve flag too)
$stmt = $mysqli->prepare("
SELECT Program_ID, Group_ID, Course_ID,
Group_Slot_ID, Reserve_Course,
Time_Slot, Start_Date, Start_Time, End_Time
FROM Schedule_Course_for_Group
WHERE Schedule_ID = ?
");
$stmt->bind_param('i', $scheduleID);
$stmt->execute();
$stmt->bind_result(
$programID,
$groupID,
$courseID,
$groupSlotID,
$reserveFlag,
$timeSlot,
$remDate,
$remStart,
$remEnd
);
if (!$stmt->fetch()) {
http_response_code(404);
echo json_encode(['error'=>'Session not found']);
exit;
}
$stmt->close();
// 3) Delete that session
$del = $mysqli->prepare("
DELETE FROM Schedule_Course_for_Group
WHERE Schedule_ID = ?
");
$del->bind_param('i', $scheduleID);
$del->execute();
$del->close();
// 4) Gather class_days, holidays, retakes
$grp = get_group_info($mysqli, $groupID);
$classDays = array_map('trim', explode(',', $grp['class_days']));
if (empty((bool)$grp['Weekend_Class'])) {
// no Saturdays at all
$classDays = array_filter($classDays, fn($d) => $d !== 'Saturday');
}
$holidays = get_holidays($mysqli, $remDate);
$retakes = get_retake_dates($mysqli, $programID, $groupID);
// 5) Generate all valid dates after the removed one
$allValid = generate_valid_dates($remDate, $classDays, $holidays, $retakes);
array_shift($allValid); // drop the very first (the one we just deleted)
// 6) Shift all future sessions forward one slot
$shiftStmt = $mysqli->prepare("
SELECT Schedule_ID
FROM Schedule_Course_for_Group
WHERE Program_ID = ?
AND Group_ID = ?
AND Start_Date > ?
ORDER BY Start_Date, Start_Time
");
$shiftStmt->bind_param('iis', $programID, $groupID, $remDate);
$shiftStmt->execute();
$shiftStmt->bind_result($sid);
$toShift = [];
while ($shiftStmt->fetch()) {
$toShift[] = $sid;
}
$shiftStmt->close();
$upd = $mysqli->prepare("
UPDATE Schedule_Course_for_Group
SET Start_Date = ?, End_Date = ?
WHERE Schedule_ID = ?
");
foreach ($toShift as $i => $sid) {
if (!isset($allValid[$i])) break;
$newD = $allValid[$i];
$upd->bind_param('ssi', $newD, $newD, $sid);
$upd->execute();
}
$upd->close();
// 7) Re-append one session for the deleted course at the end
// 7) Re-append one session for the deleted course at the end
$inserted = 0;
$newScheduleID = null;
$newDate = null;
if (!empty($allValid)) {
$idx = count($toShift);
// if that exact slot exists, use it; otherwise fall back to the last valid date
$newDate = $allValid[$idx] ?? end($allValid);
// lookup Course_Name
$nm = $mysqli->prepare("SELECT Course_Name FROM Courses WHERE Course_ID = ?");
$nm->bind_param('i', $courseID);
$nm->execute();
$nm->bind_result($courseName);
$nm->fetch();
$nm->close();
// re-insert
$ins = $mysqli->prepare("
INSERT INTO Schedule_Course_for_Group
(Program_ID,Course_ID,Group_ID,Group_Slot_ID,Course_Name,
Reserve_Course,Time_Slot,Start_Date,End_Date,
Start_Time,End_Time,Assigned,Last_Updated)
VALUES (?,?,?,?,?, ?,?,?,?, ?,?, 0, NOW())
");
$ins->bind_param(
'iiiisissssss',
$programID,
$courseID,
$groupID,
$groupSlotID,
$courseName,
$reserveFlag,
$timeSlot,
$newDate,
$newDate,
$remStart,
$remEnd
);
if ($ins->execute()) {
$inserted = 1;
$newScheduleID = $ins->insert_id;
}
$ins->close();
}
// 8) Return JSON
echo json_encode([
'deleted' => true,
'shifted' => count($toShift),
'added' => $inserted,
'new_schedule_id' => $newScheduleID,
'new_date' => $newDate
]);