-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathviewResults.php
297 lines (246 loc) · 12.9 KB
/
viewResults.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
<!DOCTYPE html>
<html lang="en-UK">
<head>
<?php include_once("./includes/headTags.php"); ?>
<link rel="stylesheet" href="./css/viewResults.css">
<link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=Material+Symbols+Outlined:opsz,wght,FILL,GRAD@48,400,1,0" />
<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
<script src="./js/TextBoxQuestion.js"></script>
<script src="./js/surveyResults.js"></script>
</head>
<body>
<!-- Header Section -->
<?php include_once("./includes/header.php"); ?>
<?php
if (!isset($_SESSION["user"])) {
header("Location: ./signup.php");
}
?>
<!-- Scripts -->
<script>
function openSurveyResults(id) { location.href = "viewResults.php?sid=" + id; }
</script>
<!-- Main Content -->
<main>
<?php
// Database connection
include_once("./php/dbConnection.php");
// Check if survey code is provided
if (!isset($_GET["sid"])) {
header("Location: ./dashboard.php");
exit();
}
$surveyCode = $_GET["sid"];
// Get survey from database
$survey = $db->query(
"SELECT * FROM surveys WHERE code='{$surveyCode}'"
);
if ($survey->rowCount() == 1) {
$survey = $survey->fetchObject();
} else {
header("Location: ./dashboard.php");
exit();
}
// Get survey id
$surveyID = $survey->survey_ID;
// Get user id
$userID = $_SESSION["user"]->user_ID;
// Check if user owns survey
$result = $db->query(
"SELECT * FROM survey_owner WHERE user_ID='{$userID}' AND survey_ID='{$surveyID}'"
);
// If user does not own survey then redirect back
if ($result->rowCount() != 1) {
header("Location: ./dashboard.php?you do not own the survey");
exit();
}
// Get total response count
$totalParticipants = $db->query(
"SELECT survey_ID, count(*) as 'count' FROM survey_response WHERE survey_ID='$surveyID'"
);
$totalParticipants = $totalParticipants->fetchObject();
$totalParticipants = $totalParticipants->count;
// Create list to store survey questions
$surveyQuestions = array();
// Get all questions
$questions = $db->query(
"SELECT q.* FROM questions AS q INNER JOIN question_order AS qo ON q.question_ID=qo.question_ID AND qo.survey_ID='$surveyID'"
);
$questions = $questions->fetchAll();
// Get all options
foreach ($questions as $q) {
if ($q["type"] == "MultipleChoice") {
$questionID = $q["question_ID"];
// Create question object
$question = new StdClass();
$question->id = $q["question_ID"];
$question->type = $q["type"];
$question->text = $q["text"];
$question->options = array();
// Get all options
$options = $db->query(
"SELECT o.* FROM options AS o INNER JOIN option_order AS oo ON o.option_ID=oo.option_ID AND oo.question_ID='$questionID'"
);
$options = $options->fetchAll();
// Create option object
foreach ($options as $o) {
// Create option object
$option = new StdClass();
$option->id = $o["option_ID"];
$option->type = $o["type"];
$option->text = $o["text"];
// Add option object to question
array_push($question->options, $option);
}
// Add question to array
array_push($surveyQuestions, $question);
} else {
// Create question object
$question = new StdClass();
$question->id = $q["question_ID"];
$question->type = $q["type"];
$question->text = $q["text"];
$question->options = array();
// Add question to array
array_push($surveyQuestions, $question);
}
}
?>
<h1 id="page-title">Results</h1>
<!-- Survey info and button group -->
<div id="survey-info-container">
<div id="title-and-description-container">
<h2><?php echo($survey->name); ?></h2>
<h3><?php echo($survey->description); ?></h3>
<h3><?php echo("Total Participants: " . $totalParticipants); ?></h3>
</div>
<div id="survey-button-group-container">
<div id="top-container">
<a><p><span class="material-symbols-outlined">edit</span>Edit Survey</p></a>
<?php
if ($survey->status == "0") {
?>
<a onclick="location.href='./php/updateSurvey.php?userID=<?php echo($userID); ?>&surveyID=<?php echo($surveyID); ?>&status=open'"><p><span class="material-symbols-outlined">visibility</span>Activate Survey</p></a>
<?php
} else if ($survey->status == "1") {
?>
<a onclick="location.href='./php/updateSurvey.php?userID=<?php echo($userID); ?>&surveyID=<?php echo($surveyID); ?>&status=close'"><p><span class="material-symbols-outlined">visibility_off</span>Close Survey</p></a>
<?php
}
?>
</div>
<div id="bottom-container">
<a><p><span class="material-symbols-outlined">download</span>Download Data</p></a>
<a onclick="location.href='./php/deleteSurvey.php?userID=<?php echo($userID); ?>&surveyID=<?php echo($surveyID); ?>'"><p><span class="material-symbols-outlined">delete</span>Delete Survey</p></a>
</div>
</div>
</div>
<hr>
<!-- List of questions results -->
<div id="question-result-container">
<?php
$index = 1;
foreach ($surveyQuestions as $q) {
if ($q->type == "TextBox") {
?>
<div class="question-result">
<div id="question-text" class="inner-question-container">
<p><?php echo("Question " . $index); ?></p>
<h2><?php echo($q->text); ?></h2>
</div>
<div id="text-box-answer-viewer-container" >
<div id="text-box-answer-button-container">
<a onclick="previousTextBoxAnswer('TextBoxAnswer<?php echo($index); ?>','<?php echo($index); ?>')">
<div>
<p id="button-previous"><span class="material-symbols-outlined">navigate_before</span>Previous</p>
</div>
</a>
<p id="TextBoxTotal<?php echo($index)?>">0 / 0</p>
<a onclick="nextTextBoxAnswer('TextBoxAnswer<?php echo($index); ?>','<?php echo($index); ?>')">
<div>
<p id="button-next">Next<span class="material-symbols-outlined">navigate_next</span></p>
</div>
</a>
</div>
<div id="text-box-answer">
<p id="TextBoxAnswer<?php echo($index); ?>">answer</p>
</div>
</div>
</div>
<?php
// Get all responses for this question
$answers = $db->query(
"SELECT a.* FROM answers AS a INNER JOIN answer_response AS ar ON a.answer_ID=ar.answer_ID AND ar.question_ID='$q->id'"
);
// Fetch all answers
$answers = $answers->fetchAll();
// Convert question answers to string
$answerString = "'";
foreach ($answers as $a) {
$answerString = $answerString . $a["text"] . "#@#";
}
// Remove last comma
$answerString = rtrim($answerString, "#@#") . "'";
echo("<script>setTextBoxData('TextBoxAnswer" . $index . "', " . $index . ", " . $answerString . ");</script>");
} else if ($q->type == "MultipleChoice") {
echo("<script>setTextBoxDataEmpty();</script>");
?>
<div class="question-result">
<div id="question-text" class="inner-question-container">
<p><?php echo("Question " . $index); ?></p>
<h2><?php echo($q->text); ?></h2>
</div>
<div id="pie-chart" class="chart-container">
<canvas id="<?php echo("pieChartQuestion" . $index); ?>"></canvas>
</div>
<div id="bar-chart" class="chart-container">
<canvas id="<?php echo("barChartQuestion" . $index); ?>"></canvas>
</div>
</div>
<?php
// Convert options to string
$labels = "'";
$options = $q->options;
foreach($options as $o) {
$labels = $labels . $o->text . ",";
}
// Remove last comma
$labels = rtrim($labels, ",") . "'";
// Get all responses for this question
$answers = $db->query(
"SELECT a.* FROM answers AS a INNER JOIN answer_response AS ar ON a.answer_ID=ar.answer_ID AND ar.question_ID='$q->id'"
);
// Fetch all answers
$answers = $answers->fetchAll();
// Create array to store count for each option
$count = array(0, 0, 0, 0);
foreach($answers as $a) {
if ($a["option_ID"] == $options[0]->id) {
$count[0]++;
} else if ($a["option_ID"] == $options[1]->id) {
$count[1]++;
} else if ($a["option_ID"] == $options[2]->id) {
$count[2]++;
} else if ($a["option_ID"] == $options[3]->id) {
$count[3]++;
}
}
// Convert count to string
$data = "'";
foreach($count as $c) {
$data = $data . $c . ",";
}
// Remove last comma
$data = rtrim($data, ",") . "'";
echo("<script>setChartData('pieChartQuestion" . $index . "', 'barChartQuestion" . $index . "', ". $labels . ", " . $data .");</script>");
}
$index++;
}
//echo("<script>addData(" . $data . ");</script>");
?>
</div>
</main>
<!-- Footer Section -->
<?php include_once("./includes/footer.php"); ?>
</body>
</html>