<?php

//view_subject_exam.php

include('../admin/orbital_001/Soes.php');

$object = new soes();

if(isset($_GET['del']))
{
$id= $_GET['del']; 
$sql =mysqli_query($conn,"DELETE FROM `student_to_class_soes` WHERE student_to_class_id = $id");
header('location:admin.php?page=booking_tr"');
}

// if(!$object->is_student_login())
// {
//     header("location:".$object->base_url."");
// }

$exam_id = '';
$exam_title = '';
$class_id = '';
$exam_duration = '';

$subject_id = '';
$exam_subject_id = '';
$total_question = '';
$marks_per_right_answer = '';
$marks_per_wrong_answer = '';
$subject_exam_start_time = '';
$subject_exam_end_time = '';
$remaining_minutes = '';
$subject_exam_status = '';

$student_name = '';
$student_roll_no = '';
$student_image = '';

if(isset($_SESSION['ec']))
{
    $object->query = "
    SELECT * FROM exam_soes 
    WHERE exam_code = '".$_SESSION['ec']."';
    ";

    $result = $object->get_result();

    foreach($result as $row)
    {
        $exam_id = $row["exam_id"];
        $exam_title = $row["exam_title"];
        $class_id = $row["exam_class_id"];
        $exam_duration = $row["exam_duration"];
    }
}
else
{
    header('location:view_exam.php');
}

if(isset($_SESSION["esc"]))
{
    $object->query = "
    SELECT * FROM subject_wise_exam_detail 
    WHERE subject_exam_code = '".$_SESSION['esc']."';
    ";

    $result = $object->get_result();

    foreach($result as $row)
    {
        $subject_id = $row["subject_id"];
        $exam_subject_id = $row["exam_subject_id"];
        $total_question = $row["subject_total_question"];
        $marks_per_right_answer = $row["marks_per_right_answer"];
        $marks_per_wrong_answer = $row["marks_per_wrong_answer"];
        $subject_exam_start_time = $row["subject_exam_datetime"];
        $subject_exam_end_time = strtotime($subject_exam_start_time . '+' . $exam_duration . ' minute');
        $subject_exam_end_time = date('Y-m-d H:i:s', $subject_exam_end_time);
        $total_second = strtotime($subject_exam_end_time) - strtotime($subject_exam_start_time);
        $remaining_minutes = strtotime($subject_exam_end_time) - time();
        $subject_exam_status = $row["subject_exam_status"];
    }
}
else
{
    header('location:view_exam.php');
}

$object->query = "
SELECT student_soes.student_name, student_soes.student_image, student_to_class_soes.student_roll_no FROM student_to_class_soes 
INNER JOIN student_soes 
ON student_soes.student_id = student_to_class_soes.student_id 
WHERE student_to_class_soes.student_id = '".$_SESSION["student_id"]."' 
ORDER BY student_to_class_soes.student_to_class_id DESC 
LIMIT 1 
";

$result = $object->get_result();

foreach($result as $row)
{
    $student_name = $row["student_name"];
    $student_roll_no = $row["student_roll_no"];
    $student_image = str_replace("../", "", $row["student_image"]);
}
 

if(time() > strtotime($subject_exam_end_time)){
    header('location:exam.php');
}

include ('../admin/includes/connection.php');  
include('sms.php');
if(isset($_GET['st_id']))
{
$Sid= $_GET['st_id']; 
$Eid= $_GET['ex_id'];

$student= mysqli_query($conn,"SELECT  SUM(marks) AS net_marks FROM `exam_subject_question_answer`,`exam_subject_question_soes` WHERE exam_subject_question_answer.exam_subject_question_id=exam_subject_question_soes.exam_subject_question_id AND exam_subject_question_soes.exam_id='$Eid' AND exam_subject_question_soes.exam_subject_id='$subject_id' AND exam_subject_question_answer.student_id='".$_SESSION["student_id"]."';")or die(mysqli_error());
$row2 = mysqli_fetch_array($student);
$check_net_marks=$row2['net_marks']; 

$query1= mysqli_query($conn,"SELECT * FROM student_soes WHERE student_id='".$_SESSION["student_id"]."'")or die(mysqli_error());
$row1 = mysqli_fetch_array($query1);
 $student_name = $row1['student_name']; 
 $s_mobile = $row1['student_contact_no']; 
 $student_id = $row1['student_id']; 
 $course = $row1['course']; 
 $student_email_id = $row1['student_email_id']; 
 
                
 $query=mysqli_query($conn,"INSERT INTO `sms_result`(`student_id`,`student_roll_no`, `exam_id`, `class_id`, `subject_id`, `total_marks`) VALUES ('$student_id','$Sid','$Eid','$class_id','$subject_id','$check_net_marks')");
 if($query){ 
        $s_mobile = substr($s_mobile, 1);
        $s_mobile = "255$s_mobile";
        $message = "Cybercode: Dear $student_name Congraturation for submition of Your exam.Your Result will be submited to your Email acount, Please check a while";
        send_message($s_mobile, $message);
         echo "<script type=\"text/javascript\">
            alert(\"Your Success fully Submit The Exam.\");
            window.location = \"../admin.php?page=dashboard&Id_2=$course\"
          </script>";   
      }  
      else{
        ?>  
        <script type="text/javascript">
         alert("Sorry Examination Failed to Submit");
        </script>
        <?php
    } 
 
 }                
?>
<!DOCTYPE html>
<html lang="en">
<head>
  <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
  <meta name="description" content="">
  <meta name="author" content="">
  <title>Cybercode Africa| Cybersecurity Training and resources</title> 
  <meta content="" name="keywords">

  <!-- Favicons -->
  <link href="../assets/img/icon1.png" rel="icon">
  <link href="../assets/img/icon1.png" rel="apple-touch-icon"> 
  <!-- DataTables -->
  <link rel="stylesheet" href="../stp_con/datatables-bs4/css/dataTables.bootstrap4.min.css">
  <link rel="stylesheet" href="../stp_con/datatables-responsive/css/responsive.bootstrap4.min.css">
  <link rel="stylesheet" href="../stp_con/datatables-buttons/css/buttons.bootstrap4.min.css"> 
  <link rel="stylesheet" href="../assets/css/adminlte.min.css"> 
 <link rel="stylesheet" href="../stp_con/summernote/summernote-bs4.min.css">
  <!-- Font Awesome -->
  <link rel="stylesheet" href="../stp_con/fontawesome-free/css/all.min.css">
  <!-- overlayScrollbars -->
  <link rel="stylesheet" href="../stp_con/overlayScrollbars/css/OverlayScrollbars.min.css"> 
  <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
    <link type="text/css" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.12.1/themes/south-street/jquery-ui.css" rel="stylesheet"> 
    <script type="text/javascript" src="../assets/js/jquery.signature.min.js"></script>
    <link rel="stylesheet" type="text/css" href="../assets/css/jquery.signature.css"> 
</head>

<body class="hold-transition sidebar-mini layout-fixed top-nav  pace-primary">
<div class="text-center mt-1 mb-1">
<button type="button" class="btn btn-primary btn-lg" id="enterFullscreen">EXPAND SCREEN</button>
 
</div>
<div class="container-fluid px-4">
	<h1 class="mt-4">Cybercode eVUE</h1>
    <div class="card shadow mb-4">
        <div class="card-header">
            <div class="row">
                <div class="col-md-6">
                    <b>Exam : </b><?php echo $exam_title; ?>
                </div>
                <div class="col-md-6">
                    <b>Subject : </b><?php echo $object->Get_Subject_name($subject_id); ?>
                </div>
            </div>
        </div>
        <div class="card-body">
            <div class="row">
                <div class="col-md-8">
                    <div id="single_question_area" class="mb-2"></div>
                    <div id="question_navigation_area" class="mb-2"></div>
                </div>
                <div class="col-md-4">
                    <div class="text-center mt-2 mb-2">
                        <button type="button" class="btn btn-primary btn-lg" id="btn_timer">
                    </div>
                    <div class="card shadow">
                        <div class="card-header"><b>Student Details</b></div>
                        <div class="card-body">
                            <div class="row">
                                <div class="col-md-4">
                                    <p class="text-center"><img src="../<?php echo $student_image; ?>" class="img-fluid img-thumbnail" width="100" /></p>
                                </div>
                                <div class="col-md-8">
                                    <b>Roll No : </b><?php echo $student_roll_no; ?><br />
                                    <b>Name : </b><?php echo $student_name; ?><br />
                                    <b>Class : </b><?php echo $object->Get_class_name($class_id); ?>
                                </div>
                            </div>
                            
                        </div>
                    </div>
                    <div class="col-md-6">
                        <a href="view_subject_exam.php?ex_id=<?php echo $exam_id; ?>&st_id=<?php echo $student_roll_no; ?>" onclick="return confirm('Are you sure you want to Submit Exam?');" class="btn btn-success btn-lg">
                              SUBMIT EXAM
                        </a>      
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<script src="../vendor/jquery/jquery-3.6.0.min.js"></script>
    <script src="../vendor/bootstrap/bootstrap.bundle.min.js"></script>
    <script src="../js/scripts.js"></script>
    <script src="../vendor/parsley/dist/parsley.min.js"></script>
    <script src="../vendor/datatables/jquery.dataTables.min.js"></script>
    <script src="../vendor/datatables/dataTables.bootstrap5.min.js"></script>

<script>
$(document).ready(function(){
    var exam_id = "<?php echo $exam_id; ?>";
    var exam_subject_id = "<?php echo $exam_subject_id; ?>";
    function load_question(question_id = '', exam_id, exam_subject_id)
    {
        $.ajax({
            url:"ajax_action.php",
            method:"POST",
            data:{exam_id:exam_id, exam_subject_id:exam_subject_id, question_id:question_id, page:'view_subject_exam', action:'load_question'},
            success:function(data)
            {
                $('#single_question_area').html(data);
            }
        })
    }

    load_question('', exam_id, exam_subject_id);

    question_navigation();

    function question_navigation()
    {
        $.ajax({
            url:"ajax_action.php",
            method:"POST",
            data:{exam_id:exam_id, exam_subject_id:exam_subject_id, page:'view_subject_exam', action:'question_navigation'},
            success:function(data)
            {
                $('#question_navigation_area').html(data);
            }
        })
    }

    $(document).on('click', '.next', function(){
        var question_id = $(this).attr('id');
        load_question(question_id, exam_id, exam_subject_id);
    });

    $(document).on('click', '.previous', function(){
        var question_id = $(this).attr('id');
        load_question(question_id, exam_id, exam_subject_id);
    });

    $(document).on('click', '.question_navigation', function(){
        var question_id = $(this).data('question_id');
        load_question(question_id, exam_id, exam_subject_id);
    });

    $(document).on('click', '.answer_option', function(){
        var question_id = $(this).data('question_id');
        var answer_option = $(this).data('id');
        $.ajax({
            url:"ajax_action.php",
            method:"POST",
            data:{question_id:question_id, answer_option:answer_option, exam_id:exam_id, exam_subject_id:exam_subject_id, page:'view_subject_exam', action:'answer'},
            success:function()
            {

            }
        });
    });

});
</script>

<script>
    document.addEventListener('DOMContentLoaded', function() {
        const timerElement = document.getElementById('btn_timer');
        const endDateTime = new Date('<?php echo $subject_exam_end_time; ?>').getTime(); // Set your end date and time here

        function startTimer() {
            const now = new Date().getTime();
            const timeRemaining = endDateTime - now;

            if (timeRemaining <= 0) {
                timerElement.textContent = "00:00:00";
                alert("Exam Time Over");
                window.location.href="../logout.php";
            }

            const hours = Math.floor((timeRemaining % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
            const minutes = Math.floor((timeRemaining % (1000 * 60 * 60)) / (1000 * 60));
            const seconds = Math.floor((timeRemaining % (1000 * 60)) / 1000);

            const formattedTime = `${String(hours).padStart(2, '0')}:${String(minutes).padStart(2, '0')}:${String(seconds).padStart(2, '0')}`;
            timerElement.textContent = formattedTime;

            setTimeout(startTimer, 1000);
        }

        startTimer();
    });
</script>
 <script>
    const enterFullscreenButton = document
.getElementById('enterFullscreen');
const exitFullscreenButton = document
.getElementById('exitFullscreen');
 
const imageFullscreen = document
    .getElementById("imageScreen")
const imageFullscreenButton = document
    .getElementById("enterImageFullScreen")
 
enterFullscreenButton.addEventListener('click', () => {
    const element = document.documentElement;
 
    if (element.requestFullscreen) {
        element.requestFullscreen();
    }
    else {
        console.log("an error occured")
    }
 
    enterFullscreenButton.style.display = 'none';
    exitFullscreenButton.style.display = 'block';
});
 
exitFullscreenButton.addEventListener('click', () => {
    if (document.exitFullscreen) {
        document.exitFullscreen();
    }
    exitFullscreenButton.style.display = 'none';
    enterFullscreenButton.style.display = 'block';
});
 
imageFullscreenButton.addEventListener('click', () => {
 
    if (imageFullscreen.requestFullscreen) {
        imageFullscreen.requestFullscreen();
    }
    else {
        console.log("an error occured")
    }
});
  </script>