35250-vm/task-view.php
Flatlogic Bot f59042f511 Version 2
2025-10-26 13:25:51 +00:00

158 lines
6.6 KiB
PHP

<?php
require_once 'db/config.php';
session_start();
$notification = null;
// Get task ID from URL, with basic validation
$task_id = isset($_GET['id']) ? (int)$_GET['id'] : 0;
// Handle form submission
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['task_id'])) {
$submitted_task_id = (int)$_POST['task_id'];
$completion_notes = trim($_POST['completion_notes'] ?? '');
$proof_file_path = null;
// Handle file upload
if (isset($_FILES['proof_file']) && $_FILES['proof_file']['error'] === UPLOAD_ERR_OK) {
$upload_dir = 'assets/uploads/';
// Create a unique filename to prevent overwriting
$file_ext = pathinfo($_FILES['proof_file']['name'], PATHINFO_EXTENSION);
$unique_name = uniqid('proof_', true) . '.' . $file_ext;
$target_file = $upload_dir . $unique_name;
if (move_uploaded_file($_FILES['proof_file']['tmp_name'], $target_file)) {
$proof_file_path = $target_file;
} else {
$notification = ['type' => 'danger', 'message' => 'Error: Could not upload the file.'];
}
}
// If file upload was successful (or no file was uploaded), update the database
if ($notification === null) {
try {
$pdo = db();
$stmt = $pdo->prepare(
"UPDATE tasks SET status = 'in review', completion_notes = ?, proof_file_path = ? WHERE id = ?"
);
$stmt->execute([$completion_notes, $proof_file_path, $submitted_task_id]);
$_SESSION['notification'] = ['type' => 'success', 'message' => 'Task submitted successfully for review!'];
header("Location: tasks.php");
exit;
} catch (PDOException $e) {
// For development: error_log($e->getMessage());
$notification = ['type' => 'danger', 'message' => 'Error: Could not update the task.'];
}
}
}
// Fetch task details for display
$task = null;
if ($task_id > 0) {
try {
$pdo = db();
$stmt = $pdo->prepare("SELECT * FROM tasks WHERE id = ?");
$stmt->execute([$task_id]);
$task = $stmt->fetch(PDO::FETCH_ASSOC);
} catch (PDOException $e) {
die("Error: Could not connect to the database or fetch task.");
}
}
// If task not found, redirect
if (!$task) {
header("Location: tasks.php");
exit;
}
// Check for notifications from previous page (e.g., after redirect)
if (isset($_SESSION['notification'])) {
$notification = $_SESSION['notification'];
unset($_SESSION['notification']);
}
$pageTitle = htmlspecialchars($task['title']);
$page = 'tasks';
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title><?php echo $pageTitle; ?> - Task App</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
<link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;700&display=swap" rel="stylesheet">
<link rel="stylesheet" href="assets/css/custom.css?v=<?php echo time(); ?>">
<script src="https://cdn.jsdelivr.net/npm/lucide@latest/dist/umd/lucide.js"></script>
</head>
<body class="bg-light">
<div class="container-fluid">
<header class="d-flex justify-content-between align-items-center py-3 mb-4 border-bottom">
<h1 class="h4"><?php echo $pageTitle; ?></h1>
<a href="tasks.php" class="btn btn-sm btn-outline-secondary">
<i data-lucide="arrow-left" class="align-middle"></i> Back to Tasks
</a>
</header>
<main class="pb-5">
<?php if ($notification): ?>
<div class="alert alert-<?php echo $notification['type']; ?> alert-dismissible fade show" role="alert">
<?php echo htmlspecialchars($notification['message']); ?>
<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
</div>
<?php endif; ?>
<div class="card rounded-4 border-0 shadow-sm mb-4">
<div class="card-body p-4">
<h5 class="card-title"><?php echo htmlspecialchars($task['title']); ?></h5>
<p class="card-text text-muted"><?php echo htmlspecialchars($task['description']); ?></p>
<div class="d-flex justify-content-between align-items-center">
<span class="badge bg-success-subtle text-success-emphasis rounded-pill px-3 py-2">
Reward: <?php echo htmlspecialchars($task['reward']); ?> BDT
</span>
<span class="badge bg-info-subtle text-info-emphasis rounded-pill px-3 py-2">
Status: <?php echo ucfirst(htmlspecialchars($task['status'])); ?>
</span>
</div>
</div>
</div>
<div class="card rounded-4 border-0 shadow-sm">
<div class="card-body p-4">
<h5 class="card-title mb-3">Submit for Review</h5>
<form action="task-view.php?id=<?php echo $task['id']; ?>" method="POST" enctype="multipart/form-data">
<input type="hidden" name="task_id" value="<?php echo $task['id']; ?>">
<div class="mb-3">
<label for="completion_notes" class="form-label">Completion Notes</label>
<textarea class="form-control" id="completion_notes" name="completion_notes" rows="4" placeholder="Provide details about your work, links, etc."></textarea>
</div>
<div class="mb-3">
<label for="proof_file" class="form-label">Upload Screenshot</label>
<input class="form-control" type="file" id="proof_file" name="proof_file">
</div>
<div class="d-grid">
<button type="submit" class="btn btn-primary btn-lg" <?php echo $task['status'] !== 'pending' ? 'disabled' : ''; ?>>
<?php echo $task['status'] !== 'pending' ? 'Already Submitted' : 'Submit Task'; ?>
</button>
</div>
</form>
</div>
</div>
</main>
</div>
<?php include 'includes/nav.php'; ?>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"></script>
<script>
lucide.createIcons();
</script>
</body>
</html>