<?php

        include('../../../config/config.php');
                                        
        $cbtSettingsID = $_POST['cbtSettingsID'];
        $StudentID = $_POST['StudentID'];
        $getanswerarr = $_POST['getanswerarr'];
        $prosquestiontypeheregotten = $_POST['prosquestiontypeheregotten'];
        $examtype = $_POST['examtype'];

        $Status = 1;


    if($examtype == 'assignment')
    {



                    $progetsetquestionquestioncontent =  mysqli_query($link,"SELECT * FROM `assignmentsettings` WHERE   AssignmentSettingsID='$cbtSettingsID' AND `deletestatus`='0'");
                    $progetsetquestionquestioncontentcnt = mysqli_num_rows($progetsetquestionquestioncontent);
                    $progetsetquestionquestioncontentcntrow = mysqli_fetch_assoc($progetsetquestionquestioncontent);



                      $SubjectOrCourseID =  $progetsetquestionquestioncontentcntrow['SubjectOrCourseID'];
                      $ClassOrDepartmentID =  $progetsetquestionquestioncontentcntrow['ClassOrDepartmentID'];
                      $CampusID =  $progetsetquestionquestioncontentcntrow['CampusID'];
                      $sessionID=  $progetsetquestionquestioncontentcntrow['sessionID'];
                      $SectionID=  $progetsetquestionquestioncontentcntrow['SectionID'];
                      $AssignmentType  =  $progetsetquestionquestioncontentcntrow['AssignmentType'];
                      $CourseOrSubjectTeacherUserID =  $progetsetquestionquestioncontentcntrow['CourseOrSubjectTeacherUserID'];
                      $TermOrSemesterID =  $progetsetquestionquestioncontentcntrow['TermOrSemesterID'];






                  

                      if( $prosquestiontypeheregotten == 'Objective')
                      {


                                    //objective only



                                    $prosgetquestionforcbt = mysqli_query($link, "SELECT * FROM `assignmentquestion` WHERE `CampusID`='$CampusID' AND  `AssignmentSettingsID`='$cbtSettingsID' AND `AssignmentCategory`='Objective'");

                                    $prosgetquestionforcbt_cnt = mysqli_num_rows($prosgetquestionforcbt);
                                    $prosgetquestionforcbt_cntrow = mysqli_fetch_assoc($prosgetquestionforcbt);
              
                                    $student_correct = implode(',',$getanswerarr);
              
                                      $prosload_reserveredquestion = array();
              
              
                                    if($prosgetquestionforcbt_cnt > 0)
                                    {
              
                                              do{
              
              
                                                         $AssignmentQuestionID =  $prosgetquestionforcbt_cntrow['AssignmentQuestionID'];
              
              
                                                           $prosload_reserveredquestion[] = $AssignmentQuestionID;
              
              
              
              
                                              }while($prosgetquestionforcbt_cntrow = mysqli_fetch_assoc($prosgetquestionforcbt));
              
                                    }else
                                    {
              
                                    }
              
              
                                         $studentobjectionquestion    = implode(',',  $prosload_reserveredquestion); //convert array to string
              
                                 
                                          $prosverify_submited = mysqli_query($link, "SELECT * FROM `assignmentanswer` WHERE StudentID='$StudentID' AND ClassOrDepartmentID='$ClassOrDepartmentID' AND `AssignmentSettingsID`='$cbtSettingsID'");
              
                                          $prosverify_submitedcnt = mysqli_num_rows($prosverify_submited);
              
                                          if($prosverify_submitedcnt  > 0)
                                          {
              
                                              
                                                            $update_answer = mysqli_query($link, "UPDATE `assignmentanswer` SET `ObjectiveQuestion`='$studentobjectionquestion', `ObjectAnswer`='$student_correct', `ObjectiveStatus`='1' WHERE `AssignmentSettingsID`='$cbtSettingsID' AND `StudentID`='$StudentID'"); 
              
                                                
                                          }else
                                          {
              
              
                                            
                                                             $update_answer = mysqli_query($link, "INSERT INTO `assignmentanswer` (
                                                                `AssignmentSettingsID`, 
                                                                `StudentID`, 
                                                                `CampusID`, 
                                                                `CourseOrSubjectTeacherUserID`, 
                                                                `ClassOrDepartmentID`, 
                                                                `SubjectOrCourseID`, 
                                                                `sessionID`, 
                                                                `TermOrSemesterID`, 
                                                                `ObjectiveQuestion`, 
                                                                `ObjectAnswer`,  
                                                                `TheoryScore`, 
                                                                `Remark`, 
                                                                `Correction`, 
                                                                `resetstatus`, 
                                                                `publishstatus`, 
                                                                `ResultStatus`, 
                                                                `submitstatus`, 
                                                                `ObjectiveStatus`, 
                                                                `TheoryStatus`
                                                            ) VALUES (
                                                                '$cbtSettingsID',
                                                                '$StudentID',
                                                                '$CampusID', 
                                                                '$CourseOrSubjectTeacherUserID', 
                                                                '$ClassOrDepartmentID', 
                                                                '$SubjectOrCourseID', 
                                                                '$sessionID', 
                                                                '$TermOrSemesterID', 
                                                                '$studentobjectionquestion', 
                                                                '$student_correct', 
                                                                '0', 
                                                                '', 
                                                                '', 
                                                                '0', 
                                                                '0', 
                                                                '0', 
                                                                '0', 
                                                                '1', 
                                                                '0'
                                                            );
                                                            "); 


                                                            
                                                        
              
              
                                          }



                                     
                                          

                                          if($update_answer == true)
                                          {
                                              echo 'Success';
                                  
                                          }else
                                          {
                                              echo 'Failed';
                                          }

                                    //objective only for assignment




                      }else
                      {





                          //objective only

                          $prosgetquestionforcbt = mysqli_query($link, "SELECT * FROM `assignmentquestion` WHERE `CampusID`='$CampusID' AND  `AssignmentSettingsID`='$cbtSettingsID' AND `AssignmentCategory`='Theory'");
                          $prosgetquestionforcbt_cnt = mysqli_num_rows($prosgetquestionforcbt);
                          $prosgetquestionforcbt_cntrow = mysqli_fetch_assoc($prosgetquestionforcbt);

                            $student_correct = implode(',',$getanswerarr);
    
                            $prosload_reserveredquestion = array();
    
    
                          if($prosgetquestionforcbt_cnt > 0)
                          {
    
                                    do{


                                               $AssignmentQuestionID =  $prosgetquestionforcbt_cntrow['AssignmentQuestionID'];
                                               $prosload_reserveredquestion[] = $AssignmentQuestionID;
    
    
    
    
                                    }while($prosgetquestionforcbt_cntrow = mysqli_fetch_assoc($prosgetquestionforcbt));
    
                          }else
                          {
    
                          }


    
    
                               $studentobjectionquestion    = implode(',',  $prosload_reserveredquestion); //convert array to string
    
                       
                                $prosverify_submited = mysqli_query($link, "SELECT * FROM `assignmentanswer` WHERE StudentID='$StudentID' AND ClassOrDepartmentID='$ClassOrDepartmentID' AND `AssignmentSettingsID`='$cbtSettingsID'");
    
                                $prosverify_submitedcnt = mysqli_num_rows($prosverify_submited);


    
                                if($prosverify_submitedcnt  > 0)
                                {
    
    
                                    
                                                  $update_answer = mysqli_query($link, "UPDATE `assignmentanswer` SET `TheoryQuestion`='$studentobjectionquestion', `TheoryAnswer`='$student_correct', `TheoryStatus`='1' WHERE `AssignmentSettingsID`='$cbtSettingsID' AND `StudentID`='$StudentID'"); 
    
                                      
                                }else
                                {
    
    
                                                   $update_answer = mysqli_query($link, "INSERT INTO `assignmentanswer`(`AssignmentSettingsID`, `StudentID`, `CampusID`, `CourseOrSubjectTeacherUserID`, `ClassOrDepartmentID`, `SubjectOrCourseID`, `sessionID`, `TermOrSemesterID`, `TheoryQuestion`, `TheoryAnswer`, `TheoryStatus`) VALUES ('$cbtSettingsID','$StudentID','$CampusID', '$CourseOrSubjectTeacherUserID', '$ClassOrDepartmentID', '$SubjectOrCourseID', '$sessionID', '$TermOrSemesterID', '$studentobjectionquestion', '$student_correct',  '1')"); 
                                                   
    
    
                                }

                                // echo mysqli_error($link);

                                
                                if($update_answer == true)
                                {
                                    echo 'Success';
                        
                                }else
                                {
                                    echo 'Failed';
                                }

    

                          //objective only for assignment








                      }


                     
                     
                        

    
    }else
    {


                    $progetsetquestionquestioncontent =  mysqli_query($link,"SELECT * FROM `cbtsetquestionssettings` WHERE cbtsetquestionssettingsID='$cbtSettingsID' AND `DeleteStatus`='0'");
                    $progetsetquestionquestioncontentcnt = mysqli_num_rows($progetsetquestionquestioncontent);
                    $progetsetquestionquestioncontentcntrow = mysqli_fetch_assoc($progetsetquestionquestioncontent);
                
                
                
                    if($progetsetquestionquestioncontentcnt > 0)
                    {
                
                
                
                        // $CampusID =  $progetsetquestionquestioncontentcntrow ['CampusID'];
                        // $session =  $progetsetquestionquestioncontentcntrow ['sessionID'];
                        // $TermOrSemesterID =  $progetsetquestionquestioncontentcntrow ['TermOrSemesterID'];
                        // $SubjectOrCourseID =  $progetsetquestionquestioncontentcntrow ['SubjectOrCourseID'];
                        // $ClassOrDepartmentID =  $progetsetquestionquestioncontentcntrow ['ClassOrDepartmentID'];
                
                        
                        // $answer =  $progetsetquestionquestioncontentcntrow ['answer'];
                
                
                    
                
                
                
                                                        
                                    if($prosquestiontypeheregotten == 'Objective'){
                
                
                                    
                
                                        $check_if_submitted = mysqli_query($link, "SELECT * FROM `cbtquestionsanswers`
                                                                                    WHERE `cbtsetquestionssettingsID` = '$cbtSettingsID'
                                                                                    AND `StudentID` = '$StudentID'");
                
                                        $fetch_student_ans = mysqli_fetch_assoc( $check_if_submitted);
                
                
                                    //   echo  mysqli_error($link);
                
                                        
                                    $student_ans = $fetch_student_ans['studentAnswer']; 
                
                
                                    $objective_status = $fetch_student_ans['objective_status']; 
                
                                    
                
                                        if($objective_status == '0'){
                
                                        $student_correct = implode(',',$getanswerarr);
                
                                            $insertstudent_anser = mysqli_query($link,"UPDATE `cbtquestionsanswers`
                                                                                    SET `studentAnswer` = '$student_correct',
                                                                                        `objective_status` = '$Status'
                                                                                    WHERE `cbtsetquestionssettingsID` = '$cbtSettingsID'
                                                                                    AND `StudentID` = '$StudentID'");
                
                
                            
                                    
                                            if($insertstudent_anser == true)
                                            {
                                                echo 'Success';
                                    
                                            }else
                                            {
                                                echo 'Failed';
                                            }
                
                                        }else{
                                            echo 'Student Already Exist';
                                        }
                
                                    }elseif($prosquestiontypeheregotten == 'Theory'){
                
                
                                        $check_if_submitted = mysqli_query($link, "SELECT * FROM `cbtquestionsanswers`
                                                                                    WHERE `cbtsetquestionssettingsID` = '$cbtSettingsID'
                                                                                    AND `StudentID` = '$StudentID'");
                
                                        $fetch_student_ans = mysqli_fetch_assoc( $check_if_submitted);
                
                                        $student_ans = $fetch_student_ans['studentTheoryAnswer']; 
                
                                        $theory_status = $fetch_student_ans['theory_status']; 
                
                                        if($theory_status == '0'){
                
                                            $student_correct = implode(',',$getanswerarr);
                
                                            $insertstudent_anser = mysqli_query($link,"UPDATE `cbtquestionsanswers`
                                                                                    SET `studentTheoryAnswer` = '$student_correct',
                                                                                        `theory_status` = '$Status'
                                                                                    WHERE `cbtsetquestionssettingsID` = '$cbtSettingsID'
                                                                                    AND `StudentID` = '$StudentID'");
                                    
                                            if($insertstudent_anser == true)
                                            {
                                                echo 'Success';
                                    
                                            }else
                                            {
                                                echo 'Failed';
                                            }
                
                                        }else{
                                            echo 'Student Already Exist';
                                        }
                
                                    
                                        
                                    }
                
                    }else
                    {
                                echo 'Exam does not exist';
                    }
    
    



    }

   





?>
                