Skip to content

Commit beb2e72

Browse files
authored
Update submitTest.php
1 parent ab72c91 commit beb2e72

File tree

1 file changed

+72
-9
lines changed

1 file changed

+72
-9
lines changed

BACKEND/submitTest.php

Lines changed: 72 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,80 @@
55
//JSON object
66
$jTestG = json_decode(file_get_contents('php://input'), true);
77

8+
/*
9+
//Log File for inputs
10+
$myfile = fopen("jamesLog.txt", "w") or die("Unable to open file!");
11+
//$txt = "John Doe\n";
12+
$txt = "Received from middle tier: " . $jTestG . "\n";
13+
fwrite($myfile, $txt);
14+
fclose($myfile);
15+
//end log file
16+
*/
17+
18+
819
//Values passed.
9-
$testID = (int)$jTestG['testID'];
20+
$testID = (int)$jTestG['testId'];
1021
$userID = (int)$jTestG['studentId'];
22+
23+
//Check if test exists for given user. If so, drop it and continue to add it again.
24+
//SQL query tu run against the DB.
25+
$sqlValidation = "SELECT * FROM TblTestGrading WHERE TestID = ".$testID." AND UserID=".$userID;
26+
27+
//Get Result
28+
$res = mysqli_query($connection, $sql) or die("Failed to register user in database " . mysql_error($connection));
29+
30+
//Check for returned results
31+
if (mysqli_num_rows($res) > 0){
32+
//SQL query tu run against the DB.
33+
$sqlDelete = "DELETE FROM TblTestGrading WHERE TestID = ".$testID." AND UserID=".$userID;
34+
35+
//Get Result
36+
$res = mysqli_query($connection, $sqlDelete) or die("Failed to delete test " .mysqli_error($connection));
37+
}
38+
39+
//// End validation
40+
1141

1242
$testSaved = 0;
1343

1444
for($i=0; $i<count($jTestG['answers']); $i++) {
1545
//Get Values
16-
$questionID = (int)$jTestG['answers'][$i]["questionID"];
17-
$answer = $jTestG['answers'][$i]["answer"];
18-
46+
$questionID = (int)$jTestG['answers'][$i]['questionId'];
47+
$answer = $jTestG['answers'][$i]['answer'];
48+
$grade = $jTestG['answers'][$i]['grade'];
49+
//$gradeExplanation = clean($jTestG['answers'][$i]['gradeExplanation']);
50+
1951
//SQL query tu run against the DB for INSERT.
20-
$sql = "INSERT INTO TblTestGrading (QuestionID, TestID, UserID, StudentAns)
21-
VALUES('".$questionID."', '".$testID."','".$userID."','".$answer."')";
22-
52+
$sql = "INSERT INTO TblTestGrading (TestID, UserID, QuestionID, StudentAns, Grade) VALUES(".$testID.",".$userID.",".$questionID.",'".$answer."',".$grade.")";
53+
54+
//echo $questionID . "-" . $testID . "-" . $userID . "-" . $answer . "-" . $grade , "\n";
2355
//Get Result
24-
$res = mysql_query($sql) or die("Failed to save test taken " .mysql_error());
56+
$res2 = mysqli_query($connection, $sql) or die("Failed to save test taken " . mysql_error($connection));
57+
58+
//Save each testCase
59+
for($k=0; $k < count($jTestG['answers'][$i]['gradeExplanation']); $k++) {
60+
//$testInput = clean($jTestG['answers'][$i]['gradeExplanation'][$k]['testInput']);
61+
//$corOutput = clean($jTestG['answers'][$i]['gradeExplanation'][$k]['correctOutput']);
62+
//$stdOutput = clean($jTestG['answers'][$i]['gradeExplanation'][$k]['studentOutput']);
63+
$testInput = 'squared(8)';
64+
$corOutput = '64';
65+
$stdOutput = '64';
66+
$correct = (int)$jTestG['answers'][$i]['gradeExplanation'][$k]['correct'];
67+
68+
//SQL query tu run against the DB for INSERT.
69+
$sqlTC = "INSERT INTO TblTestGradingTC (TestID, UserID, QuestionID, TestCase, CorrectOuput, StudentOutput, correct) VALUES(".$testID.",".$userID.",".$questionID.",'".$testInput."','".$corOutput."','".$stdOutput."',".$correct.")";
70+
71+
echo stripslashes($sqlTC);
72+
73+
//Get Result
74+
$resTC = mysqli_query($connection, $sqlTC) or die("Failed to save test cases for test taken " . mysql_error($connection));
75+
76+
77+
78+
if (!$resTC) {
79+
echo('Invalid query: ' . mysql_error());
80+
}
81+
}
2582

2683
$testSaved = 1;
2784
}
@@ -48,4 +105,10 @@
48105

49106
//close the db connection
50107
mysqli_close($connection);
51-
?>
108+
109+
function clean($string) {
110+
$string = str_replace(' ', '', $string); // Replaces all spaces with hyphens.
111+
//return $string;
112+
return preg_replace('/[^A-Za-z0-9\-]/', '', $string); // Removes special chars.
113+
}
114+
?>

0 commit comments

Comments
 (0)