 <script src="https://cdnjs.cloudflare.com/ajax/libs/sweetalert/1.1.3/sweetalert-dev.js"></script>
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/sweetalert/1.1.3/sweetalert.css">
<?php 
session_start();
include_once 'config.php';
include_once 'auth.php';
$user_id = $_SESSION['User']['id'];
  $keys   = $_POST['id'];
   $TestId = $_POST['TestId'];
   $DeptId = $_POST['DeptId'];
   $CourseId = $_POST['CourseId'];
   $StudId = $_POST['StudId'];
   $Answer = $_POST['Options'];
   $CorrectAns = $_POST['CorrectAns'];
   $option1 = $_POST['Option11'];
   $option2 = $_POST['Option22'];
   $option3 = $_POST['Option33'];
   $option4 = $_POST['Option44'];
   $subid = $_POST['subid'];
   $CreatedDate = date('Y-m-d');
   $CreatedTime = date('h:i a');

   $sql22 = "INSERT INTO tbl_sch_result SET UserId='$StudId',TestId='$TestId',DeptId='$DeptId',CourseId='$CourseId',Status='1',TestDate='$CreatedDate',CreatedDate='$CreatedDate',CreatedTime='$CreatedTime'";
   $conn->query($sql22);
   $ResultId = mysqli_insert_id($conn);
   $result = array();
      foreach ($keys as $id => $key) {
          $result[$key] = array(
              '0' => $keys[$id],
              '1' => $Answer[$id],
              '2' => $CorrectAns[$id],     
              '3' => $option1[$id], 
              '4' => $option2[$id],
              '5' => $option3[$id],  
              '6' => $option4[$id],   
              '7' => $subid[$id],
          ); 
      }

      if(is_array($result))
          {
              foreach($result as $row => $value)
              {
                $QuesId = $value[0];
                $Answer = $value[1];
                $CorrectAns = $value[2];
                $Option1 = $value[3];
                $Option2 = $value[4];
                $Option3 = $value[5];
                $Option4 = $value[6];
                $SubjectId = $value[7];
                 if($Answer == ''){
                  $GivenAns = 11;
                 }
                 else{
                  $GivenAns = $Answer;
                 }
               $sql22 = "INSERT INTO tbl_sch_result_answer SET ResultId='$ResultId',UserId='$StudId',TestId='$TestId',DeptId='$DeptId',CourseId='$CourseId',QuesId='$QuesId',SubjectId='$SubjectId',Answer='$GivenAns',CorrectAns='$CorrectAns',CreatedDate='$CreatedDate',CreatedTime='$CreatedTime'";
                $conn->query($sql22);
               }
          } 

    $sql2 = "SELECT * FROM tbl_sch_result_answer WHERE ResultId='$ResultId' AND UserId='$StudId' AND Answer=11";
    $rncnt2 = getRow($sql2);

    $sql5 = "SELECT * FROM tbl_sch_result_answer WHERE ResultId='$ResultId' AND UserId='$StudId' AND Answer!=11";
    $rncnt5 = getRow($sql5);

    $TotCorrAns = 0;
    $sql3 = "SELECT Answer,id FROM tbl_sch_question_answer WHERE TestSeriesId='$TestId'";
    $rncnt3 = getRow($sql3);
    $row3 = getList($sql3);
    foreach($row3 as $result){
      $QuesId = $result['id'];
      $CorrAns = $result['Answer'];
      $sql4 = "SELECT * FROM tbl_sch_result_answer WHERE QuesId='$QuesId' AND ResultId='$ResultId' AND Answer!=11";
      $row4 = getRecord($sql4);
      $GiveAnswer = $row4['Answer'];
       //echo $CorrAns." - ".$GiveAnswer."&nbsp;";
      if($CorrAns == $GiveAnswer){
       
           $TotCorrAns = $TotCorrAns + 1;
      }
    }
     $wrong = $rncnt5 - $TotCorrAns;

     $sql6 = "SELECT * FROM tbl_scholarship WHERE id='$TestId'";
     $row6 = getRecord($sql6);
     $CorrectPts = $row6['CorrectMark'];
     $IncorrectPts = $row6['IncorrectMark'];
     $UnattemptedPts = $row6['UnattemptedMark'];
     $tot_result = ($TotCorrAns* $CorrectPts) + ($wrong*$IncorrectPts) + ($rncnt2*$UnattemptedPts);
     $outofmarks = $rncnt3*$CorrectPts;
     $percentage = ($tot_result/$outofmarks) * 100;

     $sql7 = "UPDATE tbl_sch_result SET Marks='$tot_result',OutOfMarks='$outofmarks',Percentage='$percentage' WHERE TestId='$TestId' AND UserId='$StudId'";
     $conn->query($sql7);
     unset($_SESSION['StartTime']);
mysqli_close($conn);

?>

<script type="text/javascript">
 window.location.href="show-sch-result-popup.php?testid=<?php echo $TestId;?>&userid=<?php echo $StudId;?>&resultid=<?php echo $ResultId;?>";
 </script>
       


