<?php

    session_start();

    include "../db.php";

    date_default_timezone_set('Asia/Kolkata');

    $currentTime = date('M j, Y g:i a');



    if ( !isset($_SESSION['uid']) && !isset($_SESSION['uref']) && !isset($_SESSION['utype']) && !isset($_SESSION['uname']) )

        header("Location: logout.php");



    $u_sql = "SELECT sclass,admin_ref FROM users WHERE uref = '$_SESSION[uref]'";

    $u_result = mysqli_query($con, $u_sql);

    $u_row = mysqli_fetch_assoc($u_result);

    $userClass = $u_row["sclass"];

    $admin_2 = $u_row["admin_ref"];

    

    $mocktest = "SELECT * FROM mocktest WHERE class_id = '$userClass' AND status = 1";

    $result = mysqli_query($con, $mocktest);

    

?>



<!DOCTYPE html>

<html lang="en">



<head>

    <?php include "includes/meta.php"; ?>

    <?php include "includes/css.php"; ?>



    <style>

        .modal.fade .modal-dialog {

            width: 100%;

            margin: 0 auto;

        }

    </style>

</head>



<body id="page-top" class="mt-55">



    <div style="margin-top: 70px;"></div>



    <?php include "includes/header2.php"; ?>

    <div class="container">

        <div class="row">

            <div class="col-12 col-sm-12 col-md-12">

                <div cla ss="card-body">

                    <h1 class="h3 mb-2 text-gray-800 text-center">Mock Test</h1>

                    <hr>

                    <table id="MockTest" class="table table-striped table-borderless" style="width:100%">

                        <thead>

                            <tr>

                                <th>Sl</th>

                                <th>Name</th>

                                <th>Subject</th>

                                <th>Time(minutes)</th>

                                <th>Questions</th>

                                <th>Action</th>

                            </tr>

                        </thead>

                        <tbody>

                            <?php foreach($result as $key => $row){

                            $subject = "SELECT sc_name, sc_id FROM subject WHERE sc_id = '$row[subject_id]'";

                            $subjectresult = mysqli_query($con, $subject);

                            $subjectRow = mysqli_fetch_array($subjectresult);

                            $question = "SELECT id, mocktest_id FROM question WHERE mocktest_id = '$row[id]'";

                            $questionresult = mysqli_query($con, $question);

                            $questionRow = mysqli_num_rows($questionresult);

                            $mocktest_Sql = "SELECT mocktest_id, student_id, exam_ref FROM mocktest_result WHERE student_id ='$_SESSION[uid]'";

                            $mocktest_result = mysqli_query($con, $mocktest_Sql);

                            $mocktestCount = mysqli_num_rows($mocktest_result);

                            $mocktestRow = mysqli_fetch_assoc($mocktest_result);

                            // print_r($mocktestCount);

                            // die();

                            ?>

                            <tr>

                                <td><?php echo $key+1?></td>

                                <td><?php echo $row['mocktest_name'];?></td>

                                <td><?php echo $subjectRow['sc_name'];?></td>

                                <td><?php echo $row['mock_time'];?></td>

                                <td>

                                    <?php echo $questionRow;?>

                                </td>

                                <td>

                                <div class="py-3">

                                    <?php if($mocktestCount>0){?>

                                        <a href="examination-result.php?exam_id=<?php echo $mocktestRow['exam_ref']?>" class="btn btn-primary">View Result</a>

                                    <?php }else{?>

                                        <a href="#" class="btn btn-primary" data-toggle="modal" data-target="#examModal<?php echo $row['id'];?>">Test Now</a>

                                    <?php }

                                    ?>

                                    

                                </div>

                                <!-- Modal -->

                                <div class="modal fade" id="examModal<?php echo $row['id'];?>" tabindex="-1" role="dialog" aria-labelledby="exampleModalCenterTitle" aria-hidden="true">

                                <div class="modal-dialog modal-dialog-centered" role="document">

                                    <div class="modal-content py-4">

                                       <div class="modal-body text-center">

                                       <h3>Are you want to start this mocktest?</h3>

                                       <a href="view-mocktest.php" class="btn btn-danger">No</a>

                                       <a href="view-mocktest.php?mocktest=<?php echo $row['id'];?>" class="btn btn-primary">Yes</a>

                                       </div>

                                    </div>

                                </div>

                                </div>

                                <!-- Modal -->

                                </td>

                            </tr>

                            <?php }?>

                        </tbody>

                    </table>

                    <hr>

                </div>

            </div>

        </div>

        <?php include "includes/ft.php"; ?>

        <?php include "includes/js.php"; ?>

    </div>

</body>

<?php

if(isset($_GET['mocktest'])){?>

<?php

    $getquestion = "SELECT * FROM question WHERE mocktest_id = '$_GET[mocktest]'";

    $getRun = mysqli_query($con, $getquestion);

    $currentTime = date('M j, Y g:i a');

    $Exam_ref = mt_rand().'_'.time();

    foreach($getRun as $question){

        $question_id = $question['id'];

        $multy_sql = "INSERT INTO examination (exam_ref, question_id, created_at)

        VALUES ('$Exam_ref', '$question_id', '$currentTime')";

        mysqli_query($con, $multy_sql);

    }

    $sql = "INSERT INTO mocktest_result (exam_ref, mocktest_id, student_id, created_at)

    VALUES ('$Exam_ref', '$_GET[mocktest]', '$_SESSION[uid]', '$currentTime')";



    $mocktestTime = "SELECT * FROM mocktest WHERE id = '$_GET[mocktest]'";

    $Timeresult = mysqli_query($con, $mocktestTime);

    $RowTime = mysqli_fetch_assoc($Timeresult);

    $_SESSION['Exam_ref'] = $Exam_ref;

    $_SESSION['mocktest_id'] = $_GET['mocktest'];

    $_SESSION['start'] = date('h:i:s',time());

    $seconds2add = $RowTime['mock_time']*60;

    // $seconds2add = 1*60;

    $new_time= strtotime($_SESSION['start']);

    $new_time+=$seconds2add;

    $_SESSION['expire'] = date('h:i:s',$new_time);

    if (mysqli_query($con, $sql)) {

        ?>

        <script>

            window.location = "examination.php";

        </script>

        <?php

      } else {

        ?>

        <script>

            window.location = "examination.php";

        </script>

        <?php

      }

}

?>

</html>