/home/techb158/workloadmatch.com/Manager
NameSizeModeActions
core/-0755rm
css/-0755rm
data/-0755rm
fonts/-0755rm
Inc/-0755rm
js/-0755rm
php/-0755rm
addcourse.php180030644editdlrm
addcoursesession.php144240644editdlrm
addcoursesession_bak.php.disabled208600644editdlrm
addcoursetime.php175640644editdlrm
addgroup.php234650644editdlrm
addgroup1.php204630644editdlrm
addholiday.php182830644editdlrm
addmanager.php190600644editdlrm
addremediation.php181400644editdlrm
addretake.php222840644editdlrm
adduser.php304850644editdlrm
adduser1.php231660644editdlrm
Add_Course_Session.php69310644editdlrm
Add_Course_Session_bak.php.disabled69310644editdlrm
add_event.php20040644editdlrm
Add_Teacher_Unavailability.php17240644editdlrm
AI_Generate_Schedule.php110310644editdlrm
ai_schedule.php199750644editdlrm
ajax_fetch_schedule.php38560644editdlrm
ajax_get_group_slots.php8970644editdlrm
ajax_replace_saturday.php316740644editdlrm
ajax_replace_saturday_backup_latest.php314740644editdlrm
ajax_replace_saturday_bak.php.disabled318120644editdlrm
ajax_swap_courses.php43940644editdlrm
approve_teacher_absence.php22300644editdlrm
approve_teacher_unavailability.php22740644editdlrm
assign_course_group.php181150644editdlrm
assign_course_group1.php207520644editdlrm
Assign_Course_Report.php23180644editdlrm
assign_teacher_legacy.php164900644editdlrm
assign_teacher_manual.php175510644editdlrm
assign_teacher_manual_bak.php.disabled227180644editdlrm
assign_teacher_manual_force_conflicts.php227490644editdlrm
availableteacherreport.php125950644editdlrm
Available_Teacher_Report.php35540644editdlrm
batch_schedule.php95820644editdlrm
calendar.php315020644editdlrm
calendar_schedule.php80860644editdlrm
chat.php164370644editdlrm
chat_bak.php.disabled102650644editdlrm
Check_if_Teacher_Report.php16390644editdlrm
complete_schedule_report_by_teacher.php197990644editdlrm
courseflagtime.php35180644editdlrm
Create_Group_Schedule_Algorithm.php39390644editdlrm
Create_Schedule_Algorithm.php906290644editdlrm
Create_Schedule_Algorithm_1.php424300644editdlrm
Create_Schedule_Algorithm_2.php785320644editdlrm
Create_Schedule_Algorithm_2_combined.php921570644editdlrm
Create_Schedule_Algorithm_2_combined_both_slots.php945460644editdlrm
Create_Schedule_Algorithm_2_combined_both_slots_bak.php.disabled946110644editdlrm
Create_Schedule_Algorithm_2_fixed.php788690644editdlrm
Create_Schedule_Algorithm_3.php895940644editdlrm
Create_Schedule_Algorithm_4.php895940644editdlrm
dashboard.php211030644editdlrm
deletecoursesession.php85910644editdlrm
deleteschedulefromgroup.php174240644editdlrm
delete_course.php6070644editdlrm
delete_course_from_teacher.php51260644editdlrm
delete_course_session_handler.php7910644editdlrm
delete_duplicate_assignments.php25370644editdlrm
delete_event.php12550644editdlrm
delete_group.php19130644editdlrm
delete_holiday.php7430644editdlrm
delete_retake.php13240644editdlrm
delete_schedule_from_group.php19400644editdlrm
delete_teacher_absence.php10150644editdlrm
delete_teacher_level.php8280644editdlrm
delete_teacher_unavailability.php13500644editdlrm
delete_timeslot.php6260644editdlrm
display_schedule.php186620644editdlrm
do_remove_saturday.php64020644editdlrm
duplicate_assignment_report.php94230644editdlrm
edit_event.php16630644editdlrm
examschedulereport.php165100644editdlrm
Exam_Schedule_Report.php27620644editdlrm
export_absences.php12580644editdlrm
fetch_all_scheduled_courses.php63250644editdlrm
fetch_courses.php93730644editdlrm
fetch_courses_for_program.php8150644editdlrm
fetch_courses_for_program_by_schedule.php40470644editdlrm
fetch_courses_teacher_preference.php39540644editdlrm
fetch_course_for_added_group.php10460644editdlrm
fetch_course_hours.php7140644editdlrm
fetch_course_sessions.php14050644editdlrm
fetch_dashboard_charts.php42820644editdlrm
fetch_disabled_dates.php5430644editdlrm
fetch_duplicate_assignments.php49020644editdlrm
fetch_events.php10140644editdlrm
fetch_exist_dates.php11410644editdlrm
fetch_groups_exist_in_schedule.php9590644editdlrm
fetch_groups_for_program.php8710644editdlrm
fetch_groups_for_program_legacy.php7750644editdlrm
fetch_group_timeslot.php8780644editdlrm
fetch_lockup_courses-1-1.php140860644editdlrm
fetch_lockup_courses.php145870644editdlrm
fetch_lockup_courses_1.php136380644editdlrm
fetch_lockup_courses_2.php138500644editdlrm
fetch_lockup_courses_bak.php88990644editdlrm
fetch_reserve_courses.php75670644editdlrm
fetch_retake_courses.php9570644editdlrm
fetch_scheduled_courses.php22540644editdlrm
fetch_schedule_report_by_group.php55540644editdlrm
fetch_teachers_by_group.php48880644editdlrm
fetch_teachers_by_group_legacy.php24210644editdlrm
fetch_teachers_by_program.php18710644editdlrm
fetch_teachers_for_group.php32650644editdlrm
fetch_teacher_assignment.php254050644editdlrm
fetch_teacher_by_course.php39820644editdlrm
fetch_teacher_conflicts-1-1.php57930644editdlrm
fetch_teacher_conflicts.php57660644editdlrm
fetch_teacher_conflicts_only-1-1.php82750644editdlrm
fetch_teacher_conflicts_only-1.php87370644editdlrm
fetch_teacher_conflicts_only.php77250644editdlrm
fetch_teacher_conflicts_only_force_conflicts.php72870644editdlrm
fetch_teacher_conflicts_only_legacy.php67020644editdlrm
fetch_teacher_conflicts_showonly-1-1.php106560644editdlrm
fetch_teacher_conflicts_showonly.php105310644editdlrm
fetch_teacher_load.php16330644editdlrm
fetch_teacher_load_legacy.php11440644editdlrm
fetch_teacher_prefrence_courses.php18480644editdlrm
fetch_teacher_prefrence_courses_accounting.php16470644editdlrm
fetch_workload_data.php45200644editdlrm
fetch_workload_hours-1.php22910644editdlrm
fetch_workload_hours-2.php21470644editdlrm
fetch_workload_hours.php23410644editdlrm
find_replacement-1-1.php186030644editdlrm
find_replacement.php183130644editdlrm
Generate Group Schedule.php173300644editdlrm
Generate Group Schedule 1.php184900644editdlrm
generateschedule.php159460644editdlrm
Generate_Schedule.php347430644editdlrm
generate_schedule_handler.php73750644editdlrm
Generate_Schedule_Script.php307290644editdlrm
getlang.php3510644editdlrm
get_groups_by_program.php6290644editdlrm
get_schedule_ai.php377120644editdlrm
get_schedule_report.php24710644editdlrm
group_schedule_form.php173370644editdlrm
Lockup_to_Assign.php120520644editdlrm
Lockup_to_Assign_bak.php113010644editdlrm
main-header-inc.php77940644editdlrm
main-header.php196560644editdlrm
Preference_Accounting.php98100644editdlrm
preference_accounting.php96030644editdlrm
preference_secretarial.php98100644editdlrm
profile.php209760644editdlrm
remove_saturday_course.php193130644editdlrm
report_absence.php210560644editdlrm
report_absence_1.php63850644editdlrm
reserve_course.php192850644editdlrm
rewrite_ajax_replace_saturday.php131890644editdlrm
rewrite_Create_Schedule_Algorithm.php906720644editdlrm
rewrite_Create_Schedule_Algorithm_2.php912680644editdlrm
save_schedule.php31020644editdlrm
save_schedule_group_course.php263010644editdlrm
save_teacher_preference.php22160644editdlrm
schedulereportbygroup.php180130644editdlrm
schedulereportbyteacher.php194240644editdlrm
scheduleteacherbygroupreport.php374180644editdlrm
scheduleteacherreport.php287880644editdlrm
schedule_report.php179440644editdlrm
Schedule_Report_by_Group.php37410644editdlrm
Schedule_Report_by_Teacher.php946340644editdlrm
swapcourses.php198130644editdlrm
teacher_notes.php189360644editdlrm
teacher_preference.php184960644editdlrm
teacher_preference_by_course.php195870644editdlrm
teacher_report_visibility.php157610644editdlrm
teacher_unavailability.php205770644editdlrm
test.php142780644editdlrm
test1.php224250644editdlrm
unassigncoursereport.php150750644editdlrm
unassign_course.php193240644editdlrm
Unassign_Course_Report.php20580644editdlrm
unscheduledgroupreport.php163960644editdlrm
Unscheduled_Group_Report.php24110644editdlrm
updatecourse.php224540644editdlrm
updatecoursesessionhours.php189050644editdlrm
updatecoursetime.php226700644editdlrm
updategroup.php186190644editdlrm
updateholiday.php233320644editdlrm
updateteacherlevel.php254330644editdlrm
updateteacherunavailability.php118330644editdlrm
UpdateUser.php192610644editdlrm
Update_Course_Hours.php20270644editdlrm
update_reserve_course.php20040644editdlrm
update_teacher_absence.php121350644editdlrm
update_teacher_level25080644editdlrm
update_teacher_level.php24990644editdlrm
useraccesslist.php131450644editdlrm
users.php91710644editdlrm
view_swap_log.php9290644editdlrm
workloadhoursreport.php81820644editdlrm
Edit: /home/techb158/workloadmatch.com/Manager/ajax_replace_saturday_backup_latest.php (31474B)
prepare("SELECT Time_Slot, Time_From, Time_To, class_days, Weekend_Class FROM manager_group_name WHERE Group_ID = ?"); $stmt->bind_param("i", $groupID); $stmt->execute(); return $stmt->get_result()->fetch_assoc(); } // Fetch slot times function fetch_slot_times($mysqli, $slotLabels, $managerStart, $managerEnd) { $timeFrom = []; $timeTo = []; foreach ($slotLabels as $label) { $stmt = $mysqli->prepare("SELECT Time_From, Time_To FROM time_slot_programs WHERE Time_Slot = ?"); $stmt->bind_param("s", $label); $stmt->execute(); $res = $stmt->get_result(); if ($row = $res->fetch_assoc()) { $timeFrom[] = $row['Time_From']; $timeTo[] = $row['Time_To']; } else { $timeFrom[] = $managerStart->format('H:i:s'); $timeTo[] = $managerEnd->format('H:i:s'); } } return [$timeFrom, $timeTo]; } // Fetch holidays 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; } // Fetch retake dates 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; } // Generate all valid class dates, excluding holidays, retakes, and July function generate_valid_dates($Start_Date, $End_Date, $classDays, $holidayDates, $retakeDates) { $validDates = []; $cur = new DateTime($Start_Date); $end = $End_Date ? new DateTime($End_Date) : null; while (!$end || $cur <= $end) { $day = $cur->format('l'); $dateStr = $cur->format('Y-m-d'); $month = (int)$cur->format('m'); // ✅ Check holidays and retakes FIRST if (in_array($dateStr, $holidayDates)) { $validDates[] = ['Date' => $dateStr, 'Type' => 'Holiday']; } elseif (in_array($dateStr, $retakeDates)) { $validDates[] = ['Date' => $dateStr, 'Type' => 'Retake']; } // ✅ Then check for valid class day (not July, must be in classDays) elseif ($month !== 7 && in_array($day, $classDays)) { $validDates[] = ['Date' => $dateStr, 'Type' => 'Class']; } $cur->modify('+1 day'); if ($end === null && count($validDates) > 500) break; } return $validDates; } // Calculate the average duration of one session function calculate_session_length($timeFrom, $timeTo) { $sessionLength = 0; foreach ($timeFrom as $i => $from) { $fromTime = new DateTime($from); $toTime = new DateTime($timeTo[$i]); $sessionLength += ($toTime->getTimestamp() - $fromTime->getTimestamp()) / 3600; } $slotCount = count($timeFrom); return $slotCount > 0 ? $sessionLength / $slotCount : 3; } // Calculate the number of sessions required for each course function calculate_course_sessions($mysqli, $Program_ID, $sessionLength) { $courseSessions = []; $res = $mysqli->query("SELECT Course_ID, Course_Time FROM Courses WHERE Program_ID = $Program_ID"); while ($row = $res->fetch_assoc()) { $courseID = $row['Course_ID']; $courseHours = $row['Course_Time']; $courseSessions[$courseID] = ceil($courseHours / $sessionLength); } return $courseSessions; } // --- MAIN LOGIC --- // /* if ($_SERVER['REQUEST_METHOD'] === 'POST') { $saturdayDate = $_POST['date'] ?? ''; $programID = intval($_POST['Program_ID'] ?? 0); $groupID = intval($_POST['Group_ID'] ?? 0); $startDate = $_POST['Start_Date'] ?? ''; $selectedCourses = json_decode(urldecode($_POST['Selected_Courses'] ?? '[]'), true); $priorityCourses = json_decode(urldecode($_POST['Priority_Course'] ?? '{}'), true); if (!$programID || !$groupID || !$saturdayDate || !$startDate || empty($selectedCourses)) { echo json_encode(['status' => 'error', 'message' => 'Missing required fields.']); exit; } $group = get_group_info($mysqli, $groupID); if (!$group) { echo json_encode(['status' => 'error', 'message' => 'Group not found.']); exit; } $classDays = array_map('trim', explode(',', $group['class_days'])); $slotLabels = array_map('trim', explode(',', $group['Time_Slot'])); $managerStart = new DateTime($group['Time_From']); $managerEnd = new DateTime($group['Time_To']); list($timeFrom, $timeTo) = fetch_slot_times($mysqli, $slotLabels, $managerStart, $managerEnd); $holidayDates = get_holidays($mysqli, $startDate); $retakeDates = get_retake_dates($mysqli, $programID, $groupID); // 🧹 Find the next valid weekday (limit to 20 days) $cur = new DateTime($saturdayDate); $cur->modify('+1 day'); $replacementDate = null; $attempts = 0; while ($attempts < 20) { $day = $cur->format('l'); $dateStr = $cur->format('Y-m-d'); if ($day !== 'Saturday' && in_array($day, $classDays) && !in_array($dateStr, $holidayDates) && !in_array($dateStr, $retakeDates)) { $replacementDate = $dateStr; break; } $cur->modify('+1 day'); $attempts++; } if (!$replacementDate) { echo json_encode(['status' => 'error', 'message' => 'No available weekday found within 20 days.']); exit; } // 🎯 Sort courses by priority usort($selectedCourses, function($a, $b) use ($priorityCourses) { return ($priorityCourses[$a] ?? PHP_INT_MAX) <=> ($priorityCourses[$b] ?? PHP_INT_MAX); }); $courseID = $selectedCourses[0]; // Fetch course name $res = $mysqli->query("SELECT Course_Name FROM Courses WHERE Course_ID = $courseID"); $courseData = $res->fetch_assoc(); $courseName = $courseData ? $courseData['Course_Name'] : "Course ID $courseID"; // 🎨 Prepare new HTML row $dayName = (new DateTime($replacementDate))->format('l'); $slotName = format_time_slot_label($slotLabels[0] ?? 'Morning', $dayName); $timeRange = format_time_slot_range($slotLabels[0] ?? 'Morning', $timeFrom[0] ?? '08:30', $timeTo[0] ?? '12:30', $dayName); $newRowHtml = " ($dayName) $replacementDate $slotName $timeRange $courseName - "; echo json_encode([ 'status' => 'success', 'original' => $saturdayDate, 'replacement' => $replacementDate, 'newRowHtml' => $newRowHtml ]); exit; } */ // ------------------- Functions ------------------- function getNextCourseForSlot($remainingSessions, $courseList, $usedToday) { $available = array_filter($courseList, function($courseID) use ($remainingSessions) { return ($remainingSessions[$courseID] ?? 0) > 0; }); $unusedToday = array_filter($available, fn($courseID) => !in_array($courseID, $usedToday)); if (!empty($unusedToday)) { return reset($unusedToday); } // Allow reuse only if it's the last course left if (count($available) === 1) { return reset($available); } return null; } // Handle AJAX request // Handle AJAX request // MAIN START if ($_SERVER['REQUEST_METHOD'] === 'POST') { $Program_ID = intval($_POST['Program_ID'] ?? 0); $Group_ID = intval($_POST['Group_ID'] ?? 0); $Start_Date = $_POST['Start_Date'] ?? ''; $Removed_Date = $_POST['date'] ?? ''; $selectedCourses = json_decode($_POST['Selected_Courses'] ?? '[]', true) ?? []; $priorityCourses = json_decode($_POST['Priority_Course'] ?? '{}', true) ?? []; $Course_Sessions = json_decode($_POST['Course_Sessions'] ?? '{}', true) ?? []; /* file_put_contents(__DIR__."/debug_sessions.log", print_r([ 'RAW' => $_POST['Course_Sessions'], 'PARSED' => $Course_Sessions ], true)); */ $group = get_group_info($mysqli, $Group_ID); if (!$group) { echo json_encode(['status' => 'error', 'message' => 'Group not found']); exit; } $classDays = array_map('trim', explode(',', $group['class_days'])); //$slotLabels = explode(',', $group['Time_Slot']); $managerStart = new DateTime(trim($group['Time_From'])); $managerEnd = new DateTime(trim($group['Time_To'])); //list($timeFrom, $timeTo) = fetch_slot_times($mysqli, $slotLabels, $managerStart, $managerEnd); $stmt = $mysqli->prepare("SELECT Group_Slot_ID, Time_Slot, Time_From, Time_To FROM group_slot_mapping WHERE Group_ID = ?"); $stmt->bind_param("i", $Group_ID); $stmt->execute(); $res = $stmt->get_result(); //$group_slot_id = []; // <- Add this before the while loop $slotLabels = $timeFrom = $timeTo = []; //while ($row = $res->fetch_assoc()) { // $slotLabels[] = $row['Time_Slot']; // $timeFrom[] = $row['Time_From']; // $timeTo[] = $row['Time_To']; // //$group_slot_id[] = $row['Group_Slot_ID']; //} $slotData = []; // holds each slot's full data while ($row = $res->fetch_assoc()) { $slotLabels[] = $row['Time_Slot']; $timeFrom[] = $row['Time_From']; $timeTo[] = $row['Time_To']; $slotData[] = [ 'Group_Slot_ID' => $row['Group_Slot_ID'], 'Time_Slot' => $row['Time_Slot'], 'Time_From' => $row['Time_From'], 'Time_To' => $row['Time_To'] ]; } $sessionLength = calculate_session_length($timeFrom, $timeTo); $holidayDates = get_holidays($mysqli, $Start_Date); $retakeDates = get_retake_dates($mysqli, $Program_ID, $Group_ID); // Generate valid dates $validDates = generate_valid_dates($Start_Date, null, $classDays, $holidayDates, $retakeDates); // Replace the Saturday with the next available weekday $replacementDate = null; $cur = new DateTime($Removed_Date); $cur->modify('+1 day'); $attempts = 0; while ($attempts < 20) { $day = $cur->format('l'); $dateStr = $cur->format('Y-m-d'); if ($day !== 'Saturday' && in_array($day, $classDays) && !in_array($dateStr, $holidayDates) && !in_array($dateStr, $retakeDates)) { $replacementDate = $dateStr; break; } $cur->modify('+1 day'); $attempts++; } if (!$replacementDate) { echo json_encode(['status' => 'error', 'message' => 'No available replacement weekday found within 20 days.']); exit; } // Remove the replaced Saturday, insert the replacement date $reindexed = []; foreach ($validDates as $entry) { if ($entry['Date'] === $Removed_Date) continue; $reindexed[] = $entry; } $reindexed[] = ['Date' => $replacementDate, 'Type' => 'Class']; // Sort by date to keep chronological order usort($reindexed, function ($a, $b) { return strcmp($a['Date'], $b['Date']); }); $validDates = $reindexed; unset($reindexed); ///////////////////////////////////////////////////////// // Reorder courses by priority if (!empty($priorityCourses)) { usort($selectedCourses, function($a, $b) use ($priorityCourses) { return ($priorityCourses[$a] ?? PHP_INT_MAX) <=> ($priorityCourses[$b] ?? PHP_INT_MAX); }); } $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']; } $courseSessions = []; foreach ($selectedCourses as $courseID) { // Only use the parsed $Course_Sessions array! $custom = $Course_Sessions[$courseID] ?? null; $courseSessions[$courseID] = (is_numeric($custom) && $custom > 0) ? intval($custom) : 0; } /* // Use custom sessions from the parsed JSON array $courseSessions = []; foreach ($selectedCourses as $courseID) { $custom = $Course_Sessions[$courseID] ?? null; if (is_numeric($custom) && intval($custom) > 0) { $courseSessions[$courseID] = intval($custom); } else { // If not set or invalid, you can fall back to suggested (optional) $courseSessions[$courseID] = 0; // or use $suggestedSessions[$courseID] ?? 0; } } */ $remainingSessions = []; foreach ($selectedCourses as $courseID) { $remainingSessions[$courseID] = $courseSessions[$courseID] ?? 0; } /* file_put_contents(__DIR__.'/debug_final_sessions.log', print_r([ 'selectedCourses' => $selectedCourses, 'Course_Sessions' => $Course_Sessions, 'courseSessions' => $courseSessions, 'remainingSessions' => $remainingSessions, ], true)); */ //// Build schedule $html = ""; $html .= ""; ////$html = "
"; $html .= ""; //$html = '
DateSlotTimeCourseAction
'; //$html .= ''; $currentCourse = reset($selectedCourses); $slotCount = count($slotLabels); $slotIndex = 0; $ajaxScheduleData=[]; foreach ($validDates as $entry) { $date = $entry['Date']; $dayName = (new DateTime($date))->format('l'); $type = $entry['Type']; if ($type === 'Holiday') { $html .= ""; continue; } if ($type === 'Retake') { $html .= ""; continue; } if (!$currentCourse) break; $slotLabel = format_time_slot_label($slotLabels[$slotIndex % $slotCount], $dayName); $timeRange = format_time_slot_range($slotLabels[$slotIndex % $slotCount], $timeFrom[$slotIndex % $slotCount], $timeTo[$slotIndex % $slotCount], $dayName); $examTag = ($remainingSessions[$currentCourse] == 1) ? ' (Exam Day)' : ''; $normalizedDate = (new DateTime($date))->format('Y-m-d'); if ($dayName === 'Saturday') { $action = ""; } else { $action = "-"; } $slotDataCount = count($slotData); if ($slotDataCount === 0) { die("❌ Error: No slot data found for Group_ID = $Group_ID"); } //$html .= ""; $currentSlot = $slotData[$slotIndex % count($slotData)]; $groupSlotID = $currentSlot['Group_Slot_ID']; $slotLabel = $currentSlot['Time_Slot']; $from = $currentSlot['Time_From']; $to = $currentSlot['Time_To']; $html .= ""; $ajaxScheduleData[] = [ 'program_id' => $Program_ID, 'course_id' => $currentCourse, 'course_name' => $courseNames[$currentCourse] ?? '', 'group_id' => $Group_ID, 'group_slot_id' => $groupSlotID, 'reserve_course' => 0, 'time_slot' => $slotLabel, 'start_date' => $date, 'end_date' => $date, 'start_time' => $from, 'end_time' => $to ]; $html .= ""; $html .= ""; $remainingSessions[$currentCourse]--; if ($remainingSessions[$currentCourse] <= 0) { $currentCourse = next($selectedCourses); } $slotIndex++; } //$html .= '
DateSlotTimeCourseAction
($dayName) $date - - HOLIDAY -
($dayName) $date - - RETAKE -
($dayName) $date$slotLabel$timeRange{$courseNames[$currentCourse]}$examTag$action
'; $html .= ""; //$html = "
"; //$html .= ""; $html .= ""; //$html .= "
DateSlotTimeCourseAction
DateSlotTimeCourseAction
"; //$html .= "
// //
"; /* $html .= "
"; echo "\n"; */ $html .= ""; echo json_encode([ 'status' => 'success', 'html' => $html, 'ajaxScheduleData' => $ajaxScheduleData // <-- pass it always! ]); exit; } function generateSchedule($mysqli, $Program_ID, $Group_ID, $Start_Date, $Removed_Date, $selectedCourses, $priorityCourses, $Course_Sessions){ //function generateSchedule($mysqli, $Program_ID, $Group_ID, $Start_Date, $Removed_Date, $selectedCourses, $priorityCourses) { // Get group info $group = get_group_info($mysqli, $Group_ID); if (!$group) { return '
Error: Group not found.
'; } $classDays = explode(',', $group['class_days']); $slotLabels = explode(',', $group['Time_Slot']); $managerStart = new DateTime(trim($group['Time_From'])); $managerEnd = new DateTime(trim($group['Time_To'])); list($timeFrom, $timeTo) = fetch_slot_times($mysqli, $slotLabels, $managerStart, $managerEnd); $sessionLength = calculate_session_length($timeFrom, $timeTo); //$courseSessions = calculate_course_sessions($mysqli, $Program_ID, $sessionLength); $courseSessions = []; foreach ($selectedCourses as $courseID) { $val = $Course_Sessions[$courseID] ?? 0; $courseSessions[$courseID] = (is_numeric($val) && $val > 0) ? intval($val) : 0; } $holidayDates = get_holidays($mysqli, $Start_Date); $retakeDates = get_retake_dates($mysqli, $Program_ID, $Group_ID); $validDates = generate_valid_dates($Start_Date, null, $classDays, $holidayDates, $retakeDates); $validDates = array_filter($validDates, function($date) use ($Removed_Date) { return $date !== $Removed_Date; }); $validDates = array_values($validDates); if (!empty($priorityCourses)) { usort($selectedCourses, function ($a, $b) use ($priorityCourses) { return ($priorityCourses[$a] ?? PHP_INT_MAX) <=> ($priorityCourses[$b] ?? PHP_INT_MAX); }); } $remainingSessions = []; foreach ($selectedCourses as $courseID) { $remainingSessions[$courseID] = $courseSessions[$courseID] ?? 0; } $courseNames = []; $res = $mysqli->query("SELECT Course_ID, Course_Name FROM Courses"); while ($row = $res->fetch_assoc()) { $courseNames[$row['Course_ID']] = $row['Course_Name']; } $currentCourses = [null, null]; $htmlRows = []; $examTag = ' (Exam Day)'; //$courseQueue = $selectedCourses; // copy for rotation $ajaxScheduleData = []; foreach ($validDates as $entry) { $date = $entry['Date']; $dayName = (new DateTime($date))->format('l'); $type = $entry['Type']; if ($type === 'Holiday' || $type === 'Retake') { $htmlRows[] = [ 'Date' => $date, 'Day' => $dayName, 'Slot' => '', 'Time' => '', 'Course_Name' => strtoupper($type), 'IsExam' => false, 'Action' => '-' ]; continue; } $usedToday = []; foreach (array_keys($slotLabels) as $slotIndex) { $from = $timeFrom[$slotIndex] ?? null; $to = $timeTo[$slotIndex] ?? null; $slotLabel = $slotLabels[$slotIndex] ?? 'Unknown'; if (!$from || !$to) continue; $courseID = getNextCourseForSlot($remainingSessions, $selectedCourses, $usedToday); if (!$courseID) continue; $isExam = ($remainingSessions[$courseID] === 1); $usedToday[] = $courseID; $remainingSessions[$courseID]--; $action = ($dayName === 'Saturday' && !in_array($date, $deletedSaturdays)) ? "" : ($dayName === 'Saturday' ? '🗑Removed' : '-'); $htmlRows[] = [ 'Date' => $date, 'Day' => $dayName, 'Slot' => $slotLabel, 'Time' => format_time_slot_range($slotLabel, $from, $to, $dayName), 'Course_Name' => $courseNames[$courseID] ?? "Course $courseID", 'IsExam' => $isExam, 'Action' => $action ]; $ajaxScheduleData[] = [ 'program_id' => $Program_ID, 'course_id' => $courseID, 'course_name' => $courseNames[$courseID] ?? '', 'group_id' => $Group_ID, 'group_slot_id' => $slotIndex, 'reserve_course' => 0, 'time_slot' => $slotLabel, 'start_date' => $date, // or $normalizedDate if you format earlier 'end_date' => $date, 'start_time' => $from, 'end_time' => $to ]; if (array_sum($remainingSessions) <= 0) break 2; // Stop if everything is scheduled } } $html = "
"; $html .= "
"; $html .= ""; //$html = "
"; $html .= ""; foreach ($htmlRows as $row) { // Ensure all required keys exist $row['Slot'] = $row['Slot'] ?? '-'; $row['Time'] = $row['Time'] ?? '-'; $row['Course_Name'] = $row['Course_Name'] ?? '-'; $row['Action'] = $row['Action'] ?? '-'; $row['IsExam'] = $row['IsExam'] ?? false; $examLabel = $row['IsExam'] ? ' (Exam Day)' : ''; $formattedDate = "({$row['Day']}) {$row['Date']}"; $html .= ""; $html .= ""; $html .= ""; $html .= ""; $html .= ""; $html .= ""; $html .= ""; } //foreach ($htmlRows as $row) { // $examLabel = $row['IsExam'] ? ' (Exam Day)' : ''; // $formattedDate = "({$row['Day']}) {$row['Date']}"; // // $html .= ""; // $html .= ""; // $html .= ""; // $html .= ""; // $html .= ""; // $html .= ""; // $html .= ""; //} //$html .= "
DateSlotTimeCourseAction
{$formattedDate}{$row['Slot']}{$row['Time']}{$row['Course_Name']}{$examLabel}{$row['Action']}
{$formattedDate}{$row['Slot']}{$row['Time']}{$row['Course_Name']}{$examLabel}{$row['Action']}
"; // Footer $html .= "DateSlotTimeCourseAction"; $html .= "
"; //$html .= "
// //
"; //$html .= ""; return $html; } // ❌ Fallback if not POST http_response_code(405); echo json_encode(['status' => 'error', 'message' => 'Invalid request method.']); exit; /* function generateSchedule($mysqli, $Program_ID, $Group_ID, $Start_Date, $Removed_Date, $selectedCourses, $priorityCourses) { // Get group info $group = get_group_info($mysqli, $Group_ID); if (!$group) { return '
Error: Group not found.
'; } $classDays = explode(',', $group['class_days']); $slotLabels = explode(',', $group['Time_Slot']); $managerStart = new DateTime(trim($group['Time_From'])); $managerEnd = new DateTime(trim($group['Time_To'])); list($timeFrom, $timeTo) = fetch_slot_times($mysqli, $slotLabels, $managerStart, $managerEnd); $sessionLength = calculate_session_length($timeFrom, $timeTo); // Courses & sessions $courseSessions = calculate_course_sessions($mysqli, $Program_ID, $sessionLength); // Load holidays & retakes $holidayDates = get_holidays($mysqli, $Start_Date); $retakeDates = get_retake_dates($mysqli, $Program_ID, $Group_ID); // Generate valid dates $validDates = generate_valid_dates($Start_Date, null, $classDays, $holidayDates, $retakeDates); // Remove the Saturday we want to delete $validDates = array_filter($validDates, function($date) use ($Removed_Date) { return $date !== $Removed_Date; }); $validDates = array_values($validDates); // Re-index after filtering // Sort courses by priority if (!empty($priorityCourses)) { usort($selectedCourses, function ($a, $b) use ($priorityCourses) { $priorityA = $priorityCourses[$a] ?? PHP_INT_MAX; $priorityB = $priorityCourses[$b] ?? PHP_INT_MAX; return $priorityA <=> $priorityB; }); } // Initialize scheduling $schedule = []; $remainingSessions = []; foreach ($selectedCourses as $courseID) { $remainingSessions[$courseID] = $courseSessions[$courseID] ?? 0; } // Course Names $courseNames = []; $res = $mysqli->query("SELECT Course_ID, Course_Name FROM Courses"); while ($row = $res->fetch_assoc()) { $courseNames[$row['Course_ID']] = $row['Course_Name']; } // Main loop: assign courses $courseQueue = $selectedCourses; $currentCourses = [0 => null, 1 => null]; // Morning and Evening foreach ([0, 1] as $slotIndex) { foreach ($courseQueue as $courseID) { if ($remainingSessions[$courseID] > 0) { $currentCourses[$slotIndex] = $courseID; break; } } } $examTag = ' (Exam Day)'; // Build schedule $examDates = []; foreach ($validDates as $date) { $weekday = (new DateTime($date))->format('l'); foreach ([0, 1] as $slotIndex) { $from = $timeFrom[$slotIndex] ?? null; $to = $timeTo[$slotIndex] ?? null; if (!$from || !$to || !$currentCourses[$slotIndex]) { continue; } $courseID = $currentCourses[$slotIndex]; $isExam = false; if (isset($remainingSessions[$courseID]) && $remainingSessions[$courseID] == 1) { $isExam = true; // last session becomes exam } $schedule[] = [ 'Date' => $date, 'Day' => $weekday, 'Slot' => $slotLabels[$slotIndex] ?? 'Unknown Slot', 'Time' => format_time_slot_range($slotLabels[$slotIndex], $from, $to, $weekday), 'Course_Name' => $courseNames[$courseID] ?? 'Unknown', 'IsExam' => $isExam ]; // Reduce sessions $remainingSessions[$courseID]--; if ($remainingSessions[$courseID] <= 0) { $currentCourses[$slotIndex] = null; while (!empty($courseQueue)) { $nextCourseID = array_shift($courseQueue); if ($remainingSessions[$nextCourseID] > 0) { $currentCourses[$slotIndex] = $nextCourseID; break; } } } } if (!$currentCourses[0] && !$currentCourses[1]) { break; } } // Render HTML $html = "
"; $html .= ""; $html .= ""; foreach ($schedule as $row) { $formattedDate = "(" . $row['Day'] . ") " . $row['Date']; $examLabel = $row['IsExam'] ? $examTag : ''; $action = (stripos($row['Day'], 'Saturday') !== false) ? "🧹 Replace" : "-"; $html .= ""; $html .= ""; $html .= ""; $html .= ""; $html .= ""; $html .= ""; $html .= ""; } $html .= "
DateSlotTimeCourseAction
{$formattedDate}{$row['Slot']}{$row['Time']}{$row['Course_Name']}{$examLabel}{$action}
"; return $html; } */ ?>