<?php

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

   
    $CampusID = trim($_POST['CampusID']);
    $studentID = trim($_POST['studentID']);
    $session = trim($_POST['session']);
    $term = trim($_POST['term']);
    $classID = trim($_POST['classID']);
    $SubjectID = trim($_POST['SubjectID']);

 



        $fetch = mysqli_query($link,"SELECT * FROM `cbtsetquestionssettings` 
                                    INNER JOIN `campus` 
                                    ON `cbtsetquestionssettings`.CampusID = `campus`.CampusID
                                   
                                    INNER JOIN `subjectorcourse`
                                    ON `cbtsetquestionssettings`.SubjectOrCourseID = `subjectorcourse`.SubjectOrCourseID
                                    INNER JOIN `classordepartment`
                                    ON `cbtsetquestionssettings`.ClassOrDepartmentID = `classordepartment`.ClassOrDepartmentID
                                    WHERE `cbtsetquestionssettings`.CampusID =  '$CampusID'
                                    AND `classordepartment`.ClassOrDepartmentID = '$classID'
                                    AND `cbtsetquestionssettings`.sessionID = '$session'
                                    AND    (`cbtsetquestionssettings`.TermOrSemesterID = '$term' OR $term IS NULL)   AND    (`cbtsetquestionssettings`.SubjectOrCourseID = '$SubjectID' OR  $SubjectID  IS NULL)
                                    ORDER BY `cbtsetquestionssettings`.cbtsetquestionssettingsID DESC");

               

      $num_row = mysqli_num_rows($fetch);
     $fetch_row = mysqli_fetch_assoc($fetch);


       

        if($num_row > 0){

            do{

               $cbtsetquestionssettingsID =  $fetch_row['cbtsetquestionssettingsID'];

                $ClassOrDepartmentName =  $fetch_row['ClassOrDepartmentName'];
                $ClassOrDepartmentID =  $fetch_row['ClassOrDepartmentID'];
        
                $SubjectOrCourseTitle =  $fetch_row['SubjectOrCourseTitle'];
                $SubjectOrCourseID =  $fetch_row['SubjectOrCourseID'];
            
                $CampusName =  $fetch_row['CampusName'];
                $CampusID =  $fetch_row['CampusID'];
        
                $TermOrSemesterID =  $fetch_row['TermOrSemesterID'];
        
                $AssesementType =  $fetch_row['AssesementType'];
                $AssesementCategory =  $fetch_row['AssesementCategory'];
                $AssesementTitle =  $fetch_row['AssesementTitle'];
                $AssesementDate =  $fetch_row['AssesementDate'];
                $AssesementStartTime =  $fetch_row['AssesementStartTime'];
                $AssesementEndTime =  $fetch_row['AssesementEndTime'];

                $question = $fetch_row['question'];
                $ex = explode(",", $question);
                $questionNumbers = count($ex);

                date_default_timezone_set('Africa/Lagos');

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

                // Calculate the time difference
                $duration = $timeIn->diff($timeOut);

                // Format the duration
                $formattedDuration = $duration->format('%h hours and %i minutes');


                $getCBTid_quesverify = "SELECT * FROM `cbtquestionsanswers` 
                WHERE `cbtsetquestionssettingsID` = '$cbtsetquestionssettingsID' 
                AND `StudentID` = '$studentID'  
                AND `objective_status` ='1' 
                ";

                    $getCBTidQuery_quesver_re = mysqli_query($link, $getCBTid_quesverify);
                     $getCBTidQuery_quesver_re_cnt = mysqli_num_rows($getCBTidQuery_quesver_re);
                    $prosgetfetch_studenscore = mysqli_fetch_assoc($getCBTidQuery_quesver_re);

                   $correctanswer =  $prosgetfetch_studenscore ['Answer'];
                   $studentAnswer =  $prosgetfetch_studenscore ['studentAnswer'];


                 

                   $correctscore_array = explode(',',$correctanswer);
                   $studentsnaser_arr = explode(',', $studentAnswer);



                   $proscorrectansertotalobject = 0;


                   if( $getCBTidQuery_quesver_re_cnt  > 0)
                   {



                    
                                foreach($correctscore_array as $proskey => $correctscore_array)
                                {
                                    

                                            $corecctquestionanarraynew = trim($correctscore_array);


                                            $studentcorectansfinal = trim($studentsnaser_arr[$proskey]);

                                            if( $studentcorectansfinal ==  $corecctquestionanarraynew)
                                            {
                                                $proscorrectansertotalobject+=1;      
                                            }else
                                            {

                                            }

                                }

                             $councountotalobj = count(explode(',',$correctanswer));
                   }else
                   {

                          $councountotalobj =  $questionNumbers;
                   }


                   $percentage = ($proscorrectansertotalobject / $councountotalobj ) * 100;

				$percentageWithoutDecimals = floor($percentage);

                


                echo '<div class="card chiCourseList">
                        <div class="card-body">
                            <div class="row g-2">
                                
                                <div class="col-sm-4 col-md-4 col-lg-4">
                                    <div class="text-center" style="margin-top: 20px;">
                                        <input data-plugin="knob" data-width="75" data-height="75" data-linecap=round data-fgColor="#1abc9c" value="'.$percentageWithoutDecimals.'" data-skin="tron" data-angleOffset="360" data-readOnly=true data-thickness=".2" />
                                    </div>
                                    <h5 style="color: #1abc9c; margin-top: 20px; text-align: center; font-weight: 600;">'.$percentageWithoutDecimals.'%</h5>
                                </div>
                                
                                <div class="col-sm-8 col-md-8 col-lg-8">
                                    <div style="margin-top: 20px;">
                                        <h6 style="font-size: 14px; font-weight: 600;">'.$SubjectOrCourseTitle.' ('.$AssesementTitle.')</h6>
                                        <div class="row">
                                            <div class="col-6">
                                                <span style="font-size: 12px;"><i class="bx bx-calendar"></i>&nbsp;Date: '.$AssesementDate.'</span>
                                            </div>';




                                            if(  $getCBTidQuery_quesver_re_cnt > 0)
                                            {



                                                   echo '<div class="col-6">
                                                        <span style="font-size: 12px;"><i class="bx bx-building"></i></i>&nbsp;OBJ Score: '. $proscorrectansertotalobject.'/'.$councountotalobj.'</span>
                                                    </div>';


                                            }else
                                            {

                                                echo '<div class="col-6">
                                                        <span style="font-size: 12px;"><i class="bx bx-building"></i></i>&nbsp;OBJ Score: 0/'.$councountotalobj.'</span>
                                                    </div>';

                                            }

                                         
                                            
                                        echo '</div>

                                        <p></p>

                                        <div class="row g-2">
                                            <div class="col-6">
                                                <span style="font-size: 12px;"><i class="bx bx-timer"></i>&nbsp; Start Time: '.$AssesementStartTime.'</span>
                                            </div>
                                            <div class="col-6"> 
                                                <span style="font-size: 12px;"><i class="bx bx-timer" aria-hidden="true"></i>&nbsp;Duration: '.$formattedDuration.'</span>
                                            </div>
                                        </div>

                                        <p></p>

                                        <div class="row g-2">
                                            <div class="col-6">
                                                <span style="font-size: 12px;"><i class="bx bx-pen"></i>&nbsp; Assesement Category: '.$AssesementCategory.'</span>
                                            </div>
                                            <div class="col-6"> 
                                                <span style="font-size: 12px;"><i class="fas fa-question-circle" aria-hidden="true"></i>&nbsp; No. of Questions: '.$questionNumbers.'</span>
                                            </div>
                                        </div>
                                        

                                        <div class="row" style="margin-top: 20px;">

                                            <div class="col-6">

                                            </div>

                                            <div class="col-6">';

                                            $date = ("SELECT * FROM `cbtsetquestionssettings` 
                                                    WHERE `cbtsetquestionssettingsID` = '$cbtsetquestionssettingsID' 
                                                    ORDER BY `cbtsetquestionssettingsID` ASC");

                                            $dateQuery = mysqli_query($link, $date);
                                            $fetchDateQuery = mysqli_fetch_array($dateQuery);

                                            $CbtID = $fetchDateQuery['cbtsetquestionssettingsID'];

                                            date_default_timezone_set("Africa/Lagos");

                                            $AssesementDate;

                                            $currentDate = date('Y-m-d');

                                            $current_time = date("h:i A");


                                           

                                            //  objective_status theory_status

                                            if($currentDate  ==  $AssesementDate){

                                                if($getCBTidQuery_quesver_re_cnt > 0){



                                                    echo '<button type="button" disabled class="btn btn-success btn-sm" style="font-size: 13px; background-color: #1a78bc; border: #1a78bc solid 1px;  border-radius: 10px;">
                                                            Assesement Submitted
                                                        </button>';

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

                                                    if(strtotime($current_time) >= strtotime($AssesementStartTime)){


                                                        echo '<a href="cbt_onboarding_page.php?cbt_id='.$cbtsetquestionssettingsID.' &&userid='.$studentID.' &&institution='.$InstituteID.'&&examtype=normal" type="button" class="btn btn-success btn-sm" style="font-size: 13px; background-color: #1abc9c; border: #1abc9c solid 1px; width: 80px; border-radius: 10px;">
                                                                    Start 
                                                                </a>';


                                                    }else{



                                                        echo '<button type="button" disabled class="btn btn-success btn-sm" style="font-size: 13px; background-color: #bc8b1a; border: #bc8b1a solid 1px;  border-radius: 10px;">
                                                            Waiting for Assesement Time
                                                        </button>';



                                                    }
                                                        
                                                }
                                            }elseif($currentDate > $AssesementDate){

                                                echo '<button type="button" disabled class="btn btn-success btn-sm" style="font-size: 13px; background-color: #bc1a1a; border: #bc1a1a solid 1px; border-radius: 10px;">
                                                        Quiz Date Expired 
                                                        </button>';


                                            }elseif($currentDate < $AssesementDate){

                                                echo '<button type="button" disabled class="btn btn-success btn-sm" style="font-size: 13px; background-color: #1abc9c; border: #1abc9c solid 1px; border-radius: 10px;">
                                                        Wait for Assesement Date
                                                    </button>';

                                            }
                
                                            echo '</div>
                                            
                                        </div>
                                    </div>
                                </div>

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

            }while($fetch_row = mysqli_fetch_assoc($fetch));


        }else{



                $prosgetimageinfo = mysqli_query($link, "SELECT * FROM `defaultimages` WHERE ImageName='abba-no-record-found-image2'");
                $prosgetimageinfo_cnt = mysqli_num_rows($prosgetimageinfo);
                $prosgetimageinfo_cntrow = mysqli_fetch_assoc($prosgetimageinfo);

             

                        $BaseSixtyFour =  $prosgetimageinfo_cntrow['BaseSixtyFour'];


                echo '<div class="col-12 text-center mt-3" id="">

                        <img src="'.$BaseSixtyFour.'" class="text-center" style="width:7%;opacity:0.7;" />
                        <p class="pt-2 text-secondary text-center">
                            No Assesement Settings Available.</p>
                        
                    </div>'; 


        }

   
    

   





?>