<?php

//view_exam.php

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

$object = new Soes;

 
$ec = '';

$exam_result_datetime = '';

if(isset($_GET["ec"]))
{
    $ec = $_GET["ec"];
    $object->query = "
    SELECT exam_id, exam_result_datetime FROM exam_soes 
    WHERE exam_code = '".$_GET["ec"]."';
    ";

    $result = $object->get_result();

    foreach($result as $row)
    {
        $exam_result_datetime = $row["exam_result_datetime"];
        $object->query = "
        SELECT * FROM subject_wise_exam_detail 
        INNER JOIN subject_soes 
        ON subject_soes.subject_id = subject_wise_exam_detail.subject_id 
        WHERE subject_wise_exam_detail.exam_id = '".$row["exam_id"]."'
        ";

        $exam_result = $object->get_result();
    }
}
else
{
    header('location:exam.php');
}

if(isset($_SESSION["ec"]))
{
    unset($_SESSION["ec"]);
}
if(isset($_SESSION["esc"]))
{
    unset($_SESSION["esc"]);
}
               
?>
<!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="container-fluid px-4">
	<h1 class="mt-4">Exam Schedule Details</h1>
    <ol class="breadcrumb mb-4" style="background: linear-gradient(#d8d7e8,#f8fcf7,#d8d7e8);" >
    <li class="breadcrumb-item"><a href="../admin.php?page=dashboard&Id_2=<?php echo $user_row['course']; ?>">Dashboard</a></li>
        <li class="breadcrumb-item active"><a href="exam.php">Exam</a></li>
    </ol>
    <span id="message"></span>
    <div class="card shadow mb-4">
        <div class="card-header py-3">
            <div class="row">
                <div class="col">
                    <h6 class="m-0 font-weight-bold">Subject List</h6>
                </div>
                <div class="col" align="right">
                    
                </div>
            </div>
        </div>
        <div class="card-body">
            <div class="table-responsive">
            <?php
            if(isset($exam_result))
            {
            ?>
                <table class="table table-bordered">
                    <tr>
                        <th>Subject Name</th>
                        <th>Exam Date & Time</th>
                        <th>Day</th>
                        <th>Exam Duration</th>
                        <th>Total Question</th>
                        <th>Correct Answer Marks</th>
                        <th>Wrong Answer Marks</th>
                        <th>Status</th>
                        <th>Action</th>
                    </tr>
                    <?php
                    foreach($exam_result as $row)
                    {
                        $status = '';
                        $action_button = '';
                        $start_time = $row["subject_exam_datetime"];
                        $duration = $object->Get_exam_duration($row["exam_id"]) . ' minute';
                        $end_time = strtotime($start_time . '+' . $duration);
                        $end_time = date('Y-m-d H:i:s', $end_time);
                        if(time() >= strtotime($start_time) && time() <= strtotime($end_time))
                        {
                            $data = array(
                                ':subject_exam_status'  =>  'Started',
                                ':exam_subject_id'      =>  $row["exam_subject_id"]    
                            );
                            $object->query = "
                            UPDATE subject_wise_exam_detail 
                            SET subject_exam_status = :subject_exam_status 
                            WHERE exam_subject_id = :exam_subject_id
                            ";
                            $object->execute($data);
                            $status = '<span class="badge bg-primary">Started</span>';
                            $action_button = '<button type="button" class="btn btn-primary btn-sm view_subject_exam" data-ec="'.$ec.'" data-esc="'.$row["subject_exam_code"].'">Start Exam</button>';
                        }
                        else
                        {
                            if(time() > strtotime($end_time))
                            {
                                $data = array(
                                    ':subject_exam_status'  =>  'Completed',
                                    ':exam_subject_id'      =>  $row["exam_subject_id"]
                                );
                                $object->query = "
                                UPDATE subject_wise_exam_detail 
                                SET subject_exam_status = :subject_exam_status 
                                WHERE exam_subject_id = :exam_subject_id
                                ";
                                $object->execute($data);
                                $status = '<span class="badge bg-dark">Completed</span>';
                                if(is_null($exam_result_datetime))
                                {
                                    $action_button = '';
                                }
                                else
                                {
                                    if(time() > strtotime($exam_result_datetime))
                                    {
                                        $action_button = '<a href="single_subject_result.php?ec='.$ec.'&esc='.$row["subject_exam_code"].'" target="_blank" class="btn btn-danger btn-sm">PDF</a>';
                                    }
                                    else
                                    {
                                        $action_button = '';
                                    }
                                }
                            }
                            if(strtotime($start_time) > time())
                            {
                                $status = '<span class="badge bg-warning">Pending</span>';
                                //$action_button = '<button type="button" class="btn btn-primary btn-sm view_subject_exam" data-ec="'.$ec.'" data-esc="'.$row["subject_exam_code"].'"><i class="fas fa-pencil-alt"></i></button>';
                                $action_button = '';
                            }
                        }
                        echo '
                        <tr>
                            <td>'.$row["subject_name"].'</td>
                            <td>'.$row["subject_exam_datetime"].'</td>
                            <td>'.date('l', strtotime($row["subject_exam_datetime"])).'</td>
                            <td>'.$duration.' Minute</td>
                            <td>'.$row["subject_total_question"].' Question</td>
                            <td><b class="text-success">'.$row["marks_per_right_answer"].' Marks</b></td>
                            <td><b class="text-danger">-'.$row["marks_per_wrong_answer"].' Marks</b></td>
                            <td>'.$status.'</td>
                            <td>'.$action_button.'</td>
                        </tr>
                        ';
                    }
                    ?>
                </table>
                    <?php
                }
                ?>
            </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(){
    
    $('.view_subject_exam').click(function(){
        var ec = $(this).data('ec');
        var esc = $(this).data('esc');
        $.ajax({
            url:"ajax_action.php",
            method:"POST",
            data:{page:"view_exam", action:"view_subject_exam", ec:ec, esc:esc},
            success:function(data)
            {
                //window.open(data,'_blank');
                window.location.href = data;
            }
        });
    });  

});
</script>