36018-vm/profile.php
2025-11-22 13:52:11 +00:00

102 lines
3.9 KiB
PHP

<?php
session_start();
require_once 'db/config.php';
$profileId = $_GET['id'] ?? null;
if (!$profileId) {
header("Location: index.php");
exit();
}
$pdo = db();
// Fetch user information
try {
$stmt = $pdo->prepare('SELECT id, full_name, email, created_at FROM users WHERE id = :id');
$stmt->execute([':id' => $profileId]);
$profileUser = $stmt->fetch(PDO::FETCH_ASSOC);
} catch (PDOException $e) {
die("Database error: Could not retrieve user profile.");
}
if (!$profileUser) {
// Handle user not found
header("Location: index.php?message=user_not_found");
exit();
}
// Fetch tasks posted by the user
try {
$stmt = $pdo->prepare('SELECT * FROM tasks WHERE user_id = :user_id ORDER BY created_at DESC');
$stmt->execute([':user_id' => $profileId]);
$postedTasks = $stmt->fetchAll(PDO::FETCH_ASSOC);
} catch (PDOException $e) {
die("Database error: Could not retrieve posted tasks.");
}
// Fetch tasks the user has completed (i.e., their application was accepted)
try {
$stmt = $pdo->prepare(
'SELECT t.* FROM tasks t JOIN applications a ON t.id = a.task_id WHERE a.user_id = :user_id AND a.status = \'accepted\' ORDER BY t.created_at DESC'
);
$stmt->execute([':user_id' => $profileId]);
$completedTasks = $stmt->fetchAll(PDO::FETCH_ASSOC);
} catch (PDOException $e) {
die("Database error: Could not retrieve completed tasks.");
}
$pageTitle = htmlspecialchars($profileUser['full_name']) . "'s Profile";
include 'shared/header.php';
?>
<div class="container profile-page">
<div class="page-header">
<h1><?php echo htmlspecialchars($profileUser['full_name']); ?></h1>
<p><strong>Member Since:</strong> <?php echo date('F j, Y', strtotime($profileUser['created_at'])); ?></p>
</div>
<div class="profile-section">
<h2>Posted Tasks</h2>
<?php if (empty($postedTasks)): ?>
<p><?php echo htmlspecialchars($profileUser['name']); ?> has not posted any tasks yet.</p>
<?php else: ?>
<div class="task-list">
<?php foreach ($postedTasks as $task):
// Check if task status is not null before using it in strtolower
$taskStatus = $task['status'] ?? 'unknown';
?>
<div class="task-card-profile">
<h4><a href="manage-task.php?id=<?php echo $task['id']; ?>"><?php echo htmlspecialchars($task['title']); ?></a></h4>
<p><strong>Status:</strong> <span class="status-badge status-<?php echo strtolower(htmlspecialchars($taskStatus)); ?>"><?php echo htmlspecialchars($taskStatus); ?></span></p>
<p><strong>Payout:</strong> $<?php echo htmlspecialchars($task['payout']); ?></p>
</div>
<?php endforeach; ?>
</div>
<?php endif; ?>
</div>
<div class="profile-section">
<h2>Completed Tasks</h2>
<?php if (empty($completedTasks)): ?>
<p><?php echo htmlspecialchars($profileUser['name']); ?> has not completed any tasks yet.</p>
<?php else: ?>
<div class="task-list">
<?php foreach ($completedTasks as $task):
// Check if task status is not null before using it in strtolower
$taskStatus = $task['status'] ?? 'unknown';
?>
<div class="task-card-profile">
<h4><?php echo htmlspecialchars($task['title']); ?></h4>
<p><strong>Status:</strong> <span class="status-badge status-<?php echo strtolower(htmlspecialchars($taskStatus)); ?>"><?php echo htmlspecialchars($taskStatus); ?></span></p>
<p><strong>Payout:</strong> $<?php echo htmlspecialchars($task['payout']); ?></p>
</div>
<?php endforeach; ?>
</div>
<?php endif; ?>
</div>
</div>
<?php include 'shared/footer.php'; ?>