/home/techb158/workloadmatch.com/workloadmatch.com/BackUp/Manager
Edit: /home/techb158/workloadmatch.com/workloadmatch.com/BackUp/Manager/generate_schedule_handler.php (7095B)
'error', 'message' => 'Invalid request method.']);
exit;
}
$Program_ID = intval($_POST['Program_ID'] ?? 0);
$Group_ID = intval($_POST['Group_ID'] ?? 0);
$Start_Date = $_POST['Start_Date'] ?? '';
$Removed_Date = $_POST['date'] ?? null;
$selectedCourses = json_decode($_POST['Selected_Courses'] ?? '[]', true);
$priorityCourses = json_decode($_POST['Priority_Course'] ?? '[]', true);
if (!$Program_ID || !$Group_ID || !$Start_Date || empty($selectedCourses)) {
echo json_encode(['status' => 'error', 'message' => 'Missing input data.']);
exit;
}
function get_group_info($mysqli, $groupID) {
$stmt = $mysqli->prepare("SELECT Time_From, Time_To, class_days FROM Manager_Group_Name WHERE Group_ID = ?");
$stmt->bind_param("i", $groupID);
$stmt->execute();
return $stmt->get_result()->fetch_assoc();
}
function get_slot_mappings($mysqli, $groupID) {
$stmt = $mysqli->prepare("SELECT Time_Slot, Time_From, Time_To FROM Group_Slot_Mapping WHERE Group_ID = ?");
$stmt->bind_param("i", $groupID);
$stmt->execute();
$res = $stmt->get_result();
$slots = [];
while ($row = $res->fetch_assoc()) {
$slots[] = $row;
}
return $slots;
}
function get_holidays($mysqli, $startDate) {
$holidays = [];
$res = $mysqli->query("SELECT Event_Start, Event_End FROM Events WHERE Calendar_Year = YEAR('$startDate')");
while ($row = $res->fetch_assoc()) {
$start = new DateTime($row['Event_Start']);
$end = new DateTime($row['Event_End']);
while ($start <= $end) {
$holidays[] = $start->format('Y-m-d');
$start->modify('+1 day');
}
}
return $holidays;
}
function get_retake_dates($mysqli, $programID, $groupID) {
$retakes = [];
$stmt = $mysqli->prepare("SELECT Retake_Date FROM Retake_Records WHERE Program_ID = ? AND Group_ID = ?");
$stmt->bind_param("ii", $programID, $groupID);
$stmt->execute();
$res = $stmt->get_result();
while ($row = $res->fetch_assoc()) {
$retakes[] = $row['Retake_Date'];
}
return $retakes;
}
function calculate_session_length($slots) {
$total = 0;
foreach ($slots as $slot) {
$from = new DateTime($slot['Time_From']);
$to = new DateTime($slot['Time_To']);
$total += ($to->getTimestamp() - $from->getTimestamp()) / 3600;
}
return count($slots) ? $total / count($slots) : 3;
}
function generate_valid_dates($startDate, $classDays, $holidays, $retakes, $removedSaturdays) {
$valid = [];
$cur = new DateTime($startDate);
for ($i = 0; $i < 365; $i++) {
$day = $cur->format('l');
$dateStr = $cur->format('Y-m-d');
$month = (int)$cur->format('m');
if ($month === 7 || !in_array($day, $classDays)) {
$cur->modify('+1 day');
continue;
}
if (in_array($dateStr, $removedSaturdays)) {
$cur->modify('+1 day');
continue;
}
$type = in_array($dateStr, $holidays) ? 'Holiday' : (in_array($dateStr, $retakes) ? 'Retake' : 'Class');
$valid[] = ['Date' => $dateStr, 'Type' => $type];
$cur->modify('+1 day');
}
return $valid;
}
function format_time_slot_label($slot, $day) {
if ($day === 'Saturday') {
if (stripos($slot, 'evening') !== false) return 'Morning (Sat)';
if (stripos($slot, 'morning') !== false) return 'Morning';
}
return $slot;
}
function format_time_slot_range($slot, $from, $to, $day) {
if ($day === 'Saturday') {
if (stripos($slot, 'morning') !== false) return "08:30 AM - 12:30 PM";
if (stripos($slot, 'evening') !== false) return "09:00 AM - 01:00 PM";
}
return date('h:i A', strtotime($from)) . ' - ' . date('h:i A', strtotime($to));
}
$group = get_group_info($mysqli, $Group_ID);
$slotMappings = get_slot_mappings($mysqli, $Group_ID);
$classDays = explode(',', $group['class_days']);
$holidays = get_holidays($mysqli, $Start_Date);
$retakes = get_retake_dates($mysqli, $Program_ID, $Group_ID);
$sessionLength = calculate_session_length($slotMappings);
// Fetch course sessions
$courseSessions = [];
$res = $mysqli->query("SELECT Course_ID, Course_Time FROM Courses WHERE Program_ID = $Program_ID");
while ($row = $res->fetch_assoc()) {
$courseSessions[$row['Course_ID']] = ceil($row['Course_Time'] / $sessionLength);
}
$remainingSessions = [];
foreach ($selectedCourses as $cid) {
$remainingSessions[$cid] = $courseSessions[$cid] ?? 0;
}
if (!empty($priorityCourses)) {
usort($selectedCourses, function ($a, $b) use ($priorityCourses) {
return ($priorityCourses[$a] ?? PHP_INT_MAX) <=> ($priorityCourses[$b] ?? PHP_INT_MAX);
});
}
$deletedSaturdays = $_SESSION['deletedSaturdays'] ?? [];
if ($Removed_Date) {
$deletedSaturdays[] = $Removed_Date;
$_SESSION['deletedSaturdays'] = array_unique($deletedSaturdays);
}
$validDates = generate_valid_dates($Start_Date, $classDays, $holidays, $retakes, $deletedSaturdays);
$courseNames = [];
$res = $mysqli->query("SELECT Course_ID, Course_Name FROM Courses WHERE Program_ID = $Program_ID");
while ($row = $res->fetch_assoc()) {
$courseNames[$row['Course_ID']] = $row['Course_Name'];
}
$currentCourse = reset($selectedCourses);
$html = '
';
$html .= '| Date | Slot | Time | Course | Action |
';
$slotCount = count($slotMappings);
$slotIndex = 0;
foreach ($validDates as $entry) {
$date = $entry['Date'];
$dayName = (new DateTime($date))->format('l');
$type = $entry['Type'];
if ($type !== 'Class') {
$color = $type === 'Holiday' ? '#f0ad4e' : '#5bc0de';
$html .= "| $date ($type) |
";
continue;
}
if (!$currentCourse) break;
$slot = $slotMappings[$slotIndex % $slotCount];
$slotLabel = format_time_slot_label($slot['Time_Slot'], $dayName);
$timeRange = format_time_slot_range($slot['Time_Slot'], $slot['Time_From'], $slot['Time_To'], $dayName);
$exam = ($remainingSessions[$currentCourse] == 1);
$examTag = $exam ? ' (Exam Day)' : '';
$action = ($dayName === 'Saturday' && !in_array($date, $deletedSaturdays))
? ""
: ($dayName === 'Saturday' ? '๐๏ธ Removed' : '-');
$html .= "| ($dayName) $date | $slotLabel | $timeRange | {$courseNames[$currentCourse]}$examTag | $action |
";
$remainingSessions[$currentCourse]--;
if ($remainingSessions[$currentCourse] <= 0) {
$currentCourse = next($selectedCourses);
}
$slotIndex++;
}
$html .= '
';
echo json_encode(['status' => 'success', 'html' => $html]);
exit;