added graphs

This commit is contained in:
jostheta 2024-07-17 07:57:24 +05:30
parent 31efaa0ac6
commit 8ff1e34c65
5 changed files with 218 additions and 0 deletions

View File

@ -11,6 +11,8 @@ $route['forms/respond/(:num)'] = 'forms/respond/$1';
$route['responses'] = 'Forms/list_user_forms'; $route['responses'] = 'Forms/list_user_forms';
$route['edit_form/(:num)'] = 'Forms/edit_form/$1'; $route['edit_form/(:num)'] = 'Forms/edit_form/$1';
$route['responses/index/(:num)'] = 'Responses/index/$1';
//Routes of the pages controller //Routes of the pages controller

View File

@ -0,0 +1,53 @@
<?php
// application/controllers/Responses.php
class Responses extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->model('Response_model');
}
public function index($form_id) {
$responses = $this->Response_model->get_responses($form_id);
$data = [];
foreach ($responses as $response) {
$question_id = $response['question_id'];
$question_text = $response['question_text'];
$question_type = $response['question_type'];
$answer_text = $response['answer_text'];
if (!isset($data[$question_id])) {
$data[$question_id] = [
'question_text' => $question_text,
'question_type' => $question_type,
'answers' => []
];
if ($question_type == 'multiple-choice' || $question_type == 'checkbox') {
$options = $this->Response_model->get_options($question_id);
foreach ($options as $option) {
$data[$question_id]['options'][$option['option_text']] = 0;
}
}
}
if ($question_type == 'multiple-choice' || $question_type == 'checkbox') {
if (isset($data[$question_id]['options'][$answer_text])) {
$data[$question_id]['options'][$answer_text]++;
} else {
$data[$question_id]['options'][$answer_text] = 1; // Initialize the count for this option
}
} else {
$data[$question_id]['answers'][] = $answer_text;
}
}
// // Debug statement
// echo '<pre>';
// print_r($data);
// echo '</pre>';
$this->load->view('responses/view', ['data' => $data]);
}
}

View File

@ -0,0 +1,20 @@
<?php
class Response_model extends CI_Model {
public function get_responses($form_id) {
$this->db->select('q.question_id, q.question_text, q.question_type, ra.answer_text');
$this->db->from('Questions q');
$this->db->join('Response_Answers ra', 'q.question_id = ra.question_id');
$this->db->join('Responses r', 'ra.response_id = r.response_id');
$this->db->where('q.form_id', $form_id);
$query = $this->db->get();
return $query->result_array();
}
public function get_options($question_id) {
$this->db->select('option_id, option_text');
$this->db->from('Options');
$this->db->where('question_id', $question_id);
$query = $this->db->get();
return $query->result_array();
}
}

View File

@ -1,6 +1,8 @@
<div class="page_layout"> <div class="page_layout">
<div style="margin: 0 10%;"> <div style="margin: 0 10%;">
<h1>Responses for: <?= htmlspecialchars($form->title, ENT_QUOTES, 'UTF-8') ?></h1> <h1>Responses for: <?= htmlspecialchars($form->title, ENT_QUOTES, 'UTF-8') ?></h1>
<a href="<?php echo base_url('responses/index/' . $form->form_id); ?>">View responses Question wise</a>
<table> <table>
<thead> <thead>
<tr> <tr>

View File

@ -0,0 +1,141 @@
<!-- application/views/responses/view.php -->
<!DOCTYPE html>
<html>
<head>
<title>Form Responses</title>
<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
<style>
body {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
font-family: Arial, sans-serif;
}
.container {
max-width: 600px;
padding: 20px;
background: #f9f9f9;
border-radius: 8px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
}
.chart-container {
margin-bottom: 20px;
}
.paragraph-answer {
margin-bottom: 10px;
}
</style>
</head>
<body>
<div class="container">
<h1 style="text-align: center;">Form Responses</h1>
<?php foreach ($data as $question_id => $question): ?>
<div>
<h2><?php echo $question['question_text']; ?></h2>
<?php if ($question['question_type'] == 'multiple-choice'): ?>
<?php if (!empty($question['options'])): ?>
<div class="chart-container">
<canvas id="chart-<?php echo $question_id; ?>"></canvas>
</div>
<script>
var ctx = document.getElementById('chart-<?php echo $question_id; ?>').getContext('2d');
var chartData = {
labels: [],
datasets: [{
label: 'Responses',
data: [],
backgroundColor: [],
borderColor: [],
borderWidth: 1
}]
};
var backgroundColors = [
'rgba(255, 99, 132, 0.2)',
'rgba(54, 162, 235, 0.2)',
'rgba(255, 206, 86, 0.2)',
'rgba(75, 192, 192, 0.2)',
'rgba(153, 102, 255, 0.2)',
'rgba(255, 159, 64, 0.2)'
];
var borderColors = [
'rgba(255, 99, 132, 1)',
'rgba(54, 162, 235, 1)',
'rgba(255, 206, 86, 1)',
'rgba(75, 192, 192, 1)',
'rgba(153, 102, 255, 1)',
'rgba(255, 159, 64, 1)'
];
var colorIndex = 0;
<?php foreach ($question['options'] as $option_text => $count): ?>
chartData.labels.push('<?php echo $option_text; ?>');
chartData.datasets[0].data.push(<?php echo $count; ?>);
chartData.datasets[0].backgroundColor.push(backgroundColors[colorIndex % backgroundColors.length]);
chartData.datasets[0].borderColor.push(borderColors[colorIndex % borderColors.length]);
colorIndex++;
<?php endforeach; ?>
var chart = new Chart(ctx, {
type: 'pie',
data: chartData
});
</script>
<?php endif; ?>
<?php elseif ($question['question_type'] == 'checkbox'): ?>
<?php if (!empty($question['options'])): ?>
<div class="chart-container">
<canvas id="chart-<?php echo $question_id; ?>"></canvas>
</div>
<script>
var ctx = document.getElementById('chart-<?php echo $question_id; ?>').getContext('2d');
var chartData = {
labels: [],
datasets: [{
label: 'Responses',
data: [],
backgroundColor: 'rgba(75, 192, 192, 0.2)',
borderColor: 'rgba(75, 192, 192, 1)',
borderWidth: 1
}]
};
<?php foreach ($question['options'] as $option_text => $count): ?>
chartData.labels.push('<?php echo $option_text; ?>');
chartData.datasets[0].data.push(<?php echo $count; ?>);
<?php endforeach; ?>
var chart = new Chart(ctx, {
type: 'bar',
data: chartData,
options: {
scales: {
y: {
beginAtZero: true
}
}
}
});
</script>
<?php endif; ?>
<?php elseif ($question['question_type'] == 'paragraph'): ?>
<?php foreach ($question['answers'] as $answer): ?>
<div class="paragraph-answer">
<p>
<?php
$words = explode(' ', $answer);
echo implode(' ', array_slice($words, 0, 5));
if (count($words) > 5) echo '...';
?>
</p>
</div>
<?php endforeach; ?>
<?php endif; ?>
</div>
<?php endforeach; ?>
</div>
</body>
</html>