34988-vm/view_responses.php
2025-10-15 18:23:13 +00:00

244 lines
8.6 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();
$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;
}
$stmt = $pdo->prepare("
SELECT q.id AS question_id, q.question_text, q.question_type, sa.answer_text
FROM survey_answers sa
JOIN questions q ON sa.question_id = q.id
WHERE q.survey_id = ?
");
$stmt->execute([$survey_id]);
$answers = $stmt->fetchAll(PDO::FETCH_ASSOC);
$chart_data = [];
$text_answers = [];
foreach ($answers as $answer) {
$question_id = $answer['question_id'];
$question_text = $answer['question_text'];
$question_type = $answer['question_type'];
$answer_text = $answer['answer_text'];
if ($question_type === 'multiple-choice' || $question_type === 'checkboxes') {
if (!isset($chart_data[$question_id])) {
$chart_data[$question_id] = [
'question_text' => $question_text,
'answers' => [],
];
}
if ($question_type === 'checkboxes') {
$selected_options = explode(',', $answer_text);
foreach ($selected_options as $option) {
$option = trim($option);
if (!empty($option)) {
if (!isset($chart_data[$question_id]['answers'][$option])) {
$chart_data[$question_id]['answers'][$option] = 0;
}
$chart_data[$question_id]['answers'][$option]++;
}
}
} else { // multiple-choice
if (!isset($chart_data[$question_id]['answers'][$answer_text])) {
$chart_data[$question_id]['answers'][$answer_text] = 0;
}
$chart_data[$question_id]['answers'][$answer_text]++;
}
} else {
if (!isset($text_answers[$question_id])) {
$text_answers[$question_id] = [
'question_text' => $question_text,
'answers' => [],
];
}
$text_answers[$question_id]['answers'][] = $answer_text;
}
}
} 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">
<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
<script src="https://d3js.org/d3.v4.min.js"></script>
<script src="https://cdn.jsdelivr.net/gh/holtzy/D3-graph-gallery@master/LIB/d3.layout.cloud.js"></script>
</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($chart_data) && empty($text_answers)): ?>
<div class="alert alert-info">
No responses have been submitted for this survey yet.
</div>
<?php else: ?>
<div class="row">
<?php foreach ($chart_data as $question_id => $data): ?>
<div class="col-md-6 mb-4">
<div class="card">
<div class="card-header">
<?php echo htmlspecialchars($data['question_text']); ?>
</div>
<div class="card-body">
<canvas id="chart-<?php echo $question_id; ?>"></canvas>
</div>
</div>
</div>
<?php endforeach; ?>
</div>
<?php if (!empty($text_answers)): ?>
<hr class="my-5">
<h2>Text Answers</h2>
<?php foreach ($text_answers as $question_id => $data): ?>
<div class="card mb-4">
<div class="card-header">
<?php echo htmlspecialchars($data['question_text']); ?>
</div>
<div class="card-body">
<div id="word-cloud-<?php echo $question_id; ?>"></div>
</div>
</div>
<?php endforeach; ?>
<?php endif; ?>
<?php endif; ?>
</div>
<script>
document.addEventListener('DOMContentLoaded', function () {
<?php foreach ($chart_data as $question_id => $data): ?>
new Chart(document.getElementById('chart-<?php echo $question_id; ?>'), {
type: 'bar',
data: {
labels: <?php echo json_encode(array_keys($data['answers'])); ?>,
datasets: [{
label: 'Number of Responses',
data: <?php echo json_encode(array_values($data['answers'])); ?>,
backgroundColor: 'rgba(54, 162, 235, 0.6)',
borderColor: 'rgba(54, 162, 235, 1)',
borderWidth: 1
}]
},
options: {
scales: {
y: {
beginAtZero: true,
ticks: {
stepSize: 1
}
}
}
}
});
<?php endforeach; ?>
<?php foreach ($text_answers as $question_id => $data): ?>
var text = <?php echo json_encode(implode(" ", $data['answers'])); ?>;
var words = text.split(/\s+/).map(function(word) {
return word.replace(/[^\w\s]/gi, '').toLowerCase();
}).filter(function(word) {
return word.length > 3;
});
var wordCounts = {};
words.forEach(function(word) {
wordCounts[word] = (wordCounts[word] || 0) + 1;
});
var word_data = Object.keys(wordCounts).map(function(key) {
return {text: key, size: wordCounts[key]};
});
var margin = {top: 10, right: 10, bottom: 10, left: 10},
width = 450 - margin.left - margin.right,
height = 450 - margin.top - margin.bottom;
var svg = d3.select("#word-cloud-<?php echo $question_id; ?>").append("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
var layout = d3.layout.cloud()
.size([width, height])
.words(word_data.map(function(d) { return {text: d.text, size:d.size*10}; }))
.padding(5)
.rotate(function() { return ~~(Math.random() * 2) * 90; })
.font("Impact")
.fontSize(function(d) { return d.size; })
.on("end", draw);
layout.start();
function draw(words) {
svg
.append("g")
.attr("transform", "translate(" + layout.size()[0] / 2 + "," + layout.size()[1] / 2 + ")")
.selectAll("text")
.data(words)
.enter().append("text")
.style("font-size", function(d) { return d.size + "px"; })
.style("font-family", "Impact")
.attr("text-anchor", "middle")
.attr("transform", function(d) {
return "translate(" + [d.x, d.y] + ")rotate(" + d.rotate + ")";
})
.text(function(d) { return d.text; });
}
<?php endforeach; ?>
});
</script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>