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

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


$ekene_import_campus = $_POST['ekene_import_campus'];
$userID = $_POST['userID'];
$diplay_class = $_POST['diplay_class'];
$usertype = $_POST['usertype'];
$ekene_import_subject = $_POST['ekene_import_subject'];
// Assuming $link is your database connection

$ekene_import_title = isset($_POST['ekene_import_title']) ? mysqli_real_escape_string($link, $_POST['ekene_import_title']) : '';

// Now you can use $ekene_import_title in your database query safely

$ctbornot = $_POST['ctbornot'];
$hidden_date = $_POST['hidden_date'];
$start_time = $_POST['start_time'];
$end_time = $_POST['end_time'];
$submission = $_POST['submission'];
$startdate = $_POST['startdate'];
$ekene_overall_score = $_POST['ekene_overall_score'];

$assignmenttype = explode(',', $_POST[ 'assignmenttype' ]);


// Assuming $link is your database connection

$ekeneinstrution = isset($_POST['ekeneinstrution']) ? mysqli_real_escape_string($link, $_POST['ekeneinstrution']) : '';

// Now you can use $ekeneinstrution in your database query safely

$textareaquestiontheory = trim($_POST['textareaquestiontheory']);
$textareaquestion =trim($_POST['textareaquestion']);
 $optionA =trim($_POST['optionA']);
$optionB = trim($_POST['optionB']);
$optionC = trim($_POST['optionC']);
$optionD = trim($_POST['optionD']);

$Answer = $_POST['Answer'];


date_default_timezone_set("Africa/Lagos");
$dateAndTime = date("Y/m/d H:i:s");
$desripction = "set assignment";

// Explode each variable and count the maximum length
$questionnewexplode = explode('###||', $textareaquestion);
$textareaquestiontheoryexplode = explode('###||', $textareaquestiontheory);
$optionAexplode = explode('###||', $optionA);
$optionBexplode = explode('###||', $optionB );
$optionCexplode = explode('###||', $optionC);
$optionDexplode = explode('###||', $optionD);
$Answerexplode = explode(',', $Answer);

$maxArrayLength = max(
  count($questionnewexplode),
  count($textareaquestiontheoryexplode),
  count($optionAexplode),
  count($optionBexplode),
  count($optionCexplode),
  count($optionDexplode),
  count($Answerexplode)
);

// Add empty values to each array to make them equal in length
$lengthDifference = $maxArrayLength - count($questionnewexplode);
for ($i = 0; $i < $lengthDifference; $i++) {
  $questionnewexplode[] = '';
}

$lengthDifference = $maxArrayLength - count($textareaquestiontheoryexplode);
for ($i = 0; $i < $lengthDifference; $i++) {
  $textareaquestiontheoryexplode[] = '';
}

$lengthDifference = $maxArrayLength - count($optionAexplode);
for ($i = 0; $i < $lengthDifference; $i++) {
  $optionAexplode[] = '';
}

$lengthDifference = $maxArrayLength - count($optionBexplode);
for ($i = 0; $i < $lengthDifference; $i++) {
  $optionBexplode[] = '';
}

$lengthDifference = $maxArrayLength - count($optionCexplode);
for ($i = 0; $i < $lengthDifference; $i++) {
  $optionCexplode[] = '';
}

$lengthDifference = $maxArrayLength - count($optionDexplode);
for ($i = 0; $i < $lengthDifference; $i++) {
  $optionDexplode[] = '';
}

$lengthDifference = $maxArrayLength - count($Answerexplode);
for ($i = 0; $i < $lengthDifference; $i++) {
  $Answerexplode[] = '';
}




  $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_fromassignmentsetting = "SELECT *
  FROM `assignmentsettings`
  WHERE `CampusID`= '$ekene_import_campus' AND `UserType`= '$usertype' AND `AssignmentTitle` = '$ekene_import_title'";


$ekene_setassign = mysqli_query($link, $ekene_select_fromassignmentsetting);
$ekene_get_import_details = mysqli_fetch_assoc($ekene_setassign);
$ekene_row_setassignmentt = mysqli_num_rows($ekene_setassign);
$success = 0;


if ($ekene_row_setassignmentt > 0)
 {
    echo '1';
} else {
   
    $ekene_insert_assignmentsetting = "INSERT INTO `assignmentsettings` (
          `CourseOrSubjectTeacherUserID`,
          `UserType`,
          `CampusID`,
          `sessionID`,
          `TermOrSemesterID`,
          `SubjectOrCourseID`,
          `ClassOrDepartmentID`,
          `AssignmentTitle`,
          `AssignmentDate`,
          `AssignmentStartTime`,
          `AssignmentEndTime`,
          `Startdate`,
          `submissiondate`,
          `Instruction`,
          `Overallscore`,
          `AssignmentType`,
          `DateCreated`
      ) VALUES (
          '$userID',
          '$usertype',
          '$ekene_import_campus',
          '$ekene_session_name',
          '$ekene_term',
          '$ekene_import_subject',
          '$diplay_class',
          '$ekene_import_title',
          '$hidden_date',
          '$start_time',
          '$end_time',
          '$startdate',
          '$submission',
          '$ekeneinstrution', 
          '$ekene_overall_score', 
          '$ctbornot',   
          '$dateAndTime'
  )";

      $ekene_mysqli_query_assignmentsetting = mysqli_query( $link, $ekene_insert_assignmentsetting);

    if ( $ekene_mysqli_query_assignmentsetting == true) {
             
       $lastInsertedID = mysqli_insert_id($link);




       if($textareaquestiontheory == '')
       {

       }else
       {

        for ($x = 0; $x < count($textareaquestiontheoryexplode); $x++ ) {
      

          $theoryquestion = mysqli_real_escape_string($link, $textareaquestiontheoryexplode[$x]);

          if(!empty($theoryquestion))
          {

            $insert_import_question = "INSERT INTO `assignmentquestion`( `AssignmentSettingsID`, `CampusID`, `CourseOrSubjectTeacherUserID`, `UserType`, `sessionID`, `TermOrSemesterID`,
            `ClassOrDepartmentID`, `SubjectOrCourseID`,`question`, `AssignmentCategory`,`date/time`) VALUES ('$lastInsertedID','$ekene_import_campus','$userID',
            '$usertype','$ekene_session_name',
            '$ekene_term','$diplay_class','$ekene_import_subject','$theoryquestion','Theory','$dateAndTime')";
        
            $select_import_question = mysqli_query($link, $insert_import_question);
            $success++;
        
          }
        
            }

       }
      

        if($textareaquestion == '')
        {

        }else{


        for ( $i = 0; $i < count($questionnewexplode); $i++ ) {
    

      
            $objectivequestion = mysqli_real_escape_string($link, $questionnewexplode[$i]);
          
            //  $validatequestion = trim(strip_tags(html_entity_decode($objectivequestion)));
        // Assuming $link is your database connection

                $optionA = mysqli_real_escape_string($link, $optionAexplode[$i]);
                $optionB = mysqli_real_escape_string($link, $optionBexplode[$i]);
                $optionC = mysqli_real_escape_string($link, $optionCexplode[$i]);
                $optionD = mysqli_real_escape_string($link, $optionDexplode[$i]);
                $Answer = mysqli_real_escape_string($link, $Answerexplode[$i]);

            if(!empty($objectivequestion))
            {
  
              $insert_import_question = "INSERT INTO  `assignmentquestion`( `AssignmentSettingsID`, `CampusID`, `CourseOrSubjectTeacherUserID`, `UserType`, `sessionID`, `TermOrSemesterID`,
              `ClassOrDepartmentID`, `SubjectOrCourseID`,`question`, `optionA`, `optionB`, `optionC`, `optionD`, `AssignmentCategory`, `answer`, `date/time`) VALUES ('$lastInsertedID','$ekene_import_campus','$userID',
              '$usertype','$ekene_session_name',
            '$ekene_term','$diplay_class','$ekene_import_subject','$objectivequestion','$optionA','$optionB','$optionC','$optionD','objective','$Answer','$dateAndTime'
              )";
          
              $select_import_question = mysqli_query($link, $insert_import_question);
              $success++;
            }
    
                }



        }

      


          $insert_into_activitylog = "INSERT INTO `activitylog`(`InstitutionIDOrCampusID`, `UserID`, `UserType`, `IpAddress`, `Location`, `Longitude`, `Latitude`, `Description`, `Date/Time`)
          VALUES ('$ekene_import_campus','$userID','$usertype','0','0','0','0','$desripction','$dateAndTime')";
          
          $select_import_question = mysqli_query($link, $insert_into_activitylog);

    } else {
        echo '7';
    }

    if($success == 0)
    {
        echo "9";
    }
    else
    {
        echo "10";
    }

}


?>