<?php session_start();
require_once 'config.php';
require_once 'auth.php';
$PageName = "View Result";
$user_id = $_SESSION['User']['id'];
 $TestId = $_GET['testid'];
  $sql = "SELECT tr.*,tu.Fname,tu.Lname,tu.RollNo,tts.Title As TestName,tts.TestDate As StartDate,tts.TestMarks,tts.CorrectMark,tts.IncorrectMark,tts.UnattemptedMark FROM tbl_sch_result tr 
          LEFT JOIN tbl_users tu ON tr.UserId=tu.id 
          LEFT JOIN tbl_scholarship tts ON tr.TestId=tts.id 
          WHERE tr.TestId='$TestId' AND tr.UserId='$user_id'";
  $row = getRecord($sql);
  $ResultId = $row['id'];
  $UserId = $row['UserId'];

   $sql13 = "SELECT UserId,Marks AS marks_var, ( SELECT COUNT(Marks) + 1 FROM tbl_sch_result WHERE (Marks > marks_var) AND TestId='$TestId' ) AS Ranking FROM tbl_sch_result WHERE TestId='$TestId'";
   $rncnt13 = getRow($sql13);
   $row33 = getList($sql13);
      foreach($row33 as $result){
        if($result['UserId'] == $UserId){
          $Rank = $result['Ranking'];
        }
    }
 ?>
<!doctype html>
<html lang="en" class="h-100">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <meta name="description" content="">
    <meta name="author" content="">
    <meta name="generator" content="">
    <title><?php echo $Proj_Title; ?></title>
    <meta name="apple-mobile-web-app-capable" content="yes">
    <link rel="apple-touch-icon" href="img/favicon180.png" sizes="180x180">
    <link rel="icon" href="img/favicon32.png" sizes="32x32" type="image/png">
    <link rel="icon" href="img/favicon16.png" sizes="16x16" type="image/png">
    <link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css2?family=Roboto:wght@300;400;500&amp;display=swap" rel="stylesheet">
    <link href="vendor/swiper/css/swiper.min.css" rel="stylesheet">
    <link href="css/style.css" rel="stylesheet" id="style">
    <link rel="stylesheet" type="text/css" href="css/style2.css" />
</head>

<body class="body-scroll d-flex flex-column h-100 menu-overlay">
    <main class="flex-shrink-0 main">
        <?php include_once 'back-header.php'; ?> 
        <div class="main-container">
            <div class="container">
        <style>
table, td, th {  
  border: 1px solid #ddd;
  text-align: left;
}
table {
  border-collapse: collapse;
  width: 100%;
}
th, td {
  padding: 10px;
  padding-top: 5px;
}
</style>

<table>
  <tr>
    <th colspan="4"><!-- <p style="text-align: left;">Roll No : <?php echo $row['RollNo'];?></p> --><p style="text-align:center;"><img class="img-fluid" src="logo.png" alt="Yash Academy" style="width: 300px;"></p></th>
  </tr>
 
  <tr>
   
    <td colspan="1"><strong>Test Name : </strong></td>
    <td colspan="3"><?php echo $row['TestName'];?></td>
    
  </tr>
  <tr>
 <td colspan="1"><strong>Test Date : </strong> </td>
 <td colspan="3"><?php echo date("d-m-Y", strtotime(str_replace('-', '/',$row['TestDate'])));?></td>
  </tr>
  <?php 
    $sql2 = "SELECT * FROM tbl_sch_result_answer WHERE ResultId='$ResultId' AND UserId='$UserId' AND Answer=11";
    $rncnt2 = getRow($sql2);

    $sql5 = "SELECT * FROM tbl_sch_result_answer WHERE ResultId='$ResultId' AND UserId='$UserId' 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;

     $sql9 = "SELECT MAX(Marks) As MaxMarks FROM tbl_result WHERE TestId='$TestId'";
     $row9 = getRecord($sql9);

     $sql7 = "SELECT MIN(Marks) As MinMarks FROM tbl_result WHERE TestId='$TestId'";
     $row7 = getRecord($sql7);

     $sql8 = "SELECT SUM(Marks) As SumMarks,Count(id) As Studs FROM tbl_result WHERE TestId='$TestId' AND UserId!=0";
     $row8 = getRecord($sql8);
     $AvgMarks = $row8['SumMarks']/$row8['Studs'];
    ?>
 <tr>
  <td colspan="1"><strong>Total Marks : </strong> </td>
  <td colspan="3"><?php echo $row['Marks']." out of ".$row['OutOfMarks'];?></td>
   
   </tr>
    <tr>
        <td colspan="1" width="30%"><strong style="color:#0C0;font-size:11.5px;">Correct Ques : </strong> <?php echo $TotCorrAns;?></td>
   <td colspan="1" width="30%"><strong style="color:red;font-size:11.5px;">Incorrect Ques : </strong> <?php echo $wrong;?></td>
   <td colspan="1" width="40%"><strong style="color:orange;font-size:11.5px;">Not Attempted : </strong><br> <?php echo $rncnt2;?></td> 
  </tr>
  
</table>       

<div class="scp-quizzes-main">
<?php 
$testid = $_GET['testid'];
$i=1;
$sql = "SELECT * FROM tbl_sch_question_answer WHERE TestSeriesId='$testid'";
$result = getList($sql);
foreach($result as $row){
  $qid = $row['id'];
$sql2 = "SELECT Answer FROM tbl_sch_result_answer WHERE TestId='$testid' AND QuesId='$qid'";
$row2 = getRecord($sql2);
if($row['Type'] == 'Text'){

$Myans = $row2['Answer'];
//$row['Answer'];
if($row['Answer'] == 1){
  $AnsOpt = $row['Option1'];
  $YourOpt1 = "background-image: none;background-color: #0C0;color: #fff;border: 1px solid #0C0 !important;";
$YourOpt2 = "";
  $YourOpt3 = "";
  $YourOpt4 = "";
}
if($row['Answer'] == 2){
  $AnsOpt = $row['Option2'];
  $YourOpt2 = "background-image: none;background-color: #0C0;color: #fff;border: 1px solid #0C0 !important;";
 $YourOpt1 = "";
  $YourOpt3 = "";
  $YourOpt4 = "";
}
if($row['Answer'] == 3){
  $AnsOpt = $row['Option3'];
  $YourOpt3 = "background-image: none;background-color: #0C0;color: #fff;border: 1px solid #0C0 !important;";
 $YourOpt2 = "";
  $YourOpt1 = "";
  $YourOpt4 = "";
}
if($row['Answer'] == 4){
  $AnsOpt = $row['Option4'];
  $YourOpt4 = "background-image: none;background-color: #0C0;color: #fff;border: 1px solid #0C0 !important;";
$YourOpt2 = "";
  $YourOpt3 = "";
  $YourOpt1 = "";
}
if($Myans == 1){
  if($Myans == $row['Answer']){
    $YourOpt1 = "background-image: none;background-color: #0C0;color: #fff;border: 1px solid #0C0 !important;";
  }
  else{
    $YourOpt1 = "background-image: none;background-color: #ff0404;color: #fff;border: 1px solid #ff0404 !important;";
  }
}

if($Myans == 2){
  if($Myans == $row['Answer']){
    $YourOpt2 = "background-image: none;background-color: #0C0;color: #fff;border: 1px solid #0C0 !important;";
  }
  else{
    $YourOpt2 = "background-image: none;background-color: #ff0404;color: #fff;border: 1px solid #ff0404 !important;";
  }
}

if($Myans == 3){
  if($Myans == $row['Answer']){
    $YourOpt3 = "background-image: none;background-color: #0C0;color: #fff;border: 1px solid #0C0 !important;";
  }
  else{
    $YourOpt3 = "background-image: none;background-color: #ff0404;color: #fff;border: 1px solid #ff0404 !important;";
  }
}

if($Myans == 4){
  if($Myans == $row['Answer']){
    $YourOpt4 = "background-image: none;background-color: #0C0;color: #fff;border: 1px solid #0C0 !important;";
  }
  else{
    $YourOpt4 = "background-image: none;background-color: #ff0404;color: #fff;border: 1px solid #ff0404 !important;";
  }
}

//echo $YourOpt1;
  ?>

 <div class="scp-quizzes-data">
   <?php if($row['Type'] == 'Text'){?>
  <h3 style="font-size: 15px;text-align:justify;"><?php echo $i.") ".$row['Question']; ?></h3>
<?php } else{?>
   <img src="../uploads/<?php echo $row['Photo']; ?>" style="height: 250px;">
<?php } ?>
    <input type="radio" id="inculdefile1<?php echo $row["id"]; ?>" disabled>
    <label for="inculdefile1<?php echo $row["id"]; ?>" style="<?php echo $YourOpt1;?>">1. <?php echo $row['Option1']; ?></label>
    <br/>

    <input type="radio"  id="inculdefile2<?php echo $row["id"]; ?>" disabled>
    <label for="inculdefile2<?php echo $row["id"]; ?>" style="<?php echo $YourOpt2;?>">2. <?php echo $row['Option2']; ?></label>
  
   <br/>

    <input type="radio" id="inculdefile3<?php echo $row["id"]; ?>" disabled>
       <label for="inculdefile3<?php echo $row["id"]; ?>" style="<?php echo $YourOpt3;?>">3. <?php echo $row['Option3']; ?></label>
    
        <br/>

    <input type="radio" id="inculdefile4<?php echo $row["id"]; ?>" disabled>
     <label for="inculdefile4<?php echo $row["id"]; ?>" style="<?php echo $YourOpt4;?>">4. <?php echo $row['Option4']; ?></label>
    
 </div>

<!-- <p>Ans :- <strong style="font-size: 17px;">(<?php echo $row['Answer']; ?>) - 
          <?php echo $AnsOpt;?></strong></p> -->
  <p style="font-size: 18px;"><strong>Explaination :- </strong> <?php echo $row['Explaination'];?></p>        
<?php }
else{?>
    <p><img src="../uploads/<?php echo $row['Photo']; ?>" style="width: 500px;height: 250px;"></p>
<hr>
<p>Ans :- <strong style="font-size: 30px;font-style: italic;">(<?php echo $row['Answer']; ?>) </strong></p>
<p><strong>Explaination :- </strong> <?php echo $row['Explaination'];?></p>
<?php } ?><hr>
<?php $i++;}
?>
</div>                  
                 
           
                       
               
            </div>
        </div>
    </main><br><br><br>
 <?php include_once 'footer.php'; ?>
<script src="js/jquery-3.3.1.min.js"></script>
<script src="js/popper.min.js"></script>
<script src="vendor/bootstrap/js/bootstrap.min.js"></script>
<script src="js/jquery.cookie.js"></script>
<script src="vendor/swiper/js/swiper.min.js"></script>
<script src="js/main.js"></script>
<script src="js/color-scheme-demo.js"></script>
<script src="js/app.js"></script>

</body>
</html>
