data visualization for responses page
This commit is contained in:
parent
cdbc910be9
commit
170f400581
BIN
assets/pasted-20251015-181829-ed77e158.png
Normal file
BIN
assets/pasted-20251015-181829-ed77e158.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 30 KiB |
@ -13,7 +13,6 @@ if (!$survey_id) {
|
|||||||
try {
|
try {
|
||||||
$pdo = db();
|
$pdo = db();
|
||||||
|
|
||||||
// Fetch survey title
|
|
||||||
$stmt = $pdo->prepare("SELECT title FROM surveys WHERE id = ?");
|
$stmt = $pdo->prepare("SELECT title FROM surveys WHERE id = ?");
|
||||||
$stmt->execute([$survey_id]);
|
$stmt->execute([$survey_id]);
|
||||||
$survey = $stmt->fetch(PDO::FETCH_ASSOC);
|
$survey = $stmt->fetch(PDO::FETCH_ASSOC);
|
||||||
@ -24,24 +23,58 @@ try {
|
|||||||
exit;
|
exit;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Fetch all responses for this survey
|
$stmt = $pdo->prepare("
|
||||||
$stmt = $pdo->prepare("SELECT * FROM survey_responses WHERE survey_id = ? ORDER BY submitted_at DESC");
|
SELECT q.id AS question_id, q.question_text, q.question_type, sa.answer_text
|
||||||
$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
|
FROM survey_answers sa
|
||||||
JOIN questions q ON sa.question_id = q.id
|
JOIN questions q ON sa.question_id = q.id
|
||||||
WHERE sa.response_id = ?
|
WHERE q.survey_id = ?
|
||||||
");
|
");
|
||||||
$answer_stmt->execute([$response['id']]);
|
$stmt->execute([$survey_id]);
|
||||||
$answers = $answer_stmt->fetchAll(PDO::FETCH_ASSOC);
|
$answers = $stmt->fetchAll(PDO::FETCH_ASSOC);
|
||||||
$response['answers'] = $answers;
|
|
||||||
$responses_with_answers[] = $response;
|
$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) {
|
} catch (PDOException $e) {
|
||||||
@ -57,6 +90,9 @@ try {
|
|||||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||||
<title>Responses for <?php echo htmlspecialchars($survey['title']); ?></title>
|
<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">
|
<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>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
|
|
||||||
@ -81,34 +117,127 @@ try {
|
|||||||
|
|
||||||
<a href="admin.php" class="btn btn-secondary mb-4">← Back to Dashboard</a>
|
<a href="admin.php" class="btn btn-secondary mb-4">← Back to Dashboard</a>
|
||||||
|
|
||||||
<?php if (empty($responses_with_answers)): ?>
|
<?php if (empty($chart_data) && empty($text_answers)): ?>
|
||||||
<div class="alert alert-info">
|
<div class="alert alert-info">
|
||||||
No responses have been submitted for this survey yet.
|
No responses have been submitted for this survey yet.
|
||||||
</div>
|
</div>
|
||||||
<?php else: ?>
|
<?php else: ?>
|
||||||
<?php foreach ($responses_with_answers as $response): ?>
|
<div class="row">
|
||||||
<div class="card mb-4">
|
<?php foreach ($chart_data as $question_id => $data): ?>
|
||||||
|
<div class="col-md-6 mb-4">
|
||||||
|
<div class="card">
|
||||||
<div class="card-header">
|
<div class="card-header">
|
||||||
Response submitted on: <?php echo htmlspecialchars(date("F j, Y, g:i a", strtotime($response['submitted_at']))); ?>
|
<?php echo htmlspecialchars($data['question_text']); ?>
|
||||||
</div>
|
</div>
|
||||||
<div class="card-body">
|
<div class="card-body">
|
||||||
<ul class="list-group list-group-flush">
|
<canvas id="chart-<?php echo $question_id; ?>"></canvas>
|
||||||
<?php foreach ($response['answers'] as $answer): ?>
|
</div>
|
||||||
<li class="list-group-item">
|
</div>
|
||||||
<p class="fw-bold mb-1"><?php echo htmlspecialchars($answer['question_text']); ?></p>
|
</div>
|
||||||
<p class="mb-0 text-muted"><?php echo htmlspecialchars($answer['answer_text']); ?></p>
|
<?php endforeach; ?>
|
||||||
</li>
|
</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 endforeach; ?>
|
||||||
<?php if (empty($response['answers'])): ?>
|
|
||||||
<li class="list-group-item">No answers were provided for this submission.</li>
|
|
||||||
<?php endif; ?>
|
<?php endif; ?>
|
||||||
</ul>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
<?php endforeach; ?>
|
|
||||||
<?php endif; ?>
|
<?php endif; ?>
|
||||||
</div>
|
</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>
|
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user