sendExam.php 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. <?php
  2. $root = $_SERVER['DOCUMENT_ROOT'];
  3. include($root . "/util/session.php");//checks that the user is logged in
  4. include($root . "/util/privilege_check.php");
  5. checkPrivilege("student");
  6. if($_SERVER["REQUEST_METHOD"] == "POST"){
  7. $questions = (unserialize(base64_decode($_POST['selectedQuestions']))); # questions randomly chosen in the exam
  8. #echo $_SESSION['login_user'] . ' ';
  9. #echo 'First question is ' . $questions[0];
  10. #print_r($_POST);
  11. $username=mysqli_real_escape_string($db,$_SESSION['login_user']);
  12. $questionNumber = mysqli_real_escape_string($db,$_POST['questionNumber']);
  13. $subjectID = mysqli_real_escape_string($db,$_POST['actualSubject']);
  14. $chapterID = mysqli_real_escape_string($db,$_POST['actualChapter']);
  15. $score = 0;
  16. $maxPoints = 0;
  17. for($i = 0; $i < $_POST['questionNumber']; $i++){
  18. $question = mysqli_real_escape_string($db,$questions[$i]); # iterates through the selected questions
  19. $index = $i+1;
  20. $studentAnswer = 'studentAnswer' . $index;
  21. $answer = mysqli_real_escape_string($db,$_POST[$studentAnswer]);
  22. $query = "SELECT * FROM questions WHERE questionID = '$question'";
  23. $result = mysqli_query($db,$query);
  24. $row = mysqli_fetch_assoc($result);
  25. if($row['questionType'] == "TF"){
  26. $correctAnswer = $row['answerTrueFalse'];
  27. }else{
  28. $correctAnswer = $row['answerABCD'];
  29. }
  30. $results_info_sql_query = "INSERT INTO `results_info` (`questionID`, `subjectID`, `chapterID`, `studentUser`, `studentAnswer`, `correctAnswer`) VALUES ('$question', '$subjectID', '$chapterID', '$username', '$answer', '$correctAnswer')";
  31. $results_info_result = mysqli_query($db,$results_info_sql_query);
  32. if($answer == $correctAnswer){
  33. $score = $score+$row['correctPoints'];
  34. }else{
  35. $score = $score-$row['assHolePoints'];
  36. }
  37. $maxPoints = $maxPoints + $row['correctPoints'];
  38. }
  39. if($score < 0){ # can't have negative score
  40. $score = 0;
  41. }
  42. $results_sql_query = "INSERT INTO `results` (`subjectID`, `chapterID`, `studentUser`, `score`, `maxPoints`) VALUES ('$subjectID', '$chapterID', '$username', '$score', '$maxPoints')";
  43. $results_result = mysqli_query($db,$results_sql_query);
  44. }
  45. ?>
  46. <html>
  47. <head>
  48. <title>Ended exam</title>
  49. <link rel="stylesheet" type="text/css" href="studentStyle.css">
  50. </head>
  51. <body>
  52. <?php
  53. include($root . "/student/header.php")
  54. ?>
  55. <h1>Your exam has ended!</h1>
  56. <li><a href="/student/student.php">Go to main menu</a></li>
  57. <?php
  58. include($root . "/student/footer.php")
  59. ?>
  60. </body>
  61. </html>