/home/techb158/workloadmatch.com/api/notifications
Edit: /home/techb158/workloadmatch.com/api/notifications/data.php (3343B)
0, 'items' => [], 'debug' => 'no user']);
exit;
}
$user_type = $_GET['user_type'] ?? 'manager';
$items = [];
$totalCount = 0;
$errors = [];
if ($user_type === 'manager') {
// Unread chat messages from teachers
$stmt = $mysqli->prepare("
SELECT tp.Teacher_ID,
CONCAT(tp.First_Name, ' ', tp.Last_Name) AS name,
COUNT(*) AS cnt,
MAX(cm.created_at) AS last_time
FROM chat_messages cm
JOIN teacher_profile tp ON tp.Teacher_ID = cm.sender_id
WHERE cm.receiver_id = ? AND cm.receiver_type = 'manager' AND cm.is_read = 0
GROUP BY tp.Teacher_ID, tp.First_Name, tp.Last_Name
ORDER BY last_time DESC
LIMIT 10
");
if ($stmt) {
$stmt->bind_param("i", $userId);
$stmt->execute();
$res = $stmt->get_result();
while ($row = $res->fetch_assoc()) {
$totalCount += (int)$row['cnt'];
$time = $row['last_time'] ? timeAgo($row['last_time']) : '';
$items[] = [
'icon' => 'fa fa-comment text-aqua',
'text' => $row['name'] . ' (' . $row['cnt'] . ')',
'preview' => '',
'time' => $time,
'link' => 'chat.php'
];
}
$stmt->close();
} else {
$errors[] = 'chat query prepare failed: ' . $mysqli->error;
}
} else {
// Teacher notifications
$stmt = $mysqli->prepare("
SELECT cm.message, cm.created_at,
CONCAT(mp.First_Name, ' ', mp.Last_Name) AS name
FROM chat_messages cm
JOIN manager_profile mp ON mp.Manager_ID = cm.sender_id
WHERE cm.receiver_id = ? AND cm.receiver_type = 'teacher' AND cm.is_read = 0
ORDER BY cm.created_at DESC
LIMIT 10
");
if ($stmt) {
$stmt->bind_param("i", $userId);
$stmt->execute();
$res = $stmt->get_result();
while ($row = $res->fetch_assoc()) {
$totalCount++;
$preview = $row['message'] ? (mb_strlen($row['message']) > 50 ? mb_substr($row['message'], 0, 50) . '...' : $row['message']) : '';
$items[] = [
'icon' => 'fa fa-comment text-aqua',
'text' => 'Message from ' . $row['name'],
'preview' => $preview,
'time' => timeAgo($row['created_at']),
'link' => 'chat.php'
];
}
$stmt->close();
} else {
$errors[] = 'chat query prepare failed: ' . $mysqli->error;
}
}
echo json_encode(['count' => $totalCount, 'items' => $items, 'debug' => $errors]);
$mysqli->close();
function timeAgo($dt) {
if (!$dt) return '';
$ts = strtotime($dt);
$diff = time() - $ts;
if ($diff < 60) return 'now';
if ($diff < 3600) return floor($diff / 60) . 'm';
if ($diff < 86400) return floor($diff / 3600) . 'h';
if ($diff < 172800) return 'yesterday';
return date('M d', $ts);
}