/home/techb158/workloadmatch.com/workloadmatch.com/BackUp/Manager
Edit: /home/techb158/workloadmatch.com/workloadmatch.com/BackUp/Manager/scheduleteacherbygroupreport.php (36556B)
Schedule Teacher Report
Generate Schedule
prepare("SELECT * FROM manager_group_name WHERE Group_ID = ? AND Program_ID = ?");
$stmtGroup->bind_param("ii", $groupID, $programID);
$stmtGroup->execute();
$groupData = $stmtGroup->get_result()->fetch_assoc();
$stmtGroup->close();
if (!$groupData) die("Group not found.");
$groupName = htmlspecialchars($groupData['Group_Name']);
// 1. Load all Holidays
$holidays = [];
$res = $mysqli->query("
SELECT Event_Title, Event_Start, Event_End, Event_Color
FROM Events
WHERE Calendar_Year IN (
SELECT DISTINCT YEAR(Start_Date)
FROM Schedule_Course_for_Group
WHERE Program_ID = $programID
AND Group_ID = $groupID
)
");
while ($row = $res->fetch_assoc()) {
$start = new DateTime($row['Event_Start']);
$end = new DateTime($row['Event_End']);
while ($start <= $end) {
$d = $start->format('Y-m-d');
$holidays[$d] = [
'title' => $row['Event_Title'],
'color' => $row['Event_Color'] ?: '#FFD700'
];
$start->modify('+1 day');
}
}
// 2. Load Retake Records
$retakes = [];
$stmt = $mysqli->prepare("
SELECT Type, Retake_Date
FROM Retake_Records
WHERE Program_ID = ? AND Group_ID = ?
");
$stmt->bind_param('ii', $programID, $groupID);
$stmt->execute();
$stmt->bind_result($rType, $rDate);
while ($stmt->fetch()) {
$retakes[$rDate] = $rType;
}
$stmt->close();
// 3. Get Exam Dates per Course
$examDates = [];
$stmt = $mysqli->prepare("
SELECT Course_ID, MAX(End_Date)
FROM Schedule_Course_for_Group
WHERE Program_ID = ? AND Group_ID = ?
GROUP BY Course_ID
");
$stmt->bind_param('ii', $programID, $groupID);
$stmt->execute();
$stmt->bind_result($eCourseID, $eDate);
while ($stmt->fetch()) {
$examDates[$eCourseID] = $eDate;
}
$stmt->close();
// 4. Load Scheduled Sessions WITH Teacher Info
$scheduleRows = [];
$stmt = $mysqli->prepare("
SELECT
scf.Schedule_ID,
scf.Course_ID,
scf.Course_Name,
scf.Reserve_Course,
scf.Start_Date,
scf.End_Date,
gsm.Time_Slot,
gsm.Time_From,
gsm.Time_To,
tp.First_Name,
tp.Last_Name
FROM Schedule_Course_for_Group AS scf
JOIN Group_Slot_Mapping AS gsm
ON scf.Group_Slot_ID = gsm.Group_Slot_ID
LEFT JOIN teacher_course_assignments AS tca
ON scf.Schedule_ID = tca.Schedule_ID
LEFT JOIN teacher_profile AS tp
ON tca.Teacher_ID = tp.Teacher_ID
WHERE scf.Program_ID = ?
AND scf.Group_ID = ?
ORDER BY scf.Start_Date, scf.Start_Time
");
$stmt->bind_param("ii", $programID, $groupID);
$stmt->execute();
$stmt->bind_result(
$scheduleID,
$courseID,
$courseName,
$reserveCourse,
$startDate,
$endDate,
$slotLabel,
$slotFrom,
$slotTo,
$firstName,
$lastName
);
while ($stmt->fetch()) {
//echo $firstName .'"-"'. $lastName;
$scheduleRows[] = [
'schedule_id' => $scheduleID,
'course_id' => $courseID,
'course_name' => $courseName,
'reserve' => $reserveCourse,
'start_date' => $startDate,
'end_date' => $endDate,
'slot' => $slotLabel,
'from' => $slotFrom,
'to' => $slotTo,
'teacher' => trim("$firstName $lastName")
];
}
$stmt->close();
// 5. Group sessions by date
$byDate = [];
foreach ($scheduleRows as $r) {
$period = new DatePeriod(
new DateTime($r['start_date']),
new DateInterval('P1D'),
(new DateTime($r['end_date']))->modify('+1 day')
);
foreach ($period as $dt) {
$current = $dt->format('Y-m-d');
$byDate[$current][] = $r;
}
}
// 6. Combine all possible dates
$allDates = array_unique(
array_merge(
array_keys($byDate),
array_keys($holidays),
array_keys($retakes)
)
);
sort($allDates, SORT_STRING);
// 7. Render table
echo "
";
echo "";
echo "Schedule for Group: $groupName ";
echo "
Date
Time Slot
Course
Teacher
Time
Type
";
foreach ($allDates as $date) {
$dayName = date('l', strtotime($date));
$isHoliday = isset($holidays[$date]);
$isRetake = isset($retakes[$date]);
// Holiday row
if ($isHoliday && !isset($byDate[$date])) {
$h = $holidays[$date];
echo "
($dayName) $date
{$h['title']}
Holiday
";
continue;
}
// Retake row
if ($isRetake && !isset($byDate[$date])) {
echo "
($dayName) $date
Retake Session
{$retakes[$date]}
";
continue;
}
// Scheduled sessions
foreach ($byDate[$date] as $r) {
$isExam = isset($examDates[$r['course_id']]) && $examDates[$r['course_id']] === $date;
$type = $isExam ? 'Exam Day' : 'Regular';
$style = $isExam ? "style='background-color:#FFCCCC; font-weight:bold;'" : "";
$timeRange = date("h:i A", strtotime($r['from'])) . " – " . date("h:i A", strtotime($r['to']));
$teacher = $r['teacher'] ?: "-";
echo "
($dayName) $date
{$r['slot']}
{$r['course_name']}
" . htmlspecialchars($teacher) . "
{$timeRange}
{$type}
";
}
}
echo "
";
}
/*************
if (isset($_POST['Program_ID'], $_POST['Group_ID'])) {
$Program_ID = intval($_POST['Program_ID']);
$Group_ID = intval($_POST['Group_ID']);
// 1. Group Info
$stmtGroup = $mysqli->prepare("SELECT * FROM manager_group_name WHERE Group_ID = ? AND Program_ID = ?");
$stmtGroup->bind_param("ii", $Group_ID, $Program_ID);
$stmtGroup->execute();
$groupData = $stmtGroup->get_result()->fetch_assoc();
$stmtGroup->close();
if (!$groupData) die("Group not found.");
$groupName = htmlspecialchars($groupData['Group_Name']);
$classDays = array_map('trim', explode(',', $groupData['class_days']));
// 2. Assignments
$stmt = $mysqli->prepare("SELECT tca.*, tp.First_Name, tp.Last_Name, c.Course_Name, cg.Time_Slot, cg.Start_Time, cg.End_Time
FROM teacher_course_assignments tca
JOIN teacher_profile tp ON tca.Teacher_ID = tp.Teacher_ID
JOIN Courses c ON tca.Course_ID = c.Course_ID
JOIN course_group_schedule_main cg ON tca.Schedule_ID = cg.Schedule_ID
WHERE tca.Program_ID = ? AND tca.Group_ID = ?");
$stmt->bind_param("ii", $Program_ID, $Group_ID);
$stmt->execute();
$assignments = $stmt->get_result()->fetch_all(MYSQLI_ASSOC);
$stmt->close();
// 3. Retakes
$stmt = $mysqli->prepare("
SELECT rr.Retake_Date, gsm.Time_Slot
FROM Retake_Records rr
JOIN Group_Slot_Mapping gsm
ON rr.Group_ID = gsm.Group_ID
WHERE rr.Program_ID = ? AND rr.Group_ID = ?
");
//$stmt = $mysqli->prepare("SELECT rr.Retake_Date, tsp.Time_Slot
// FROM Retake_Records rr
// JOIN Time_Slot_Programs tsp ON rr.Time_Slot_Programs_ID = tsp.Time_Slot_Programs_ID
// WHERE rr.Program_ID = ? AND rr.Group_ID = ?");
//$stmt->bind_param("ii", $Program_ID, $Group_ID);
//$stmt->execute();
//$retakes = $stmt->get_result()->fetch_all(MYSQLI_ASSOC);
//$stmt->close();
// 4. Holidays
$holidayMap = [];
$res = $mysqli->query("SELECT Event_Title, Event_Start, Event_End, Event_Color FROM Events");
while ($event = $res->fetch_assoc()) {
$start = new DateTime($event['Event_Start']);
$end = (new DateTime($event['Event_End']))->modify('+1 day');
foreach (new DatePeriod($start, new DateInterval('P1D'), $end) as $dt) {
$holidayMap[$dt->format('Y-m-d')] = [
'title' => $event['Event_Title'],
'color' => $event['Event_Color']
];
}
}
// 5. Date Range
$dates = array_merge(array_column($assignments, 'Start_Date'), array_column($assignments, 'End_Date'));
$start = new DateTime(min($dates));
$end = (new DateTime(max($dates)))->modify('+1 day');
$range = new DatePeriod($start, new DateInterval('P1D'), $end);
echo "
Schedule for Group: {$groupName} ";
echo "
";
echo "
";
echo "Date Course Teacher Time Slot Time Type ";
foreach ($range as $date) {
$current = $date->format('Y-m-d');
$day = $date->format('l');
// Always display holiday regardless of class day
//if (isset($holidayMap[$current])) {
// $h = $holidayMap[$current];
// echo "$current {$h['title']} - - - Holiday ";
//}
if (isset($holidayMap[$current])) {
$h = $holidayMap[$current];
echo "$current {$h['title']} - - - Holiday ";
continue; // ✅ prevent further rows on this holiday
}
// Only skip non-class days if not a holiday
if (!in_array($day, $classDays) && !isset($holidayMap[$current])) continue;
$usedSlots = [];
// Holidays (full day event, doesn't skip other events)
// Holidays (skip the rest of the loop if it's a holiday)
// Holidays (display holiday but still show other events on this day)
// Retakes
foreach ($retakes as $r) {
if ($r['Retake_Date'] === $current) {
$slotKey = strtolower(trim($r['Time_Slot']));
if (!in_array($slotKey, $usedSlots)) {
$usedSlots[] = $slotKey;
echo "$current Retake - {$r['Time_Slot']} - Retake ";
}
}
}
// Assignments (handle multiple slots per day)
foreach ($assignments as $a) {
if ($current >= $a['Start_Date'] && $current <= $a['End_Date']) {
$slotKey = strtolower(trim($a['Time_Slot']));
if (in_array($slotKey, $usedSlots)) continue;
$usedSlots[] = $slotKey;
$type = ($current === $a['End_Date']) ? 'Exam Day' : 'Regular';
$bg = ($type === 'Exam Day') ? "style='background-color:#ffeeba'" : "";
$slot = format_time_slot_label($a['Time_Slot'], $day);
$time = format_time_slot_range($a['Time_Slot'], $a['Start_Time'], $a['End_Time'], $day);
$teacher = htmlspecialchars("{$a['First_Name']} {$a['Last_Name']}");
echo "$current {$a['Course_Name']} {$teacher} {$slot} {$time} {$type} ";
}
}
}
echo "
";
}
************/
/*
if (isset($_POST['Program_ID'], $_POST['Group_ID'])) {
$Program_ID = intval($_POST['Program_ID']);
$Group_ID = intval($_POST['Group_ID']);
// Get Group Name
$stmt = $mysqli->prepare("SELECT Group_Name, class_days FROM manager_group_name WHERE Program_ID = ? AND Group_ID = ?");
$stmt->bind_param("ii", $Program_ID, $Group_ID);
$stmt->execute();
$groupData = $stmt->get_result()->fetch_assoc();
$stmt->close();
$groupName = $groupData['Group_Name'] ?? 'Unknown Group';
$groupClassDays = explode(',', trim($groupData['class_days']));
// Assignments
$stmt = $mysqli->prepare("SELECT tca.*, tp.First_Name, tp.Last_Name, c.Course_Name, cg.Time_Slot, cg.Start_Time, cg.End_Time
FROM teacher_course_assignments tca
JOIN teacher_profile tp ON tp.Teacher_ID = tca.Teacher_ID
JOIN Courses c ON c.Course_ID = tca.Course_ID
JOIN Course_Group_Schedule cg ON cg.Schedule_ID = tca.Schedule_ID
WHERE tca.Program_ID = ? AND tca.Group_ID = ?");
$stmt->bind_param("ii", $Program_ID, $Group_ID);
$stmt->execute();
$assignments = $stmt->get_result()->fetch_all(MYSQLI_ASSOC);
$stmt->close();
// Retakes
$stmt = $mysqli->prepare("SELECT rr.Retake_Date, tsp.Time_Slot FROM Retake_Records rr JOIN Time_Slot_Programs tsp ON rr.Time_Slot_Programs_ID = tsp.Time_Slot_Programs_ID WHERE rr.Program_ID = ? AND rr.Group_ID = ?");
$stmt->bind_param("ii", $Program_ID, $Group_ID);
$stmt->execute();
$retakes = $stmt->get_result()->fetch_all(MYSQLI_ASSOC);
$stmt->close();
// Holidays
$holidayMap = [];
$res = $mysqli->query("SELECT Event_Title, Event_Start, Event_End, Event_Color FROM Events");
while ($event = $res->fetch_assoc()) {
$start = new DateTime($event['Event_Start']);
$end = (new DateTime($event['Event_End']))->modify('+1 day');
foreach (new DatePeriod($start, new DateInterval('P1D'), $end) as $dt) {
$holidayMap[$dt->format('Y-m-d')] = [
'title' => $event['Event_Title'],
'color' => $event['Event_Color']
];
}
}
// Date range
$dates = array_merge(array_column($assignments, 'Start_Date'), array_column($assignments, 'End_Date'));
$start = new DateTime(min($dates));
$end = (new DateTime(max($dates)))->modify('+1 day');
$range = new DatePeriod($start, new DateInterval('P1D'), $end);
echo "
";
echo "
";
echo "Schedule for Group: $groupName ";
echo "Date Course Teacher Time Slot Time Type ";
foreach ($range as $date) {
$current = $date->format('Y-m-d');
$day = $date->format('l');
$shownSlots = [];
if (isset($holidayMap[$current])) {
$h = $holidayMap[$current];
echo "$current {$h['title']} - - - Holiday ";
continue;
}
foreach ($retakes as $r) {
if ($r['Retake_Date'] === $current) {
$slot = format_time_slot_label($r['Time_Slot'], $day);
echo "$current Retake - $slot - Retake ";
$shownSlots[] = strtolower(trim($r['Time_Slot']));
}
}
foreach ($assignments as $a) {
if ($current >= $a['Start_Date'] && $current <= $a['End_Date']) {
$slotKey = strtolower(trim($a['Time_Slot']));
if (in_array($slotKey, $shownSlots)) continue;
$type = ($current === $a['End_Date']) ? 'Exam Day' : 'Regular';
$bg = ($type === 'Exam Day') ? "style='background-color:#ffeeba'" : "";
$slotLabel = format_time_slot_label($a['Time_Slot'], $day);
$time = format_time_slot_range($a['Time_Slot'], $a['Start_Time'], $a['End_Time'], $day);
$teacher = $a['First_Name'] . ' ' . $a['Last_Name'];
echo "$current {$a['Course_Name']} $teacher $slotLabel $time $type ";
}
}
}
echo "
";
}
*/
/*
if (isset($_POST['Program_ID'], $_POST['Group_ID'])) {
include_once '../includes/db_connect.php';
$Program_ID = intval($_POST['Program_ID']);
$Group_ID = intval($_POST['Group_ID']);
// 1. Group Details
$stmtGroup = $mysqli->prepare("SELECT * FROM manager_group_name WHERE Group_ID = ? AND Program_ID = ?");
$stmtGroup->bind_param("ii", $Group_ID, $Program_ID);
$stmtGroup->execute();
$groupData = $stmtGroup->get_result()->fetch_assoc();
$stmtGroup->close();
if (!$groupData) die("Group not found.");
$groupName = $groupData['Group_Name'];
$classDays = array_map('trim', explode(',', $groupData['class_days']));
// 2. Assignments
$stmt = $mysqli->prepare("SELECT tca.*, tp.First_Name, tp.Last_Name, c.Course_Name, cg.Time_Slot, cg.Start_Time, cg.End_Time
FROM teacher_course_assignments tca
JOIN teacher_profile tp ON tca.Teacher_ID = tp.Teacher_ID
JOIN Courses c ON tca.Course_ID = c.Course_ID
JOIN Course_Group_Schedule cg ON tca.Schedule_ID = cg.Schedule_ID
WHERE tca.Program_ID = ? AND tca.Group_ID = ?");
$stmt->bind_param("ii", $Program_ID, $Group_ID);
$stmt->execute();
$assignments = $stmt->get_result()->fetch_all(MYSQLI_ASSOC);
$stmt->close();
// 3. Replacements
$stmt = $mysqli->prepare("SELECT tra.*, tp.First_Name, tp.Last_Name, c.Course_Name, cg.Time_Slot, cg.Start_Time, cg.End_Time
FROM teacher_replacement_assignments tra
JOIN teacher_profile tp ON tra.Replacing_Teacher_ID = tp.Teacher_ID
JOIN Course_Group_Schedule cg ON tra.Schedule_ID = cg.Schedule_ID
JOIN Courses c ON cg.Course_ID = c.Course_ID
WHERE cg.Group_ID = ? AND cg.Program_ID = ?");
$stmt->bind_param("ii", $Group_ID, $Program_ID);
$stmt->execute();
$replacements = $stmt->get_result()->fetch_all(MYSQLI_ASSOC);
$stmt->close();
// 4. Retakes
$stmt = $mysqli->prepare("SELECT rr.Retake_Date, tsp.Time_Slot FROM Retake_Records rr JOIN Time_Slot_Programs tsp ON rr.Time_Slot_Programs_ID = tsp.Time_Slot_Programs_ID WHERE rr.Program_ID = ? AND rr.Group_ID = ?");
$stmt->bind_param("ii", $Program_ID, $Group_ID);
$stmt->execute();
$retakes = $stmt->get_result()->fetch_all(MYSQLI_ASSOC);
$stmt->close();
// 5. Holidays
$holidayMap = [];
$res = $mysqli->query("SELECT Event_Title, Event_Start, Event_End, Event_Color FROM Events");
while ($event = $res->fetch_assoc()) {
$start = new DateTime($event['Event_Start']);
$end = (new DateTime($event['Event_End']))->modify('+1 day');
foreach (new DatePeriod($start, new DateInterval('P1D'), $end) as $dt) {
$holidayMap[$dt->format('Y-m-d')] = [
'title' => $event['Event_Title'],
'color' => $event['Event_Color']
];
}
}
// 6. Dates range
$dates = array_merge(
array_column($assignments, 'Start_Date'),
array_column($assignments, 'End_Date'),
array_column($replacements, 'Replacement_From'),
array_column($replacements, 'Replacement_To')
);
$start = new DateTime(min($dates));
$end = (new DateTime(max($dates)))->modify('+1 day');
$range = new DatePeriod($start, new DateInterval('P1D'), $end);
echo "
Schedule for Group: " . htmlspecialchars($groupName) . " ";
echo "
";
echo "
";
echo "Date Course Teacher Time Slot Time Type ";
foreach ($range as $date) {
$current = $date->format('Y-m-d');
$day = $date->format('l');
if (isset($holidayMap[$current])) {
echo "$current {$holidayMap[$current]['title']} - - - Holiday ";
continue;
}
$usedSlots = [];
foreach ($retakes as $r) {
if ($r['Retake_Date'] == $current) {
$slot = format_time_slot_label($r['Time_Slot'], $day);
echo "$current Retake - $slot - Retake ";
$usedSlots[] = strtolower(trim($r['Time_Slot']));
}
}
foreach ($replacements as $r) {
if ($current >= $r['Replacement_From'] && $current <= $r['Replacement_To']) {
$slotKey = strtolower(trim($r['Time_Slot']));
if (in_array($slotKey, $usedSlots)) continue;
$usedSlots[] = $slotKey;
$time = format_time_slot_range($r['Time_Slot'], $r['Start_Time'], $r['End_Time'], $day);
$slot = format_time_slot_label($r['Time_Slot'], $day);
$teacher = htmlspecialchars("{$r['First_Name']} {$r['Last_Name']}");
echo "$current {$r['Course_Name']} $teacher $slot $time Replacement ";
}
}
foreach ($assignments as $a) {
if ($current >= $a['Start_Date'] && $current <= $a['End_Date']) {
$slotKey = strtolower(trim($a['Time_Slot']));
if (in_array($slotKey, $usedSlots)) continue;
$usedSlots[] = $slotKey;
$type = ($current == $a['End_Date']) ? 'Exam Day' : 'Regular';
$bg = ($type === 'Exam Day') ? "style='background-color:#ffeeba'" : "";
$time = format_time_slot_range($a['Time_Slot'], $a['Schedule_Start_Time'], $a['Schedule_End_Time'], $day);
$slot = format_time_slot_label($a['Time_Slot'], $day);
$teacher = htmlspecialchars("{$a['First_Name']} {$a['Last_Name']}");
echo "$current {$a['Course_Name']} $teacher $slot $time $type ";
}
}
}
echo "
";
}
*/
?>