<?php

      require_once('../../assets/config/config.php');
      $obj=new Database();
      $time=date('d-M-Y H:i:s');
      $date=date("d-M-Y",strtotime($_POST['date']));
      $c=0;$d=0;
      if (isset($_POST['option_id'])) 
      {
         foreach ($_POST['option_id'] as $option_id) 
         {
            $id="batch_no='".$option_id."'";
            $obj->select('students_course','*',null,$id,null,null);
            $result=$obj->getResult();                                    
            foreach($result as list("student_id"=>$student_id,"student_name"=>$student_name,"batch_no"=>$batch))
            {
               if(!attendence_checker($option_id,$date))
               {
                  $checker=$obj->insert('students_attendence',['attendence_time'=>$time,'student_id'=>$student_id,'student_name'=>$student_name,'batch_no'=>$option_id,'attendence_date'=>$date,'status'=>'Present']);           
                  $c=$c+1;
               }
               else
               {
                  continue;  
               }     
            }    
         }
         foreach ($_POST['option_id1'] as $option_id1) 
         {
               
               if(attendence_checker($option_id1,$date))
               {
                  $id="batch_no='".$option_id1."' and attendence_date='".$date."'";
                  $c=$obj->delete("students_attendence",$id); 
                  $d=$d+1;
               }
            
         }
         if(($c>=1) || ($d>=1)){
            echo "1";
         }
         else{
            echo "0";
         }
      } 
 
   
   function attendence_checker($batch_no,$date)
   {
      $obj=new Database();
      $id="batch_no='".$batch_no."' and attendence_date='".$date."'";
      $obj->select('students_attendence','*',null,$id,null,null);
      $result1=$obj->getResult(); 
      if($result1)
      {
         return true;
      } 
   }
?>
