<?php
ob_start();
require_once('operations.php');
session_start();
$db = new Operations();
$db2 = new Operations();


if(isset($_GET['examcode']) && ($_GET['admissionno'])  && ($_GET['class'])
&& ($_GET['lname'])  && ($_GET['fname'])  ) {
  $examcode =  $_GET['examcode'];
  $admissionno = $_GET['admissionno'];
  $class2 = $_GET['class'];
  $lname = $_GET['lname'];
 $fname =  $_GET['fname'];
  
  
  
  //$db->findExamsById($param);
  
  // $db2->
  
  
  }

  $db->findExamsById($examcode);

  if($db->status  == "closed") {
    ?>
    <script>
    alert('exam is still closed, contact admin to open it');
  window.location.href='studDashboard.php';
  
  </script>
    <?php
  }



  $checkCandidate = $db->checkCountScores($examcode,$admissionno);

 

  if($checkCandidate  >= 1) {
    ?>
    <script>
    alert('You  have already written this exam');
  window.location.href='studDashboard.php';
  
  </script>
    <?php
  }


//$examcode = $_SESSION['examcode'];
$countExam = $db->checkExamsCode($examcode);

//echo $examcode;

//$name = $_SESSION['name'];

$db->findExamsById($examcode);

$duration = $db->duration;

$statement = $db2->getQuestionsByExamID($examcode);

$statement2 = $db->getQuestionsByExamID($examcode);




//$statement = $db2->getQuestionsByExamIDRandom($examcode);

//$statement2 = $db->getQuestionsByExamIDRandom($examcode);


if($countExam < 1) {
  ?>
  <script>
  alert('exam code does not exist');
window.location.href='studDashboard.php';

</script>
  <?php
}

else {

$countQuestion = $db->getCountQuestionsByExamID($examcode);

if($countQuestion < 1) {

  ?>
  <script>
  alert('questions are not ready for this exams');
window.location.href='studDashboard.php';

</script>
  <?php
  
}

else {

 


   $db->findSubjectById($db->subject_id);

  // echo $db->subject_id;


}



}
 ?>

<!DOCTYPE html>
<html>

<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
  <meta name="description" content="cbt app">
  <meta name="author" content="Creative Tim">
  <title>CBT APP</title>
  <!-- Favicon 
  <link rel="icon" href="../assets/img/brand/favicon.png" type="image/png">
  -->
  <!-- Fonts -->
  <script src='https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js'></script>
  <script src="//cdn.jsdelivr.net/npm/sweetalert2@11"></script>
  <link rel="stylesheet" href="style.css" type="text/css"> 
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css" integrity="sha384-rHyoN1iRsVXV4nD0JutlnGaslCJuC7uwjduW9SVrLvRYooPp2bWYgmgJQIXwl/Sp" crossorigin="anonymous">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
  <link href='https://use.fontawesome.com/releases/v5.7.2/css/all.css'>
  
  <style>

div.sticky {
  position: -webkit-sticky;
  position: sticky;
  top: 0;
  background-color: yellow;
  padding: 50px;
  font-size: 20px;
}

</style>
<script>
//prevent enter from submitting form using jquery
$(document).ready(function() {
  $(window).keydown(function(event){
    if(event.keyCode == 13) {
      event.preventDefault();
      return false;
    }
  });
});
</script>
</head>

<body style = "background-color:white">

<div id="quiz-time-left" class="sticky">I will stick to the screen when you reach my scroll position</div>
<h1>Question</h1>
<h5>Choose the correct answer.</h5>
<div style="font-weight: bold" id="quiz-time-left"></div>
<!--get the time by lesson id from table exam -->
<?php
 




  
  

?>
<script type="text/javascript">
var max_time = <?php echo $duration ?>;
var c_seconds  = 0;


var total_seconds =60*max_time;
max_time = parseInt(total_seconds/60);
c_seconds = parseInt(total_seconds%60);
document.getElementById("quiz-time-left").innerHTML='Time Left: ' + max_time + ' minutes ' + c_seconds + ' seconds';
function init(){
document.getElementById("quiz-time-left").innerHTML='Time Left: ' + max_time + ' minutes ' + c_seconds + ' seconds';
setTimeout("CheckTime()",999);
}
function CheckTime(){
document.getElementById("quiz-time-left").innerHTML='Time Left: ' + max_time + ' minutes ' + c_seconds + ' seconds' ;
if(total_seconds <=0){
setTimeout('document.form.submit()',1);
   
    } else
    {
total_seconds = total_seconds -1;
max_time = parseInt(total_seconds/60);
c_seconds = parseInt(total_seconds%60);
setTimeout("CheckTime()",999);
}

}
init();


</script>
<style>
#quiz-time-left{
	color:blue;
	padding:5px;
	font-size:18px;
	
	}
</style>

<div class = "container">
<div class="row">
                    
                    
<div style="overflow-y:auto;"> 



<div  style="color:blue; font:bold; font-size:16; padding:5px;">
<div id="quiz-time-left"></div>
</div>

<div style = "padding:2px">
<?php  echo $db->passage; ?>
</div>

<?php 

$rowCount1 = 0;
while ($row = $statement2->fetch(PDO::FETCH_ASSOC)){
    extract($row);
$rowCount1++;
	
 
?> 



<form method="post" name="form" action="computeEnglish.php" id = "compute" "> 
<div align = "left">
<strong>
<?php echo "(" .$rowCount1 .")" ?>  <?php echo $row['question'] ; ?><input type="hidden" name="id[<?php echo $rowCount1 ?>]" value="<?php echo $row['id'];?>" /></strong> </div>
<div class="pt-4" style = "font-size:14px; margin-top:1px">
<label class="options" style = "font-size:14px; margin-top:0px;"> <?php echo $row['optionA'] ?><input class="radios" type="radio"   name="choices[<?php echo $rowCount1 ?>]" value="A"/><span class="checkmark"></span> </label><br>
<label class="options" style = "font-size:14px; margin-bottom:0px;"> <?php echo $row['optionB'] ?><input class="radios" type="radio"   name="choices[<?php echo $rowCount1 ?>]" value="B"/><span class="checkmark"></span> </label> <br> 
<label class="options" style = "font-size:14px; margin-bottom:0px;"> <?php echo $row['optionC'] ?><input class="radios" type="radio"   name="choices[<?php echo $rowCount1 ?>]" value="C"/><span class="checkmark"></span> </label> <br>
<label class="options" style = "font-size:14px; margin-bottom:0px;"> <?php echo $row['optionD'] ?><input class="radios" type="radio"  name="choices[<?php echo $rowCount1 ?>]" value="D"/> <span class="checkmark"></span> </label> <br>
<div>
<?php }  
	 
	 

?>
</div>

<div style="margin-top: 10px;text-align: center;">
	<input type="hidden" name="examcode" value="<?php echo $examcode ?>">
  <input type="hidden" name="admissionno" value="<?php echo $admissionno ?>">
  <input type="hidden" name="class2" value="<?php echo $class2 ?>">
  <input type="hidden" name="fname" value="<?php echo $fname ?>">
  <input type="hidden" name="lname" value="<?php echo $lname ?>">

	
		<button class="btn btn-md btn-primary" type="submit" name="btnSubmit"  onclick="return confirm('ARE YOU SURE YOU WANT TO SUBMIT?')">Submit  <i class="fa fa-arrow-right"></i></button>
    </div>
</form>

<div>
</div>
<!--  

	<button class="btn btn-md btn-primary" type="submit" name="btnSubmit"   >Submit  <i class="fa fa-arrow-right"></i></button>

-->

<?php

if (isset($_POST['btnSubmit'])){
?>

  <script>

    Swal.fire({
  title: 'Are you sure you want to submit?',
  text: "You won't be able to revert this!",
  icon: 'warning',
  showCancelButton: true,
  confirmButtonColor: '#3085d6',
  cancelButtonColor: '#d33',
  confirmButtonText: 'Yes!',
  timer: 10000
}).then((result) => {
  if (result.isConfirmed) {
   
    document.forms["form"].submit();
    form.action = "compute.php";
    document.form.action = "compute.php";
    
  }
})


    </script>
<?php

}
?>


</body>

</html>