-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheditQuestion.php
More file actions
306 lines (281 loc) · 14.6 KB
/
editQuestion.php
File metadata and controls
306 lines (281 loc) · 14.6 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
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
298
299
300
301
302
303
304
305
306
<?php require "includes/config.php"; ?>
<?php
$qid = $_GET['var'];
$qtitle = $_GET['var1'];
$question = $_GET['question'];
$answer = $_GET['answer'];
$action = $_GET['action'];
$totalans = $_GET['totalans'];
$refresh = $_GET['r'];
$i = 1;
$answerValue = "none";
//$_SESSION['usertxt']=array($answer);
if ($refresh == 0)
$_SESSION['usertxt'] = array(30);
//retrieve number of answers if action is edit
if ($answer == -1) {
$stmt = $db->prepare('SELECT acounter FROM question WHERE qid=:qid AND lid=:lid;');
$stmt->bindParam(':qid', $question);
$stmt->bindParam(':lid', $qid);
$stmt->execute();
$result = $stmt->fetch(PDO::FETCH_ASSOC);
$answer = $result['acounter'];
$stmt = $db->prepare('SELECT qcounter FROM lecture WHERE lid=:lid;');
$stmt->bindParam(':lid', $qid);
$stmt->execute();
$result = $stmt->fetch(PDO::FETCH_ASSOC);
$qcounter = $result['qcounter'];
} else if ($action == 1)
$qcounter = $question;
else if ($action == 0) {
$stmt = $db->prepare('SELECT qcounter FROM lecture WHERE lid=:lid;');
$stmt->bindParam(':lid', $qid);
$stmt->execute();
$result = $stmt->fetch(PDO::FETCH_ASSOC);
$qcounter = $result['qcounter'];
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Edit Question</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="css1/bootstrap.css" rel="stylesheet" media="screen">
<link href="css1/pstyle.css" rel="stylesheet" media="screen">
<link rel="stylesheet" href="https://www.w3schools.com/w3css/4/w3.css">
<script src="functionEditQuestion.js"></script>
</head>
<body>
<h1>Question Tab<h1>
<form method="post">
<?php
//if action is add question
if ($action == 1) {
if ($refresh == 0) {
?>
Name: Question <?php if ($action == 1) echo $question + 1;
else echo $question; ?> <br>
Question Body: <input type="text" name="questionbody" id="qstnd" value="Write the body of the question here" size="35"><br>
Choose the correct one:<br>
Answers:
<input type="text" name="<?php echo $i; ?>" id="ansr<?php echo $i; ?>" value="Answer <?php echo $i; ?>">
<input type="submit" class="w3-button w3-xlarge w3-circle w3-green w3-card-4" name="plus" value="+">
<input type="radio" id="radioAnswer <?php echo $answer; ?>" name="radioAnswer" value="<?php echo $i; ?>" ><br>
<?php }else {
?>
Name: Question <?php if ($action == 1) echo $question + 1;
else echo $question; ?><br>
Question Body: <input type="text" name="questionbody" id="qstnd" value="<?php echo $_SESSION['usertxt'][0] ?>" size="35"><br>
Choose the correct one:<br>
Answers:
<input type="text" name="<?php echo $i; ?>" id="ansr<?php echo $i; ?>" value="<?php echo $_SESSION['usertxt'][$i]; ?>">
<input type="submit" class="w3-button w3-xlarge w3-circle w3-green w3-card-4" name="plus" value="+">
<input type="radio" id="radioAnswer <?php echo $answer; ?>" name="radioAnswer" value="<?php echo $i; ?>" ><br>
<?php while ($i < $answer) { ?>
<input type="text" name="<?php echo $i + 1; ?>" id="ansr<?php echo $i + 1; ?>" value="<?php echo $_SESSION['usertxt'][$i + 1]; ?>">
<input type="radio" id="radioAnswer <?php echo $answer; ?>" name="radioAnswer" value="<?php echo $i + 1; ?>">
<?php if ($i != $answer - 1) { ?><br>
<?php
}
$i++;
}if ($answer > 1) {
?>
<input type="submit" class="w3-button w3-xlarge w3-circle w3-red w3-card-4" name="minus" value="-"><br>
<?php
}
}
}
//if action is edit question, retrieve from db the answers
else {
$stmt = $db->prepare('SELECT textofquestion FROM question WHERE qid=:qid AND lid=:lid;');
$stmt->bindParam(':qid', $question);
$stmt->bindParam(':lid', $qid);
$stmt->execute();
$result = $stmt->fetch(PDO::FETCH_ASSOC);
if ($refresh == 0) {
?>
Name: Question <?php if ($action == 1) echo $question + 1;
else echo $question; ?><br>
Question Body: <input type="text" name="questionbody" id="qstnd" value="<?php echo $result['textofquestion']; ?>" size="35"><br>
Choose the correct one:<br>
Answers:
<?php
$stmt = $db->prepare('SELECT textofanswer FROM answer WHERE qid=:qid AND lid=:lid;');
$stmt->bindParam(':qid', $question);
$stmt->bindParam(':lid', $qid);
$stmt->execute();
while ($result = $stmt->fetch(PDO::FETCH_ASSOC)) {
if ($i == 1) {
?>
<input type="text" name="<?php echo $i; ?>" id="ansr<?php echo $i; ?>" value="<?php echo $result['textofanswer']; ?>">
<input type="submit" name="plus" class="w3-button w3-xlarge w3-circle w3-green w3-card-4" value="+">
<input type="radio" id="radioAnswer <?php echo $answer; ?>" name="radioAnswer" value="<?php echo $i; ?>" ><br>
<?php } else if ($i <= $answer) {
?>
<input type="text" name="<?php echo $i; ?>" id="ansr<?php echo $i; ?>" value="<?php echo $result['textofanswer']; ?>">
<input type="radio" id="radioAnswer <?php echo $answer; ?>" name="radioAnswer" value="<?php echo $i; ?>" >
<?php if ($i <= $answer - 1) { ?><br><?php
}
}
$i++;
}
$j = $i - 1;
if ($j == $answer && $answer > 1) {
?>
<input type="submit" name="minus" class="w3-button w3-xlarge w3-circle w3-red w3-card-4" value="-"><br>
<?php } else {
?><br><?php
}
} else {
$j = 1;
?>
Name: Question <?php if ($action == 1) echo $question + 1;
else echo $question; ?><br>
Question Body: <input type="text" name="questionbody" id="qstnd" value="<?php echo $_SESSION['usertxt'][0] ?>" size="35"><br>
Choose the correct one:<br>
Answers:
<input type="text" name="<?php echo $i; ?>" id="ansr<?php echo $i; ?>" value="<?php echo $_SESSION['usertxt'][$j]; ?>">
<input type="submit" name="plus" class="w3-button w3-xlarge w3-circle w3-green w3-card-4" value="+">
<input type="radio" id="radioAnswer <?php echo $answer; ?>" name="radioAnswer" value="<?php echo $j; ?>">
<?php if ($j <= $answer - 1 || $answer == 1) { ?><br> <?php
}
}
while ($j < $answer) {
?>
<input type="text" name="<?php echo $j + 1; ?>" id="ansr<?php echo $j + 1; ?>" value="<?php echo $_SESSION['usertxt'][$j + 1]; ?>">
<input type="radio" id="radioAnswer <?php echo $j + 1; ?>" name="radioAnswer" value="<?php echo $j + 1; ?>">
<?php if ($j < $answer - 1) { ?><br><?php
}
$j++;
}
if (($j > $answer || ($refresh == 1 && $action != 1)) && $answer > 1) {
?>
<input type="submit" name="minus" class="w3-button w3-xlarge w3-circle w3-red w3-card-4" value="-"><br>
<?php
}
}
?>
<input class="btn btn-lg btn-success navbar-btn active v1 " type="submit" name="submit" value="Submit" id="submit">
<input class="btn btn-lg btn-warning navbar-btn active v1 " type="button" onclick="deleteAllAnswers(<?php echo $qid; ?>, '<?php echo $qtitle; ?>',<?php echo $qcounter; ?>,<?php echo $totalans; ?>,<?php echo $action; ?>)" value="Clear All">
</form>
</body>
</html>
<?php
if ($action == 1) {
//write documents
if (isset($_POST['submit'])) {
if (isset($_POST['radioAnswer'])) {
$question++;
$answerValue = $_POST[$_POST['radioAnswer']];
$stmt = $db->prepare('INSERT INTO question (qid,lid,textofquestion,textofanswer, acounter) values (:qid,:lid,:tq,:ta,:ca);');
$stmt->bindParam(':qid', $question);
$stmt->bindParam(':tq', $_POST['questionbody']);
$stmt->bindParam(':ta', $answerValue);
$stmt->bindParam(':ca', $i);
$stmt->bindParam(':lid', $qid);
$id = $db->lastInsertId('qidd');
$stmt->execute();
$stmt = $db->prepare('SELECT qidd FROM question WHERE qid=:qid AND lid=:lid;');
$stmt->bindParam(':qid', $question);
$stmt->bindParam(':lid', $qid);
$stmt->execute();
$result = $stmt->fetch(PDO::FETCH_ASSOC);
$id = $result['qidd'];
for ($j = 1; $j <= $answer; $j++) {
$ansid = $j + $totalans;
$stmt = $db->prepare('INSERT INTO answer (aid,qidd,qid,lid,textofanswer) values (:aid,:qidd,:qid,:lid,:ta);');
$stmt->bindParam(':aid', $ansid);
$stmt->bindParam(':qid', $question);
$stmt->bindParam(':ta', $_POST[$j]);
$stmt->bindParam(':lid', $qid);
$stmt->bindParam(':qidd', $id);
$idd = $db->lastInsertId('aidd');
$stmt->execute();
}
$stmt = $db->prepare('UPDATE lecture SET qcounter=:qc WHERE lid= :lid;');
$stmt->bindParam(':lid', $qid);
$stmt->bindParam(':qc', $question);
$stmt->execute();
echo "<script> alert('Question created'); </script>";
?>
<script>goBack(<?php echo $qid; ?>, '<?php echo $qtitle; ?>',<?php echo $qcounter; ?>,<?php echo $answer; ?>,<?php echo $totalans; ?>,<?php echo $action; ?>);</script>
<?php
} else {
echo "<script>alert('You have to select a correct answer first');</script>";
$_SESSION['usertxt'][0] = $_POST['questionbody'];
for ($u = 1; $u <= $answer; $u++) {
$_SESSION['usertxt'][$u] = $_POST[$u]; //echo $_SESSION['usertxt'][$u]." ";
}
echo "<script>window.location.href=\"editQuestion.php?var=\"+$qid+\"&var1=\"+'$qtitle'+\"&question=\"+$question+\"&answer=\"+$answer+\"&totalans=\"+$totalans+\"&action=\"+$action+\"&r=1\";</script>";
}
}
}
if ($action == 0) {
//update documents
if (isset($_POST['submit'])) {
if (isset($_POST['radioAnswer'])) {
$answerValue = $_POST[$_POST['radioAnswer']];
$stmt = $db->prepare('UPDATE question SET textofquestion=:tq, textofanswer=:ta, acounter=:ac WHERE qid=:qid AND lid=:lid;');
$stmt->bindParam(':qid', $question);
$stmt->bindParam(':lid', $qid);
$stmt->bindParam(':tq', $_POST['questionbody']);
$stmt->bindParam(':ta', $answerValue);
$stmt->bindParam(':ac', $answer);
$stmt->execute();
$stmt = $db->prepare('DELETE FROM answer WHERE qid =:qid AND lid=:lid;');
$stmt->bindParam(':qid', $question);
$stmt->bindParam(':lid', $qid);
$stmt->execute();
//problem me koubouthkia refresh 0
$stmt = $db->prepare('SELECT qidd FROM question WHERE qid=:qid AND lid=:lid;');
$stmt->bindParam(':qid', $question);
$stmt->bindParam(':lid', $qid);
$stmt->execute();
$result = $stmt->fetch(PDO::FETCH_ASSOC);
$id = $result['qidd'];
for ($j = 1; $j <= $answer; $j++) {
$ansid = $j + $totalans;
$stmt = $db->prepare('INSERT INTO answer (aid,qidd,qid,lid,textofanswer) values (:aid,:qidd,:qid,:lid,:ta);');
$stmt->bindParam(':aid', $ansid);
$stmt->bindParam(':qid', $question);
$stmt->bindParam(':qidd', $id);
$stmt->bindParam(':lid', $qid);
$stmt->bindParam(':ta', $_POST[$j]);
$idd = $db->lastInsertId('aidd');
$stmt->execute();
}
echo "<script> alert('Question updated'); </script>";
?>
<script>goBack(<?php echo $qid; ?>, '<?php echo $qtitle; ?>',<?php echo $qcounter; ?>,<?php echo $answer; ?>,<?php echo $totalans; ?>,<?php echo $action; ?>);</script>
<?php
} else {
echo "<script>alert('You have to select a correct answer first');</script>";
$_SESSION['usertxt'][0] = $_POST['questionbody'];
for ($u = 1; $u <= $answer; $u++) {
$_SESSION['usertxt'][$u] = $_POST[$u]; //echo $_SESSION['usertxt'][$u]." ";
}
echo "<script>window.location.href=\"editQuestion.php?var=\"+$qid+\"&var1=\"+'$qtitle'+\"&question=\"+$question+\"&answer=\"+$answer+\"&totalans=\"+$totalans+\"&action=\"+$action+\"&r=1\";</script>";
}
}
}
//save user documents
if (isset($_POST['plus'])) {
$_SESSION['usertxt'][0] = $_POST['questionbody'];
for ($u = 1; $u <= $answer; $u++) {
$_SESSION['usertxt'][$u] = $_POST[$u]; //echo $_SESSION['usertxt'][$u]." ";
}
$_SESSION['usertxt'][$u] = "Answer " . $u;
?>
<script>addAnswer(<?php echo $qid; ?>, '<?php echo $qtitle; ?>',<?php echo $question; ?>,<?php echo $answer; ?>,<?php echo $totalans; ?>,<?php echo $action; ?>);</script>
<?php
} else if (isset($_POST['minus'])) {
$_SESSION['usertxt'][0] = $_POST['questionbody'];
for ($u = 1; $u <= $answer; $u++) {
$_SESSION['usertxt'][$u] = $_POST[$u]; //echo $_SESSION['usertxt'][$u]." ";
}
?>
<script>deleteAnswer(<?php echo $qid; ?>, '<?php echo $qtitle; ?>',<?php echo $question; ?>,<?php echo $answer; ?>,<?php echo $totalans; ?>,<?php echo $action; ?>);</script>
<?php
}
?>