    <?php




        error_reporting(E_ALL);
        ini_set('display_errors', 1);

       

        include('../../../config/config.php');

        mysqli_set_charset($link, 'utf8');

          $cbtSettingsID = $_POST['cbtSettingsID'];
        $StudentID = $_POST['StudentID'];
        $examtype = $_POST['examtype'];







        if($examtype == 'assignment')
        {

                    // assignment content here


                        // Get CBT Category
                        $selectquestionset = mysqli_query($link,"SELECT * FROM `assignmentsettings` 
                        WHERE `AssignmentSettingsID`='$cbtSettingsID'");
        
                       $selectquestionsetrow = mysqli_num_rows($selectquestionset);
                        $selectquestionsetrow_cnt = mysqli_fetch_assoc($selectquestionset);


                        // $cbtSettingsID = $_POST['cbtSettingsID'];
                        // $StudentID = $_POST['StudentID'];



                        


                        $check_if_student_exist = mysqli_query($link,"SELECT * FROM `assignmentanswer` 
                                    WHERE `AssignmentSettingsID`='$cbtSettingsID'
                                    AND `StudentID` = '$StudentID'
                                    AND  `ObjectiveStatus`= '1'");




                            $exist_num = mysqli_num_rows($check_if_student_exist);
                            $exist_fetch = mysqli_fetch_assoc($check_if_student_exist);



                            if($exist_num  > 0)
                            {


                                    
                            }else
                            {



                                        echo '<div class="row p-3">
                                        <div class="col-lg-8" style="margin-top: 20px;">';


                                        

                                        $selectquestionset = mysqli_query($link,"SELECT * FROM `assignmentsettings` 
                                        INNER JOIN `classordepartment` 
                                        ON `assignmentsettings`.`ClassOrDepartmentID` = `classordepartment`.ClassOrDepartmentID 
                                        INNER JOIN `subjectorcourse` 
                                        ON `assignmentsettings`.`SubjectOrCourseID` = `subjectorcourse`.SubjectOrCourseID
                                        
                                        INNER JOIN `session`
                                        ON `assignmentsettings`.`sessionID` = `session`.`sessionName`
                                        WHERE `assignmentsettings`.`AssignmentSettingsID`='$cbtSettingsID'");


                                    $selectquestionsetrow = mysqli_num_rows($selectquestionset);
                                    $selectquestionsetrow_cnt = mysqli_fetch_assoc($selectquestionset);

                                    // If the CBT Settings Exist
                                    if($selectquestionsetrow > 0)
                                    {



                                    

                                                        $cbtsetquestionssettingsID = $selectquestionsetrow_cnt['AssignmentSettingsID'];
                                                        
                                                        $CampusID = $selectquestionsetrow_cnt['CampusID'];
                                                        $TermOrSemesterID = $selectquestionsetrow_cnt['TermOrSemesterID'];
                                                        $SubjectOrCourseID = $selectquestionsetrow_cnt['SubjectOrCourseID'];
                                                        $sessionID = $selectquestionsetrow_cnt['sessionName'];
                                                        $ClassOrDepartmentID = $selectquestionsetrow_cnt['ClassOrDepartmentID'];


                                                            $AssesementCategory = $selectquestionsetrow_cnt['AssignmentCategory'];


                                                        // $TheoryQuestion = explode(',',$selectquestionsetrow_cnt['TheoryQuestion']);

                                                        //     $Questions = explode(',',$selectquestionsetrow_cnt['question']);
                                                        //     $answer = explode(',',$selectquestionsetrow_cnt['answer']);

                                                        // print_r($Questions);

                                                        // $numquestion = count($Questions);

                                                        $AssesementStartTime = $selectquestionsetrow_cnt['AssignmentStartTime'];
                                                        $AssesementEndTime  =  $selectquestionsetrow_cnt['AssignmentEndTime'];


                                                        $timeIn = new DateTime($AssesementStartTime);
                                                        $timeOut = new DateTime($AssesementEndTime);

                                                        date_default_timezone_set('Africa/Lagos');

                                                        // Calculate the time difference in seconds
                                                        $timeDiffInSeconds = $timeOut->getTimestamp() - $timeIn->getTimestamp();

                                                        // Convert the time difference to hours, minutes, and seconds
                                                        $hour = floor($timeDiffInSeconds / 3600);
                                                        $minute = floor(($timeDiffInSeconds % 3600) / 60);
                                                        $seconds = $timeDiffInSeconds % 60;

                                                        // Format the values as strings with leading zeros
                                                        $hour = str_pad($hour, 2, '0', STR_PAD_LEFT);
                                                        $minute = str_pad($minute, 2, '0', STR_PAD_LEFT);
                                                        $seconds = str_pad($seconds, 2, '0', STR_PAD_LEFT);

                                                        $totaldiff = ($hour . ':' . $minute . ':' . $seconds);

                                                        $midnight = strtotime('12:00:00 AM');
                                                        $midtwo =  strtotime(date('h:i:sa',$midnight));

                                                        echo '<input type="hidden" id="prosmidnighttime" value="'.$midtwo.'">
                                                        <input type="hidden" id="prostimedifference" value="'.$totaldiff.'">';


                                                        
                                                                            
                                                                       


                                                                    if($AssesementCategory  == 'Objective'){



                                                                        echo '<div class="card-body" style="background-color: rgb(255, 255, 255); height: 100%; width: 100%; box-shadow: 1px 3px 10px #C4DDEB;">';


                                                                            $countkey = 0;
                                                                            $counquestion_num =1;
                                                                            $countkey = 0;

                                                                           

                                                                            $arraynew = array();
                                                                            $correctanswer  = array();

                                                                            $arrayquestionsum = array();
                                                                            $studanser = array();
                                                                        

                                                                          
                                                                            $select_question = "SELECT * FROM `assignmentquestion` WHERE `AssignmentSettingsID`='$cbtsetquestionssettingsID' AND CampusID = '$CampusID' GROUP BY AssignmentQuestionID ASC";
                                                                            $select_question_result = mysqli_query($link, $select_question);
                                                                            $select_question_result_cnt = mysqli_num_rows($select_question_result);
                                                                            $select_question_result_cnt_row = mysqli_fetch_assoc($select_question_result);


                                                                            do{




                                                                                            $qustionall = $select_question_result_cnt_row['question'];
                                                                                            $Questionid_1 = $select_question_result_cnt_row['AssignmentQuestionID'];
                                                                                            $optionA = $select_question_result_cnt_row['optionA'];
                                                                                            $optionB = $select_question_result_cnt_row['optionB'];
                                                                                            $optionC = $select_question_result_cnt_row['optionC'];
                                                                                            $optionD = $select_question_result_cnt_row['optionD'];
                
                                                                                            $canswer = $select_question_result_cnt_row['answer'];
                
                                                                                            $QuestionType = $select_question_result_cnt_row['AssignmentCategory'];
                
                
                                                                                                $questionobl = (object) [
                                                                                                    'question' =>  $qustionall ,
                                                                                                    'optionA' => $optionA,
                                                                                                    'optionB' => $optionB,
                                                                                                    'optionC' => $optionC,
                                                                                                    'optionD' =>  $optionD,
                                                                                                    
                                                                                                ];
                        
                                                                                                $arraynew[] = $questionobl;
                                                                                                $correctanswer[] = $canswer;
                                                                                                $arrayquestionsum[] =   $Questionid_1;

                                                                                                $studanser[] = 0;



                                                                            }while($select_question_result_cnt_row = mysqli_fetch_assoc($select_question_result));
                                                                        
                                                                            


                                                                          
                                                                                 $settingarrfirt =    $arrayquestionsum[0];


                                                                                 $getquestionforcontent_keys = $arrayquestionsum;




                                                                                $ecodedquestions = json_encode($arraynew,JSON_FORCE_OBJECT);
                                                                                $currectanswer = implode(',',$correctanswer);
                                                                                $studentdefaultansw =  implode(',', $studanser);
                                                                                $imploadquestionforloc = implode(',', $arrayquestionsum);
                
                                                                                echo '<textarea hidden="hidden" value="" id="pros-loadjsoncontentforcbt">'.$ecodedquestions.'</textarea>';
                                                                                echo '<input type="hidden" value="'.$imploadquestionforloc.'" id="tari_question_default">';
                
                                                                                echo '<input type="hidden" value="Objective" id="pros-loadexamtypehere">';
                
                                                                                echo '<input type="hidden" value="0" id="nxtprevquestion">
                                                                                    <textarea hidden="hidden"  id="currentanwernxt">'.$studentdefaultansw.'</textarea>
                                                                                    <input type="hidden" value="1" id="questionNo">';
                                                                                    echo '<input type="hidden" id="currectquestionID" value="'.$currectanswer.'">';







                                                                                                        
                                                                                    $countQ = 0;
                                                                                  

                                                                                    $Select = "SELECT * FROM `assignmentquestion` WHERE `AssignmentQuestionID`='$settingarrfirt' AND  CampusID = '$CampusID' GROUP BY AssignmentQuestionID ASC  LIMIT 1";
                                                                                    $selectQuery = mysqli_query($link, $Select);
                                                                                    $fetch2 = mysqli_num_rows($selectQuery);
                                                                                
                                                                                //If Question Doesnt Exist 
                                                                                if($fetch2 == 0){
                                                                                                            
                                                                                    echo '<div class="alert alert-info alert-dismissible d-flex align-items-center show">
                                                                                            <i class="fa fa-info-circle"></i>
                                                                                        <strong class="mx-2">Hey!</strong>Please no question attached to this settings.
                                                                                        </div>
                                                                                        <a  href="cbtSetting.php"  class="close" aria-label="Close" style="color:red; margin-right:20px;"><i clas="fa fa-back"></i>Back
                                                                                        </a>';
                                                                                }
                                                                                //Question ID Exist
                                                                                else{

                                                                                    while($fetch = mysqli_fetch_array($selectQuery))
                                                                                    {


                                                                                        $countQ =  $countQ + 1;
                                                                                        //    $rowid1 =    $Selectsettings_result_cnt_row['cbtsetquestionssettings'];  

                                                                                        $questionfirst = $fetch['question'];
                                                                                        $optionA =  $fetch['optionA'];
                                                                                        $optionB =  $fetch['optionB'];
                                                                                        $optionC =  $fetch['optionC'];
                                                                                        $optionD =  $fetch['optionD'];

                                                                                        
                                                                                        // Replace <p></p> with empty space
                                                                                        $optionA1 = str_replace(array('<p>', '</p>'), '', $optionA);
                                                                                        $optionB1 = str_replace(array('<p>', '</p>'), '', $optionB);
                                                                                        $optionC1 = str_replace(array('<p>', '</p>'), '', $optionC);
                                                                                        $optionD1 = str_replace(array('<p>', '</p>'), '', $optionD);

                                                                                        // Replace < with &lt;
                                                                                        $optionAA = str_replace('<', '&lt;', $optionA1);
                                                                                        $optionBB = str_replace('<', '&lt;', $optionB1);
                                                                                        $optionCC = str_replace('<', '&lt;', $optionC1);
                                                                                        $optionDD = str_replace('<', '&lt;', $optionD1);





                                                                                      $QuestionTypeFirst =  $fetch['AssignmentCategory'];
                                                                                        
                                                                                        $rowid =  $fetch['AssignmentQuestionID'];

                                                                                        echo '<div id ="displayResults"> 

                                                                                            <h1 style="font-style: italic; margin-top: 10px;">(Question '.$countQ.')</h1>

                                                                                            <div class="myborder"></div>

                                                                                                <div style="margin-top: 50px; margin-left: 20px;">
                                                                                                    <h4>'.$questionfirst.'</h4>';
                                                                                                        
                                                                                                        if($QuestionTypeFirst  == 'Objective')
                                                                                                        {
                                                                                                            echo '<form class="selectSubject">
                                                                                                                <label>
                                                                                                                    <input type="radio" name="group1" class="radioBtnClass"  value="A"  id="radio_1"/>
                                                                                                                    <span>A. '.$optionAA.'</span>
                                                                                                                </label>
                                                                                                                <label>
                                                                                                                    <input type="radio" name="group1" class="radioBtnClass"  value="B"  id="radio_2"/>
                                                                                                                    <span>B. '.$optionBB.'</span>
                                                                                                                </label>
                                                                                                                <label>
                                                                                                                    <input type="radio" name="group1" class="radioBtnClass"  value="C"  id="radio_3"/>
                                                                                                                    <span>C. '.$optionCC.'</span>
                                                                                                                </label>
                                                                                                                <label>
                                                                                                                    <input type="radio" name="group1" class="radioBtnClass"  value="D"  id="radio_4"/>
                                                                                                                    <span>D. '.$optionDD.'</span>
                                                                                                                </label>
                                                                                                            </form>';

                                                                                                        }elseif($QuestionTypeFirst  == 'Theory')
                                                                                                        {

                                                                                                                echo '<textarea   id="optionA1" class="wrs_div_box example question" ></textarea>';
                                                                                                                
                                                                                                        }

                                                                                                echo '</div>

                                                                                            <div class="row" style="margin-top: 50px;">
                                                                                                <hr/>
                                                                                                <div class="col-8">
                                                                                                    <!-- <button type="button" style="font-size: 15px; background-color:#0d6efd;" class="btn waves-effect waves-light btn-rounded">Previous</button> -->
                                                                                                </div>

                                                                                                <div class="col-4">
                                                                                                    <button type="button" style="font-size: 15px;" class="btn waves-effect waves-light btn-rounded btn-secondary next float-end">Next</button>
                                                                                                </div>
                                                                                            </div>

                                                                                            <div class="row mt-3" >
                                                                                                <div class="col-lg-12">
                                                                                                    <div>
                                                                                                        <small style="color: red; font-size: 12px; text-align: justify;">
                                                                                                            <b>Note:</b> Kindly pick the correct option using the mouse to select the answer to the question
                                                                                                            by the right before the time runs out. You can also select any question below to answer. 
                                                                                                            Don\'t forget to click Submit when you are done.
                                                                                                        </small>
                                                                                                    </div>
                                                                                                </div>
                                                                                            </div>
                                                                                        
                                                                                        </div>';

                                                                                    }                     
                                                                                }





                                                                        echo'</div>   
                                                                        </div>';




                                                                        echo'<div  class="col-lg-4" style="margin-top: 20px;">
                                                                        <div class="card">
                                                                        <div class="card-body" style="background-color: rgb(255, 255, 255); box-shadow: 1px 3px 10px #C4DDEB;">
                                                                        <h2 style="font-weight: 700;" align="center">Time Left</h2>
                
                                                                        <div class="row">
                                                                            <div class="col-2">
                                                                                
                                                                            </div>
                
                                                                            <div class="col-3">
                                                                                <h3 id="examhour">00</h3>
                                                                                <small align="center">Hours</small>
                                                                            </div>
                                                                            
                                                                            <div class="col-3">
                                                                                <h3 id="examminutes">00</h3>
                                                                                <small>Minutes</small>
                                                                            </div>
                                                                            <div class="col-4">
                                                                                <h3 id="examseconds">00</h3>
                                                                                <small>Seconds</small>
                                                                            </div>
                
                                                                        </div>
                
                                                                        <div style="margin-top: 63px; padding: none;">
                                                                        <div class="">
                                                                        <div class="col-lg-1">
                
                                                                        </div>';
                
                                                                            // $AssesementCategory = $QuestionTypeFirst; 
                
                                                                            $countkey = 0;
                                                                            $counquestion_num =1;
                                                                            $countkey = 0;
                
                                                                            if($AssesementCategory  == 'Objective'){
                
                                                                                foreach($getquestionforcontent_keys  as $questionnew)
                                                                                {
                
                
                                                                                        $questionnew;
                    
                                                                                        $counquestion_num++; 
                                                                                        $countkey++;
                    
                                                                                        $newquenum  =   $counquestion_num -1;
                                                                                        $questionkey2  =  $countkey -1;
                                                                                        
                                                                                        $slit2arr; 

                                                                                        

                                                                                        
                                                                                
                                                                                        $Select1 = "SELECT * FROM  `assignmentquestion` WHERE  `CampusID` = '$CampusID'  AND AssignmentQuestionID='$questionnew' ORDER BY AssignmentQuestionID ASC";
                                                                                        $selectQuery1 = mysqli_query($link, $Select1);
                                                                                        $fetch1 = mysqli_fetch_array($selectQuery1);
                
                                                                            
                                                                                        $question =  $fetch1['question'];
                                                                                        $optionA =  $fetch1['optionA'];
                                                                                        $optionB =  $fetch1['optionB'];
                                                                                        $optionC =  $fetch1['optionC'];
                                                                                        $optionD =  $fetch1['optionD'];
                    
                                                                                        $QuestionTypenew =  $fetch1['AssignmentCategory'];
                    
                                                                                        $cbt_setting_id =  $fetch1['AssignmentQuestionID'];
                                                                                    
                    
                                                                                        if($QuestionTypenew == 'Objective')
                                                                                        {
                                                                                            $questiontype ='Objective';
                    
                                                                                        }
                                                                                        echo ' <button style="font-weight: 600; font-size: 12px;margin: 2px; background-color:#0d6efd;" " type="button" data-question="'.$questiontype.'" class="btn btn-success viewer" id="'.$newquenum.'" data-id="'.$questionkey2.'">'.$newquenum.'</button>';
                                                                                                                
                                                                                    }
                
                                                                            }
                                                                                                                                                                                
                                                                        echo '</div>
                                                                        <div class="row mt-3">
                                                                        <div class="col-8">
                                                                            
                                                                        </div>
                                                                        <div class="col-4">
                                                                            <button type="button" class="btn waves-effect waves-light btn-success" data-bs-toggle="modal" data-bs-target="#pros-submitexam"  style="font-size: 15px; background-color:#4db8ff; border: #2CEB6C; margin-top: 30px;">Submit</button>
                                                                        </div>
                                                                        </div>
                                                                        </div>
                                                                        </div>
                
                                                                        </div>';







                                                                    }else if($AssesementCategory  == 'Theory')
                                                                    {







                                                                        echo '<div class="card-body" style="background-color: rgb(255, 255, 255); height: 100%; width: 100%; box-shadow: 1px 3px 10px #C4DDEB;">';


                                                                     

                                                                        // SELECT `AssignmentanswerID`, `AssignmentSettingsID`, `StudentID`, `CampusID`, `CourseOrSubjectTeacherUserID`, `ClassOrDepartmentID`, `SubjectOrCourseID`, `sessionID`, `TermOrSemesterID`, `ObjectiveQuestion`, `ObjectAnswer`, `TheoryQuestion`, `TheoryAnswer`, `TheoryScore`, `Remark`, `Correction`, `resetstatus`, `publishstatus`, `ResultStatus`, `submitstatus`, `ObjectiveStatus`, `TheoryStatus` FROM `assignmentanswer` WHERE 1
                                                                      
                
                                                                                 // Check if Student has been Inserted into the Student Answer Table

                                                                                    $legendcoderr_track_status = 0;

                                                                                    // PROS VALIDATE THEORY EXAMS HERE
                                                                                    $check_if_student_exist_general = mysqli_query($link,"SELECT * FROM `assignmentanswer` 
                                                                                    WHERE `assignmentanswer`.`AssignmentSettingsID`='$cbtSettingsID'
                                                                                    AND `assignmentanswer`.StudentID = '$StudentID'
                                                                                    AND `assignmentanswer`.CampusID = '$CampusID'
                                                                                    AND `assignmentanswer`.TermOrSemesterID = '$TermOrSemesterID'
                                                                                    AND `assignmentanswer`.ClassOrDepartmentID = '$ClassOrDepartmentID'
                                                                                    AND `assignmentanswer`.SubjectOrCourseID = '$SubjectOrCourseID'
                                                                                     ");
    
                                                                                   $exist_num_general = mysqli_num_rows($check_if_student_exist);
                                                                                  

                                                                                    $check_if_student_exist = mysqli_query($link,"SELECT * FROM `assignmentanswer` 
                                                                                    WHERE `assignmentanswer`.`AssignmentSettingsID`='$cbtSettingsID'
                                                                                    AND `assignmentanswer`.StudentID = '$StudentID'
                                                                                    AND `assignmentanswer`.CampusID = '$CampusID'
                                                                                    AND `assignmentanswer`.TermOrSemesterID = '$TermOrSemesterID'
                                                                                    AND `assignmentanswer`.ClassOrDepartmentID = '$ClassOrDepartmentID'
                                                                                    AND `assignmentanswer`.SubjectOrCourseID = '$SubjectOrCourseID'
                                                                                    AND `assignmentanswer`.TheoryStatus = '0'");


                                                                                    $exist_num = mysqli_num_rows($check_if_student_exist);





                                                                                    if( $exist_num_general  > 0)
                                                                                    {

                                                                                        

                                                                                           if($exist_num > 0)
                                                                                           {
                                                                                                 $legendcoderr_track_status+=1;
                                                                                           }else
                                                                                           {
                                                                                                 $legendcoderr_track_status+=0;
                                                                                           }
                                                                                             
                                                                                    }else
                                                                                    {
                                                                                        $legendcoderr_track_status+=1;
                                                                                    }

                                                                                      // PROS VALIDATE THEORY EXAMS HERE

                                                                                  
                                        
                                                                                    // Check if Student Exist and has not Submitted
                                                                                    if($legendcoderr_track_status == 1){
                            
                                                                                         $countkey = 0;
                                                                                        $counquestion_num =1;
                                                                                        $countkey = 0;

                                                                           

                                                                                        $arraynew = array();
                                                                                        $correctanswer  = array();

                                                                                        $arrayquestionsum = array();
                                                                                        $studanser = array();
                                                                        

                                                                          
                                                                                    $select_question = "SELECT * FROM `assignmentquestion` WHERE `AssignmentSettingsID`='$cbtsetquestionssettingsID' AND CampusID = '$CampusID' GROUP BY AssignmentQuestionID ASC";
                                                                                    $select_question_result = mysqli_query($link, $select_question);
                                                                                    $select_question_result_cnt = mysqli_num_rows($select_question_result);
                                                                                    $select_question_result_cnt_row = mysqli_fetch_assoc($select_question_result);


                                                                                    do{




                                                                                            $qustionall = $select_question_result_cnt_row['question'];
                                                                                            $Questionid_1 = $select_question_result_cnt_row['AssignmentQuestionID'];
                                                                                            $optionA = $select_question_result_cnt_row['optionA'];
                                                                                            $optionB = $select_question_result_cnt_row['optionB'];
                                                                                            $optionC = $select_question_result_cnt_row['optionC'];
                                                                                            $optionD = $select_question_result_cnt_row['optionD'];
                
                                                                                            $canswer = $select_question_result_cnt_row['answer'];
                
                                                                                            $QuestionType = $select_question_result_cnt_row['AssignmentCategory'];
                
                
                                                                                                $questionobl = (object) [
                                                                                                    'question' =>  $qustionall ,
                                                                                                    'optionA' => $optionA,
                                                                                                    'optionB' => $optionB,
                                                                                                    'optionC' => $optionC,
                                                                                                    'optionD' =>  $optionD,
                                                                                                    
                                                                                                ];
                        
                                                                                                $arraynew[] = $questionobl;
                                                                                                $correctanswer[] = $canswer;
                                                                                                $arrayquestionsum[] =   $Questionid_1;

                                                                                                $studanser[] = 0;



                                                                                      }while($select_question_result_cnt_row = mysqli_fetch_assoc($select_question_result));
                                                                        
                                                                            


                                                                          
                                                                                 $settingarrfirt =    $arrayquestionsum[0];


                                                                                 $getquestionforcontent_keys = $arrayquestionsum;




                                                                                $ecodedquestions = json_encode($arraynew,JSON_FORCE_OBJECT);
                                                                                $currectanswer = implode(',',$correctanswer);
                                                                                $studentdefaultansw =  implode(',', $studanser);
                                                                                $imploadquestionforloc = implode(',', $arrayquestionsum);
                            
                                                                                        echo '<textarea hidden="hidden" value="" id="pros-loadjsoncontentforcbt">'.$ecodedquestions.'</textarea>';
                                                                                        echo '<input type="hidden" value="'. $QuestionType.'" id="pros-loadexamtypehere">
                                                                                                <input type="hidden" value="0" id="nxtprevquestion">';
                                                                                        echo '<input type="hidden" value="1" id="questionNo">
                                                                                        <textarea hidden="hidden"  id="currentanwernxt">'.$studentdefaultansw.'</textarea>';
                                                                                            
                            
                            
                                                                                        $Select = "SELECT * FROM `assignmentquestion` WHERE `AssignmentQuestionID`='$settingarrfirt' AND  CampusID = '$CampusID' GROUP BY AssignmentQuestionID ASC  LIMIT 1";
                                                                                        $selectQuery = mysqli_query($link, $Select);
                                                                                        $fetch2 = mysqli_num_rows($selectQuery);
                                                                                        
                                                                                        $countQ = 0;
                                                                                        //If Question Doesnt Exist 
                                                                                        if($fetch2 == 0){
                                                                                                                    
                                                                                            echo '<div class="alert alert-info alert-dismissible d-flex align-items-center show">
                                                                                                    <i class="fa fa-info-circle"></i>
                                                                                                <strong class="mx-2">Hey!</strong>Please no question attached to this settings.
                                                                                                </div>
                                                                                                <a  href="cbtSetting.php"  class="close" aria-label="Close" style="color:red; margin-right:20px;"><i clas="fa fa-back"></i>Back
                                                                                                </a>';
                                                                                        }
                                                                                        //Question ID Exist
                                                                                        else{


                                                                                            while($fetch = mysqli_fetch_array($selectQuery))
                                                                                            {
                                                                                                $countQ =  $countQ + 1;
                                                                                                //    $rowid1 =    $Selectsettings_result_cnt_row['cbtsetquestionssettings'];  
                            
                                                                                                        $questionfirst = $fetch['question'];
                                                                                                        
                                    
                                                                                                        


                                                                                                        $QuestionTypeFirst =  $fetch['AssignmentCategory'];
                                                                                        
                                                                                                        $rowid =  $fetch['AssignmentQuestionID'];
                
                                    
                                                                                                echo '<div id ="displayResults"> 
                            
                                                                                                    <h1 style="font-style: italic; margin-top: 10px;">(Question '.$countQ.')</h1>
                            
                                                                                                    <div class="myborder"></div>
                            
                                                                                                        <div style="margin-top: 50px; margin-left: 20px;">
                                                                                                            <h4>'.$questionfirst.'</h4>
                            
                                                                                                            <div id="pros-displaytheoryfirstoptionhere"></div>';
                            
                                                                                                        echo '</div>
                            
                                                                                                    <div class="row" style="margin-top: 50px;">
                                                                                                        <hr/>
                                                                                                        <div class="col-8">
                                                                                                            <!-- <button type="button" style="font-size: 15px; background-color:#0d6efd;" class="btn waves-effect waves-light btn-rounded">Previous</button> -->
                                                                                                        </div>
                            
                                                                                                        <div class="col-4" id="submitnex">
                                                                                                            <button type="button" style="font-size: 15px; " class="btn waves-effect waves-light btn-rounded btn-secondary next float-end">Next</button>
                                                                                                        </div>
                                                                                                    </div>
                            
                                                                                                    <div class="row mt-3" >
                                                                                                        <div class="col-lg-12">
                                                                                                            <div>
                                                                                                                <small style="color: red; font-size: 12px; text-align: justify;">
                                                                                                                    <b>Note:</b> Kindly pick the correct option using the mouse to select the answer to the question
                                                                                                                    by the right before the time runs out. You can also select any question below to answer. 
                                                                                                                    Don\'t forget to click Submit when you are done.
                                                                                                                </small>
                                                                                                            </div>
                                                                                                        </div>
                                                                                                    </div>
                                                                                                
                                                                                                </div>';
                            
                                                                                            }                     
                                                                                        }
                            
                                                                                    }
                                                                                    else{
                                                                                        
                                                                                    }
                            
                                                                     
                
                
                                                                        echo'</div>   
                                                                        </div>';
                
                                                                        echo'<div  class="col-lg-4" style="margin-top: 20px;">
                                                                        <div class="card">
                                                                        <div class="card-body" style="background-color: rgb(255, 255, 255); box-shadow: 1px 3px 10px #C4DDEB;">
                                                                        <h2 style="font-weight: 700;" align="center">Time Left</h2>
                
                                                                        <div class="row">
                                                                            <div class="col-2">
                                                                                
                                                                            </div>
                
                                                                            <div class="col-3">
                                                                                <h3 id="examhour">00</h3>
                                                                                <small align="center">Hours</small>
                                                                            </div>
                                                                            
                                                                            <div class="col-3">
                                                                                <h3 id="examminutes">00</h3>
                                                                                <small>Minutes</small>
                                                                            </div>
                                                                            <div class="col-4">
                                                                                <h3 id="examseconds">00</h3>
                                                                                <small>Seconds</small>
                                                                            </div>
                
                                                                        </div>
                
                                                                        <div style="margin-top: 63px; padding: none;">
                                                                        <div class="">
                                                                        <div class="col-lg-1">
                
                                                                        </div>';
                
                                                                            $$countkey = 0;
                                                                            $counquestion_num =1;
                                                                            $countkey = 0;
                
                                                                            if($AssesementCategory  == 'Theory'){
                
                                                                                foreach($getquestionforcontent_keys  as $questionnew)
                                                                                {
                
                
                                                                                        $questionnew;
                    
                                                                                        $counquestion_num++; 
                                                                                        $countkey++;
                    
                                                                                        $newquenum  =   $counquestion_num -1;
                                                                                        $questionkey2  =  $countkey -1;
                                                                                        
                                                                                        $slit2arr; 

                                                                                        

                                                                                        
                                                                                
                                                                                        $Select1 = "SELECT * FROM  `assignmentquestion` WHERE  `CampusID` = '$CampusID'  AND AssignmentQuestionID='$questionnew' ORDER BY AssignmentQuestionID ASC";
                                                                                        $selectQuery1 = mysqli_query($link, $Select1);
                                                                                        $fetch1 = mysqli_fetch_array($selectQuery1);
                
                                                                            
                                                                                        $question =  $fetch1['question'];
                                                                                        $optionA =  $fetch1['optionA'];
                                                                                        $optionB =  $fetch1['optionB'];
                                                                                        $optionC =  $fetch1['optionC'];
                                                                                        $optionD =  $fetch1['optionD'];
                    
                                                                                        $QuestionTypenew =  $fetch1['AssignmentCategory'];
                    
                                                                                        $cbt_setting_id =  $fetch1['AssignmentQuestionID'];
                                                                                    
                    
                                                                                        if($QuestionTypenew == 'Theory')
                                                                                        {
                                                                                            $questiontype ='Theory';
                    
                                                                                        }

                                                                                        
                                                                                        echo ' <button style="font-weight: 600; font-size: 12px;margin: 2px; background-color:#0d6efd;" " type="button" data-question="'.$questiontype.'" class="btn btn-success viewer" id="'.$newquenum.'" data-id="'.$questionkey2.'">'.$newquenum.'</button>';
                                                                                                                
                                                                                    }
                
                                                                            }                                                                      
                                                                        echo '</div>
                                                                        <div class="row mt-3">
                                                                        <div class="col-8">
                                                                            
                                                                        </div>
                                                                        <div class="col-4">
                                                                            <button type="button" class="btn waves-effect waves-light btn-success" data-bs-toggle="modal" data-bs-target="#pros-submitexam"  style="font-size: 15px; background-color:#4db8ff; border: #2CEB6C; margin-top: 30px;">Submit</button>
                                                                        </div>
                                                                        </div>
                                                                        </div>
                                                                        </div>
                
                                                                        </div>';
                                                                         
                                                                          

                                                                    }


                                    }

                                    // If the CBT Settings does not Exist
                                    else
                                    {
                                    echo 'No CBT Settings';

                                    }







                            }
                        

        // assignment content here


        }else
        {








                                                                                                                        // Get CBT Category
                                                                $selectquestionset = mysqli_query($link,"SELECT * FROM `cbtsetquestionssettings` 
                                                                WHERE `cbtsetquestionssettingsID`='$cbtSettingsID'");

                                                        $selectquestionsetrow = mysqli_num_rows($selectquestionset);
                                                        $selectquestionsetrow_cnt = mysqli_fetch_assoc($selectquestionset);

                                                        if($selectquestionsetrow > 0)
                                                        {


                                                        $AssesementCategory = $selectquestionsetrow_cnt['AssesementCategory'];
                                                        $CampusIDnew = $selectquestionsetrow_cnt['CampusID'];


                                                        $selectinstitution = mysqli_query($link, "SELECT * FROM `campus` WHERE `CampusID`='$CampusIDnew'");

                                                        $selectinstitution_cnt = mysqli_num_rows($selectinstitution);
                                                        $selectinstitutionrow = mysqli_fetch_assoc($selectinstitution);


                                                        $InstitutionID = $selectinstitutionrow['InstitutionID'];


                                                        // If its Objective Check if the objective status is 1 (Meaning the student has written his exams)
                                                        if($AssesementCategory == 'Objective'){

                                                        // Check if Student has been Inserted into the Student Answer Table
                                                        $check_if_student_exist = mysqli_query($link,"SELECT * FROM `cbtquestionsanswers` 
                                                                                WHERE `cbtquestionsanswers`.`cbtsetquestionssettingsID`='$cbtSettingsID'
                                                                                AND `StudentID` = '$StudentID'
                                                                                AND  `objective_status`= '1'");

                                                        $exist_num = mysqli_num_rows($check_if_student_exist);
                                                        $exist_fetch = mysqli_fetch_assoc($check_if_student_exist);

                                                        if($exist_num > 0){

                                                             echo 'Student Have Answered Objective';

                                                        }


                                                        // Student Have Not Answered Objective (Load the Objective Question)
                                                        else{



                                                        echo '<div class="row p-3">
                                                        <div class="col-lg-8" style="margin-top: 20px;">';

                                                        // Get CBT Settings and Questions



                                                        if($examtype  == 'admission')
                                                        {


                                                                $selectquestionset = mysqli_query($link,"SELECT * FROM `cbtsetquestionssettings` 
                                                                INNER JOIN `classtemplate` 
                                                                ON `cbtsetquestionssettings`.`ClassOrDepartmentID` = `classtemplate`.ClassTemplateID 
                                                                INNER JOIN `subjectorcourse` 
                                                                ON `cbtsetquestionssettings`.`SubjectOrCourseID` = `subjectorcourse`.SubjectOrCourseID
                                                                
                                                                INNER JOIN `session`
                                                                ON `cbtsetquestionssettings`.`sessionID` = `session`.`sessionName`
                                                                WHERE `cbtsetquestionssettings`.`cbtsetquestionssettingsID`='$cbtSettingsID'");



                                                        }else
                                                        {


                                                            $selectquestionset = mysqli_query($link,"SELECT * FROM `cbtsetquestionssettings` 
                                                            INNER JOIN `classordepartment` 
                                                            ON `cbtsetquestionssettings`.`ClassOrDepartmentID` = `classordepartment`.ClassOrDepartmentID 
                                                            INNER JOIN `subjectorcourse` 
                                                            ON `cbtsetquestionssettings`.`SubjectOrCourseID` = `subjectorcourse`.SubjectOrCourseID
                                                            
                                                            INNER JOIN `session`
                                                            ON `cbtsetquestionssettings`.`sessionID` = `session`.`sessionName`
                                                            WHERE `cbtsetquestionssettings`.`cbtsetquestionssettingsID`='$cbtSettingsID'");

                                                        }

                                                                                        
                                                        $selectquestionsetrow = mysqli_num_rows($selectquestionset);
                                                        $selectquestionsetrow_cnt = mysqli_fetch_assoc($selectquestionset);

                                                        // If the CBT Settings Exist
                                                        if($selectquestionsetrow > 0)
                                                        {



                                                        $cbtsetquestionssettingsID = $selectquestionsetrow_cnt['cbtsetquestionssettingsID'];
                                                        $ShuffleOption = $selectquestionsetrow_cnt['ShuffleOption'];
                                                        $CampusID = $selectquestionsetrow_cnt['CampusID'];
                                                        $TermOrSemesterID = $selectquestionsetrow_cnt['TermOrSemesterID'];
                                                        $SubjectOrCourseID = $selectquestionsetrow_cnt['SubjectOrCourseID'];
                                                        $sessionID = $selectquestionsetrow_cnt['sessionName'];
                                                        $ClassOrDepartmentID = $selectquestionsetrow_cnt['ClassOrDepartmentID'];


                                                        $AssesementCategory = $selectquestionsetrow_cnt['AssesementCategory'];


                                                        $TheoryQuestion = explode(',',$selectquestionsetrow_cnt['TheoryQuestion']);

                                                        $Questions = explode(',',$selectquestionsetrow_cnt['question']);
                                                        $answer = explode(',',$selectquestionsetrow_cnt['answer']);

                                                        // print_r($Questions);

                                                        $numquestion = count($Questions);

                                                        $AssesementStartTime = $selectquestionsetrow_cnt['AssesementStartTime'];
                                                        $AssesementEndTime  =  $selectquestionsetrow_cnt['AssesementEndTime'];


                                                        $timeIn = new DateTime($AssesementStartTime);
                                                        $timeOut = new DateTime($AssesementEndTime);

                                                        date_default_timezone_set('Africa/Lagos');

                                                        // Calculate the time difference in seconds
                                                        $timeDiffInSeconds = $timeOut->getTimestamp() - $timeIn->getTimestamp();

                                                        // Convert the time difference to hours, minutes, and seconds
                                                        $hour = floor($timeDiffInSeconds / 3600);
                                                        $minute = floor(($timeDiffInSeconds % 3600) / 60);
                                                        $seconds = $timeDiffInSeconds % 60;

                                                        // Format the values as strings with leading zeros
                                                        $hour = str_pad($hour, 2, '0', STR_PAD_LEFT);
                                                        $minute = str_pad($minute, 2, '0', STR_PAD_LEFT);
                                                        $seconds = str_pad($seconds, 2, '0', STR_PAD_LEFT);

                                                        $totaldiff = ($hour . ':' . $minute . ':' . $seconds);

                                                        $midnight = strtotime('12:00:00 AM');
                                                        $midtwo =  strtotime(date('h:i:sa',$midnight));

                                                        echo '<input type="hidden" id="prosmidnighttime" value="'.$midtwo.'">
                                                        <input type="hidden" id="prostimedifference" value="'.$totaldiff.'">';


                                                        }

                                                        // If the CBT Settings does not Exist
                                                        else
                                                        {
                                                             echo 'No CBT Settings';

                                                        }


                                                        //    echo $ShuffleOption ;
                                                        $countkey = 0;
                                                        $counquestion_num =1;
                                                        $countkey = 0;


                                                        echo '<div class="card-body" style="background-color: rgb(255, 255, 255); height: 100%; width: 100%; box-shadow: 1px 3px 10px #C4DDEB;">';


                                                        if($AssesementCategory == 'Objective'){



                                                        // echo $AssesementCategory;


                                                        // Check if Student has been Inserted into the Student Answer Table
                                                        $check_if_student_exist = mysqli_query($link,"SELECT * FROM `cbtquestionsanswers` 
                                                                                                WHERE `cbtquestionsanswers`.`cbtsetquestionssettingsID`='$cbtSettingsID'
                                                                                                AND   `StudentID` = '$StudentID'
                                                                                                AND `CampusID` = '$CampusID'
                                                                                                AND  `TermOrSemesterID` = '$TermOrSemesterID'
                                                                                                AND `ClassOrDepartmentID` = '$ClassOrDepartmentID'
                                                                                                AND `SubjectOrCourseID` = '$SubjectOrCourseID'
                                                                                                AND  `objective_status` = '0'");
                                                                            
                                                        $exist_num = mysqli_num_rows($check_if_student_exist);
                                                        $exist_fetch = mysqli_fetch_assoc($check_if_student_exist);

                                                        // Check if Student Exist and has not Submitted
                                                        if($exist_num > 0){


                                                            $question = explode(',',$exist_fetch['question']);
                                                            $answer = explode(',',$exist_fetch['Answer']);
                                                            
                                                            
                                                           

                                                            foreach($question as $arrquestion)
                                                            {

                                                                 $arrquestion;
                                                                $studcorectans[] = 0;



                                                                $select_question = "SELECT * FROM `questionbank` WHERE `QuestionBankID`='$arrquestion' AND CampusID = '$CampusID' GROUP BY QuestionBankID ORDER BY QuestionBankID ASC";
                                                                $select_question_result = mysqli_query($link, $select_question);
                                                                $select_question_result_cnt = mysqli_num_rows($select_question_result);
                                                                $select_question_result_cnt_row = mysqli_fetch_assoc($select_question_result);
                                                            
                                                                $qustionall = $select_question_result_cnt_row['question'];
                                                                $Questionid_1 = $select_question_result_cnt_row['QuestionBankID'];
                                                                $optionA = $select_question_result_cnt_row['optionA'];
                                                                $optionB = $select_question_result_cnt_row['optionB'];
                                                                $optionC = $select_question_result_cnt_row['optionC'];
                                                                $optionD = $select_question_result_cnt_row['optionD'];
                                                                $canswer = $select_question_result_cnt_row['answer'];

                                                                $QuestionType = $select_question_result_cnt_row['QuestionType'];

                                                                if($QuestionType == 'Objective')
                                                                {
                                                                    

                                                                    $questionobl = (object) [
                                                                        'question' => $qustionall,
                                                                        'optionA' => $optionA,
                                                                        'optionB' => $optionB,
                                                                        'optionC' => $optionC,
                                                                        'optionD' => $optionD,
                                                                        
                                                                    ];

                                                                        $arraynew[] = $questionobl;
                                                                        $correctanswer[] = $canswer;

                                                                }else
                                                                {

                                                                    $questionobl = (object) [
                                                                        'question' => $qustionall
                                                                    ];

                                                                    $arraynew[] = $questionobl;
                                                                    $correctanswer[] = 0;

                                                                }
                                                                
                                                                
                                                            }

                                                            
                                                                $ecodedquestions = json_encode($arraynew,JSON_FORCE_OBJECT);
                                                                $currectanswer = implode(',',$answer);
                                                                $studentdefaultansw =  implode(',',$studcorectans);
                                                                $imploadquestionforloc = implode(',',$question);

                                                                echo '<textarea hidden="hidden" value="" id="pros-loadjsoncontentforcbt">'.$ecodedquestions.'</textarea>';
                                                                echo '<input type="hidden" value="'.$imploadquestionforloc.'" id="tari_question_default">';

                                                                echo '<input type="hidden" value="'.$AssesementCategory.'" id="pros-loadexamtypehere">';

                                                                echo '<input type="hidden" value="0" id="nxtprevquestion">
                                                                    <textarea hidden="hidden"  id="currentanwernxt">'.$studentdefaultansw.'</textarea>
                                                                    <input type="hidden" value="1" id="questionNo">';
                                                                    echo '<input type="hidden" id="currectquestionID" value="'.$currectanswer.'">';



                                                                $countQ = 0;
                                                                $settingarrfirt =  $Questions[0];
                                                                $Select = "SELECT * FROM `questionbank` WHERE `QuestionBankID`='$settingarrfirt' AND 
                                                                CampusID = '$CampusID' ORDER BY `QuestionBankID` ASC LIMIT 1";

                                                                $selectQuery = mysqli_query($link, $Select);
                                                                $fetch2 = mysqli_num_rows($selectQuery);
                                                            
                                                            //If Question Doesnt Exist 
                                                            if($fetch2 == 0){
                                                                                        
                                                                echo '<div class="alert alert-info alert-dismissible d-flex align-items-center show">
                                                                        <i class="fa fa-info-circle"></i>
                                                                    <strong class="mx-2">Hey!</strong>Please no question attached to this settings.
                                                                    </div>
                                                                    <a  href="cbtSetting.php"  class="close" aria-label="Close" style="color:red; margin-right:20px;"><i clas="fa fa-back"></i>Back
                                                                    </a>';
                                                            }
                                                            //Question ID Exist
                                                            else{
                                                                while($fetch = mysqli_fetch_array($selectQuery))
                                                                {
                                                                    $countQ =  $countQ + 1;
                                                                    //    $rowid1 =    $Selectsettings_result_cnt_row['cbtsetquestionssettings'];  

                                                                    $questionfirst = $fetch['question'];
                                                                    $optionA =  $fetch['optionA'];
                                                                    $optionB =  $fetch['optionB'];
                                                                    $optionC =  $fetch['optionC'];
                                                                    $optionD =  $fetch['optionD'];

                                                                    
                                                                    // Replace <p></p> with empty space
                                                                    $optionA1 = str_replace(array('<p>', '</p>'), '', $optionA);
                                                                    $optionB1 = str_replace(array('<p>', '</p>'), '', $optionB);
                                                                    $optionC1 = str_replace(array('<p>', '</p>'), '', $optionC);
                                                                    $optionD1 = str_replace(array('<p>', '</p>'), '', $optionD);

                                                                    // Replace < with &lt;
                                                                    $optionAA = str_replace('<', '&lt;', $optionA1);
                                                                    $optionBB = str_replace('<', '&lt;', $optionB1);
                                                                    $optionCC = str_replace('<', '&lt;', $optionC1);
                                                                    $optionDD = str_replace('<', '&lt;', $optionD1);


                                                                    $QuestionTypeFirst =  $fetch['QuestionType'];
                                                                    
                                                                    $rowid =  $fetch['QuestionBankID'];

                                                                    echo '<div id ="displayResults"> 

                                                                        <h1 style="font-style: italic; margin-top: 10px;">(Question '.$countQ.')</h1>

                                                                        <div class="myborder"></div>

                                                                            <div style="margin-top: 50px; margin-left: 20px;">
                                                                                <h4>'.$questionfirst.'</h4>';
                                                                                    
                                                                                    if($QuestionTypeFirst  == 'Objective')
                                                                                    {
                                                                                        echo '<form class="selectSubject">
                                                                                            <label>
                                                                                                <input type="radio" name="group1" class="radioBtnClass"  value="A"  id="radio_1"/>
                                                                                                <span>A. '.$optionAA.'</span>
                                                                                            </label>
                                                                                            <label>
                                                                                                <input type="radio" name="group1" class="radioBtnClass"  value="B"  id="radio_2"/>
                                                                                                <span>B. '.$optionBB.'</span>
                                                                                            </label>
                                                                                            <label>
                                                                                                <input type="radio" name="group1" class="radioBtnClass"  value="C"  id="radio_3"/>
                                                                                                <span>C. '.$optionCC.'</span>
                                                                                            </label>
                                                                                            <label>
                                                                                                <input type="radio" name="group1" class="radioBtnClass"  value="D"  id="radio_4"/>
                                                                                                <span>D. '.$optionDD.'</span>
                                                                                            </label>
                                                                                        </form>';

                                                                                    }elseif($QuestionTypeFirst  == 'Theory')
                                                                                    {
                                                                                    echo '<textarea   id="optionA1" class="wrs_div_box example question" ></textarea>';
                                                                                    }

                                                                            echo '</div>

                                                                        <div class="row" style="margin-top: 50px;">
                                                                            <hr/>
                                                                            <div class="col-8">
                                                                                <!-- <button type="button" style="font-size: 15px; background-color:#0d6efd;" class="btn waves-effect waves-light btn-rounded">Previous</button> -->
                                                                            </div>

                                                                            <div class="col-4">
                                                                                <button type="button" style="font-size: 15px;" class="btn waves-effect waves-light btn-rounded btn-secondary next float-end">Next</button>
                                                                            </div>
                                                                        </div>

                                                                        <div class="row mt-3" >
                                                                            <div class="col-lg-12">
                                                                                <div>
                                                                                    <small style="color: red; font-size: 12px; text-align: justify;">
                                                                                        <b>Note:</b> Kindly pick the correct option using the mouse to select the answer to the question
                                                                                        by the right before the time runs out. You can also select any question below to answer. 
                                                                                        Don\'t forget to click Submit when you are done.
                                                                                    </small>
                                                                                </div>
                                                                            </div>
                                                                        </div>
                                                                    
                                                                    </div>';

                                                                }                     
                                                            }

                                                        }
                                                        else{
                                                            

                                                        


                                                            if($ShuffleOption == '1'){


                                                                
                                                                $Questions = explode(',',$selectquestionsetrow_cnt['question']);
                                                                $answer = explode(',',$selectquestionsetrow_cnt['answer']);


                                                                $studcorectans = array();

                                                                // Sample questions and answers arrays
                                                                // print_r($Questions);
                                                                // print_r($answer);

                                                                $questions = $Questions;
                                                                $answers = $answer;
                                                            
                                                                // Combine questions and answers into an array of arrays
                                                                $combined = array();
                                                                
                                                                foreach ($questions as $index => $question) {
                                                                    $combined[] = array('question' => $question, 'answer' => $answers[$index]);
                                                                }

                                                                // Shuffle the combined array
                                                            shuffle($combined);

                                                                // Separate the shuffled array back into questions and answers
                                                                $shuffledQuestions = array();
                                                                $shuffledAnswers = array();

                                                                foreach ($combined as $item) {
                                                                    $shuffledQuestions1[] = $item['question'];
                                                                    $shuffledAnswers1[] = $item['answer'];
                                                                }

                                                                $shuffledQuestions = implode(',', $shuffledQuestions1);
                                                                $shuffledAnswers = implode(',', $shuffledAnswers1);

                                                                // Insert the Question and Answer Into the CBT Questions and Answer Table
                                                                $insert_shuffled_question = mysqli_query($link, "INSERT INTO `cbtquestionsanswers` (`cbtsetquestionssettingsID`,`StudentID`,`CampusID`,`sessionID`,`TermOrSemesterID`,`ClassOrDepartmentID`,`SubjectOrCourseID`,`question`,`Answer`) 
                                                                    VALUES ('$cbtsetquestionssettingsID','$StudentID','$CampusID','$sessionID','$TermOrSemesterID','$ClassOrDepartmentID','$SubjectOrCourseID','$shuffledQuestions','$shuffledAnswers')");


                                                                    //    echo mysqli_error($link);

                                                                if($insert_shuffled_question){

                                                                    foreach($shuffledQuestions1 as $arrquestion)
                                                                    {

                                                                        $arrquestion;
                                                                        $studcorectans[] = 0;

                                                                

                                                                        $select_question = "SELECT * FROM `questionbank` WHERE `QuestionBankID`='$arrquestion' AND InstitutionID = '$InstitutionID' GROUP BY QuestionBankID ASC";

                                                                        $select_question_result = mysqli_query($link, $select_question);
                                                                        $select_question_result_cnt = mysqli_num_rows($select_question_result);
                                                                        $select_question_result_cnt_row = mysqli_fetch_assoc($select_question_result);
                                                                    
                                                                        $qustionall = $select_question_result_cnt_row['question'];
                                                                        $Questionid_1 = $select_question_result_cnt_row['QuestionBankID'];
                                                                        $optionA = $select_question_result_cnt_row['optionA'];
                                                                        $optionB = $select_question_result_cnt_row['optionB'];
                                                                        $optionC = $select_question_result_cnt_row['optionC'];
                                                                        $optionD = $select_question_result_cnt_row['optionD'];

                                                                        $canswer = $select_question_result_cnt_row['answer'];

                                                                        $QuestionType = $select_question_result_cnt_row['QuestionType'];

                                                                        if($QuestionType == 'Objective')
                                                                        {
                                                                            

                                                                            $questionobl = (object) [
                                                                                'question' => $qustionall,
                                                                                'optionA' => $optionA,
                                                                                'optionB' => $optionB,
                                                                                'optionC' => $optionC,
                                                                                'optionD' => $optionD,
                                                                                
                                                                            ];

                                                                                $arraynew[] = $questionobl;
                                                                                $correctanswer[] = $canswer;

                                                                        }else
                                                                        {

                                                                        }

                                                                
                                                                    }


                                                            

                                                                    $ecodedquestions = json_encode($arraynew,JSON_FORCE_OBJECT);
                                                                    $currectanswer = implode(',',$shuffledAnswers1);
                                                                    $studentdefaultansw =  implode(',',$studcorectans);
                                                                    $imploadquestionforloc = implode(',',$shuffledQuestions1);

                                                                    echo '<textarea hidden="hidden" value="" id="pros-loadjsoncontentforcbt">'.$ecodedquestions.'</textarea>';
                                                                    echo '<input type="hidden" value="'.$imploadquestionforloc.'" id="tari_question_default">';

                                                                    echo '<input type="hidden" value="'.$AssesementCategory.'" id="pros-loadexamtypehere">';

                                                                    echo '<input type="hidden" value="0" id="nxtprevquestion">
                                                                        <textarea hidden="hidden"  id="currentanwernxt">'.$studentdefaultansw.'</textarea>
                                                                        <input type="hidden" value="1" id="questionNo">';
                                                                    echo '<input type="hidden" id="currectquestionID" value="'.$currectanswer.'">';



                                                                    $countQ = 0;
                                                                $settingarrfirt =  $Questions[0];

                                                                    $Select = "SELECT * FROM `questionbank` WHERE `QuestionBankID`='$settingarrfirt' AND  InstitutionID = '$InstitutionID'GROUP BY QuestionBankID ASC  LIMIT 1";
                                                                    $selectQuery = mysqli_query($link, $Select);
                                                                    $fetch2 = mysqli_num_rows($selectQuery);

                                                                    if($fetch2 == 0){
                                                                                                
                                                                        echo '<div class="alert alert-info alert-dismissible d-flex align-items-center show">
                                                                                <i class="fa fa-info-circle"></i>
                                                                            <strong class="mx-2">Hey!</strong>Please no question attached to this settings.
                                                                            </div>
                                                                            <a  href="cbtSetting.php"  class="close" aria-label="Close" style="color:red; margin-right:20px;"><i clas="fa fa-back"></i>Back
                                                                            </a>'
                                                                                ;
                                                                    }
                                                                    else{


                                                                        while($fetch = mysqli_fetch_array($selectQuery))
                                                                        {
                                                                            $countQ =  $countQ + 1;
                                                                            //    $rowid1 =    $Selectsettings_result_cnt_row['cbtsetquestionssettings'];  
                                                                            $questionfirst = $fetch['question'];
                                                                            $optionA =  $fetch['optionA'];
                                                                            $optionB =  $fetch['optionB'];
                                                                            $optionC =  $fetch['optionC'];
                                                                            $optionD =  $fetch['optionD'];

                                                                            
                                                                            // Replace <p></p> with empty space
                                                                            $optionA1 = str_replace(array('<p>', '</p>'), '', $optionA);
                                                                            $optionB1 = str_replace(array('<p>', '</p>'), '', $optionB);
                                                                            $optionC1 = str_replace(array('<p>', '</p>'), '', $optionC);
                                                                            $optionD1 = str_replace(array('<p>', '</p>'), '', $optionD);

                                                                            // Replace < with &lt;
                                                                            $optionAA = str_replace('<', '&lt;', $optionA1);
                                                                            $optionBB = str_replace('<', '&lt;', $optionB1);
                                                                            $optionCC = str_replace('<', '&lt;', $optionC1);
                                                                            $optionDD = str_replace('<', '&lt;', $optionD1);


                                                                        $QuestionTypeFirst =  $fetch['QuestionType'];
                                                                            
                                                                            $rowid =  $fetch['QuestionBankID'];

                                                                            echo '<div id ="displayResults"> 

                                                                                <h1 style="font-style: italic; margin-top: 10px;">(Question '.$countQ.')</h1>

                                                                                <div class="myborder"></div>

                                                                                    <div style="margin-top: 50px; margin-left: 20px;">
                                                                                        <h4>'.$questionfirst.'</h4>';
                                                                                            
                                                                                            if($QuestionTypeFirst  == 'Objective')
                                                                                            {
                                                                                                echo '<form class="selectSubject">
                                                                                                    <label>
                                                                                                        <input type="radio" name="group1" class="radioBtnClass"  value="A"  id="radio_1"/>
                                                                                                        <span>A. '.$optionAA.'</span>
                                                                                                    </label>
                                                                                                    <label>
                                                                                                        <input type="radio" name="group1" class="radioBtnClass"  value="B"  id="radio_2"/>
                                                                                                        <span>B. '.$optionBB.'</span>
                                                                                                    </label>
                                                                                                    <label>
                                                                                                        <input type="radio" name="group1" class="radioBtnClass"  value="C"  id="radio_3"/>
                                                                                                        <span>C. '.$optionCC.'</span>
                                                                                                    </label>
                                                                                                    <label>
                                                                                                        <input type="radio" name="group1" class="radioBtnClass"  value="D"  id="radio_4"/>
                                                                                                        <span>D. '.$optionDD.'</span>
                                                                                                    </label>
                                                                                                </form>';

                                                                                            }elseif($QuestionTypeFirst  == 'Theory')
                                                                                            {
                                                                                            echo '<textarea   id="optionA1" class="wrs_div_box example question" ></textarea>';
                                                                                            }

                                                                                    echo '</div>

                                                                                <div class="row" style="margin-top: 50px;">
                                                                                    <hr/>
                                                                                    <div class="col-8">
                                                                                        <!-- <button type="button" style="font-size: 15px; background-color:#0d6efd;" class="btn waves-effect waves-light btn-rounded">Previous</button> -->
                                                                                    </div>

                                                                                    <div class="col-4">
                                                                                        <button type="button" style="font-size: 15px; " class="btn waves-effect waves-light btn-rounded btn-secondary next float-end">Next</button>
                                                                                    </div>
                                                                                </div>

                                                                                <div class="row mt-3" >
                                                                                    <div class="col-lg-12">
                                                                                        <div>
                                                                                            <small style="color: red; font-size: 12px; text-align: justify;">
                                                                                                <b>Note:</b> Kindly pick the correct option using the mouse to select the answer to the question
                                                                                                by the right before the time runs out. You can also select any question below to answer. 
                                                                                                Don\'t forget to click Submit when you are done.
                                                                                            </small>
                                                                                        </div>
                                                                                    </div>
                                                                                </div>
                                                                            
                                                                            </div>';

                                                                        }                     
                                                                    }

                                                                }else{
                                                                    echo 'Not Inserted';
                                                                }

                                                            }
                                                            elseif($ShuffleOption == '0'){

                                                                $Question = implode(',',$Questions);
                                                                $Answer = implode(',',$answer);
                                                            
                                                                $studcorectans = array();

                                                                // Insert the Question and Answer Into the CBT Questions and Answer Table
                                                                $insert_shuffled_question = mysqli_query($link, "INSERT INTO `cbtquestionsanswers` (`cbtsetquestionssettingsID`,`StudentID`,`CampusID`,`sessionID`,`TermOrSemesterID`,`ClassOrDepartmentID`,`SubjectOrCourseID`,`question`,`Answer`) 
                                                                                                        VALUES ('$cbtsetquestionssettingsID','$StudentID','$CampusID','$sessionID','$TermOrSemesterID','$ClassOrDepartmentID','$SubjectOrCourseID','$Question','$Answer')");



                                                                    // echo mysqli_error($link);

                                                                // if($insert_shuffled_question){

                                                                    foreach($Questions as $arrquestion)
                                                                    {

                                                                        $arrquestion;
                                                                        $studcorectans[] = 0;

                                                                        $select_question = "SELECT * FROM `questionbank` WHERE `QuestionBankID`='$arrquestion' AND InstitutionID = '$InstitutionID' GROUP BY QuestionBankID ASC";
                                                                        $select_question_result = mysqli_query($link, $select_question);
                                                                        $select_question_result_cnt = mysqli_num_rows($select_question_result);
                                                                        $select_question_result_cnt_row = mysqli_fetch_assoc($select_question_result);

                                                                        $qustionall = $select_question_result_cnt_row['question'];
                                                                        $optionA = $select_question_result_cnt_row['optionA'];
                                                                        $optionB = $select_question_result_cnt_row['optionB'];
                                                                        $optionC = $select_question_result_cnt_row['optionC'];
                                                                        $optionD = $select_question_result_cnt_row['optionD'];

                                                                        $canswer = $select_question_result_cnt_row['answer'];

                                                                        $QuestionType = $select_question_result_cnt_row['QuestionType'];

                                                                        if($QuestionType == 'Objective')
                                                                        {


                                                                            $questionobl = (object) [
                                                                                'question' => $qustionall,
                                                                                'optionA' => $optionA,
                                                                                'optionB' => $optionB,
                                                                                'optionC' => $optionC,
                                                                                'optionD' => $optionD,
                                                                                
                                                                            ];

                                                                                $arraynew[] = $questionobl;
                                                                                $correctanswer[] = $canswer;

                                                                        }
                                                                        else
                                                                        {

                                                                            $questionobl = (object) [
                                                                                'question' => $qustionall
                                                                            ];

                                                                            $arraynew[] = $questionobl;
                                                                                $correctanswer[] = 0;

                                                                        }


                                                                    }

                                                                        $ecodedquestions = json_encode($arraynew,JSON_FORCE_OBJECT);
                                                                        $currectanswer = implode(',',$answer);
                                                                        $studentdefaultansw =  implode(',',$studcorectans);
                                                                        $imploadquestionforloc = implode(',',$Questions);

                                                                        echo '<textarea hidden="hidden" value="" id="pros-loadjsoncontentforcbt">'.$ecodedquestions.'</textarea>';
                                                                        echo '<input type="hidden" value="'.$imploadquestionforloc.'" id="tari_question_default">';

                                                                        echo '<input type="hidden" value="'.$AssesementCategory.'" id="pros-loadexamtypehere">';

                                                                        echo '<input type="hidden" value="0" id="nxtprevquestion">
                                                                        <textarea hidden="hidden"  id="currentanwernxt">'.$studentdefaultansw.'</textarea>
                                                                        <input type="hidden" value="1" id="questionNo">';
                                                                        echo '<input type="hidden" id="currectquestionID" value="'.$currectanswer.'">';



                                                                        $countQ = 0;
                                                                        $settingarrfirt =  $Questions[0];
                                                                        $Select = "SELECT * FROM `questionbank` WHERE `QuestionBankID`='$settingarrfirt' AND  CampusID = '$CampusID' GROUP BY QuestionBankID ASC  LIMIT 1";
                                                                        $selectQuery = mysqli_query($link, $Select);
                                                                    $fetch2 = mysqli_num_rows($selectQuery);

                                                                    if($fetch2 == 0){
                                                                                    
                                                                        echo '<div class="alert alert-info alert-dismissible d-flex align-items-center show">
                                                                        <i class="fa fa-info-circle"></i>
                                                                        <strong class="mx-2">Hey!</strong>Please no question attached to this settings.
                                                                        </div>
                                                                        <a  href="cbtSetting.php"  class="close" aria-label="Close" style="color:red; margin-right:20px;"><i clas="fa fa-back"></i>Back
                                                                        </a>' ;

                                                                    }
                                                                    else
                                                                    {
                                                                        while($fetch = mysqli_fetch_array($selectQuery))
                                                                        {

                                                                            $countQ =  $countQ + 1;
                                                                            //    $rowid1 =    $Selectsettings_result_cnt_row['cbtsetquestionssettings'];  
                                                                            $questionfirst = $fetch['question'];
                                                                            $optionA =  $fetch['optionA'];
                                                                            $optionB =  $fetch['optionB'];
                                                                            $optionC =  $fetch['optionC'];
                                                                            $optionD =  $fetch['optionD'];


                                                                            // Replace <p></p> with empty space
                                                                            $optionA1 = str_replace(array('<p>', '</p>'), '', $optionA);
                                                                            $optionB1 = str_replace(array('<p>', '</p>'), '', $optionB);
                                                                            $optionC1 = str_replace(array('<p>', '</p>'), '', $optionC);
                                                                            $optionD1 = str_replace(array('<p>', '</p>'), '', $optionD);

                                                                            // Replace < with &lt;
                                                                            $optionAA = str_replace('<', '&lt;', $optionA1);
                                                                            $optionBB = str_replace('<', '&lt;', $optionB1);
                                                                            $optionCC = str_replace('<', '&lt;', $optionC1);
                                                                            $optionDD = str_replace('<', '&lt;', $optionD1);


                                                                            $QuestionTypeFirst =  $fetch['QuestionType'];

                                                                            $rowid =  $fetch['QuestionBankID'];

                                                                            echo '<div id ="displayResults"> 

                                                                            <h1 style="font-style: italic; margin-top: 10px;">(Question '.$countQ.')</h1>

                                                                            <div class="myborder"></div>

                                                                                <div style="margin-top: 50px; margin-left: 20px;">
                                                                                    <h4>'.$questionfirst.'</h4>';
                                                                                        
                                                                                        if($QuestionTypeFirst  == 'Objective')
                                                                                        {
                                                                                            echo '<form class="selectSubject">
                                                                                                <label>
                                                                                                    <input type="radio" name="group1" class="radioBtnClass"  value="A"  id="radio_1"/>
                                                                                                    <span>A. '.$optionAA.'</span>
                                                                                                </label>
                                                                                                <label>
                                                                                                    <input type="radio" name="group1" class="radioBtnClass"  value="B"  id="radio_2"/>
                                                                                                    <span>B. '.$optionBB.'</span>
                                                                                                </label>
                                                                                                <label>
                                                                                                    <input type="radio" name="group1" class="radioBtnClass"  value="C"  id="radio_3"/>
                                                                                                    <span>C. '.$optionCC.'</span>
                                                                                                </label>
                                                                                                <label>
                                                                                                    <input type="radio" name="group1" class="radioBtnClass"  value="D"  id="radio_4"/>
                                                                                                    <span>D. '.$optionDD.'</span>
                                                                                                </label>
                                                                                            </form>';

                                                                                        }elseif($QuestionTypeFirst  == 'Theory')
                                                                                        {
                                                                                        echo '<textarea   id="optionA1" class="wrs_div_box example question" ></textarea>';
                                                                                        }

                                                                                echo '</div>

                                                                            <div class="row" style="margin-top: 50px;">
                                                                                <hr/>
                                                                                <div class="col-8">
                                                                                    <!-- <button type="button" style="font-size: 15px; background-color:#0d6efd" class="btn waves-effect waves-light btn-rounded">Previous</button> -->
                                                                                </div>

                                                                                <div class="col-4">
                                                                                    <button type="button" style="font-size: 15px; " class="btn waves-effect waves-light btn-rounded btn-secondary next float-end">Next</button>
                                                                                </div>
                                                                            </div>

                                                                            <div class="row mt-3" >
                                                                                <div class="col-lg-12">
                                                                                    <div>
                                                                                        <small style="color: red; font-size: 12px; text-align: justify;">
                                                                                            <b>Note:</b> Kindly pick the correct option using the mouse to select the answer to the question
                                                                                            by the right before the time runs out. You can also select any question below to answer. 
                                                                                            Don\'t forget to click Submit when you are done.
                                                                                        </small>
                                                                                    </div>
                                                                                </div>
                                                                            </div>

                                                                            </div>';

                                                                        }                     
                                                                    }

                                                            
                                                            }
                                                        }

                                                        }else{

                                                        echo "No Category Found";
                                                        }


                                                        echo'</div>   
                                                        </div>';

                                                        echo'<div  class="col-lg-4" style="margin-top: 20px;">
                                                        <div class="card">
                                                        <div class="card-body" style="background-color: rgb(255, 255, 255); box-shadow: 1px 3px 10px #C4DDEB;">
                                                        <h2 style="font-weight: 700;" align="center">Time Left</h2>

                                                        <div class="row">
                                                            <div class="col-2">
                                                                
                                                            </div>

                                                            <div class="col-3">
                                                                <h3 id="examhour">00</h3>
                                                                <small align="center">Hours</small>
                                                            </div>
                                                            
                                                            <div class="col-3">
                                                                <h3 id="examminutes">00</h3>
                                                                <small>Minutes</small>
                                                            </div>
                                                            <div class="col-4">
                                                                <h3 id="examseconds">00</h3>
                                                                <small>Seconds</small>
                                                            </div>

                                                        </div>

                                                        <div style="margin-top: 63px; padding: none;">
                                                        <div class="">
                                                        <div class="col-lg-1">

                                                        </div>';

                                                            // $AssesementCategory = $QuestionTypeFirst; 

                                                            $countkey = 0;
                                                            $counquestion_num =1;
                                                            $countkey = 0;

                                                            if($AssesementCategory == 'Objective'){

                                                                foreach($Questions as $questionnew)
                                                                {


                                                                    $questionnew;

                                                                    $counquestion_num++; 
                                                                    $countkey++;

                                                                    $newquenum  =   $counquestion_num -1;
                                                                    $questionkey2  =  $countkey -1;
                                                                    
                                                                    $slit2arr; 
                                                            
                                                                    $Select1 = "SELECT * FROM  `questionbank` WHERE  `CampusID` = '$CampusID'  AND QuestionBankID='$questionnew' ORDER BY QuestionBankID ASC";
                                                                    $selectQuery1 = mysqli_query($link, $Select1);
                                                                    $fetch1 = mysqli_fetch_array($selectQuery1);

                                                            
                                                                    $question =  $fetch1['question'];
                                                                    $optionA =  $fetch1['optionA'];
                                                                    $optionB =  $fetch1['optionB'];
                                                                    $optionC =  $fetch1['optionC'];
                                                                    $optionD =  $fetch1['optionD'];

                                                                    $QuestionTypenew =  $fetch1['QuestionType'];

                                                                    $cbt_setting_id =  $fetch1['QuestionBankID'];
                                                                

                                                                    if($QuestionTypenew == 'Objective')
                                                                    {
                                                                        $questiontype ='Objective';

                                                                    }
                                                                    echo ' <button style="font-weight: 600; font-size: 12px;margin: 2px; background-color:#0d6efd;" " type="button" data-question="'.$questiontype.'" class="btn btn-success viewer" id="'.$newquenum.'" data-id="'.$questionkey2.'">'.$newquenum.'</button>';
                                                                                            
                                                                }

                                                            }
                                                                                                                                                                
                                                        echo '</div>
                                                        <div class="row mt-3">
                                                        <div class="col-8">
                                                            
                                                        </div>
                                                        <div class="col-4">
                                                            <button type="button" class="btn waves-effect waves-light btn-success" data-bs-toggle="modal" data-bs-target="#pros-submitexam"  style="font-size: 15px; background-color:#4db8ff; border: #2CEB6C; margin-top: 30px;">Submit</button>
                                                        </div>
                                                        </div>
                                                        </div>
                                                        </div>

                                                        </div>';
                                                        }

                                                        }
                                                        // If its Theory Check if the Theory status is 1 (Meaning the student has written his exams)
                                                        elseif($AssesementCategory == 'Theory')
                                                        {


                                                        // Check if Student has been Inserted into the Student Answer Table
                                                        $check_if_student_exist = mysqli_query($link,"SELECT * FROM `cbtquestionsanswers` 
                                                                                WHERE `cbtquestionsanswers`.`cbtsetquestionssettingsID`='$cbtSettingsID'  AND `StudentID` = '$StudentID'   AND `theory_status` = '1'");
                                                                            
                                                                            

                                                        $exist_num = mysqli_num_rows($check_if_student_exist);
                                                        $exist_fetch = mysqli_fetch_assoc($check_if_student_exist);

                                                        if($exist_num > 0){


                                                        echo 'Student Have Answered Theory';

                                                        }

                                                        // Student Have Not Answered Objective (Load the Objective Question)
                                                        else{



                                                        echo '<div class="row p-3">
                                                        <div class="col-lg-8" style="margin-top: 20px;">';

                                                        // Get CBT Settings and Questions



                                                        if($examtype  == 'admission')
                                                        {


                                                            $selectquestionset = mysqli_query($link,"SELECT * FROM `cbtsetquestionssettings` 
                                                                    INNER JOIN `classtemplate` 
                                                                    ON `cbtsetquestionssettings`.`ClassOrDepartmentID` = `classtemplate`.ClassTemplateID 
                                                                    INNER JOIN `subjectorcourse` 
                                                                    ON `cbtsetquestionssettings`.`SubjectOrCourseID` = `subjectorcourse`.SubjectOrCourseID
                                                                
                                                                    INNER JOIN `session`
                                                                    ON `cbtsetquestionssettings`.`sessionID` = `session`.`sessionName`
                                                                    WHERE `cbtsetquestionssettings`.`cbtsetquestionssettingsID`='$cbtSettingsID'");



                                                        }else
                                                        {

                                                            $selectquestionset = mysqli_query($link,"SELECT * FROM `cbtsetquestionssettings` 
                                                                    INNER JOIN `classordepartment` 
                                                                    ON `cbtsetquestionssettings`.`ClassOrDepartmentID` = `classordepartment`.ClassOrDepartmentID 
                                                                    INNER JOIN `subjectorcourse` 
                                                                    ON `cbtsetquestionssettings`.`SubjectOrCourseID` = `subjectorcourse`.SubjectOrCourseID
                                                                
                                                                    INNER JOIN `session`
                                                                    ON `cbtsetquestionssettings`.`sessionID` = `session`.`sessionName`
                                                                    WHERE `cbtsetquestionssettings`.`cbtsetquestionssettingsID`='$cbtSettingsID'");

                                                        }

                                                                                        
                                                        $selectquestionsetrow = mysqli_num_rows($selectquestionset);
                                                        $selectquestionsetrow_cnt = mysqli_fetch_assoc($selectquestionset);

                                                        // If the CBT Settings Exist
                                                        if($selectquestionsetrow > 0)
                                                        {

                                                        $cbtsetquestionssettingsID = $selectquestionsetrow_cnt['cbtsetquestionssettingsID'];
                                                        $ShuffleOption = $selectquestionsetrow_cnt['ShuffleOption'];
                                                        $CampusID = $selectquestionsetrow_cnt['CampusID'];
                                                        $TermOrSemesterID = $selectquestionsetrow_cnt['TermOrSemesterID'];
                                                        $SubjectOrCourseID = $selectquestionsetrow_cnt['SubjectOrCourseID'];
                                                        $sessionID = $selectquestionsetrow_cnt['sessionName'];
                                                        $ClassOrDepartmentID = $selectquestionsetrow_cnt['ClassOrDepartmentID'];


                                                        $AssesementCategory = $selectquestionsetrow_cnt['AssesementCategory'];


                                                        $TheoryQuestion = explode(',',$selectquestionsetrow_cnt['TheoryQuestion']);

                                                        $Questions = explode(',',$selectquestionsetrow_cnt['question']);
                                                        $answer = explode(',',$selectquestionsetrow_cnt['answer']);

                                                        // print_r($Questions);

                                                        $numquestion = count($Questions);

                                                        $AssesementStartTime = $selectquestionsetrow_cnt['AssesementStartTime'];
                                                        $AssesementEndTime  =  $selectquestionsetrow_cnt['AssesementEndTime'];


                                                        $timeIn = new DateTime($AssesementStartTime);
                                                        $timeOut = new DateTime($AssesementEndTime);

                                                        date_default_timezone_set('Africa/Lagos');

                                                        // Calculate the time difference in seconds
                                                        $timeDiffInSeconds = $timeOut->getTimestamp() - $timeIn->getTimestamp();

                                                        // Convert the time difference to hours, minutes, and seconds
                                                        $hour = floor($timeDiffInSeconds / 3600);
                                                        $minute = floor(($timeDiffInSeconds % 3600) / 60);
                                                        $seconds = $timeDiffInSeconds % 60;

                                                        // Format the values as strings with leading zeros
                                                        $hour = str_pad($hour, 2, '0', STR_PAD_LEFT);
                                                        $minute = str_pad($minute, 2, '0', STR_PAD_LEFT);
                                                        $seconds = str_pad($seconds, 2, '0', STR_PAD_LEFT);

                                                        $totaldiff = ($hour . ':' . $minute . ':' . $seconds);

                                                        $midnight = strtotime('12:00:00 AM');
                                                        $midtwo =  strtotime(date('h:i:sa',$midnight));

                                                        echo '<input type="hidden" id="prosmidnighttime" value="'.$midtwo.'">
                                                        <input type="hidden" id="prostimedifference" value="'.$totaldiff.'">';


                                                        }

                                                        // If the CBT Settings does not Exist

                                                        else
                                                        {
                                                        echo 'No CBT Settings';

                                                        }

                                                        $countkey = 0;
                                                        $counquestion_num =1;
                                                        $countkey = 0;


                                                        echo '<div class="card-body" style="background-color: rgb(255, 255, 255); height: 100%; width: 100%; box-shadow: 1px 3px 10px #C4DDEB;">';



                                                        if($AssesementCategory == 'Theory')
                                                        {

                                                        // Check if Student has been Inserted into the Student Answer Table
                                                        $check_if_student_exist = mysqli_query($link,"SELECT * FROM `cbtquestionsanswers` 
                                                                                                WHERE `cbtquestionsanswers`.`cbtsetquestionssettingsID`='$cbtSettingsID'
                                                                                                AND `cbtquestionsanswers`.StudentID = '$StudentID'
                                                                                                AND `cbtquestionsanswers`.CampusID = '$CampusID'
                                                                                                AND `cbtquestionsanswers`.TermOrSemesterID = '$TermOrSemesterID'
                                                                                                AND `cbtquestionsanswers`.ClassOrDepartmentID = '$ClassOrDepartmentID'
                                                                                                AND `cbtquestionsanswers`.SubjectOrCourseID = '$SubjectOrCourseID'
                                                                                                AND `cbtquestionsanswers`.theory_status = '0'");
                                                                            
                                                        $exist_num = mysqli_num_rows($check_if_student_exist);


                                                        // Check if Student Exist and has not Submitted
                                                        if($exist_num > 0){

                                                            $exist_fetch = mysqli_fetch_assoc($check_if_student_exist);

                                                            $question = explode(',',$exist_fetch['TheoryQuestion']);
                                                            
                                                            foreach($question as $arrquestion)
                                                            {

                                                                $arrquestion;
                                                                $studcorectans[] = 0;


                                                                $select_question = "SELECT * FROM `questionbank` WHERE `QuestionBankID`='$arrquestion' AND  InstitutionID = '$InstitutionID' GROUP BY QuestionBankID ASC";
                                                                $select_question_result = mysqli_query($link, $select_question);
                                                                $select_question_result_cnt = mysqli_num_rows($select_question_result);
                                                                $select_question_result_cnt_row = mysqli_fetch_assoc($select_question_result);
                                                            
                                                                $qustionall = $select_question_result_cnt_row['question'];
                                                                $Questionid_1 = $select_question_result_cnt_row['QuestionBankID'];

                                                                $QuestionType = $select_question_result_cnt_row['QuestionType'];
                                                                

                                                                $questionobl = (object) [
                                                                    'question' => $qustionall
                                                                ];

                                                                $arraynew[] = $questionobl;
                                                            
                                                                
                                                            }

                                                            $ecodedquestions = json_encode($arraynew,JSON_FORCE_OBJECT);
                                                            $studentdefaultansw =  implode(',',$studcorectans);
                                                            $imploadquestionforloc = implode(',',$TheoryQuestion);

                                                            echo '<textarea hidden="hidden" value="" id="pros-loadjsoncontentforcbt">'.$ecodedquestions.'</textarea>';
                                                            echo '<input type="hidden" value="'. $QuestionType.'" id="pros-loadexamtypehere">
                                                                    <input type="hidden" value="0" id="nxtprevquestion">';
                                                            echo '<input type="hidden" value="1" id="questionNo">
                                                            <textarea hidden="hidden"  id="currentanwernxt">'.$studentdefaultansw.'</textarea>';
                                                                


                                                                $countQ = 0;
                                                                $settingarrfirt =  $TheoryQuestion[0];
                                                                $Select = "SELECT * FROM `questionbank` WHERE `QuestionBankID`='$settingarrfirt' AND  InstitutionID = '$InstitutionID' GROUP BY QuestionBankID ASC  LIMIT 1";
                                                                $selectQuery = mysqli_query($link, $Select);
                                                                $fetch2 = mysqli_num_rows($selectQuery);
                                                            
                                                            //If Question Doesnt Exist 
                                                            if($fetch2 == 0){
                                                                                        
                                                                echo '<div class="alert alert-info alert-dismissible d-flex align-items-center show">
                                                                        <i class="fa fa-info-circle"></i>
                                                                    <strong class="mx-2">Hey!</strong>Please no question attached to this settings.
                                                                    </div>
                                                                    <a  href="cbtSetting.php"  class="close" aria-label="Close" style="color:red; margin-right:20px;"><i clas="fa fa-back"></i>Back
                                                                    </a>';
                                                            }
                                                            //Question ID Exist
                                                            else{
                                                                while($fetch = mysqli_fetch_array($selectQuery))
                                                                {
                                                                    $countQ =  $countQ + 1;
                                                                    //    $rowid1 =    $Selectsettings_result_cnt_row['cbtsetquestionssettings'];  

                                                                    $questionfirst = $fetch['question'];
                                                                    

                                                                    $QuestionTypeFirst =  $fetch['QuestionType'];
                                                                    
                                                                    $rowid =  $fetch['QuestionBankID'];

                                                                    echo '<div id ="displayResults"> 

                                                                        <h1 style="font-style: italic; margin-top: 10px;">(Question '.$countQ.')</h1>

                                                                        <div class="myborder"></div>

                                                                            <div style="margin-top: 50px; margin-left: 20px;">
                                                                                <h4>'.$questionfirst.'</h4>

                                                                                <div id="pros-displaytheoryfirstoptionhere"></div>';

                                                                            echo '</div>

                                                                        <div class="row" style="margin-top: 50px;">
                                                                            <hr/>
                                                                            <div class="col-8">
                                                                                <!-- <button type="button" style="font-size: 15px; background-color:#0d6efd;" class="btn waves-effect waves-light btn-rounded">Previous</button> -->
                                                                            </div>

                                                                            <div class="col-4" id="submitnex">
                                                                                <button type="button" style="font-size: 15px; " class="btn waves-effect waves-light btn-rounded btn-secondary next float-end">Next</button>
                                                                            </div>
                                                                        </div>

                                                                        <div class="row mt-3" >
                                                                            <div class="col-lg-12">
                                                                                <div>
                                                                                    <small style="color: red; font-size: 12px; text-align: justify;">
                                                                                        <b>Note:</b> Kindly pick the correct option using the mouse to select the answer to the question
                                                                                        by the right before the time runs out. You can also select any question below to answer. 
                                                                                        Don\'t forget to click Submit when you are done.
                                                                                    </small>
                                                                                </div>
                                                                            </div>
                                                                        </div>
                                                                    
                                                                    </div>';

                                                                }                     
                                                            }

                                                        }
                                                        else{
                                                            
                                                            $Question = implode(',',$TheoryQuestion);
                                                            
                                                            $studcorectans = array();

                                                        
                                                                foreach($TheoryQuestion as $arrquestion)
                                                                {

                                                                    $arrquestion;
                                                                    $studcorectans[] = 0;

                                                                    $select_question = "SELECT * FROM `questionbank` WHERE `QuestionBankID`='$arrquestion' AND CampusID = '$CampusID' GROUP BY QuestionBankID ASC";
                                                                    $select_question_result = mysqli_query($link, $select_question);
                                                                    $select_question_result_cnt = mysqli_num_rows($select_question_result);
                                                                    $select_question_result_cnt_row = mysqli_fetch_assoc($select_question_result);

                                                                    $qustionall = $select_question_result_cnt_row['question'];

                                                                    $QuestionType = $select_question_result_cnt_row['QuestionType'];

                                                                        if($select_question_result_cnt > 0)
                                                                        {

                                                                            $questionobl = (object) [
                                                                                'question' => $qustionall
                                                                            ];

                                                                            $arraynew[] = $questionobl;
                                                                            $correctanswer[] = 0;

                                                                        }


                                                                    }

                                                                    $ecodedquestions = json_encode($arraynew,JSON_FORCE_OBJECT);
                                                                    $studentdefaultansw =  implode(',',$studcorectans);
                                                                    $imploadquestionforloc = implode(',',$TheoryQuestion);

                                                                    echo '<textarea hidden="hidden" value="" id="pros-loadjsoncontentforcbt">'.$ecodedquestions.'</textarea>';
                                                                    echo '<input type="hidden" value="'.$QuestionType.'" id="pros-loadexamtypehere">
                                                                            <input type="hidden" value="0" id="nxtprevquestion">';
                                                                    echo '<input type="hidden" value="1" id="questionNo">
                                                                    <textarea hidden="hidden"  id="currentanwernxt">'.$studentdefaultansw.'</textarea>';
                                                                
                                                                    $countQ = 0;
                                                                    $settingarrfirt =  $TheoryQuestion[0];
                                                                    $Select = "SELECT * FROM `questionbank` WHERE `QuestionBankID`='$settingarrfirt' AND  CampusID = '$CampusID' GROUP BY QuestionBankID ASC  LIMIT 1";
                                                                    $selectQuery = mysqli_query($link, $Select);
                                                                    $fetch2 = mysqli_num_rows($selectQuery);

                                                                if($fetch2 == 0){
                                                                                
                                                                    echo '<div class="alert alert-info alert-dismissible d-flex align-items-center show">
                                                                    <i class="fa fa-info-circle"></i>
                                                                    <strong class="mx-2">Hey!</strong>Please no question attached to this settings.
                                                                    </div>
                                                                    <a  href="cbtSetting.php"  class="close" aria-label="Close" style="color:red; margin-right:20px;"><i clas="fa fa-back"></i>Back
                                                                    </a>' ;

                                                                }
                                                                else
                                                                {
                                                                    while($fetch = mysqli_fetch_array($selectQuery))
                                                                    {

                                                                        $countQ =  $countQ + 1;
                                                                        //    $rowid1 =    $Selectsettings_result_cnt_row['cbtsetquestionssettings'];  
                                                                        $questionfirst = $fetch['question'];
                                                                    
                                                                        $QuestionTypeFirst =  $fetch['QuestionCategory'];

                                                                        $rowid =  $fetch['QuestionBankID'];

                                                                        echo '<div id ="displayResults"> 

                                                                        <h1 style="font-style: italic; margin-top: 10px;">(Question '.$countQ.')</h1>

                                                                        <div class="myborder"></div>

                                                                            <div style="margin-top: 50px; margin-left: 20px;">
                                                                                <h4>'.$questionfirst.'</h4>

                                                                                <div id="pros-displaytheoryfirstoptionhere"></div>';


                                                                            echo '</div>

                                                                        <div class="row" style="margin-top: 50px;">
                                                                            <hr/>
                                                                            <div class="col-8">
                                                                                <!-- <button type="button" style="font-size: 15px; background-color:#0d6efd;" class="btn waves-effect waves-light btn-rounded">Previous</button> -->
                                                                            </div>

                                                                            <div class="col-12" id="submitnex">
                                                                                <button type="button" style="font-size: 15px; " class="btn waves-effect waves-light btn-rounded btn-secondary next float-end">Next</button>
                                                                            </div>
                                                                        </div>

                                                                        <div class="row mt-3" >
                                                                            <div class="col-lg-12">
                                                                                <div>
                                                                                    <small style="color: red; font-size: 12px; text-align: justify;">
                                                                                        <b>Note:</b> Kindly pick the correct option using the mouse to select the answer to the question
                                                                                        by the right before the time runs out. You can also select any question below to answer. 
                                                                                        Don\'t forget to click Submit when you are done.
                                                                                    </small>
                                                                                </div>
                                                                            </div>
                                                                        </div>

                                                                        </div>';

                                                                    }                     
                                                                }

                                                            
                                                        }

                                                        }
                                                        else{
                                                        echo "Category Does Not exist";
                                                        }


                                                        echo'</div>   
                                                        </div>';

                                                        echo'<div  class="col-lg-4" style="margin-top: 20px;">
                                                        <div class="card">
                                                        <div class="card-body" style="background-color: rgb(255, 255, 255); box-shadow: 1px 3px 10px #C4DDEB;">
                                                        <h2 style="font-weight: 700;" align="center">Time Left</h2>

                                                        <div class="row">
                                                            <div class="col-2">
                                                                
                                                            </div>

                                                            <div class="col-3">
                                                                <h3 id="examhour">00</h3>
                                                                <small align="center">Hours</small>
                                                            </div>
                                                            
                                                            <div class="col-3">
                                                                <h3 id="examminutes">00</h3>
                                                                <small>Minutes</small>
                                                            </div>
                                                            <div class="col-4">
                                                                <h3 id="examseconds">00</h3>
                                                                <small>Seconds</small>
                                                            </div>

                                                        </div>

                                                        <div style="margin-top: 63px; padding: none;">
                                                        <div class="">
                                                        <div class="col-lg-1">

                                                        </div>';

                                                            $AssesementCategory = $QuestionTypeFirst; 

                                                            $countkey = 0;
                                                            $counquestion_num =1;
                                                            $countkey = 0;

                                                            if($AssesementCategory == 'Theory')
                                                            {

                                                                foreach($TheoryQuestion as $questionnew)
                                                                {


                                                                    $questionnew;

                                                                    $counquestion_num++; 
                                                                    $countkey++;

                                                                    $newquenum  =   $counquestion_num -1;
                                                                    $questionkey2  =  $countkey -1;
                                                                    
                                                                    $Select1 = "SELECT * FROM  `questionbank` WHERE  `CampusID` = '$CampusID'  AND QuestionBankID='$questionnew' ORDER BY QuestionBankID ASC";
                                                                    $selectQuery1 = mysqli_query($link, $Select1);
                                                                    $fetch1 = mysqli_fetch_array($selectQuery1);

                                                            
                                                                    $question =  $fetch1['question'];

                                                                    $QuestionTypenew =  $fetch1['QuestionCategory'];

                                                                    $cbt_setting_id =  $fetch1['QuestionBankID'];
                                                                

                                                                    if($QuestionTypenew == 'Theory')
                                                                    {
                                                                        $questiontype = 'Theory'; 
                                                                    }

                                                                    echo ' <button style="font-weight: 600; font-size: 12px;margin: 2px; background-color:#0d6efd;" " type="button" data-question="'.$questiontype.'" class="btn btn-success viewer" id="'.$newquenum.'" data-id="'.$questionkey2.'">'.$newquenum.'</button>';
                                                                                            
                                                                }

                                                            }
                                                                                                                                                                
                                                        echo '</div>
                                                        <div class="row mt-3">
                                                        <div class="col-8">
                                                            
                                                        </div>
                                                        <div class="col-4">
                                                            <button type="button" class="btn waves-effect waves-light btn-success" data-bs-toggle="modal" data-bs-target="#pros-submitexam"  style="font-size: 15px; background-color:#4db8ff; border: #2CEB6C; margin-top: 30px;">Submit</button>
                                                        </div>
                                                        </div>
                                                        </div>
                                                        </div>

                                                        </div>';
                                                        }


                                                        }
                                                        // If its Both Check if the Theory and Objective status is 1 (Meaning the student has written his exams)
                                                        elseif($AssesementCategory == 'Both'){

                                                        // Check if Student has been Inserted into the Student Answer Table
                                                        $check_if_student_exist = mysqli_query($link,"SELECT * FROM `cbtquestionsanswers` 
                                                                                WHERE `cbtsetquestionssettingsID`='$cbtSettingsID'
                                                                                AND `StudentID` = '$StudentID'
                                                                                AND `theory_status` = '1'
                                                                                AND `objective_status` = '1'");
                                                                                

                                                        $exist_num = mysqli_num_rows($check_if_student_exist);
                                                        $exist_fetch = mysqli_fetch_assoc($check_if_student_exist);

                                                        if($exist_num > 0){

                                                        echo 'Student Have Answered Objective and Theory';

                                                        }
                                                        // Student Have Not Answered Objective (Load the Objective Question)
                                                        else{

                                                        echo '<div class="row p-3">
                                                        <div class="col-lg-8" style="margin-top: 20px;">';

                                                        // Get CBT Settings and Questions


                                                        if($examtype  == 'admission')
                                                        {


                                                            $selectquestionset = mysqli_query($link,"SELECT * FROM `cbtsetquestionssettings` 
                                                                    INNER JOIN `classtemplate` 
                                                                    ON `cbtsetquestionssettings`.`ClassOrDepartmentID` = `classtemplate`.ClassTemplateID 
                                                                    INNER JOIN `subjectorcourse` 
                                                                    ON `cbtsetquestionssettings`.`SubjectOrCourseID` = `subjectorcourse`.SubjectOrCourseID
                                                                
                                                                    INNER JOIN `session`
                                                                    ON `cbtsetquestionssettings`.`sessionID` = `session`.`sessionName`
                                                                    WHERE `cbtsetquestionssettings`.`cbtsetquestionssettingsID`='$cbtSettingsID'");



                                                        }else
                                                        {

                                                            $selectquestionset = mysqli_query($link,"SELECT * FROM `cbtsetquestionssettings` 
                                                                    INNER JOIN `classordepartment` 
                                                                    ON `cbtsetquestionssettings`.`ClassOrDepartmentID` = `classordepartment`.ClassOrDepartmentID 
                                                                    INNER JOIN `subjectorcourse` 
                                                                    ON `cbtsetquestionssettings`.`SubjectOrCourseID` = `subjectorcourse`.SubjectOrCourseID
                                                                
                                                                    INNER JOIN `session`
                                                                    ON `cbtsetquestionssettings`.`sessionID` = `session`.`sessionName`
                                                                    WHERE `cbtsetquestionssettings`.`cbtsetquestionssettingsID`='$cbtSettingsID'");

                                                        }


                                                                                        
                                                        $selectquestionsetrow = mysqli_num_rows($selectquestionset);
                                                        $selectquestionsetrow_cnt = mysqli_fetch_assoc($selectquestionset);

                                                        // If the CBT Settings Exist
                                                        if($selectquestionsetrow > 0)
                                                        {

                                                        $cbtsetquestionssettingsID = $selectquestionsetrow_cnt['cbtsetquestionssettingsID'];
                                                        $ShuffleOption = $selectquestionsetrow_cnt['ShuffleOption'];
                                                        $CampusID = $selectquestionsetrow_cnt['CampusID'];
                                                        $TermOrSemesterID = $selectquestionsetrow_cnt['TermOrSemesterID'];
                                                        $SubjectOrCourseID = $selectquestionsetrow_cnt['SubjectOrCourseID'];
                                                        $sessionID = $selectquestionsetrow_cnt['sessionName'];
                                                        $ClassOrDepartmentID = $selectquestionsetrow_cnt['ClassOrDepartmentID'];


                                                        $AssesementCategory = $selectquestionsetrow_cnt['AssesementCategory'];


                                                        $TheoryQuestion = explode(',',$selectquestionsetrow_cnt['TheoryQuestion']);

                                                        $Questions = explode(',',$selectquestionsetrow_cnt['question']);
                                                        $answer = explode(',',$selectquestionsetrow_cnt['answer']);

                                                        // print_r($Questions);

                                                        $numquestion = count($Questions);

                                                        $AssesementStartTime = $selectquestionsetrow_cnt['AssesementStartTime'];
                                                        $AssesementEndTime  =  $selectquestionsetrow_cnt['AssesementEndTime'];


                                                        $timeIn = new DateTime($AssesementStartTime);
                                                        $timeOut = new DateTime($AssesementEndTime);

                                                        date_default_timezone_set('Africa/Lagos');

                                                        // Calculate the time difference in seconds
                                                        $timeDiffInSeconds = $timeOut->getTimestamp() - $timeIn->getTimestamp();

                                                        // Convert the time difference to hours, minutes, and seconds
                                                        $hour = floor($timeDiffInSeconds / 3600);
                                                        $minute = floor(($timeDiffInSeconds % 3600) / 60);
                                                        $seconds = $timeDiffInSeconds % 60;

                                                        // Format the values as strings with leading zeros
                                                        $hour = str_pad($hour, 2, '0', STR_PAD_LEFT);
                                                        $minute = str_pad($minute, 2, '0', STR_PAD_LEFT);
                                                        $seconds = str_pad($seconds, 2, '0', STR_PAD_LEFT);

                                                        $totaldiff = ($hour . ':' . $minute . ':' . $seconds);

                                                        $midnight = strtotime('12:00:00 AM');
                                                        $midtwo =  strtotime(date('h:i:sa',$midnight));

                                                        echo '<input type="hidden" id="prosmidnighttime" value="'.$midtwo.'">
                                                        <input type="hidden" id="prostimedifference" value="'.$totaldiff.'">';


                                                        }

                                                        // If the CBT Settings does not Exist
                                                        else
                                                        {
                                                        echo 'No CBT Settings';

                                                        }

                                                        $countkey = 0;
                                                        $counquestion_num =1;
                                                        $countkey = 0;


                                                        echo '<div class="card-body" style="background-color: rgb(255, 255, 255); height: 100%; width: 100%; box-shadow: 1px 3px 10px #C4DDEB;">';

                                                        if($AssesementCategory == 'Both'){

                                                        // Check if Student has been Inserted into the Student Answer Table
                                                        $check_if_student_exist = mysqli_query($link,"SELECT * FROM `cbtquestionsanswers` 
                                                                                                WHERE `cbtquestionsanswers`.`cbtsetquestionssettingsID`='$cbtSettingsID'
                                                                                                AND `cbtquestionsanswers`.StudentID = '$StudentID'
                                                                                                AND `cbtquestionsanswers`.CampusID = '$CampusID'
                                                                                                AND `cbtquestionsanswers`.TermOrSemesterID = '$TermOrSemesterID'
                                                                                                AND `cbtquestionsanswers`.ClassOrDepartmentID = '$ClassOrDepartmentID'
                                                                                                AND `cbtquestionsanswers`.SubjectOrCourseID = '$SubjectOrCourseID'
                                                                                                AND `cbtquestionsanswers`.objective_status = '1'");
                                                                            
                                                        $exist_num = mysqli_num_rows($check_if_student_exist);
                                                        $exist_fetch = mysqli_fetch_assoc($check_if_student_exist);

                                                        // Check if Student Exist in Answer Table
                                                        if($exist_num > 0){

                                                            $studentAnswer = $exist_fetch['studentAnswer'];
                                                            $status = $exist_fetch['objective_status'];

                                                            // Check if student have answered objective Question Load Theory Questions
                                                            if(($studentAnswer != '') && ($status == '1')){


                                                                    // Check if Student has been Inserted into the Student Answer Table
                                                                    $check_if_student_exist = mysqli_query($link,"SELECT * FROM `cbtquestionsanswers` 
                                                                                                            WHERE `cbtsetquestionssettingsID`='$cbtSettingsID'
                                                                                                            AND `StudentID` = '$StudentID'
                                                                                                            AND `CampusID` = '$CampusID'
                                                                                                            AND `TermOrSemesterID` = '$TermOrSemesterID'
                                                                                                            AND `ClassOrDepartmentID` = '$ClassOrDepartmentID'
                                                                                                            AND `SubjectOrCourseID` = '$SubjectOrCourseID'");
                                                                                
                                                                    $exist_num = mysqli_num_rows($check_if_student_exist);
                                                                    $exist_fetch = mysqli_fetch_assoc($check_if_student_exist);

                                                                    // Check if Student Exist and has not Submitted
                                                                    if($exist_num > 0){

                                                                        $student_have_answered_theory = $exist_fetch['studentTheoryAnswer'];

                                                                        if( $student_have_answered_theory  != ''){
                                                                            echo 'Student_Ansered_Both';
                                                                        }
                                                                        // Load Theory Questions
                                                                        else{

                                                                            $question = $TheoryQuestion;
                                                                        
                                                                            foreach($question as $arrquestion)
                                                                            {

                                                                                $arrquestion;
                                                                                $studcorectans[] = 0;


                                                                                $select_question = "SELECT * FROM `questionbank` WHERE `QuestionBankID`='$arrquestion' AND InstitutionID = '$InstitutionID' GROUP BY QuestionBankID ASC";
                                                                                $select_question_result = mysqli_query($link, $select_question);
                                                                                $select_question_result_cnt = mysqli_num_rows($select_question_result);
                                                                                $select_question_result_cnt_row = mysqli_fetch_assoc($select_question_result);
                                                                            
                                                                                $qustionall = $select_question_result_cnt_row['question'];
                                                                                $Questionid_1 = $select_question_result_cnt_row['QuestionBankID'];

                                                                                $QuestionType = $select_question_result_cnt_row['QuestionCategory'];
                                                                                

                                                                                $questionobl = (object) [
                                                                                    'question' => $qustionall
                                                                                ];

                                                                                $arraynew[] = $questionobl;
                                                                            
                                                                                
                                                                            }

                                                                            $ecodedquestions = json_encode($arraynew,JSON_FORCE_OBJECT);
                                                                            $studentdefaultansw =  implode(',',$studcorectans);
                                                                            $imploadquestionforloc = implode(',',$TheoryQuestion);

                                                                            echo '<textarea hidden="hidden" value="" id="pros-loadjsoncontentforcbt">'.$ecodedquestions.'</textarea>';
                                                                            echo '<input type="hidden" value="'.$QuestionType.'" id="pros-loadexamtypehere">
                                                                                    <input type="hidden" value="0" id="nxtprevquestion">';
                                                                            echo '<input type="hidden" value="1" id="questionNo">
                                                                            <textarea hidden="hidden"  id="currentanwernxt">'.$studentdefaultansw.'</textarea>';
                                                                            


                                                                            $countQ = 0;
                                                                            $settingarrfirt =  $TheoryQuestion[0];
                                                                            $Select = "SELECT * FROM `questionbank` WHERE `QuestionBankID`='$settingarrfirt' AND  CampusID = '$CampusID' GROUP BY QuestionBankID ASC  LIMIT 1";
                                                                            $selectQuery = mysqli_query($link, $Select);
                                                                            $fetch2 = mysqli_num_rows($selectQuery);
                                                                        
                                                                            //If Question Doesnt Exist 
                                                                            if($fetch2 == 0){
                                                                                                        
                                                                                echo '<div class="alert alert-info alert-dismissible d-flex align-items-center show">
                                                                                        <i class="fa fa-info-circle"></i>
                                                                                    <strong class="mx-2">Hey!</strong>Please no question attached to this settings.
                                                                                    </div>
                                                                                    <a  href="cbtSetting.php"  class="close" aria-label="Close" style="color:red; margin-right:20px;"><i clas="fa fa-back"></i>Back
                                                                                    </a>';
                                                                            }
                                                                            
                                                                            //Question ID Exist
                                                                            else
                                                                            {
                                                                                while($fetch = mysqli_fetch_array($selectQuery))
                                                                                {
                                                                                    $countQ =  $countQ + 1;


                                                                                

                                                                                    $questionfirst = $fetch['question'];
                                                                                    

                                                                                    $QuestionTypeFirst =  $fetch['QuestionCategory'];
                                                                                    
                                                                                    $rowid =  $fetch['QuestionBankID'];

                                                                                    echo '<div id ="displayResults"> 

                                                                                        <h1 style="font-style: italic; margin-top: 10px;">(Question '.$countQ.')</h1>

                                                                                        <div class="myborder"></div>

                                                                                            <div style="margin-top: 50px; margin-left: 20px;">
                                                                                                <h4>'.$questionfirst.'</h4>

                                                                                                <div id="pros-displaytheoryfirstoptionhere"></div>';

                                                                                            echo '</div>

                                                                                        <div class="row" style="margin-top: 50px;">
                                                                                            <hr/>
                                                                                            <div class="col-8">
                                                                                                <!-- <button type="button" style="font-size: 15px; background-color:#0d6efd;" class="btn waves-effect waves-light btn-rounded">Previous</button> -->
                                                                                            </div>

                                                                                            <div class="col-4" id="submitnex">
                                                                                                <button type="button" style="font-size: 15px; " class="btn waves-effect waves-light btn-rounded btn-secondary next float-end">Next</button>
                                                                                            </div>
                                                                                        </div>

                                                                                        <div class="row mt-3" >
                                                                                            <div class="col-lg-12">
                                                                                                <div>
                                                                                                    <small style="color: red; font-size: 12px; text-align: justify;">
                                                                                                        <b>Note:</b> Kindly pick the correct option using the mouse to select the answer to the question
                                                                                                        by the right before the time runs out. You can also select any question below to answer. 
                                                                                                        Don\'t forget to click Submit when you are done.
                                                                                                    </small>
                                                                                                </div>
                                                                                            </div>
                                                                                        </div>
                                                                                    
                                                                                    </div>';

                                                                                }                     
                                                                            }

                                                                            }

                                                                    }
                                                                    else{
                                                                        
                                                                        $Question = implode(',',$TheoryQuestion);
                                                                        
                                                                        $studcorectans = array();

                                                                        $insert_shuffled_question = mysqli_query($link, "INSERT INTO `cbtquestionsanswers` (`cbtsetquestionssettingsID`,`StudentID`,`CampusID`,`sessionID`,`TermOrSemesterID`,`ClassOrDepartmentID`,`SubjectOrCourseID`,`TheoryQuestion`) 
                                                                                                                VALUES ('$cbtsetquestionssettingsID','$StudentID','$CampusID','$sessionID','$TermOrSemesterID','$ClassOrDepartmentID','$SubjectOrCourseID','$Question')");


                                                                        if($insert_shuffled_question > 0){

                                                                            foreach($TheoryQuestion as $arrquestion)
                                                                            {

                                                                                $arrquestion;
                                                                                $studcorectans[] = 0;

                                                                                $select_question = "SELECT * FROM `questionbank` WHERE `QuestionBankID`='$arrquestion' AND InstitutionID = '$InstitutionID' GROUP BY QuestionBankID ASC";
                                                                                $select_question_result = mysqli_query($link, $select_question);
                                                                                $select_question_result_cnt = mysqli_num_rows($select_question_result);
                                                                                $select_question_result_cnt_row = mysqli_fetch_assoc($select_question_result);

                                                                                $qustionall = $select_question_result_cnt_row['question'];

                                                                                $QuestionType = $select_question_result_cnt_row['QuestionCategory'];

                                                                                    if($select_question_result_cnt > 0)
                                                                                    {

                                                                                        $questionobl = (object) [
                                                                                            'question' => $qustionall
                                                                                        ];

                                                                                        $arraynew[] = $questionobl;
                                                                                        $correctanswer[] = 0;

                                                                                    }


                                                                                }

                                                                                $ecodedquestions = json_encode($arraynew,JSON_FORCE_OBJECT);
                                                                                $studentdefaultansw =  implode(',',$studcorectans);
                                                                                $imploadquestionforloc = implode(',',$TheoryQuestion);

                                                                                echo '<textarea hidden="hidden" value="" id="pros-loadjsoncontentforcbt">'.$ecodedquestions.'</textarea>';
                                                                                echo '<input type="hidden" value="'.$QuestionType.'" id="pros-loadexamtypehere">
                                                                                        <input type="hidden" value="0" id="nxtprevquestion">';
                                                                                echo '<input type="hidden" value="1" id="questionNo">
                                                                                <textarea hidden="hidden"  id="currentanwernxt">'.$studentdefaultansw.'</textarea>';
                                                                            
                                                                                $countQ = 0;
                                                                                $settingarrfirt =  $TheoryQuestion[0];
                                                                                $Select = "SELECT * FROM `questionbank` WHERE `QuestionBankID`='$settingarrfirt' AND  CampusID = '$CampusID' GROUP BY QuestionBankID ASC  LIMIT 1";
                                                                                $selectQuery = mysqli_query($link, $Select);
                                                                                $fetch2 = mysqli_num_rows($selectQuery);

                                                                            if($fetch2 == 0){
                                                                                            
                                                                                echo '<div class="alert alert-info alert-dismissible d-flex align-items-center show">
                                                                                <i class="fa fa-info-circle"></i>
                                                                                <strong class="mx-2">Hey!</strong>Please no question attached to this settings.
                                                                                </div>
                                                                                <a  href="cbtSetting.php"  class="close" aria-label="Close" style="color:red; margin-right:20px;"><i clas="fa fa-back"></i>Back
                                                                                </a>' ;

                                                                            }
                                                                            else
                                                                            {
                                                                                while($fetch = mysqli_fetch_array($selectQuery))
                                                                                {

                                                                                    $countQ =  $countQ + 1;
                                                                                    //    $rowid1 =    $Selectsettings_result_cnt_row['cbtsetquestionssettings'];  
                                                                                    echo $questionfirst = $fetch['question'];
                                                                                
                                                                                    $QuestionTypeFirst =  $fetch['QuestionCategory'];

                                                                                    $rowid =  $fetch['QuestionBankID'];

                                                                                    echo '<div id ="displayResults"> 

                                                                                    <h1 style="font-style: italic; margin-top: 10px;">(Question '.$countQ.')</h1>

                                                                                    <div class="myborder"></div>

                                                                                        <div style="margin-top: 50px; margin-left: 20px;">
                                                                                            <h4>'.$questionfirst.'</h4>

                                                                                            <div id="pros-displaytheoryfirstoptionhere"></div>';


                                                                                        echo '</div>

                                                                                    <div class="row" style="margin-top: 50px;">
                                                                                        <hr/>
                                                                                        <div class="col-8">
                                                                                            <!-- <button type="button" style="font-size: 15px; background-color:#0d6efd;" class="btn waves-effect waves-light btn-rounded">Previous</button> -->
                                                                                        </div>

                                                                                        <div class="col-12" id="submitnex">
                                                                                            <button type="button" style="font-size: 15px; " class="btn waves-effect waves-light btn-rounded btn-secondary next float-end">Next</button>
                                                                                        </div>
                                                                                    </div>

                                                                                    <div class="row mt-3" >
                                                                                        <div class="col-lg-12">
                                                                                            <div>
                                                                                                <small style="color: red; font-size: 12px; text-align: justify;">
                                                                                                    <b>Note:</b> Kindly pick the correct option using the mouse to select the answer to the question
                                                                                                    by the right before the time runs out. You can also select any question below to answer. 
                                                                                                    Don\'t forget to click Submit when you are done.
                                                                                                </small>
                                                                                            </div>
                                                                                        </div>
                                                                                    </div>

                                                                                    </div>';

                                                                                }                     
                                                                            }

                                                                        }
                                                                    }

                                                            }
                                                                // Load OBJ Questions
                                                            else{

                                                                    // $TheoryQuestion = explode(',',$selectquestionsetrow_cnt['TheoryQuestion']);
                                                                    $Questions = explode(',',$selectquestionsetrow_cnt['question']);
                                                                    $answer = explode(',',$selectquestionsetrow_cnt['answer']);

                                                                    // Check if Student has been Inserted into the Student Answer Table
                                                                    $check_if_student_exist = mysqli_query($link,"SELECT * FROM `cbtquestionsanswers` 
                                                                                                            WHERE `cbtquestionsanswers`.`cbtsetquestionssettingsID`='$cbtSettingsID'
                                                                                                            AND `cbtquestionsanswers`.StudentID = '$StudentID'
                                                                                                            AND `cbtquestionsanswers`.CampusID = '$CampusID'
                                                                                                            AND `cbtquestionsanswers`.TermOrSemesterID = '$TermOrSemesterID'
                                                                                                            AND `cbtquestionsanswers`.ClassOrDepartmentID = '$ClassOrDepartmentID'
                                                                                                            AND `cbtquestionsanswers`.SubjectOrCourseID = '$SubjectOrCourseID'
                                                                                                            AND `cbtquestionsanswers`.`status` = '0'");
                                                                                    
                                                                    $exist_num = mysqli_num_rows($check_if_student_exist);
                                                                    $exist_fetch = mysqli_fetch_assoc($check_if_student_exist);
                                                            
                                                                    // Check if Student Exist and has not Submitted
                                                                    if($exist_num > 0){

                                                                        $question = explode(',',$exist_fetch['question']);
                                                                        $answer = explode(',',$exist_fetch['Answer']);
                                                                        

                                                                        foreach($question as $arrquestion)
                                                                        {

                                                                            $arrquestion;
                                                                            $studcorectans[] = 0;


                                                                            $select_question = "SELECT * FROM `questionbank` WHERE `QuestionBankID`='$arrquestion' AND InstitutionID = '$InstitutionID' GROUP BY QuestionBankID ASC";
                                                                            $select_question_result = mysqli_query($link, $select_question);
                                                                            $select_question_result_cnt = mysqli_num_rows($select_question_result);
                                                                            $select_question_result_cnt_row = mysqli_fetch_assoc($select_question_result);
                                                                        
                                                                            $qustionall = $select_question_result_cnt_row['question'];
                                                                            $Questionid_1 = $select_question_result_cnt_row['QuestionBankID'];
                                                                            $optionA = $select_question_result_cnt_row['optionA'];
                                                                            $optionB = $select_question_result_cnt_row['optionB'];
                                                                            $optionC = $select_question_result_cnt_row['optionC'];
                                                                            $optionD = $select_question_result_cnt_row['optionD'];

                                                                            $canswer = $select_question_result_cnt_row['answer'];

                                                                            $QuestionType = $select_question_result_cnt_row['QuestionCategory'];

                                                                            if($QuestionType == 'Objective')
                                                                            {
                                                                                

                                                                                $questionobl = (object) [
                                                                                    'question' => $qustionall,
                                                                                    'optionA' => $optionA,
                                                                                    'optionB' => $optionB,
                                                                                    'optionC' => $optionC,
                                                                                    'optionD' => $optionD,
                                                                                    
                                                                                ];

                                                                                    $arraynew[] = $questionobl;
                                                                                    $correctanswer[] = $canswer;

                                                                            }else
                                                                            {

                                                                                $questionobl = (object) [
                                                                                    'question' => $qustionall
                                                                                ];

                                                                                $arraynew[] = $questionobl;
                                                                                $correctanswer[] = 0;

                                                                            }
                                                                            
                                                                            
                                                                        }
                                                                            $ecodedquestions = json_encode($arraynew,JSON_FORCE_OBJECT);
                                                                            $currectanswer = implode(',',$answer);
                                                                            $studentdefaultansw =  implode(',',$studcorectans);
                                                                            $imploadquestionforloc = implode(',',$question);

                                                                            echo '<textarea hidden="hidden" value="" id="pros-loadjsoncontentforcbt">'.$ecodedquestions.'</textarea>';
                                                                            echo '<input type="hidden" value="'.$imploadquestionforloc.'" id="tari_question_default">';

                                                                            echo '<input type="hidden" value="'.$QuestionType.'" id="pros-loadexamtypehere">';

                                                                            echo '<input type="hidden" value="0" id="nxtprevquestion">
                                                                                <textarea hidden="hidden"  id="currentanwernxt">'.$studentdefaultansw.'</textarea>
                                                                                <input type="hidden" value="1" id="questionNo">';
                                                                                echo '<input type="hidden" id="currectquestionID" value="'.$currectanswer.'">';



                                                                            $countQ = 0;
                                                                            $settingarrfirt =  $Questions[0];
                                                                            $Select = "SELECT * FROM `questionbank` WHERE `QuestionBankID`='$settingarrfirt' AND  InstitutionID = '$InstitutionID' GROUP BY QuestionBankID ASC  LIMIT 1";
                                                                            $selectQuery = mysqli_query($link, $Select);
                                                                            $fetch2 = mysqli_num_rows($selectQuery);
                                                                        
                                                                        //If Question Doesnt Exist 
                                                                        if($fetch2 == 0){
                                                                                                    
                                                                            echo '<div class="alert alert-info alert-dismissible d-flex align-items-center show">
                                                                                    <i class="fa fa-info-circle"></i>
                                                                                <strong class="mx-2">Hey!</strong>Please no question attached to this settings.
                                                                                </div>
                                                                                <a  href="cbtSetting.php"  class="close" aria-label="Close" style="color:red; margin-right:20px;"><i clas="fa fa-back"></i>Back
                                                                                </a>';
                                                                        }

                                                                        //Question ID Exist
                                                                        else{

                                                                            while($fetch = mysqli_fetch_array($selectQuery))
                                                                            {
                                                                                $countQ =  $countQ + 1;
                                                                                // $rowid1 =    $Selectsettings_result_cnt_row['cbtsetquestionssettings'];  

                                                                                $questionfirst = $fetch['question'];
                                                                                $optionA =  $fetch['optionA'];
                                                                                $optionB =  $fetch['optionB'];
                                                                                $optionC =  $fetch['optionC'];
                                                                                $optionD =  $fetch['optionD'];

                                                                                
                                                                                // Replace <p></p> with empty space
                                                                                $optionA1 = str_replace(array('<p>', '</p>'), '', $optionA);
                                                                                $optionB1 = str_replace(array('<p>', '</p>'), '', $optionB);
                                                                                $optionC1 = str_replace(array('<p>', '</p>'), '', $optionC);
                                                                                $optionD1 = str_replace(array('<p>', '</p>'), '', $optionD);

                                                                                // Replace < with &lt;
                                                                                $optionAA = str_replace('<', '&lt;', $optionA1);
                                                                                $optionBB = str_replace('<', '&lt;', $optionB1);
                                                                                $optionCC = str_replace('<', '&lt;', $optionC1);
                                                                                $optionDD = str_replace('<', '&lt;', $optionD1);


                                                                                $QuestionTypeFirst =  $fetch['QuestionType'];
                                                                                
                                                                                $rowid =  $fetch['QuestionBankID'];

                                                                                echo '<div id ="displayResults"> 

                                                                                    <h1 style="font-style: italic; margin-top: 10px;">(Question '.$countQ.')</h1>

                                                                                    <div class="myborder"></div>

                                                                                        <div style="margin-top: 50px; margin-left: 20px;">
                                                                                            <h4>'.$questionfirst.'</h4>';
                                                                                                
                                                                                                if($QuestionTypeFirst  == 'Objective')
                                                                                                {
                                                                                                    echo '<form class="selectSubject">
                                                                                                        <label>
                                                                                                            <input type="radio" name="group1" class="radioBtnClass"  value="A"  id="radio_1"/>
                                                                                                            <span>A. '.$optionAA.'</span>
                                                                                                        </label>
                                                                                                        <label>
                                                                                                            <input type="radio" name="group1" class="radioBtnClass"  value="B"  id="radio_2"/>
                                                                                                            <span>B. '.$optionBB.'</span>
                                                                                                        </label>
                                                                                                        <label>
                                                                                                            <input type="radio" name="group1" class="radioBtnClass"  value="C"  id="radio_3"/>
                                                                                                            <span>C. '.$optionCC.'</span>
                                                                                                        </label>
                                                                                                        <label>
                                                                                                            <input type="radio" name="group1" class="radioBtnClass"  value="D"  id="radio_4"/>
                                                                                                            <span>D. '.$optionDD.'</span>
                                                                                                        </label>
                                                                                                    </form>';

                                                                                                }elseif($QuestionTypeFirst  == 'Theory')
                                                                                                {
                                                                                                echo '<textarea   id="optionA1" class="wrs_div_box example question" ></textarea>';
                                                                                                }

                                                                                        echo '</div>

                                                                                    <div class="row" style="margin-top: 50px;">
                                                                                        <hr/>
                                                                                        <div class="col-8">
                                                                                            <!-- <button type="button" style="font-size: 15px; background-color:#0d6efd;" class="btn waves-effect waves-light btn-rounded">Previous</button> -->
                                                                                        </div>

                                                                                        <div class="col-4">
                                                                                            <button type="button" style="font-size: 15px;" class="btn waves-effect waves-light btn-rounded btn-secondary next float-end">Next</button>
                                                                                        </div>
                                                                                    </div>

                                                                                    <div class="row mt-3" >
                                                                                        <div class="col-lg-12">
                                                                                            <div>
                                                                                                <small style="color: red; font-size: 12px; text-align: justify;">
                                                                                                    <b>Note:</b> Kindly pick the correct option using the mouse to select the answer to the question
                                                                                                    by the right before the time runs out. You can also select any question below to answer. 
                                                                                                    Don\'t forget to click Submit when you are done.
                                                                                                </small>
                                                                                            </div>
                                                                                        </div>
                                                                                    </div>
                                                                                
                                                                                </div>';

                                                                            }                     
                                                                        }

                                                                    }
                                                                    else{

                                                                        if($ShuffleOption == '1'){



                                                                            $Questions = explode(',',$selectquestionsetrow_cnt['question']);
                                                                            $answer = explode(',',$selectquestionsetrow_cnt['answer']);


                                                                            $studcorectans = array();

                                                                            // Sample questions and answers arrays
                                                                            // print_r($Questions);
                                                                            // print_r($answer);

                                                                            $questions = $Questions;
                                                                            $answers = $answer;
                                                                        
                                                                            // Combine questions and answers into an array of arrays
                                                                            $combined = array();
                                                                            foreach ($questions as $index => $question) {
                                                                                $combined[] = array('question' => $question, 'answer' => $answers[$index]);
                                                                            }

                                                                            // Shuffle the combined array
                                                                            shuffle($combined);

                                                                            // Separate the shuffled array back into questions and answers
                                                                            $shuffledQuestions = array();
                                                                            $shuffledAnswers = array();

                                                                            foreach ($combined as $item) {
                                                                                $shuffledQuestions1[] = $item['question'];
                                                                                $shuffledAnswers1[] = $item['answer'];
                                                                            }

                                                                            $shuffledQuestions = implode(',', $shuffledQuestions1);
                                                                            $shuffledAnswers = implode(',', $shuffledAnswers1);


                                                                        
                                                                                foreach($shuffledQuestions1 as $arrquestion)
                                                                                {

                                                                                    $arrquestion;
                                                                                    $studcorectans[] = 0;

                                                                            

                                                                                    $select_question = "SELECT * FROM `questionbank` WHERE `QuestionBankID`='$arrquestion' AND InstitutionID = '$InstitutionID' GROUP BY QuestionBankID ASC";
                                                                                    $select_question_result = mysqli_query($link, $select_question);
                                                                                    $select_question_result_cnt = mysqli_num_rows($select_question_result);
                                                                                    $select_question_result_cnt_row = mysqli_fetch_assoc($select_question_result);
                                                                                
                                                                                    $qustionall = $select_question_result_cnt_row['question'];
                                                                                    $Questionid_1 = $select_question_result_cnt_row['QuestionBankID'];
                                                                                    $optionA = $select_question_result_cnt_row['optionA'];
                                                                                    $optionB = $select_question_result_cnt_row['optionB'];
                                                                                    $optionC = $select_question_result_cnt_row['optionC'];
                                                                                    $optionD = $select_question_result_cnt_row['optionD'];

                                                                                    $canswer = $select_question_result_cnt_row['answer'];

                                                                                    $QuestionType = $select_question_result_cnt_row['QuestionType'];

                                                                                    if($QuestionType == 'Objective')
                                                                                    {
                                                                                        

                                                                                        $questionobl = (object) [
                                                                                            'question' => $qustionall,
                                                                                            'optionA' => $optionA,
                                                                                            'optionB' => $optionB,
                                                                                            'optionC' => $optionC,
                                                                                            'optionD' => $optionD,
                                                                                            
                                                                                        ];

                                                                                            $arraynew[] = $questionobl;
                                                                                            $correctanswer[] = $canswer;

                                                                                    }else
                                                                                    {

                                                                                        $questionobl = (object) [
                                                                                            'question' => $qustionall
                                                                                        ];

                                                                                        $arraynew[] = $questionobl;
                                                                                            $correctanswer[] = 0;

                                                                                    }

                                                                            
                                                                                }

                                                                                    $ecodedquestions = json_encode($arraynew,JSON_FORCE_OBJECT);
                                                                                    $currectanswer = implode(',',$shuffledAnswers1);
                                                                                    $studentdefaultansw =  implode(',',$studcorectans);
                                                                                    $imploadquestionforloc = implode(',',$shuffledQuestions1);

                                                                                    echo '<textarea hidden="hidden" value="" id="pros-loadjsoncontentforcbt">'.$ecodedquestions.'</textarea>';
                                                                                    echo '<input type="hidden" value="'.$imploadquestionforloc.'" id="tari_question_default">';

                                                                                    echo '<input type="hidden" value="'.$QuestionType.'" id="pros-loadexamtypehere">';

                                                                                    echo '<input type="hidden" value="0" id="nxtprevquestion">
                                                                                        <textarea hidden="hidden"  id="currentanwernxt">'.$studentdefaultansw.'</textarea>
                                                                                        <input type="hidden" value="1" id="questionNo">';
                                                                                    echo '<input type="hidden" id="currectquestionID" value="'.$currectanswer.'">';



                                                                                    $countQ = 0;
                                                                                    $settingarrfirt =  $Questions[0];
                                                                                    $Select = "SELECT * FROM `questionbank` WHERE `QuestionBankID`='$settingarrfirt' AND  InstitutionID = '$InstitutionID' GROUP BY QuestionBankID ASC  LIMIT 1";
                                                                                    $selectQuery = mysqli_query($link, $Select);
                                                                                    $fetch2 = mysqli_num_rows($selectQuery);

                                                                                if($fetch2 == 0){
                                                                                                            
                                                                                    echo '<div class="alert alert-info alert-dismissible d-flex align-items-center show">
                                                                                            <i class="fa fa-info-circle"></i>
                                                                                        <strong class="mx-2">Hey!</strong>Please no question attached to this settings.
                                                                                        </div>
                                                                                        <a  href="cbtSetting.php"  class="close" aria-label="Close" style="color:red; margin-right:20px;"><i clas="fa fa-back"></i>Back
                                                                                        </a>'
                                                                                            ;
                                                                                }
                                                                                else{

                                                                                    while($fetch = mysqli_fetch_array($selectQuery))
                                                                                    {
                                                                                        $countQ =  $countQ + 1;
                                                                                        //    $rowid1 =    $Selectsettings_result_cnt_row['cbtsetquestionssettings'];  
                                                                                        $questionfirst = $fetch['question'];
                                                                                        $optionA =  $fetch['optionA'];
                                                                                        $optionB =  $fetch['optionB'];
                                                                                        $optionC =  $fetch['optionC'];
                                                                                        $optionD =  $fetch['optionD'];

                                                                                        
                                                                                        // Replace <p></p> with empty space
                                                                                        $optionA1 = str_replace(array('<p>', '</p>'), '', $optionA);
                                                                                        $optionB1 = str_replace(array('<p>', '</p>'), '', $optionB);
                                                                                        $optionC1 = str_replace(array('<p>', '</p>'), '', $optionC);
                                                                                        $optionD1 = str_replace(array('<p>', '</p>'), '', $optionD);

                                                                                        // Replace < with &lt;
                                                                                        $optionAA = str_replace('<', '&lt;', $optionA1);
                                                                                        $optionBB = str_replace('<', '&lt;', $optionB1);
                                                                                        $optionCC = str_replace('<', '&lt;', $optionC1);
                                                                                        $optionDD = str_replace('<', '&lt;', $optionD1);


                                                                                        $QuestionTypeFirst =  $fetch['QuestionType'];
                                                                                        
                                                                                        $rowid =  $fetch['QuestionBankID'];

                                                                                        echo '<div id ="displayResults"> 

                                                                                            <h1 style="font-style: italic; margin-top: 10px;">(Question '.$countQ.')</h1>

                                                                                            <div class="myborder"></div>

                                                                                                <div style="margin-top: 50px; margin-left: 20px;">
                                                                                                    <h4>'.$questionfirst.'</h4>';
                                                                                                        
                                                                                                        if($QuestionTypeFirst  == 'Objective')
                                                                                                        {
                                                                                                            echo '<form class="selectSubject">
                                                                                                                <label>
                                                                                                                    <input type="radio" name="group1" class="radioBtnClass"  value="A"  id="radio_1"/>
                                                                                                                    <span>A. '.$optionAA.'</span>
                                                                                                                </label>
                                                                                                                <label>
                                                                                                                    <input type="radio" name="group1" class="radioBtnClass"  value="B"  id="radio_2"/>
                                                                                                                    <span>B. '.$optionBB.'</span>
                                                                                                                </label>
                                                                                                                <label>
                                                                                                                    <input type="radio" name="group1" class="radioBtnClass"  value="C"  id="radio_3"/>
                                                                                                                    <span>C. '.$optionCC.'</span>
                                                                                                                </label>
                                                                                                                <label>
                                                                                                                    <input type="radio" name="group1" class="radioBtnClass"  value="D"  id="radio_4"/>
                                                                                                                    <span>D. '.$optionDD.'</span>
                                                                                                                </label>
                                                                                                            </form>';

                                                                                                        }elseif($QuestionTypeFirst  == 'Theory')
                                                                                                        {
                                                                                                        echo '<textarea   id="optionA1" class="wrs_div_box example question" ></textarea>';
                                                                                                        }

                                                                                                echo '</div>

                                                                                            <div class="row" style="margin-top: 50px;">
                                                                                                <hr/>
                                                                                                <div class="col-8">
                                                                                                    <!-- <button type="button" style="font-size: 15px; background-color:#0d6efd;" class="btn waves-effect waves-light btn-rounded">Previous</button> -->
                                                                                                </div>

                                                                                                <div class="col-4">
                                                                                                    <button type="button" style="font-size: 15px; " class="btn waves-effect waves-light btn-rounded btn-secondary next float-end">Next</button>
                                                                                                </div>
                                                                                            </div>

                                                                                            <div class="row mt-3" >
                                                                                                <div class="col-lg-12">
                                                                                                    <div>
                                                                                                        <small style="color: red; font-size: 12px; text-align: justify;">
                                                                                                            <b>Note:</b> Kindly pick the correct option using the mouse to select the answer to the question
                                                                                                            by the right before the time runs out. You can also select any question below to answer. 
                                                                                                            Don\'t forget to click Submit when you are done.
                                                                                                        </small>
                                                                                                    </div>
                                                                                                </div>
                                                                                            </div>
                                                                                        
                                                                                        </div>';

                                                                                    }                     
                                                                                }

                                                                    
                                                                        }
                                                                        elseif($ShuffleOption == '0'){

                                                                            $Question = implode(',',$Questions);
                                                                            $Answer = implode(',',$answer);
                                                                        
                                                                            $studcorectans = array();

                                                                        
                                                                                foreach($Questions as $arrquestion)
                                                                                {

                                                                                    $arrquestion;
                                                                                    $studcorectans[] = 0;

                                                                                    $select_question = "SELECT * FROM `questionbank` WHERE `QuestionBankID`='$arrquestion' AND InstitutionID = '$InstitutionID' GROUP BY QuestionBankID ASC";
                                                                                    $select_question_result = mysqli_query($link, $select_question);
                                                                                    $select_question_result_cnt = mysqli_num_rows($select_question_result);
                                                                                    $select_question_result_cnt_row = mysqli_fetch_assoc($select_question_result);

                                                                                    $qustionall = $select_question_result_cnt_row['question'];
                                                                                    $optionA = $select_question_result_cnt_row['optionA'];
                                                                                    $optionB = $select_question_result_cnt_row['optionB'];
                                                                                    $optionC = $select_question_result_cnt_row['optionC'];
                                                                                    $optionD = $select_question_result_cnt_row['optionD'];

                                                                                    $canswer = $select_question_result_cnt_row['answer'];

                                                                                    $QuestionType = $select_question_result_cnt_row['QuestionType'];

                                                                                    if($QuestionType == 'Objective')
                                                                                    {


                                                                                        $questionobl = (object) [
                                                                                            'question' => $qustionall,
                                                                                            'optionA' => $optionA,
                                                                                            'optionB' => $optionB,
                                                                                            'optionC' => $optionC,
                                                                                            'optionD' => $optionD,
                                                                                            
                                                                                        ];

                                                                                            $arraynew[] = $questionobl;
                                                                                            $correctanswer[] = $canswer;

                                                                                    }
                                                                                    else
                                                                                    {

                                                                                        $questionobl = (object) [
                                                                                            'question' => $qustionall
                                                                                        ];

                                                                                        $arraynew[] = $questionobl;
                                                                                            $correctanswer[] = 0;

                                                                                    }


                                                                                }

                                                                                    $ecodedquestions = json_encode($arraynew,JSON_FORCE_OBJECT);
                                                                                    $currectanswer = implode(',',$answer);
                                                                                    $studentdefaultansw =  implode(',',$studcorectans);
                                                                                    $imploadquestionforloc = implode(',',$Questions);

                                                                                    echo '<textarea hidden="hidden" value="" id="pros-loadjsoncontentforcbt">'.$ecodedquestions.'</textarea>';
                                                                                    echo '<input type="hidden" value="'.$imploadquestionforloc.'" id="tari_question_default">';

                                                                                    echo '<input type="hidden" value="'.$QuestionType.'" id="pros-loadexamtypehere">';

                                                                                    echo '<input type="hidden" value="0" id="nxtprevquestion">
                                                                                    <textarea hidden="hidden"  id="currentanwernxt">'.$studentdefaultansw.'</textarea>
                                                                                    <input type="hidden" value="1" id="questionNo">';
                                                                                    echo '<input type="hidden" id="currectquestionID" value="'.$currectanswer.'">';



                                                                                    $countQ = 0;
                                                                                    $settingarrfirt =  $Questions[0];
                                                                                    $Select = "SELECT * FROM `questionbank` WHERE `QuestionBankID`='$settingarrfirt' AND InstitutionID = '$InstitutionID' GROUP BY QuestionBankID ASC  LIMIT 1";
                                                                                    $selectQuery = mysqli_query($link, $Select);
                                                                                    $fetch2 = mysqli_num_rows($selectQuery);

                                                                                if($fetch2 == 0){
                                                                                                
                                                                                    echo '<div class="alert alert-info alert-dismissible d-flex align-items-center show">
                                                                                    <i class="fa fa-info-circle"></i>
                                                                                    <strong class="mx-2">Hey!</strong>Please no question attached to this settings.
                                                                                    </div>
                                                                                    <a  href="cbtSetting.php"  class="close" aria-label="Close" style="color:red; margin-right:20px;"><i clas="fa fa-back"></i>Back
                                                                                    </a>' ;

                                                                                }
                                                                                else
                                                                                {
                                                                                    while($fetch = mysqli_fetch_array($selectQuery))
                                                                                    {

                                                                                        $countQ =  $countQ + 1;
                                                                                        //    $rowid1 =    $Selectsettings_result_cnt_row['cbtsetquestionssettings'];  
                                                                                        $questionfirst = $fetch['question'];
                                                                                        $optionA =  $fetch['optionA'];
                                                                                        $optionB =  $fetch['optionB'];
                                                                                        $optionC =  $fetch['optionC'];
                                                                                        $optionD =  $fetch['optionD'];


                                                                                        // Replace <p></p> with empty space
                                                                                        $optionA1 = str_replace(array('<p>', '</p>'), '', $optionA);
                                                                                        $optionB1 = str_replace(array('<p>', '</p>'), '', $optionB);
                                                                                        $optionC1 = str_replace(array('<p>', '</p>'), '', $optionC);
                                                                                        $optionD1 = str_replace(array('<p>', '</p>'), '', $optionD);

                                                                                        // Replace < with &lt;
                                                                                        $optionAA = str_replace('<', '&lt;', $optionA1);
                                                                                        $optionBB = str_replace('<', '&lt;', $optionB1);
                                                                                        $optionCC = str_replace('<', '&lt;', $optionC1);
                                                                                        $optionDD = str_replace('<', '&lt;', $optionD1);


                                                                                        $QuestionTypeFirst =  $fetch['QuestionType'];

                                                                                        $rowid =  $fetch['QuestionBankID'];

                                                                                        echo '<div id ="displayResults"> 

                                                                                        <h1 style="font-style: italic; margin-top: 10px;">(Question '.$countQ.')</h1>

                                                                                        <div class="myborder"></div>

                                                                                            <div style="margin-top: 50px; margin-left: 20px;">
                                                                                                <h4>'.$questionfirst.'</h4>';
                                                                                                    
                                                                                                    if($QuestionTypeFirst  == 'Objective')
                                                                                                    {
                                                                                                        echo '<form class="selectSubject">
                                                                                                            <label>
                                                                                                                <input type="radio" name="group1" class="radioBtnClass"  value="A"  id="radio_1"/>
                                                                                                                <span>A. '.$optionAA.'</span>
                                                                                                            </label>
                                                                                                            <label>
                                                                                                                <input type="radio" name="group1" class="radioBtnClass"  value="B"  id="radio_2"/>
                                                                                                                <span>B. '.$optionBB.'</span>
                                                                                                            </label>
                                                                                                            <label>
                                                                                                                <input type="radio" name="group1" class="radioBtnClass"  value="C"  id="radio_3"/>
                                                                                                                <span>C. '.$optionCC.'</span>
                                                                                                            </label>
                                                                                                            <label>
                                                                                                                <input type="radio" name="group1" class="radioBtnClass"  value="D"  id="radio_4"/>
                                                                                                                <span>D. '.$optionDD.'</span>
                                                                                                            </label>
                                                                                                        </form>';

                                                                                                    }elseif($QuestionTypeFirst  == 'Theory')
                                                                                                    {
                                                                                                    echo '<textarea   id="optionA1" class="wrs_div_box example question" ></textarea>';
                                                                                                    }

                                                                                            echo '</div>

                                                                                        <div class="row" style="margin-top: 50px;">
                                                                                            <hr/>
                                                                                            <div class="col-8">
                                                                                                <!-- <button type="button" style="font-size: 15px; background-color:#0d6efd" class="btn waves-effect waves-light btn-rounded">Previous</button> -->
                                                                                            </div>

                                                                                            <div class="col-4">
                                                                                                <button type="button" style="font-size: 15px; " class="btn waves-effect waves-light btn-rounded btn-secondary next float-end">Next</button>
                                                                                            </div>
                                                                                        </div>

                                                                                        <div class="row mt-3" >
                                                                                            <div class="col-lg-12">
                                                                                                <div>
                                                                                                    <small style="color: red; font-size: 12px; text-align: justify;">
                                                                                                        <b>Note:</b> Kindly pick the correct option using the mouse to select the answer to the question
                                                                                                        by the right before the time runs out. You can also select any question below to answer. 
                                                                                                        Don\'t forget to click Submit when you are done.
                                                                                                    </small>
                                                                                                </div>
                                                                                            </div>
                                                                                        </div>

                                                                                        </div>';

                                                                                    }                     
                                                                                }

                                                                            
                                                                        }
                                                                    }


                                                                    


                                                            }

                                                        }

                                                        // Load OBJ Questions
                                                        else{

                                                            // $TheoryQuestion = explode(',',$selectquestionsetrow_cnt['TheoryQuestion']);
                                                            $Questions = explode(',',$selectquestionsetrow_cnt['question']);
                                                            $answer = explode(',',$selectquestionsetrow_cnt['answer']);



                                                            // Check if Student has been Inserted into the Student Answer Table
                                                            $check_if_student_exist = mysqli_query($link,"SELECT * FROM `cbtquestionsanswers` 
                                                                                                    WHERE `cbtquestionsanswers`.`cbtsetquestionssettingsID`='$cbtSettingsID'
                                                                                                    AND StudentID = '$StudentID'
                                                                                                    AND CampusID = '$CampusID'
                                                                                                    AND TermOrSemesterID = '$TermOrSemesterID'
                                                                                                    AND  ClassOrDepartmentID = '$ClassOrDepartmentID'
                                                                                                    AND SubjectOrCourseID = '$SubjectOrCourseID'
                                                                                                    AND objective_status = '0'");
                                                                                
                                                            $exist_num = mysqli_num_rows($check_if_student_exist);
                                                            $exist_fetch = mysqli_fetch_assoc($check_if_student_exist);
                                                            
                                                                // Check if Student Exist and has not Submitted
                                                                if($exist_num > 0){

                                                                    $question = explode(',',$exist_fetch['question']);
                                                                    $answer = explode(',',$exist_fetch['Answer']);
                                                                    

                                                                    foreach($question as $arrquestion)
                                                                    {

                                                                        $arrquestion;
                                                                        $studcorectans[] = 0;


                                                                        $select_question = "SELECT * FROM `questionbank` WHERE `QuestionBankID`='$arrquestion' AND InstitutionID = '$InstitutionID'  GROUP BY QuestionBankID ASC";
                                                                        $select_question_result = mysqli_query($link, $select_question);
                                                                        $select_question_result_cnt = mysqli_num_rows($select_question_result);
                                                                        $select_question_result_cnt_row = mysqli_fetch_assoc($select_question_result);
                                                                    
                                                                        $qustionall = $select_question_result_cnt_row['question'];
                                                                        $Questionid_1 = $select_question_result_cnt_row['QuestionBankID'];
                                                                        $optionA = $select_question_result_cnt_row['optionA'];
                                                                        $optionB = $select_question_result_cnt_row['optionB'];
                                                                        $optionC = $select_question_result_cnt_row['optionC'];
                                                                        $optionD = $select_question_result_cnt_row['optionD'];

                                                                        $canswer = $select_question_result_cnt_row['answer'];

                                                                        $QuestionType = $select_question_result_cnt_row['QuestionCategory'];

                                                                        if($QuestionType == 'Objective')
                                                                        {
                                                                            

                                                                            $questionobl = (object) [
                                                                                'question' => $qustionall,
                                                                                'optionA' => $optionA,
                                                                                'optionB' => $optionB,
                                                                                'optionC' => $optionC,
                                                                                'optionD' => $optionD,
                                                                                
                                                                            ];

                                                                                $arraynew[] = $questionobl;
                                                                                $correctanswer[] = $canswer;

                                                                        }else
                                                                        {

                                                                            $questionobl = (object) [
                                                                                'question' => $qustionall
                                                                            ];

                                                                            $arraynew[] = $questionobl;
                                                                            $correctanswer[] = 0;

                                                                        }
                                                                        
                                                                        
                                                                    }
                                                                        $ecodedquestions = json_encode($arraynew,JSON_FORCE_OBJECT);
                                                                        $currectanswer = implode(',',$answer);
                                                                        $studentdefaultansw =  implode(',',$studcorectans);
                                                                        $imploadquestionforloc = implode(',',$question);

                                                                        echo '<textarea hidden="hidden" value="" id="pros-loadjsoncontentforcbt">'.$ecodedquestions.'</textarea>';
                                                                        echo '<input type="hidden" value="'.$imploadquestionforloc.'" id="tari_question_default">';

                                                                        echo '<input type="hidden" value="'.$QuestionType.'" id="pros-loadexamtypehere">';

                                                                        echo '<input type="hidden" value="0" id="nxtprevquestion">
                                                                            <textarea hidden="hidden"  id="currentanwernxt">'.$studentdefaultansw.'</textarea>
                                                                            <input type="hidden" value="1" id="questionNo">';
                                                                            echo '<input type="hidden" id="currectquestionID" value="'.$currectanswer.'">';



                                                                        $countQ = 0;
                                                                        $settingarrfirt =  $Questions[0];
                                                                        $Select = "SELECT * FROM `questionbank` WHERE `QuestionBankID`='$settingarrfirt' AND  CampusID = '$CampusID' GROUP BY QuestionBankID ASC  LIMIT 1";
                                                                        $selectQuery = mysqli_query($link, $Select);
                                                                        $fetch2 = mysqli_num_rows($selectQuery);
                                                                    
                                                                    //If Question Doesnt Exist 
                                                                    if($fetch2 == 0){
                                                                                                
                                                                        echo '<div class="alert alert-info alert-dismissible d-flex align-items-center show">
                                                                                <i class="fa fa-info-circle"></i>
                                                                            <strong class="mx-2">Hey!</strong>Please no question attached to this settings.
                                                                            </div>
                                                                            <a  href="cbtSetting.php"  class="close" aria-label="Close" style="color:red; margin-right:20px;"><i clas="fa fa-back"></i>Back
                                                                            </a>';
                                                                    }
                                                                    //Question ID Exist
                                                                    else{
                                                                        while($fetch = mysqli_fetch_array($selectQuery))
                                                                        {
                                                                            $countQ =  $countQ + 1;
                                                                            //    $rowid1 =    $Selectsettings_result_cnt_row['cbtsetquestionssettings'];  

                                                                            $questionfirst = $fetch['question'];
                                                                            $optionA =  $fetch['optionA'];
                                                                            $optionB =  $fetch['optionB'];
                                                                            $optionC =  $fetch['optionC'];
                                                                            $optionD =  $fetch['optionD'];

                                                                            
                                                                            // Replace <p></p> with empty space
                                                                            $optionA1 = str_replace(array('<p>', '</p>'), '', $optionA);
                                                                            $optionB1 = str_replace(array('<p>', '</p>'), '', $optionB);
                                                                            $optionC1 = str_replace(array('<p>', '</p>'), '', $optionC);
                                                                            $optionD1 = str_replace(array('<p>', '</p>'), '', $optionD);

                                                                            // Replace < with &lt;
                                                                            $optionAA = str_replace('<', '&lt;', $optionA1);
                                                                            $optionBB = str_replace('<', '&lt;', $optionB1);
                                                                            $optionCC = str_replace('<', '&lt;', $optionC1);
                                                                            $optionDD = str_replace('<', '&lt;', $optionD1);


                                                                            $QuestionTypeFirst =  $fetch['QuestionCategory'];
                                                                            
                                                                            $rowid =  $fetch['QuestionBankID'];

                                                                            echo '<div id ="displayResults"> 

                                                                                <h1 style="font-style: italic; margin-top: 10px;">(Question '.$countQ.')</h1>

                                                                                <div class="myborder"></div>

                                                                                    <div style="margin-top: 50px; margin-left: 20px;">
                                                                                        <h4>'.$questionfirst.'</h4>';
                                                                                            
                                                                                            if($QuestionTypeFirst  == 'Objective')
                                                                                            {
                                                                                                echo '<form class="selectSubject">
                                                                                                    <label>
                                                                                                        <input type="radio" name="group1" class="radioBtnClass"  value="A"  id="radio_1"/>
                                                                                                        <span>A. '.$optionAA.'</span>
                                                                                                    </label>
                                                                                                    <label>
                                                                                                        <input type="radio" name="group1" class="radioBtnClass"  value="B"  id="radio_2"/>
                                                                                                        <span>B. '.$optionBB.'</span>
                                                                                                    </label>
                                                                                                    <label>
                                                                                                        <input type="radio" name="group1" class="radioBtnClass"  value="C"  id="radio_3"/>
                                                                                                        <span>C. '.$optionCC.'</span>
                                                                                                    </label>
                                                                                                    <label>
                                                                                                        <input type="radio" name="group1" class="radioBtnClass"  value="D"  id="radio_4"/>
                                                                                                        <span>D. '.$optionDD.'</span>
                                                                                                    </label>
                                                                                                </form>';

                                                                                            }elseif($QuestionTypeFirst  == 'Theory')
                                                                                            {
                                                                                            echo '<textarea   id="optionA1" class="wrs_div_box example question" ></textarea>';
                                                                                            }

                                                                                    echo '</div>

                                                                                <div class="row" style="margin-top: 50px;">
                                                                                    <hr/>
                                                                                    <div class="col-8">
                                                                                        <!-- <button type="button" style="font-size: 15px; background-color:#0d6efd;" class="btn waves-effect waves-light btn-rounded">Previous</button> -->
                                                                                    </div>

                                                                                    <div class="col-4">
                                                                                        <button type="button" style="font-size: 15px;" class="btn waves-effect waves-light btn-rounded btn-secondary next float-end">Next</button>
                                                                                    </div>
                                                                                </div>

                                                                                <div class="row mt-3" >
                                                                                    <div class="col-lg-12">
                                                                                        <div>
                                                                                            <small style="color: red; font-size: 12px; text-align: justify;">
                                                                                                <b>Note:</b> Kindly pick the correct option using the mouse to select the answer to the question
                                                                                                by the right before the time runs out. You can also select any question below to answer. 
                                                                                                Don\'t forget to click Submit when you are done.
                                                                                            </small>
                                                                                        </div>
                                                                                    </div>
                                                                                </div>
                                                                            
                                                                            </div>';

                                                                        }                     
                                                                    }

                                                                }
                                                                else{

                                                                    if($ShuffleOption == '1'){

                                                                        $Questions = explode(',',$selectquestionsetrow_cnt['question']);
                                                                        $answer = explode(',',$selectquestionsetrow_cnt['answer']);


                                                                        $studcorectans = array();

                                                                        // Sample questions and answers arrays
                                                                        // print_r($Questions);
                                                                        // print_r($answer);

                                                                        $questions = $Questions;
                                                                        $answers = $answer;
                                                                    
                                                                        // Combine questions and answers into an array of arrays
                                                                        $combined = array();
                                                                        foreach ($questions as $index => $question) {
                                                                            $combined[] = array('question' => $question, 'answer' => $answers[$index]);
                                                                        }

                                                                        // Shuffle the combined array
                                                                        shuffle($combined);

                                                                        // Separate the shuffled array back into questions and answers
                                                                        $shuffledQuestions = array();
                                                                        $shuffledAnswers = array();

                                                                        foreach ($combined as $item) {
                                                                            $shuffledQuestions1[] = $item['question'];
                                                                            $shuffledAnswers1[] = $item['answer'];
                                                                        }

                                                                        $shuffledQuestions = implode(',', $shuffledQuestions1);
                                                                        $shuffledAnswers = implode(',', $shuffledAnswers1);


                                                                        $insert_shuffled_question = mysqli_query($link, "INSERT INTO `cbtquestionsanswers` (`cbtsetquestionssettingsID`,`StudentID`,`CampusID`,`sessionID`,`TermOrSemesterID`,`ClassOrDepartmentID`,`SubjectOrCourseID`,`question`,`Answer`) 
                                                                                                                        VALUES ('$cbtsetquestionssettingsID','$StudentID','$CampusID','$sessionID','$TermOrSemesterID','$ClassOrDepartmentID','$SubjectOrCourseID','$shuffledQuestions','$shuffledAnswers')");


                                                                        if($insert_shuffled_question){

                                                                            foreach($shuffledQuestions1 as $arrquestion)
                                                                            {

                                                                                $arrquestion;
                                                                                $studcorectans[] = 0;

                                                                        

                                                                                $select_question = "SELECT * FROM `questionbank` WHERE `QuestionBankID`='$arrquestion' AND InstitutionID = '$InstitutionID' GROUP BY QuestionBankID ASC";
                                                                                $select_question_result = mysqli_query($link, $select_question);
                                                                                $select_question_result_cnt = mysqli_num_rows($select_question_result);
                                                                                $select_question_result_cnt_row = mysqli_fetch_assoc($select_question_result);
                                                                            
                                                                                $qustionall = $select_question_result_cnt_row['question'];
                                                                                $Questionid_1 = $select_question_result_cnt_row['QuestionBankID'];
                                                                                $optionA = $select_question_result_cnt_row['optionA'];
                                                                                $optionB = $select_question_result_cnt_row['optionB'];
                                                                                $optionC = $select_question_result_cnt_row['optionC'];
                                                                                $optionD = $select_question_result_cnt_row['optionD'];

                                                                                $canswer = $select_question_result_cnt_row['answer'];

                                                                                $QuestionType = $select_question_result_cnt_row['QuestionCategory'];

                                                                                if($QuestionType == 'Objective')
                                                                                {
                                                                                    

                                                                                    $questionobl = (object) [
                                                                                        'question' => $qustionall,
                                                                                        'optionA' => $optionA,
                                                                                        'optionB' => $optionB,
                                                                                        'optionC' => $optionC,
                                                                                        'optionD' => $optionD,
                                                                                        
                                                                                    ];

                                                                                        $arraynew[] = $questionobl;
                                                                                        $correctanswer[] = $canswer;

                                                                                }else
                                                                                {

                                                                                    $questionobl = (object) [
                                                                                        'question' => $qustionall
                                                                                    ];

                                                                                    $arraynew[] = $questionobl;
                                                                                        $correctanswer[] = 0;

                                                                                }

                                                                        
                                                                            }

                                                                            $ecodedquestions = json_encode($arraynew,JSON_FORCE_OBJECT);
                                                                            $currectanswer = implode(',',$shuffledAnswers1);
                                                                            $studentdefaultansw =  implode(',',$studcorectans);
                                                                            $imploadquestionforloc = implode(',',$shuffledQuestions1);

                                                                            echo '<textarea hidden="hidden" value="" id="pros-loadjsoncontentforcbt">'.$ecodedquestions.'</textarea>';
                                                                            echo '<input type="hidden" value="'.$imploadquestionforloc.'" id="tari_question_default">';

                                                                            echo '<input type="hidden" value="'.$QuestionType.'" id="pros-loadexamtypehere">';

                                                                            echo '<input type="hidden" value="0" id="nxtprevquestion">
                                                                                <textarea hidden="hidden"  id="currentanwernxt">'.$studentdefaultansw.'</textarea>
                                                                                <input type="hidden" value="1" id="questionNo">';
                                                                            echo '<input type="hidden" id="currectquestionID" value="'.$currectanswer.'">';



                                                                            $countQ = 0;
                                                                            $settingarrfirt =  $Questions[0];
                                                                            $Select = "SELECT * FROM `questionbank` WHERE `QuestionBankID`='$settingarrfirt' AND  InstitutionID = '$InstitutionID' GROUP BY QuestionBankID ASC  LIMIT 1";
                                                                            $selectQuery = mysqli_query($link, $Select);
                                                                            $fetch2 = mysqli_num_rows($selectQuery);

                                                                            if($fetch2 == 0){
                                                                                                        
                                                                                echo '<div class="alert alert-info alert-dismissible d-flex align-items-center show">
                                                                                        <i class="fa fa-info-circle"></i>
                                                                                    <strong class="mx-2">Hey!</strong>Please no question attached to this settings.
                                                                                    </div>
                                                                                    <a  href="cbtSetting.php"  class="close" aria-label="Close" style="color:red; margin-right:20px;"><i clas="fa fa-back"></i>Back
                                                                                    </a>'
                                                                                        ;
                                                                            }
                                                                            else{
                                                                                while($fetch = mysqli_fetch_array($selectQuery))
                                                                                {
                                                                                    $countQ =  $countQ + 1;
                                                                                    //    $rowid1 =    $Selectsettings_result_cnt_row['cbtsetquestionssettings'];  
                                                                                    $questionfirst = $fetch['question'];
                                                                                    $optionA =  $fetch['optionA'];
                                                                                    $optionB =  $fetch['optionB'];
                                                                                    $optionC =  $fetch['optionC'];
                                                                                    $optionD =  $fetch['optionD'];

                                                                                    
                                                                                    // Replace <p></p> with empty space
                                                                                    $optionA1 = str_replace(array('<p>', '</p>'), '', $optionA);
                                                                                    $optionB1 = str_replace(array('<p>', '</p>'), '', $optionB);
                                                                                    $optionC1 = str_replace(array('<p>', '</p>'), '', $optionC);
                                                                                    $optionD1 = str_replace(array('<p>', '</p>'), '', $optionD);

                                                                                    // Replace < with &lt;
                                                                                    $optionAA = str_replace('<', '&lt;', $optionA1);
                                                                                    $optionBB = str_replace('<', '&lt;', $optionB1);
                                                                                    $optionCC = str_replace('<', '&lt;', $optionC1);
                                                                                    $optionDD = str_replace('<', '&lt;', $optionD1);


                                                                                    $QuestionTypeFirst =  $fetch['QuestionCategory'];
                                                                                    
                                                                                    $rowid =  $fetch['QuestionBankID'];

                                                                                    echo '<div id ="displayResults"> 

                                                                                        <h1 style="font-style: italic; margin-top: 10px;">(Question '.$countQ.')</h1>

                                                                                        <div class="myborder"></div>

                                                                                            <div style="margin-top: 50px; margin-left: 20px;">
                                                                                                <h4>'.$questionfirst.'</h4>';
                                                                                                    
                                                                                                    if($QuestionTypeFirst  == 'Objective')
                                                                                                    {
                                                                                                        echo '<form class="selectSubject">
                                                                                                            <label>
                                                                                                                <input type="radio" name="group1" class="radioBtnClass"  value="A"  id="radio_1"/>
                                                                                                                <span>A. '.$optionAA.'</span>
                                                                                                            </label>
                                                                                                            <label>
                                                                                                                <input type="radio" name="group1" class="radioBtnClass"  value="B"  id="radio_2"/>
                                                                                                                <span>B. '.$optionBB.'</span>
                                                                                                            </label>
                                                                                                            <label>
                                                                                                                <input type="radio" name="group1" class="radioBtnClass"  value="C"  id="radio_3"/>
                                                                                                                <span>C. '.$optionCC.'</span>
                                                                                                            </label>
                                                                                                            <label>
                                                                                                                <input type="radio" name="group1" class="radioBtnClass"  value="D"  id="radio_4"/>
                                                                                                                <span>D. '.$optionDD.'</span>
                                                                                                            </label>
                                                                                                        </form>';

                                                                                                    }elseif($QuestionTypeFirst  == 'Theory')
                                                                                                    {
                                                                                                    echo '<textarea   id="optionA1" class="wrs_div_box example question" ></textarea>';
                                                                                                    }

                                                                                            echo '</div>

                                                                                        <div class="row" style="margin-top: 50px;">
                                                                                            <hr/>
                                                                                            <div class="col-8">
                                                                                                <!-- <button type="button" style="font-size: 15px; background-color:#0d6efd;" class="btn waves-effect waves-light btn-rounded">Previous</button> -->
                                                                                            </div>

                                                                                            <div class="col-4">
                                                                                                <button type="button" style="font-size: 15px; " class="btn waves-effect waves-light btn-rounded btn-secondary next float-end">Next</button>
                                                                                            </div>
                                                                                        </div>

                                                                                        <div class="row mt-3" >
                                                                                            <div class="col-lg-12">
                                                                                                <div>
                                                                                                    <small style="color: red; font-size: 12px; text-align: justify;">
                                                                                                        <b>Note:</b> Kindly pick the correct option using the mouse to select the answer to the question
                                                                                                        by the right before the time runs out. You can also select any question below to answer. 
                                                                                                        Don\'t forget to click Submit when you are done.
                                                                                                    </small>
                                                                                                </div>
                                                                                            </div>
                                                                                        </div>
                                                                                    
                                                                                    </div>';

                                                                                }                     
                                                                            }

                                                                        }else{
                                                                            echo 'Not Inserted';
                                                                        }
                                                                
                                                                    }
                                                                    elseif($ShuffleOption == '0'){

                                                                        $Question = implode(',',$Questions);
                                                                        $Answer = implode(',',$answer);
                                                                    
                                                                        $studcorectans = array();

                                                                        $insert_shuffled_question = mysqli_query($link, "INSERT INTO `cbtquestionsanswers` (`cbtsetquestionssettingsID`,`StudentID`,`CampusID`,`sessionID`,`TermOrSemesterID`,`ClassOrDepartmentID`,`SubjectOrCourseID`,`question`,`Answer`) 
                                                                                                                VALUES ('$cbtsetquestionssettingsID','$StudentID','$CampusID','$sessionID','$TermOrSemesterID','$ClassOrDepartmentID','$SubjectOrCourseID','$Question','$Answer')");


                                                                        if($insert_shuffled_question){

                                                                            foreach($Questions as $arrquestion)
                                                                            {

                                                                                $arrquestion;
                                                                                $studcorectans[] = 0;

                                                                                $select_question = "SELECT * FROM `questionbank` WHERE `QuestionBankID`='$arrquestion' AND InstitutionID = '$InstitutionID' GROUP BY QuestionBankID ASC";
                                                                                $select_question_result = mysqli_query($link, $select_question);
                                                                                $select_question_result_cnt = mysqli_num_rows($select_question_result);
                                                                                $select_question_result_cnt_row = mysqli_fetch_assoc($select_question_result);

                                                                                $qustionall = $select_question_result_cnt_row['question'];
                                                                                $optionA = $select_question_result_cnt_row['optionA'];
                                                                                $optionB = $select_question_result_cnt_row['optionB'];
                                                                                $optionC = $select_question_result_cnt_row['optionC'];
                                                                                $optionD = $select_question_result_cnt_row['optionD'];

                                                                                $canswer = $select_question_result_cnt_row['answer'];

                                                                                $QuestionType = $select_question_result_cnt_row['QuestionCategory'];

                                                                                if($QuestionType == 'Objective')
                                                                                {


                                                                                    $questionobl = (object) [
                                                                                        'question' => $qustionall,
                                                                                        'optionA' => $optionA,
                                                                                        'optionB' => $optionB,
                                                                                        'optionC' => $optionC,
                                                                                        'optionD' => $optionD,
                                                                                        
                                                                                    ];

                                                                                        $arraynew[] = $questionobl;
                                                                                        $correctanswer[] = $canswer;

                                                                                }
                                                                                else
                                                                                {

                                                                                    $questionobl = (object) [
                                                                                        'question' => $qustionall
                                                                                    ];

                                                                                    $arraynew[] = $questionobl;
                                                                                        $correctanswer[] = 0;

                                                                                }


                                                                            }

                                                                                $ecodedquestions = json_encode($arraynew,JSON_FORCE_OBJECT);
                                                                                $currectanswer = implode(',',$answer);
                                                                                $studentdefaultansw =  implode(',',$studcorectans);
                                                                                $imploadquestionforloc = implode(',',$Questions);

                                                                                echo '<textarea hidden="hidden" value="" id="pros-loadjsoncontentforcbt">'.$ecodedquestions.'</textarea>';
                                                                                echo '<input type="hidden" value="'.$imploadquestionforloc.'" id="tari_question_default">';

                                                                                echo '<input type="hidden" value="'.$QuestionType.'" id="pros-loadexamtypehere">';

                                                                                echo '<input type="hidden" value="0" id="nxtprevquestion">
                                                                                <textarea hidden="hidden"  id="currentanwernxt">'.$studentdefaultansw.'</textarea>
                                                                                <input type="hidden" value="1" id="questionNo">';
                                                                                echo '<input type="hidden" id="currectquestionID" value="'.$currectanswer.'">';



                                                                                $countQ = 0;
                                                                                $settingarrfirt =  $Questions[0];
                                                                                $Select = "SELECT * FROM `questionbank` WHERE `QuestionBankID`='$settingarrfirt' AND  InstitutionID = '$InstitutionID' GROUP BY QuestionBankID ASC  LIMIT 1";
                                                                                $selectQuery = mysqli_query($link, $Select);
                                                                                $fetch2 = mysqli_num_rows($selectQuery);

                                                                            if($fetch2 == 0){
                                                                                            
                                                                                echo '<div class="alert alert-info alert-dismissible d-flex align-items-center show">
                                                                                <i class="fa fa-info-circle"></i>
                                                                                <strong class="mx-2">Hey!</strong>Please no question attached to this settings.
                                                                                </div>
                                                                                <a  href="cbtSetting.php"  class="close" aria-label="Close" style="color:red; margin-right:20px;"><i clas="fa fa-back"></i>Back
                                                                                </a>' ;

                                                                            }
                                                                            else
                                                                            {
                                                                                while($fetch = mysqli_fetch_array($selectQuery))
                                                                                {

                                                                                    $countQ =  $countQ + 1;
                                                                                    //    $rowid1 =    $Selectsettings_result_cnt_row['cbtsetquestionssettings'];  
                                                                                    $questionfirst = $fetch['question'];
                                                                                    $optionA =  $fetch['optionA'];
                                                                                    $optionB =  $fetch['optionB'];
                                                                                    $optionC =  $fetch['optionC'];
                                                                                    $optionD =  $fetch['optionD'];


                                                                                    // Replace <p></p> with empty space
                                                                                    $optionA1 = str_replace(array('<p>', '</p>'), '', $optionA);
                                                                                    $optionB1 = str_replace(array('<p>', '</p>'), '', $optionB);
                                                                                    $optionC1 = str_replace(array('<p>', '</p>'), '', $optionC);
                                                                                    $optionD1 = str_replace(array('<p>', '</p>'), '', $optionD);

                                                                                    // Replace < with &lt;
                                                                                    $optionAA = str_replace('<', '&lt;', $optionA1);
                                                                                    $optionBB = str_replace('<', '&lt;', $optionB1);
                                                                                    $optionCC = str_replace('<', '&lt;', $optionC1);
                                                                                    $optionDD = str_replace('<', '&lt;', $optionD1);


                                                                                    $QuestionTypeFirst =  $fetch['QuestionCategory'];

                                                                                    $rowid =  $fetch['QuestionBankID'];

                                                                                    echo '<div id ="displayResults"> 

                                                                                    <h1 style="font-style: italic; margin-top: 10px;">(Question '.$countQ.')</h1>

                                                                                    <div class="myborder"></div>

                                                                                        <div style="margin-top: 50px; margin-left: 20px;">
                                                                                            <h4>'.$questionfirst.'</h4>';
                                                                                                
                                                                                                if($QuestionTypeFirst  == 'Objective')
                                                                                                {
                                                                                                    echo '<form class="selectSubject">
                                                                                                        <label>
                                                                                                            <input type="radio" name="group1" class="radioBtnClass"  value="A"  id="radio_1"/>
                                                                                                            <span>A. '.$optionAA.'</span>
                                                                                                        </label>
                                                                                                        <label>
                                                                                                            <input type="radio" name="group1" class="radioBtnClass"  value="B"  id="radio_2"/>
                                                                                                            <span>B. '.$optionBB.'</span>
                                                                                                        </label>
                                                                                                        <label>
                                                                                                            <input type="radio" name="group1" class="radioBtnClass"  value="C"  id="radio_3"/>
                                                                                                            <span>C. '.$optionCC.'</span>
                                                                                                        </label>
                                                                                                        <label>
                                                                                                            <input type="radio" name="group1" class="radioBtnClass"  value="D"  id="radio_4"/>
                                                                                                            <span>D. '.$optionDD.'</span>
                                                                                                        </label>
                                                                                                    </form>';

                                                                                                }elseif($QuestionTypeFirst  == 'Theory')
                                                                                                {
                                                                                                echo '<textarea   id="optionA1" class="wrs_div_box example question" ></textarea>';
                                                                                                }

                                                                                        echo '</div>

                                                                                    <div class="row" style="margin-top: 50px;">
                                                                                        <hr/>
                                                                                        <div class="col-8">
                                                                                            <!-- <button type="button" style="font-size: 15px; background-color:#0d6efd" class="btn waves-effect waves-light btn-rounded">Previous</button> -->
                                                                                        </div>

                                                                                        <div class="col-4">
                                                                                            <button type="button" style="font-size: 15px; " class="btn waves-effect waves-light btn-rounded btn-secondary next float-end">Next</button>
                                                                                        </div>
                                                                                    </div>

                                                                                    <div class="row mt-3" >
                                                                                        <div class="col-lg-12">
                                                                                            <div>
                                                                                                <small style="color: red; font-size: 12px; text-align: justify;">
                                                                                                    <b>Note:</b> Kindly pick the correct option using the mouse to select the answer to the question
                                                                                                    by the right before the time runs out. You can also select any question below to answer. 
                                                                                                    Don\'t forget to click Submit when you are done.
                                                                                                </small>
                                                                                            </div>
                                                                                        </div>
                                                                                    </div>

                                                                                    </div>';

                                                                                }                     
                                                                            }

                                                                        }
                                                                    }
                                                                }
                                                            

                                                        }

                                                        }
                                                        else
                                                        {

                                                        echo "Category Does Not Exist";
                                                        }

                                                        echo'</div>   
                                                        </div>';

                                                        echo'<div  class="col-lg-4" style="margin-top: 20px;">
                                                        <div class="card">
                                                        <div class="card-body" style="background-color: rgb(255, 255, 255); box-shadow: 1px 3px 10px #C4DDEB;">
                                                        <h2 style="font-weight: 700;" align="center">Time Left</h2>

                                                        <div class="row">
                                                            <div class="col-2">
                                                                
                                                            </div>

                                                            <div class="col-3">
                                                                <h3 id="examhour">00</h3>
                                                                <small align="center">Hours</small>
                                                            </div>
                                                            
                                                            <div class="col-3">
                                                                <h3 id="examminutes">00</h3>
                                                                <small>Minutes</small>
                                                            </div>
                                                            <div class="col-4">
                                                                <h3 id="examseconds">00</h3>
                                                                <small>Seconds</small>
                                                            </div>

                                                        </div>

                                                        <div style="margin-top: 63px; padding: none;">
                                                        <div class="">
                                                        <div class="col-lg-1">

                                                        </div>';

                                                            $AssesementCategory = $QuestionTypeFirst; 

                                                            $countkey = 0;
                                                            $counquestion_num =1;
                                                            $countkey = 0;

                                                            if($AssesementCategory == 'Objective'){

                                                                foreach($Questions as $questionnew)
                                                                {


                                                                    $questionnew;

                                                                    $counquestion_num++; 
                                                                    $countkey++;

                                                                    $newquenum  =   $counquestion_num -1;
                                                                    $questionkey2  =  $countkey -1;
                                                                    
                                                                    $slit2arr; 
                                                            
                                                                    $Select1 = "SELECT * FROM  `questionbank` WHERE  InstitutionID = '$InstitutionID'  AND QuestionBankID='$questionnew' ORDER BY QuestionBankID ASC";
                                                                    $selectQuery1 = mysqli_query($link, $Select1);
                                                                    $fetch1 = mysqli_fetch_array($selectQuery1);

                                                            
                                                                    $question =  $fetch1['question'];
                                                                    $optionA =  $fetch1['optionA'];
                                                                    $optionB =  $fetch1['optionB'];
                                                                    $optionC =  $fetch1['optionC'];
                                                                    $optionD =  $fetch1['optionD'];

                                                                    $QuestionTypenew =  $fetch1['QuestionType'];

                                                                    $cbt_setting_id =  $fetch1['QuestionBankID'];
                                                                

                                                                    if($QuestionTypenew == 'Objective')
                                                                    {
                                                                        $questiontype ='Objective';

                                                                    }else
                                                                    {
                                                                        $questiontype = 'Theory'; 
                                                                    }

                                                                    echo ' <button style="font-weight: 600; font-size: 12px;margin: 2px; background-color:#0d6efd;" " type="button" data-question="'.$questiontype.'" class="btn btn-success viewer" id="'.$newquenum.'" data-id="'.$questionkey2.'">'.$newquenum.'</button>';
                                                                                            
                                                                }

                                                            }elseif($AssesementCategory == 'Theory'){

                                                                foreach($TheoryQuestion as $questionnew)
                                                                {


                                                                    $questionnew;

                                                                    $counquestion_num++; 
                                                                    $countkey++;

                                                                    $newquenum  =   $counquestion_num -1;
                                                                    $questionkey2  =  $countkey -1;
                                                                    
                                                                    $Select1 = "SELECT * FROM  `questionbank` WHERE  InstitutionID = '$InstitutionID'  AND QuestionBankID='$questionnew' ORDER BY QuestionBankID ASC";
                                                                    $selectQuery1 = mysqli_query($link, $Select1);
                                                                    $fetch1 = mysqli_fetch_array($selectQuery1);

                                                            
                                                                    $question =  $fetch1['question'];

                                                                    $QuestionTypenew =  $fetch1['QuestionType'];

                                                                    $cbt_setting_id =  $fetch1['QuestionBankID'];
                                                                

                                                                    if($QuestionTypenew == 'Theory')
                                                                    {
                                                                        $questiontype = 'Theory'; 
                                                                    }

                                                                    echo ' <button style="font-weight: 600; font-size: 12px;margin: 2px; background-color:#0d6efd;" " type="button" data-question="'.$questiontype.'" class="btn btn-success viewer" id="'.$newquenum.'" data-id="'.$questionkey2.'">'.$newquenum.'</button>';
                                                                                            
                                                                }

                                                            }
                                                                                                                                                                
                                                        echo '</div>
                                                        <div class="row mt-3">
                                                        <div class="col-8">
                                                            
                                                        </div>
                                                        <div class="col-4">
                                                            <button type="button" class="btn waves-effect waves-light btn-success" data-bs-toggle="modal" data-bs-target="#pros-submitexam"  style="font-size: 15px; background-color:#4db8ff; border: #2CEB6C; margin-top: 30px;">Submit</button>
                                                        </div>
                                                        </div>
                                                        </div>
                                                        </div>

                                                        </div>';
                                                        }


                                                        }

                                                        }











        }


      
        
        

    ?>

