prepare("SELECT Time_Slot, Time_From, Time_To FROM group_slot_mapping WHERE Group_ID = ? ORDER BY Group_Slot_ID ASC");
$slotMapStmt->bind_param("i", $Group_ID);
$slotMapStmt->execute();
$slotMapRes = $slotMapStmt->get_result();
while ($r = $slotMapRes->fetch_assoc()) {
$slotLabels[] = trim($r['Time_Slot']);
$start = new DateTime($r['Time_From']);
$end = new DateTime($r['Time_To']);
$duration = $end->diff($start);
$hours = $duration->h + ($duration->i / 60);
$sessionLengths[] = $hours;
}
$slotMapStmt->close();
// Fallback to older manager_group_name.Time_Slot + time_slot_programs if mapping is missing
if (empty($slotLabels)) {
// Get the group’s available time range
$groupStmt = $mysqli->prepare("SELECT Time_Slot FROM manager_group_name WHERE Group_ID = ?");
$groupStmt->bind_param("i", $Group_ID);
$groupStmt->execute();
$groupData = $groupStmt->get_result()->fetch_assoc();
$groupStmt->close();
$slotLabels = explode(',', $groupData['Time_Slot'] ?? '');
foreach ($slotLabels as $slot) {
$slot = trim($slot);
if ($slot === '') continue;
$stmt = $mysqli->prepare("SELECT Time_From, Time_To FROM time_slot_programs WHERE Time_Slot = ? LIMIT 1");
$stmt->bind_param("s", $slot);
$stmt->execute();
$res = $stmt->get_result();
if ($row = $res->fetch_assoc()) {
$start = new DateTime($row['Time_From']);
$end = new DateTime($row['Time_To']);
$duration = $end->diff($start);
$hours = $duration->h + ($duration->i / 60);
$sessionLengths[] = $hours;
}
$stmt->close();
}
}
// Compute average session length if multiple slots exist
$averageSessionLength = !empty($sessionLengths) ? array_sum($sessionLengths) / count($sessionLengths) : 3;
$courses = $mysqli->query("SELECT Course_ID, Course_Name, Course_Time FROM Courses WHERE Program_ID = $Program_ID ORDER BY Course_ID");
while ($course = $courses->fetch_assoc()) {
$courseHours = $course['Course_Time'];
$suggested_sessions = ceil($courseHours / $averageSessionLength);
echo "
";
}
//error_log("Slot {$slot['Time_From']} - {$slot['Time_To']} => $hours hrs matched");
?>