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

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


$campusid = $_POST['campusid'];
$userID = $_POST['userID'];
$assignmentid = $_POST['assignmentid'];


$subjectid = $_POST['subjectid'];
if (isset($_POST['Answer'])) {
    // If set, assign the value to $Answer
    $Answer = $_POST['Answer'];
}


$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` = '$campusid' 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'];
 }

 

 $textareaquestiontheory = $_POST[ 'textareaquestiontheory'];
 $assignmentquestiontheoryid = $_POST[ 'assignmentquestiontheoryid'];
 $textareaquestion = $_POST[ 'textareaquestion'];

 $implodetextareaquestiontheory = implode('###||', $textareaquestiontheory);
 $implodedassignmentquestiontheoryid = implode(',', $assignmentquestiontheoryid);
 $implodedtextareaquestion = implode(',', $textareaquestion);
 $implodedAnswer = implode(',', $Answer);
 $success = 0;
 
       if($implodetextareaquestiontheory == '')
       {

       }else
       {
        $theoryquestion = mysqli_real_escape_string($link, $implodetextareaquestiontheory);
        if(!empty($theoryquestion))
        {
      
            $insert_into_answertable = "INSERT INTO `assignmentanswer`
            ( `AssignmentSettingsID`, `StudentID`, `CampusID`,  `ClassOrDepartmentID`, 
            `SubjectOrCourseID`, `sessionID`, `TermOrSemesterID`, `ObjectiveQuestion`, `ObjectAnswer`, `TheoryQuestion`, `TheoryAnswer`, `submitstatus`)
             VALUES ('$assignmentid','$userID','$campusid','$classid','$subjectid','$ekene_session_name','$ekene_term','$implodedtextareaquestion','$implodedAnswer','$implodedassignmentquestiontheoryid','$implodetextareaquestiontheory','1')";
              $select_question = mysqli_query($link, $insert_into_answertable);
              $success++;
        }
        
        //   if(!empty($theoryquestion))
        //   {
        //   }
        
    }

    if ($success == 0)
    {
        echo "3";

    }
    else
    {
        echo "10";
    }
 
 ?>