<?php
 error_reporting(E_ALL);
 ini_set('display_errors', 1);

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

 
 $ekene_campus = $_POST["ekene_campus"];
 $userID = $_POST["userID"];
//  $diplay_class = $_POST["diplay_class"];
 $ekene_subject = $_POST["ekene_subject"];

 $sessionone = $_POST["session"];
 $termone = $_POST["term"];

 date_default_timezone_set("Africa/Lagos");
  $currentDate = date('Y-m-d');
 $session = "SELECT * FROM `session` WHERE `sessionStatus` = 1";

 $ekeneselect = mysqli_query($link, $session);
 $ekene_get_select_details = mysqli_fetch_assoc($ekeneselect);
 $ekene_row_select = mysqli_num_rows($ekeneselect);

 $ekene_session_name = $ekene_get_select_details['sessionName'];


 $term = "SELECT * FROM `termorsemester` WHERE `status` = 1";
  $ekeneterm = mysqli_query($link, $term);
  $ekene_get_term_details = mysqli_fetch_assoc($ekeneterm);
  $ekene_row_term = mysqli_num_rows($ekeneterm);
       
  $ekene_term = $ekene_get_term_details['TermOrSemesterID'];

 
 $ekene_select_currentclass = "SELECT * FROM `classordepartmentstudentallocation` WHERE `CampusID` = '$ekene_campus' AND `Session` = '$ekene_session_name' AND `StudentID` = '$userID'";
 $ekene_query_link_classcurrent = mysqli_query($link, $ekene_select_currentclass);
 $ekene_get_details_currentclass = mysqli_fetch_assoc($ekene_query_link_classcurrent);
  $ekene_row_cnt_ekenecurrentclass = mysqli_num_rows($ekene_query_link_classcurrent);

  if ($ekene_row_cnt_ekenecurrentclass > 0)
  {
      $classid = $ekene_get_details_currentclass['ClassOrDepartmentID'];
  }

  
//  $ekene_select_fromassignmentsettingstwo =" SELECT * FROM `assignmentsettings` WHERE `CampusID` = '$ekene_campus' 
//  AND `ClassOrDepartmentID` = '$classid' AND `SubjectOrCourseID` = '$ekene_subject' AND  `deletestatus` = 0";

//    $ekene_mysqli_querytwo = mysqli_query($link, $ekene_select_fromassignmentsettingstwo);
//    $ekene_get_detailstwo = mysqli_fetch_assoc($ekene_mysqli_querytwo);
//   $ekene_row_cnt_ekenetwo = mysqli_num_rows($ekene_mysqli_querytwo);
//   if ($ekene_row_cnt_ekenetwo > 0)
//   {
//      echo $AssignmentSettingsID = $ekene_get_detailstwo['AssignmentSettingsID'];
//   }





 $ekene_select_fromassignmentsettings =" SELECT * FROM `assignmentsettings` WHERE `CampusID` = '$ekene_campus' 
 AND `ClassOrDepartmentID` = '$classid' AND `sessionID` = '$sessionone' AND `TermOrSemesterID` = '$termone' AND `SubjectOrCourseID` = '$ekene_subject' AND  `deletestatus` = 0";

   $ekene_mysqli_query = mysqli_query($link, $ekene_select_fromassignmentsettings);
   $ekene_get_details = mysqli_fetch_assoc($ekene_mysqli_query);
  $ekene_row_cnt_ekene = mysqli_num_rows($ekene_mysqli_query);

 if($ekene_row_cnt_ekene > 0)
 {
      echo '<div class="row" style="display: flex;">';
     
     do{
     
        $AssignmentTitle = $ekene_get_details['AssignmentTitle'];
        $Startdate = $ekene_get_details['Startdate'];
        $AssignmentStartTime = $ekene_get_details['AssignmentStartTime'];
        $AssignmentEndTime = $ekene_get_details['AssignmentEndTime'];
        $submissiondate = $ekene_get_details['submissiondate'];
        $CampusID = $ekene_get_details['CampusID'];
        $AssignmentSettingsID = $ekene_get_details['AssignmentSettingsID'];

        $CampusID = $ekene_get_details['CampusID'];
        $AssignmentType = $ekene_get_details['AssignmentType'];
        $AssignmentSettingsID = $ekene_get_details['AssignmentSettingsID'];
        
            $ekene_select_fromanswer = "SELECT * FROM `assignmentanswer` WHERE `AssignmentSettingsID` = '$AssignmentSettingsID' AND `sessionID` = '$ekene_session_name' AND `TermOrSemesterID` = '$ekene_term' AND `StudentID` = '$userID' AND `CampusID` = '$ekene_campus' AND `ClassOrDepartmentID` = '$classid'";
            $ekene_mysqli_queryanswer = mysqli_query($link, $ekene_select_fromanswer);
            $ekene_get_detailsanswer = mysqli_fetch_assoc($ekene_mysqli_queryanswer);
            $ekene_row_cnt_ekeneanswer = mysqli_num_rows($ekene_mysqli_queryanswer);

            if ($ekene_row_cnt_ekeneanswer > 0)
            {
                $TheoryScore = $ekene_get_detailsanswer['TheoryScore'];
                $publishstatus = $ekene_get_detailsanswer['publishstatus'];
                $ObjectiveQuestion = $ekene_get_detailsanswer['ObjectiveQuestion'];
                $submitstatus = $ekene_get_detailsanswer['submitstatus'];
                $implodedTheoryQuestion = explode(',', $TheoryScore);
                $ObjectiveQuestion = explode(',', $ObjectiveQuestion);

                $sum = array_sum($implodedTheoryQuestion);

                $totalstudentscore_obj = 0;
                $overallobject = count($ObjectiveQuestion);

                $select_fromassignmentsetting = "SELECT * FROM `assignmentsettings` WHERE AssignmentSettingsID = '$AssignmentSettingsID'"; 
                $ekene_mysqli = mysqli_query($link, $select_fromassignmentsetting);
                $ekene_get_details = mysqli_fetch_assoc($ekene_mysqli);
                $ekene_row = mysqli_num_rows($ekene_mysqli);

                if($ekene_row > 0)
                {
                    $Overallscore = $ekene_get_details['Overallscore'];
                }

                $totalquestionscore = $overallobject + $Overallscore;

                $totalscore = $sum + $totalstudentscore_obj;

            }else
            {
               
               $publishstatus = 0;
               $submitstatus = 0;
               $sum = "NA";
            }

            $currentDate = (new DateTime())->format('Y-m-d');

            $submissionDateTime = new DateTime($submissiondate);
          
            $currentDateTime = new DateTime($currentDate);

            
          


         if ($AssignmentType == "yes")
         {
            echo '
            <div class="col-sm-3 col-md-6 col-lg-3 carditems card_search_get">
           
                <div class="topSecCards" style="width: 100%;">
             
                     <div align="center" style="margin-top: 18px; padding-top: 20px;">
                       
                        <h6 style="font-weight: bold; cursor: pointer; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; margin-top: 5px; font-size: 14px;" class="ekenedisplaysubmissiondate_title'.$AssignmentSettingsID.'"> '. $AssignmentTitle.'</h6>
                        <p style="font-weight: 500; color: #b8b8b8;">Start Time: <span>'.$AssignmentStartTime.'</span></p>
                        <p style="font-weight: 500; color: #b8b8b8;">End  Time: <span>'.$AssignmentEndTime.'</span></p>
                        
                    </div>
            
                    <div style="padding: 7px;">
                        <div style="width: 100%; border-radius: 5px; background-color: #e8ebf1;">
                            <div style="padding: 5px;" align="center">
                            
                           
                                    <button type="button" class="btn btn-sm text-white  m-1  rounded-3 btn-primary mb-2"  data-cam="'.$CampusID.'" data-id="'.$AssignmentSettingsID.'"   id="receiptIcon"    style="font-size: 11px;">
                                        <i class="bx bx-receipt" style="color: white; cursor: pointer;" aria-hidden="true"></i>
                                    </button>
                          
                              
                             
                                &nbsp;
                                <!-- Add Download Icon -->
                            
                                    <button type="button" class="btn btn-sm text-white  m-1 rounded-3 btn-success mb-2" data-cam="'.$CampusID.'" data-id="'.$AssignmentSettingsID.'"  data-bs-toggle="modal" data-bs-target="#ekene_view_dowloadquestion" id="ekene_download_fullassignment" style="font-size: 11px;">
                                        <i class="fas fa-download"></i>
                                    </button>
                                  
                             
                            
                             
                            </div>
                        </div>
                    </div>
                </div>
            
            </div>
            ';
         }
         else
         {

           
            echo '
            <div class="col-sm-3 col-md-6 col-lg-3 carditems card_search_get">
           
                <div class="topSecCards" style="width: 100%;">
             
                     <div align="center" style="margin-top: 18px; padding-top: 20px;">
                       
                        <h6 style="font-weight: bold; cursor: pointer; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; margin-top: 5px; font-size: 14px;" class="ekenedisplaysubmissiondate_title'.$AssignmentSettingsID.'"> '. $AssignmentTitle.'</h6>
                        <p style="font-weight: 500; color: #b8b8b8;">Start date: <span>'.$Startdate.'</span></p>';
                        if ($currentDateTime > $submissionDateTime) {
                            echo '<p style="font-weight: 500; color: red;">Submission date: <span>'.$submissiondate.'</span></p>';
                        } else {
                            echo '<p style="font-weight: 500; color: green;">Submission date: <span>'.$submissiondate.'</span></p>';
                        }
                      
                       
                        if ($publishstatus == 1)
                        {
                          echo '<p style="font-weight: 500; color: #b8b8b8;">Total Score: <span>'.$totalscore.'/ '.$totalquestionscore.'</span></p>';
                        }
                        else
                        {
                           echo '<p style="font-weight: 500; color: #b8b8b8;"> Total Score:<span>NA</span></p>';
                        }
                    
                        
                     echo  ' </div>
            
                    <div style="padding: 7px;">
                        <div style="width: 100%; border-radius: 5px; background-color: #e8ebf1;">
                            <div style="padding: 5px;" align="center">';
                            
                         
                             
                               echo '&nbsp;
                            
                                    <button type="button" class="btn btn-sm text-white  m-1 rounded-3 btn-success mb-2" data-cam="'.$CampusID.'" data-id="'.$AssignmentSettingsID.'"  data-bs-toggle="modal" data-bs-target="#ekene_view_dowloadquestion" id="ekene_download_fullassignment" style="font-size: 11px;">
                                        <i class="fas fa-download"></i>
                                    </button>
                              
                            </div>
                        </div>
                    </div>
                </div>
            
            </div>
            ';
         }
      

        
         
     }while($ekene_get_details= mysqli_fetch_assoc($ekene_mysqli_query));
     echo '</div>';
 }
 else{
   

    $pros_select_record_not_found = mysqli_query($link, "SELECT * FROM defaultimages WHERE ImageName='abba-no-record-found-image2'");
    $pros_select_record_not_found_count = mysqli_num_rows($pros_select_record_not_found);
    $pros_select_record_not_found_row = mysqli_fetch_assoc($pros_select_record_not_found);

    if ($pros_select_record_not_found_count > 0) {
        $pros_general_no_record = $pros_select_record_not_found_row['BaseSixtyFour'];
        echo '
    
        <center><img class="mb-1" src="'.$pros_general_no_record.'" width="100" alt="">
        <p style="color: black;">No Assignment set.</p> </center>';
    }



 }





?>