5
5
//JSON object
6
6
$ jTestG = json_decode (file_get_contents ('php://input ' ), true );
7
7
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
+
8
19
//Values passed.
9
- $ testID = (int )$ jTestG ['testID ' ];
20
+ $ testID = (int )$ jTestG ['testId ' ];
10
21
$ 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
+
11
41
12
42
$ testSaved = 0 ;
13
43
14
44
for ($ i =0 ; $ i <count ($ jTestG ['answers ' ]); $ i ++) {
15
45
//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
+
19
51
//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";
23
55
//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
+ }
25
82
26
83
$ testSaved = 1 ;
27
84
}
48
105
49
106
//close the db connection
50
107
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