/home/techb158/workloadmatch.com/workloadmatch.com/BackUp/api/teacher
Edit: /home/techb158/workloadmatch.com/workloadmatch.com/BackUp/api/teacher/availability.php (5178B)
"error",
"message" => "Unauthorized: Please log in."
]);
exit();
}
$teacherId = $_SESSION['user_id'];
// Retrieve all course assignments for this teacher.
$query = "SELECT Start_Date, End_Date FROM Teacher_Course_Assignments WHERE Teacher_ID = ? ORDER BY Start_Date ASC";
if ($stmt = $mysqli->prepare($query)) {
$stmt->bind_param("i", $teacherId);
$stmt->execute();
$result = $stmt->get_result();
$assignments = [];
while ($row = $result->fetch_assoc()) {
// Expecting dates in 'Y-m-d' format.
$assignments[] = [
'start' => $row['Start_Date'],
'end' => $row['End_Date']
];
}
$stmt->close();
} else {
http_response_code(500);
echo json_encode([
"status" => "error",
"message" => "Database error: cannot prepare statement for assignments."
]);
exit();
}
// If no assignments exist, you may choose what to return;
// Here, we'll indicate that no assignments were found.
if (empty($assignments)) {
echo json_encode([
"status" => "success",
"message" => "No course assignments found. Teacher appears to be available throughout the period.",
"overall_period" => null,
"available_days" => []
]);
exit();
}
// Determine the overall period: from earliest Start_Date to latest End_Date.
$overallStart = new DateTime($assignments[0]['start']);
$overallEnd = new DateTime($assignments[0]['end']);
foreach ($assignments as $assignment) {
$start = new DateTime($assignment['start']);
$end = new DateTime($assignment['end']);
if ($start < $overallStart) {
$overallStart = $start;
}
if ($end > $overallEnd) {
$overallEnd = $end;
}
}
// Build available days based on the overall period
// We'll iterate day by day between the overall start and end dates.
$interval = new DateInterval('P1D'); // 1 day interval
$period = new DatePeriod($overallStart, $interval, (clone $overallEnd)->modify('+1 day'));
$availableDays = [];
// Function to check if a given date falls within any busy (assigned) interval.
function isDateBusy(DateTime $date, array $assignments) {
foreach ($assignments as $assignment) {
$start = new DateTime($assignment['start']);
$end = new DateTime($assignment['end']);
// If the date falls within an assignment period (inclusive)
if ($date >= $start && $date <= $end) {
return true;
}
}
return false;
}
foreach ($period as $date) {
// Exclude weekends: in PHP, date->format('N') returns 6 for Saturday and 7 for Sunday.
if (in_array($date->format('N'), ['6', '7'])) {
continue;
}
// If the day is not busy (i.e. no assignment covers it), it is available.
if (!isDateBusy($date, $assignments)) {
$availableDays[] = $date->format('Y-m-d');
}
}
// Return overall assignment period and the array of available weekdays.
echo json_encode([
"status" => "success",
"overall_period" => [
"start_date" => $overallStart->format('Y-m-d'),
"end_date" => $overallEnd->format('Y-m-d')
],
"available_days" => $availableDays
]);
exit();
?>
/*
// Query to determine the overall assignment period for this teacher
$query = "SELECT MIN(Start_Date) AS start_date, MAX(End_Date) AS end_date
FROM Teacher_Course_Assignments
WHERE Teacher_ID = ?";
if ($stmt = $mysqli->prepare($query)) {
$stmt->bind_param("i", $teacherId);
$stmt->execute();
$result = $stmt->get_result();
if ($row = $result->fetch_assoc()) {
// Check if both dates are found
if (!is_null($row['start_date']) && !is_null($row['end_date'])) {
echo json_encode([
"status" => "success",
"period" => [
"start_date" => $row['start_date'],
"end_date" => $row['end_date']
]
]);
} else {
// No assignments found for this teacher
http_response_code(404);
echo json_encode([
"status" => "error",
"message" => "No course assignments found for this teacher."
]);
}
} else {
http_response_code(500);
echo json_encode([
"status" => "error",
"message" => "Could not retrieve assignment period."
]);
}
$stmt->close();
} else {
http_response_code(500);
echo json_encode([
"status" => "error",
"message" => "Database error: cannot prepare statement."
]);
}
exit();
*/
?>