34988-vm/view_responses.php
2025-10-15 18:14:20 +00:00

115 lines
4.0 KiB
PHP

<?php
require_once 'session.php';
require_once 'db/config.php';
$survey_id = isset($_GET['survey_id']) ? (int)$_GET['survey_id'] : 0;
if (!$survey_id) {
http_response_code(400);
echo "Error: Survey ID is missing.";
exit;
}
try {
$pdo = db();
// Fetch survey title
$stmt = $pdo->prepare("SELECT title FROM surveys WHERE id = ?");
$stmt->execute([$survey_id]);
$survey = $stmt->fetch(PDO::FETCH_ASSOC);
if (!$survey) {
http_response_code(404);
echo "Error: Survey not found.";
exit;
}
// Fetch all responses for this survey
$stmt = $pdo->prepare("SELECT * FROM survey_responses WHERE survey_id = ? ORDER BY submitted_at DESC");
$stmt->execute([$survey_id]);
$responses = $stmt->fetchAll(PDO::FETCH_ASSOC);
// For each response, fetch the answers
$responses_with_answers = [];
foreach ($responses as $response) {
$answer_stmt = $pdo->prepare("
SELECT sa.answer_text, q.question_text
FROM survey_answers sa
JOIN questions q ON sa.question_id = q.id
WHERE sa.response_id = ?
");
$answer_stmt->execute([$response['id']]);
$answers = $answer_stmt->fetchAll(PDO::FETCH_ASSOC);
$response['answers'] = $answers;
$responses_with_answers[] = $response;
}
} catch (PDOException $e) {
http_response_code(500);
echo "Database error: " . htmlspecialchars($e->getMessage());
exit;
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Responses for <?php echo htmlspecialchars($survey['title']); ?></title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<nav class="navbar navbar-expand-lg navbar-light bg-light">
<div class="container">
<a class="navbar-brand" href="admin.php">Admin Panel</a>
<div class="collapse navbar-collapse">
<ul class="navbar-nav ms-auto">
<li class="nav-item">
<a class="nav-link" href="create_survey.php">Create Survey</a>
</li>
<li class="nav-item">
<a class="nav-link" href="logout.php">Logout</a>
</li>
</ul>
</div>
</div>
</nav>
<div class="container mt-5">
<h1 class="mb-4">Responses for "<?php echo htmlspecialchars($survey['title']); ?>"</h1>
<a href="admin.php" class="btn btn-secondary mb-4">&larr; Back to Dashboard</a>
<?php if (empty($responses_with_answers)): ?>
<div class="alert alert-info">
No responses have been submitted for this survey yet.
</div>
<?php else: ?>
<?php foreach ($responses_with_answers as $response): ?>
<div class="card mb-4">
<div class="card-header">
Response submitted on: <?php echo htmlspecialchars(date("F j, Y, g:i a", strtotime($response['submitted_at']))); ?>
</div>
<div class="card-body">
<ul class="list-group list-group-flush">
<?php foreach ($response['answers'] as $answer): ?>
<li class="list-group-item">
<p class="fw-bold mb-1"><?php echo htmlspecialchars($answer['question_text']); ?></p>
<p class="mb-0 text-muted"><?php echo htmlspecialchars($answer['answer_text']); ?></p>
</li>
<?php endforeach; ?>
<?php if (empty($response['answers'])): ?>
<li class="list-group-item">No answers were provided for this submission.</li>
<?php endif; ?>
</ul>
</div>
</div>
<?php endforeach; ?>
<?php endif; ?>
</div>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>