<?php
include("../../db.php");
header("Access-Control-Allow-Origin: * ");
// mysql_set_charset('utf8');

	$idno=$_REQUEST['idno'];
	$session=$_REQUEST['session'];
	$school_id=$_REQUEST['school_id'];
	
// 	$idno=mysql_real_escape_string($idno);
// 	$session=mysql_real_escape_string($session);
// 	$school_id=mysql_real_escape_string($school_id);
	
	
  $class=$db->getVal("Select class FROM student where idno='".$idno."'");
//   echo $class;
 $section=$db->getVal("Select section FROM student where idno='".$idno."'");
//   echo $section;
$aryList=$db->getRows("SELECT date FROM assignment where school_id='".$school_id."' and session='".$session."' and (class='".$class."' OR class='') group by date order by date asc");
// echo "SELECT date FROM assignment where school_id='".$school_id."' and session='".$session."' and (class='".$class."' OR class='') group by date order by date asc";
// print_r($aryList).'<br>';
//////////////Fetching values from array ///////
	$i=0;
	$data=array();
	$homework=array();
	$same_date="";
	
foreach($aryList as $list1){
		
		
// 		print_r($list1).'<br>';
		
	if($list1["date"]!=$same_date){
		
		$data[$i]["order"]=$i;
		
		$data[$i]["date"]=date("d-m-Y",strtotime($list1["date"]));
		
		//$same_date=$list1["date"];
		
		$aryList1=$db->getRows("SELECT * FROM assignment where school_id='".$school_id."' and session='".$session."' and class='".$class."' and (section='".$section."' OR section='') and date='".$list1["date"]."'");
// 			print_r($aryList1);
		$j=0;
		$homework=[];
	foreach($aryList1 as $list){
// 		print_r($list);
		 $var=$list["detail"];		
		$homework[$j]["subject"]= htmlspecialchars($list["subject"]);
		
		$homework[$j]["assignment_type"]= htmlspecialchars($list["assignment_type"]);
		$homework[$j]["detail"]=htmlspecialchars($var);
		//$homework[$j]["detail"]=str_replace( array('\r\\n','\n\r','\n','\r'), '<br>' , $var ); 
		
		if($list["upload_assignment"]!='')	{
		$homework[$j]["file"]=URL_ROOT."uploads/assignment/".$list['upload_assignment'];
		}
		else{ 
			$homework[$j]["file"]=NULL;	
		}
		$j++;
		}
	
	$data[$i]["homework"]=$homework;
	$i++;
	}
}
// print_r($data);
// 	echo "adf";
	if(count($aryList)>0)
	{  
	   //  print_r($data);
	   // echo "shello";
		echo json_encode(array('status' => 1, 'msg'=>'Homework found Successfully..!', "data"=>$data));    
			
	}else
	{		
	 	echo json_encode(array('status' => 0, 'msg'=>'No Homework Found..!'));
	}
