-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtake_test.php
More file actions
215 lines (183 loc) · 6.73 KB
/
take_test.php
File metadata and controls
215 lines (183 loc) · 6.73 KB
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
<?php
require_once('includes/user.config.inc.php');
require_once('includes/utilities.php');
global $connection;
$test_id = (int)$_GET['test'];
if(isset($_SESSION['user']) && (int)$_SESSION['user']['id'] > 0){
$finish = $_POST['submit'] == 'Finish';
$submit = $_POST['submit'] == 'Next' || $finish;
if($test_id > 0 && !$submit && !$finish && !isset($_SESSION['test']) && !isset($_SESSION['question_ids'])){
$query = "SELECT * FROM tests WHERE id = $test_id LIMIT 1";
$testData = mysql_query($query, $connection);
if (!$testData) {
echo "Could not successfully run query ($query) from DB: " . mysql_error();
exit;
}
if($test = mysql_fetch_assoc($testData)){
$_SESSION['test'] = $test;
}
else{
echo 'No Test Found';
exit;
}
$query = "SELECT id FROM questions WHERE test_id = $test_id ORDER BY id ASC";
$questionsData = mysql_query($query, $connection);
if(!$questionsData) {
echo "Could not successfully run query ($query) from DB: " . mysql_error();
exit;
}
$_SESSION['question_ids'] = array();
while($question_ids = mysql_fetch_assoc($questionsData)){
$_SESSION['question_ids'][] = $question_ids['id'];
}
$_SESSION['current_id'] = $_SESSION['question_ids'][0];
$questionNumber = 0;
}
elseif($submit){
$question_id = (int)$_POST['question_id'];
$questionNumber = (int)$_POST['questionNumber'];
$answer = $_POST["quest{$question_id}answer"];
$questionNumber++;
$_SESSION['current_id'] = $_SESSION['question_ids'][$questionNumber];
$query = "SELECT answers FROM users_tests WHERE test_id = ".$_SESSION['test']['id']." AND user_id = ".$_SESSION['user']['id'];
if(!$data = mysql_query($query)){
echo "Could not successfully run query ($query) from DB: " . mysql_error();
exit;
}
$answers = array();
if($question = mysql_fetch_assoc($data)){
$answers = unserialize($question['answers']);
}
$answers[$question_id] = $answer;
$answers = serialize($answers);
if($questionNumber == 1){
$query = "INSERT INTO users_tests VALUES({$_SESSION['user']['id']}, {$_SESSION['test']['id']}, NULL, '{$answers}', DEFAULT, DEFAULT)";
}
else{
$query = "UPDATE users_tests SET answers = '$answers' WHERE user_id = {$_SESSION['user']['id']} AND test_id = {$_SESSION['test']['id']}";
}
if(!mysql_query($query)){
echo "Could not successfully run query ($query) from DB: " . mysql_error();
exit;
}
if($finish){
$query = "SELECT id, type, answers FROM questions WHERE test_id = {$_SESSION['test']['id']} ORDER BY id ASC";
if(!$data = mysql_query($query)){
echo "Could not successfully run query ($query) from DB: " . mysql_error();
exit;
}
$userAnswers = unserialize($answers);
$correct = 0;
//calculate result
while($correctAnswers = mysql_fetch_assoc($data)){
switch($correctAnswers['type']){
case 1:
$answer = unserialize($correctAnswers['answers']);
break;
default:
$answer = $correctAnswers['answers'];
break;
}
if($userAnswers[$correctAnswers['id']] == $answer){
$correct++;
}
}
$query = "UPDATE users_tests SET correct = $correct, is_complete = 1 WHERE user_id = {$_SESSION['user']['id']} AND test_id = {$_SESSION['test']['id']}";
if(!mysql_query($query)){
echo "Could not successfully run query ($query) from DB: " . mysql_error();
exit;
}
$_SESSION['test_id'] = $_SESSION['test']['id'];
unset($_SESSION['test'], $_SESSION['question_ids'], $_SESSION['current_id']);
header('Location: result.php');
exit;
}
}
if((int)$_SESSION['current_id'] < 1){
echo 'Invalid Question ID';
exit;
}
$submit = 'Next';
if(!isset($questionNumber)){
$questionNumber = array_keys($_SESSION['question_ids'], $_SESSION['current_id']);
$questionNumber = $questionNumber[0];
$questionNumber;
}
if(($questionNumber + 1) == (int)$_SESSION['test']['questions_count']){
$submit = 'Finish';
}
$query = "SELECT * FROM questions WHERE id=".(int)$_SESSION['current_id'];
$questionData = mysql_query($query);
if(!$questionData) {
echo "Could not successfully run query ($query) from DB: " . mysql_error();
exit;
}
$pageTitle = $_SESSION['test']['title'];
}
else{
header('Location: login.php');
exit;
}
?>
<? require_once('includes/header.inc.php'); ?>
<div class="content">
<p>Question <?=$questionNumber+1 ?> Of <?=$_SESSION['test']['questions_count'] ?></p>
<form action="take_test.php" method="post">
<? if($questionRow = mysql_fetch_assoc($questionData)): ?>
<input type="hidden" name="question_id" value="<?=$questionRow['id'] ?>" />
<input type="hidden" name="questionNumber" value="<?=$questionNumber ?>" />
<?
switch($questionRow['type']){
case 2:
$questionOptions = unserialize($questionRow['options']);
$optionsCount = count($questionOptions);
?>
<div class="questionTitle">
<?=$questionRow['title'] ?>
</div>
<div class="questionOptionLeft">
<? for($i=0; $i < $optionsCount; $i += 2): ?>
<p class="option"><input type="radio" name="quest<?=$questionRow['id'] ?>answer" value="<?=$i ?>" /><?=$questionOptions[$i] ?></p>
<? endfor; ?>
</div>
<div class="questionOptionRight">
<? for($i=1; $i < $optionsCount; $i += 2): ?>
<p class="option"><input type="radio" name="quest<?=$questionRow['id'] ?>answer" value="<?=$i ?>" /><?=$questionOptions[$i] ?></p>
<? endfor; ?>
</div>
<div style="clear:both"></div>
<?
break;
case 1:
$questionOptions = unserialize($questionRow['options']);
$optionsCount = count($questionOptions);
?>
<div class="questionTitle">
<?=$questionRow['title'] ?>
</div>
<div class="questionOptionLeft" style="float:left;">
<? for($i=0; $i < $optionsCount; $i += 2): ?>
<p class="option"><?=$questionOptions[$i] ?><input type="checkbox" name="quest<?=$questionRow['id'] ?>answer[]" value="<?=$i ?>" /></p>
<? endfor; ?>
</div>
<div class="questionOptionRight" style="float:left; margin-left:15px">
<? for($i=1; $i < $optionsCount; $i += 2): ?>
<p class="option"><?=$questionOptions[$i] ?><input type="checkbox" name="quest<?=$questionRow['id'] ?>answer[]" value="<?=$i ?>" /></p>
<? endfor; ?>
</div>
<div style="clear:both"></div>
<? break;
case 3:
$question = $questionRow['id'];
?>
<div class="questionTitle">
<?=str_replace('____',"<input class='blank' type='text' name='quest{$questionRow['id']}answer' />", $questionRow['title']); ?>
</div>
<?
break;
}
endif; ?>
<input type="submit" name="submit" value="<?=$submit ?>" />
</form>
</div>
<? require_once('includes/footer.inc.php'); ?>