-
Notifications
You must be signed in to change notification settings - Fork 0
/
maketest.php
596 lines (429 loc) · 23.7 KB
/
maketest.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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
<?php
// initialize modules on start
require 'php/auth-mods/auth-login.php';
require 'php/mod_conn.php';
$req_type = $_GET['request'];
// test credentials for update
$test_ID;
$test_owner;
// variables for test credentials from the DB
$testName;
$test_type;
$current_testID;
$current_test_chapter;
if($req_type == "update"){
$test_ID = $_GET['testID'];
$test_owner = $_GET['owner'];
// fetch test credentials from the DB and set the test credential values
$q_db_testCredentials = mysqli_query($conn,
"SELECT test_ID, test_name as testName, test_type, test_chapter FROM tests WHERE tests.test_ID = $test_ID AND test_staffAuthor = $test_owner
") or die (mysqli_error($conn));
$r_db_testCredentials = mysqli_fetch_assoc($q_db_testCredentials);
$testName = $r_db_testCredentials['testName'];
$testType = $r_db_testCredentials['test_type'];
$current_testID = $r_db_testCredentials['test_ID'];
$current_test_chapter = $r_db_testCredentials['test_chapter'];
}
?>
<html>
<header>
<title>Make Test</title>
<link href="css/bootstrap.css" rel="stylesheet" type="text/css"/>
<link href="css/global-style.css" rel="stylesheet" type="text/css"/>
<link href="css/class-search.css" rel="stylesheet" type="text/css"/>
<script src = "js/jquery.js"></script>
<script src = "js/functions.js"></script>
<style>
#question_item{
border-style: solid;
border-color: black;
border-width: thin;
padding: 20px;
box-shadow: 0px 0px 10px 2px silver;
background-color: ghostwhite;
}
#question_container{
min-height:620px;
max-height: 620px;
overflow-y: auto;
position: relative;
}
</style>
</header>
<body>
<div><?php include 'widgets/navigation.php';?></div>
<div><?php include 'widgets/logged_user.php';?></div>
<!-- Main contnainer -->
<div style = "margin: 0 auto; height: 1050px;" class="col-lg-6 col-lg-offset-3" id="container">
<center><h1><?php if($req_type == "update"){echo "Update";}else{echo "Make";}?> Test</h1></center>
<div>
<!--Form redirect-->
<?php
if($req_type == "create"){
echo "<form action = \"php/mod_insert_test.php\" method = \"post\">";}
else{
echo "<form action = \"php/mod_update_test.php?testID=$test_ID&owner=$test_owner\" method = \"post\">";
}
?>
<div class = "form-group">
<input onkeyup = "textOnly(this)" required class = "form-control" type = "text" name = "test-name" placeholder = "Test Name" <?php if($req_type == "update"){echo "value = \"".$testName."\"";}?>>
<div class="input-group mb-3">
<input onkeyup = "numericOnly(this)" id = "question_count_value" type="text" class="form-control" placeholder="Question item count" aria-label="Question item count" aria-describedby="basic-addon2">
<div class="input-group-append">
<button id = "question_create_count" class="btn btn-warning" type="button">Create</button>
</div>
</div>
</div>
<!--Test question items goes here-->
<div id = "question_container">
<?php
// query each test question items if request type is update
if($req_type == "update"){
$q_questionItems = mysqli_query($conn,
"SELECT * FROM `questions` WHERE questions.question_testID = $current_testID
") or die(mysqli_error($conn));
while($r_questionItems = mysqli_fetch_assoc($q_questionItems)){
$questionValues = explode(":", $r_questionItems['question_formattedQuestion']);
//print_r($questionValues);
if($questionValues[1] == "mc"){
dbQuestion_panels($r_questionItems['question_id'], $questionValues[1], $questionValues[2], $questionValues[6], array(substr($questionValues[3],2),substr($questionValues[4],2), substr($questionValues[5],2)));
}else{
dbQuestion_panels($r_questionItems['question_id'], $questionValues[1], $questionValues[2], $questionValues[5]);
}
}
}
?>
</div>
<div hidden id = "formatted_question"><!--Formatted questions goes here--></div>
<div hidden id = "questionID"><!--Question IDs goes here--></div>
<input hidden name = "testID" value = "<?php echo $test_ID;?>">
<!--Update test-->
<h4><?php if($req_type == "update"){echo "Update Test";}else{echo "Publish Test";}?></h4>
<hr style = "margin: 10px;">
<div id = "test_options" class="form-group">
<div class="form-check">
<?php
if($_SESSION['user_account_level'] == 1){
$test_state;
if($req_type == "update"){
if($testType == "Built-in"){
$test_state = "checked";
echo '<input class="form-check-input" onclick = "marked_builtin(this)" name = "test-type" type="checkbox" value="built-in" id="invalidCheck2" '.$test_state.'>';
}else{
echo '<input class="form-check-input" onclick = "marked_builtin(this)" name = "test-type" type="checkbox" value="built-in" id="invalidCheck2">';
}
echo '
<label class="form-check-label" for="invalidCheck2">Built-in test</label>
';
}else{
echo '<input class="form-check-input" onclick = "marked_builtin(this)" name = "test-type" type="checkbox" value="built-in" id="invalidCheck2">';
echo '
<label class="form-check-label" for="invalidCheck2">Built-in test</label>
';
}
}
?>
<div id = "test_options">
<?php
if($req_type == "update" && $testType == "Built-in"){
echo '<input onkeyup = "numericOnly(this)" name = "chapter" style = "text-align: center;" required type="text" class="form-control" placeholder="Chapter no." aria-label="Chapter no." aria-describedby="basic-addon2" value = "'.$current_test_chapter.'">';
}
?>
</div>
</div>
<!--<div id = "upload_video">
<hr>
<h4>Select a video lesson</h4>
<hr>
<input type = "file" accept = ".mp4, .dv, .m4v, .mov, .mp4, .mpg, .mpeg, .ogv, .vp8, .webm, .wmv" name = "video_file">
</div>-->
</div>
<center><button style = "width: 100%;" id = "btnPublish" class="<?php if($req_type == "update"){echo "btn btn-warning";}else{echo "btn btn-primary";}?>" type="submit"><?php if($req_type == "update"){echo "Update";}else{echo "Publish";}?></button></center>
</form>
</div>
</div>
</body>
<?php
// panels
function dbQuestion_panels($question_ID, $questionType, $questionText, $questionAnswer, $mcValues = array()){
// question panel vars
$cmb_qType;
$txt_questionValues;
$answer_value;
// set combobox
if($questionType == "mc" && count($mcValues) >= 0){
// question type combo box
$cmb_qType =
'<span class = "badge badge-secondary">Question Type</span>'.
'<span class = "badge badge-success">Registered</span>'.
'<select required onchange = "question_values(this.value, this)" style = "width: 200px;" id = "test-type" class = "form-control">'.
'<option value = "mc" selected>Multiple choice</option>'.
'<option value = "tf">True or False</option>'.
'</select>';
// question values concatinated
$txt_questionValues =
'<div id = "mc_value" class="col">'.
'<input onkeyup = "makeTestCharFilter(this)" required type="text" class="form-control" value = "'.trim(htmlspecialchars($mcValues[0])).'" name = "choice_a" placeholder="Choice value for A" style = "text-align: center">'.
'</div>';
$txt_questionValues .=
'<div id = "mc_value" class="col">'.
'<input onkeyup = "makeTestCharFilter(this)" required type="text" class="form-control" value = "'.trim(htmlspecialchars($mcValues[1])).'" name = "choice_b" placeholder="Choice value for B" style = "text-align: center">'.
'</div>';
$txt_questionValues .=
'<div id = "mc_value" class="col">'.
'<input onkeyup = "makeTestCharFilter(this)" required type="text" class="form-control" value = "'.trim(htmlspecialchars($mcValues[2])).'" name = "choice_c" placeholder="Choice value for C" style = "text-align: center">'.
'</div>';
//set answer values default the answers from the current test
$answer_value =
'<span class = "badge badge-success">Question Answer</span>'.
'<select class = "form-control" id = "q_answer">';
switch($questionAnswer){
case "A":
$answer_value .=
'<option value = "A" selected>A</option>'.
'<option value = "B">B</option>'.
'<option value = "C">C</option>';
break;
case "B":
$answer_value .=
'<option value = "A">A</option>'.
'<option value = "B" selected>B</option>'.
'<option value = "C">C</option>';
break;
case "C":
$answer_value .=
'<option value = "A">A</option>'.
'<option value = "B">B</option>'.
'<option value = "C" selected>C</option>';
break;
}
$answer_value .= '</select>';
}else{
// question type combo box
$cmb_qType =
'<span class = "badge badge-secondary">Question Type</span>'.
'<span class = "badge badge-success">Registered</span>'.
'<select required onchange = "question_values(this.value, this)" style = "width: 200px;" id = "test-type" class = "form-control">'.
'<option value = "mc">Multiple choice</option>'.
'<option value = "tf" selected>True or False</option>'.
'</select>';
$txt_questionValues = "";
//set answer values default the answers from the current test
$answer_value =
'<span class = "badge badge-success">Question Answer</span>'.
'<select class = "form-control" id = "q_answer">';
switch($questionAnswer){
case "T":
$answer_value .=
'<option value = "T" selected>True</option>'.
'<option value = "F">False</option>';
break;
case "F":
$answer_value .=
'<option value = "T">True</option>'.
'<option value = "F" selected>False</option>';
break;
}
$answer_value .= '</select>';
}
echo $questionPanel =
'<div id = "question_item" class = "form-group">'.
'<hr size = "30" style = "margin: 0 0 0 0;">'.
'<input id = "deleteMark" value = "'.$question_ID.'" onclick = "markDelete_handler(this)" type="checkbox" class="close" aria-label="Close">'.
'<span aria-hidden="true">×</span>'.
'</button>'.
$cmb_qType.
'<div class="input-group mb-3">'.
'<div class="input-group-prepend">'.
'<span class="input-group-text" id="basic-addon3">Question</span>'.
'</div>'.
'<input onkeyup = "makeTestCharFilter(this)" required type="text" class="form-control" id="txt_question" aria-describedby="basic-addon3" value = "'.trim(htmlspecialchars($questionText)).'">'.
'<input hidden required type="text" name = "questionID[]" class="form-control" id="txt_ID" aria-describedby="basic-addon3" value = "'.$question_ID.'">'.
'</div>'.
'<div id = "questionValue" class="form-row">'.$txt_questionValues.'</div>'.
'<div id = "questionAnswer">'.$answer_value.'</div>'.
'</div>';
}
?>
<script>
var question_count = 0;
$(document).ready(function(){
var element =
'<div id = "question_item" class = "form-group">'+
'<hr size = "30" style = "margin: 0 0 0 0;">'+
'<button onclick = "remove_question(this)" id = "delete" type="button" class="close" aria-label="Close">'+
'<span aria-hidden="true">×</span>'+
'</button>'+
'<span class = "badge badge-secondary">Question Type</span>'+
'<select required onchange = "question_values(this.value, this)" style = "width: 200px;" id = "test-type" class = "form-control">'+
'<option value = "" disabled selected>Question Type</option>'+
'<option value = "mc">Multiple choice</option>'+
'<option value = "tf">True or False</option>'+
'</select>'+
'<div class="input-group mb-3">'+
'<div class="input-group-prepend">'+
'<span class="input-group-text" id="basic-addon3">Question</span>'+
'</div>'+
'<input onkeyup = "makeTestCharFilter(this)" required type="text" class="form-control" id="txt_question" aria-describedby="basic-addon3">'+
'</div>'+
'<div id = "questionValue" class="form-row"></div>'+
'<div id = "questionAnswer"></div>'
'</div>';
$('#question_create_count').click('load', function(){
var questioncount = $('#question_count_value').val();
var questionItemCount = $('#question_container').children().length;
var totalquestions = questionItemCount + parseInt(questioncount);
if(totalquestions <= 100){
for(var i = 0; i < questioncount; i++){
$('#question_container').append(element);
}
}else if(questioncount == ""){
alert("Please fill in the desired number of question items.");
}else{
var remaining = 100 - questionItemCount;
alert("Maximum test question item count is limited to 100! You can only add "+remaining+" question/s left.");
}
console.log(questionItemCount);
});
});
// wip
function marked_builtin(ele){
var confirmation;
var txtbox_chapter = '<input onkeyup = "numericOnly(this)" name = "chapter" style = "text-align: center;" required type="text" class="form-control" placeholder="Chapter no." aria-label="Chapter no." aria-describedby="basic-addon2">'
if($(ele).prop('checked') == true){
confirmation = confirm("Make this test built-in inside the game?");
if(!confirmation){
$(ele).prop('checked', false);
$(ele).parent().children('#test_options').children().remove();
}else{
$(ele).parent().children('#test_options').append(txtbox_chapter);
}
}else{
// remove or set elements to hidden
$(ele).parent().children('#test_options').children().remove();
}
}
// test item delete marker
function markDelete_handler(ele){
var confirmation;
if($(ele).prop('checked') == true){
confirmation = confirm("Mark this question for deletion?");
// change color as a representation of "Marked for deletion"
$(ele).parent('#question_item').css("background-color", "#e74c3c");
}else{
// change color to default if not marked
$(ele).parent('#question_item').css("background-color", "ghostwhite");
}
if(!confirmation){
$(ele).prop('checked', false);
$(ele).parent('#question_item').css("background-color", "ghostwhite");
}
}
function remove_question(ele){
var confirmation = confirm("Delete this item?");
if(confirmation){
$(ele).parent('#question_item').remove();
}else{
return false;
}
}
function question_values(value, ele){
//control clean up
$(ele).parent('#question_item').children("#questionValue").children('#mc_value').remove();
$(ele).parent('#question_item').children("#questionAnswer").children('span, #q_answer').remove();
var mc_a =
'<div id = "mc_value" class="col">'+
'<input onkeyup = "makeTestCharFilter(this)" required type="text" class="form-control" name = "choice_a" placeholder="Choice value for A" style = "text-align: center">'+
'</div>';
var mc_b =
'<div id = "mc_value" class="col">'+
'<input onkeyup = "makeTestCharFilter(this)" required type="text" class="form-control" name = "choice_b" placeholder="Choice value for B" style = "text-align: center">'+
'</div>';
var mc_c =
'<div id = "mc_value" class="col">'+
'<input onkeyup = "makeTestCharFilter(this)" required type="text" class="form-control" name = "choice_c" placeholder="Choice value for C" style = "text-align: center">'+
'</div>';
var tf_answer =
'<span class = "badge badge-success">Question Answer</span>'+
'<select class = "form-control" id = "q_answer">'+
'<option value = "T">True</option>'+
'<option value = "F">False</option>'+
'</select>';
var mc_answer =
'<span class = "badge badge-success">Question Answer</span>'+
'<select class = "form-control" id = "q_answer">'+
'<option value = "A">A</option>'+
'<option value = "B">B</option>'+
'<option value = "C">C</option>'+
'</select>';
if(value == "mc"){
$(ele).parent('#question_item').children("#questionValue").append(mc_a+mc_b+mc_c);
$(ele).parent('#question_item').children("#questionAnswer").append(mc_answer);
}else if(value == "tf"){
$(ele).parent('#question_item').children("#questionAnswer").append(tf_answer);
}
}
$('#btnPublish').on('click', function(){
var questionItemCount = $('#question_container').children('#question_item').length;
if(questionItemCount > 0){
// clean up
$(this).parent().parent().find('#formatted_question').children('input').remove();
var confirmation = confirm("Confirm publication?");
var question_number = 0;
var formattedText;
if(confirmation){
// store ID of marked questions into q_IDs array and send the array to mod_update.php
var q_IDs = [];
$('#question_container').children('#question_item').find('#deleteMark').map(function(){
if($(this).prop('checked') == true){
q_IDs.push($(this).val()); // push/add IDs into the array
}
});
for(let i = 0; i < q_IDs.length; i++){
$(this).parent().parent().find('#questionID').append('<input type = "text" name = "markedQuestions[]" value = "'+q_IDs[i]+'">');
}
// loop through each question item and parse the values for insert
$('#question_container').children('#question_item').each(function (q_index){
// delete marked panels
var deleteMarker = $(this).find('#deleteMark').prop('checked');
if(deleteMarker){
$(this).find('#deleteMark').parent().remove();
}
// set and parse question values
var test_mode = $(this).find('#test-type').val();
var question = $(this).find('#txt_question').val();
var question_answer = $(this).find('#questionAnswer').children('#q_answer').val();
//choices
var choices;
if(test_mode == "mc"){
choices = $(this).children('#questionValue').find('input[name ^= "choice"]').map(function(index, elem){
var letter = String.fromCharCode(64 + (index+1));
return letter+". "+$(this).val();
}).get().join(':');
}else{
choices = "True:False"
}
console.log(choices);
formattedText = q_index+":"+test_mode+":"+question+":"+choices+":"+question_answer;
$(this).parent().parent().find('#formatted_question').append('<input type = "text" name = "formatted_question[]" value = "'+htmlspecialchars(formattedText)+'">');
});
}else{
return false;
}
}else{
alert("You must make at least 1 question for this test!");
return false;
}
function htmlspecialchars(str) {
if (typeof(str) == "string") {
str = str.replace(/&/g, "&"); /* must do & first */
str = str.replace(/"/g, """);
str = str.replace(/'/g, "'");
str = str.replace(/</g, "<");
str = str.replace(/>/g, ">");
}
return str;
}
});
</script>
</html>