prepare('SELECT * FROM tasks WHERE id = :task_id AND user_id = :user_id'); $stmt->execute([':task_id' => $taskId, ':user_id' => $userId]); $task = $stmt->fetch(PDO::FETCH_ASSOC); if (!$task) { // If the user does not own this task, redirect them header("Location: manage-tasks.php?message=access_denied"); exit(); } } catch (PDOException $e) { die("Database error: Could not verify task ownership."); } // Handle POST request to update application status if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['application_id']) && isset($_POST['action'])) { $applicationId = $_POST['application_id']; $action = $_POST['action']; try { $pdo->beginTransaction(); if ($action === 'accept') { // 1. Set this application to 'accepted' $stmt = $pdo->prepare('UPDATE applications SET status = 'accepted' WHERE id = :application_id AND task_id = :task_id'); $stmt->execute([':application_id' => $applicationId, ':task_id' => $taskId]); // 2. Set the task status to 'assigned' $stmt = $pdo->prepare('UPDATE tasks SET status = 'assigned' WHERE id = :task_id'); $stmt->execute([':task_id' => $taskId]); // 3. Set all other pending applications for this task to 'rejected' $stmt = $pdo->prepare('UPDATE applications SET status = 'rejected' WHERE task_id = :task_id AND id != :application_id AND status = 'pending''); $stmt->execute([':task_id' => $taskId, ':application_id' => $applicationId]); $message = 'Application accepted! The task is now assigned.'; $message_type = 'success'; } elseif ($action === 'reject') { // Just reject this single application $stmt = $pdo->prepare('UPDATE applications SET status = 'rejected' WHERE id = :application_id AND task_id = :task_id'); $stmt->execute([':application_id' => $applicationId, ':task_id' => $taskId]); $message = 'Application rejected.'; $message_type = 'info'; } $pdo->commit(); header("Location: manage-task.php?id=$taskId&message=" . urlencode($message) . "&type=" . $message_type); exit(); } catch (PDOException $e) { $pdo->rollBack(); // Log error instead of dying in production die("Database error: Could not update application status. " . $e->getMessage()); } } // Fetch all applications for this task try { $stmt = $pdo->prepare( 'SELECT a.id, a.status, u.username, u.email FROM applications a JOIN users u ON a.user_id = u.id WHERE a.task_id = :task_id ORDER BY a.created_at DESC' ); $stmt->execute([':task_id' => $taskId]); $applications = $stmt->fetchAll(PDO::FETCH_ASSOC); } catch (PDOException $e) { die("Database error: Could not retrieve applications."); } $pageTitle = "Manage Task: " . htmlspecialchars($task['title']); include 'shared/header.php'; ?>
Posted on:
Status:
This applicant was awarded the task.