<?php
 require "../commonincludefiles.php";
 
 $arr = array();
 $total = 0;
	
	if($_REQUEST['branch_id']!=""){
	
		 $college_id	= $_SESSION["VedantnrCollegeId"];
		 $start   	= isset($_REQUEST['start'])  ? $_REQUEST['start']  :  0;
		 $limit   	= isset($_REQUEST['limit'])  ? $_REQUEST['limit']  : 30;
		 $sort    	= isset($_REQUEST['sort'])   ? $_REQUEST['sort']   : 'student_gturegno';
		 $dir     	= isset($_REQUEST['dir'])    ? $_REQUEST['dir']    : 'ASC';
		 $filters 	= isset($_REQUEST['filter']) ? $_REQUEST['filter'] : null;
 
 
	if (is_array($filters)) {
		$encoded = false;
	} else {
		$encoded = true;
		$filters = json_decode($filters);
	}
	$where = '';
	$qs = '';
	if (is_array($filters)) {
		for ($i=0;$i<count($filters);$i++){
			$filter = $filters[$i];
			if ($encoded) {
				$field = $filter->field;
				$value = $filter->value;
				$compare = isset($filter->comparison) ? $filter->comparison : null;
				$filterType = $filter->type;
			} else {
				$field = $filter['field'];
				$value = $filter['data']['value'];
				$compare = isset($filter['data']['comparison']) ? $filter['data']['comparison'] : null;
				$filterType = $filter['data']['type'];
			}
			switch($filterType){
				case 'string' : 
					if($field == "studentname") {
						$qs .= " AND (student_surname LIKE '%".$value."%' or 
								 student_firstname LIKE '%".$value."%' or student_lastname LIKE '%".$value."%') ";
					}
					elseif($field == "semester_name") {
						$qs .= " AND semester_name LIKE '%".$value."%'  ";
					}
					elseif($field == "student_gturegno") {
						$qs .= " AND student_gturegno LIKE '%".$value."%'  ";
					}
					break;
				case 'list' :
					if (strstr($value,','))
					{
						   if(strcmp($field,"branch_name")==0)
						   {
								$fi = explode(',',$value);
								
								for ($q=0;$q < count($fi);$q++)
								{
									$fi[$q] = " '".$fi[$q]."' ";
								}
								$value = implode(',',$fi);
								$field = "sm.branch_id";
								$qs .= " AND ".$field." IN (".$value.")";
								
						
							}
						else{
								$fi = explode(',',$value);
								for ($q=0;$q < count($fi);$q++){
									$fi[$q] = "'".$fi[$q]."'";
								}
								$value = implode(',',$fi);
								$qs .= " AND ".$field." IN (".$value.")";
							}	
					  }
					else
					{
							if(strcmp($field,"branch_name")==0)
							{
								
								 $field = "sm.branch_id";
								 $qs .= " AND ".$field." = '".$value."'";
							 }
							 else
							 {
								 $qs .= " AND ".$field." = '".$value."'";
							 }
					}	
					
				break;
				case 'boolean' : $qs .= " AND ".$field." = ".($value); break;
				case 'numeric' :
					switch ($compare) {
						case 'eq' : $qs .= " AND ".$field." = ".$value; break;
						case 'lt' : $qs .= " AND ".$field." < ".$value; break;
						case 'gt' : $qs .= " AND ".$field." > ".$value; break;
					}
				break;
				case 'date' :
					switch ($compare) {
						case 'eq' : $qs .= " AND ".$field." = '".date('Y-m-d',strtotime($value))."'"; break;
						case 'lt' : $qs .= " AND ".$field." < '".date('Y-m-d',strtotime($value))."'"; break;
						case 'gt' : $qs .= " AND ".$field." > '".date('Y-m-d',strtotime($value))."'"; break;
					}
				break;
			}
		}
		$where .= $qs;
	}
	
	$sortorder="";
    $order_field = "sm.branch_id ";
    $order_type  = "ASC";
	
	if ($sort != "") 
	{
	   if($sort=="branch_name")
	   { 
		  $order_field = "branch_id";
		  $order_type  = $dir;
	   }	
	   else if($sort=="semester_name")
	   { 
		  $order_field = "semester_id,".$sort;
		  $order_type  = $dir;		  
	   }
	   else
	   { 
		 $order_field = $sort;
		 $order_type  = $dir;
	   } 		
	}	       		              
		
		if($_REQUEST['branch_id'] != "All"){
			$where .= " and sd.cur_branchid = '".Escape($_REQUEST['branch_id'])."'";
		}
		
		
		$objStudentMgmt = new StudentMgmt();		
		$k=0;	
		 $qry = "SELECT 
	  			sm.student_id,
				sm.student_gturegno,
				sm.student_admdate, 
				CONCAT(sm.student_surname , ' ' , sm.student_firstname , ' ' ,sm.student_lastname) AS studentname,
				sm.student_birthdate,
				sm.student_gender,
				sm.student_admby,
				sm.student_category,	
				sm.student_mobilestudents,
				sm.student_mobileparents, 
				sm.leave_type,
				sm.leave_reason,
				sm.leave_date,
				sd.year_id,
				sd.cur_branchyearid,
				sd.cur_semesterid,	  
			  	(SELECT branch_name 
				FROM cms_branchmaster sec 
				WHERE sec.college_id   = '" . $_SESSION["VedantnrCollegeId"] . "' and sec.branch_id = sd.cur_branchid) AS branch_name,
	  		  	(SELECT year_name
			   	 FROM cms_yearmaster ym 
			     WHERE ym.year_id = sd.year_id) AS year_name,
				(SELECT branchyear_name
			   	 FROM 	cms_branchyear bry 
			     WHERE bry.college_id   = '" . $_SESSION["VedantnrCollegeId"] . "' AND bry.branchyear_id = sd.cur_branchyearid) AS branchyear_name,
				 (SELECT semester_name
			   	 FROM cms_semestermaster sem 
			     WHERE sem.college_id   = '" . $_SESSION["VedantnrCollegeId"] . "' AND sem.semester_id = sd.cur_semesterid) AS semester_name		   	 
		  		   	 	   	 
		  	  FROM
			  	cms_studentmaster sm,
				cms_studentdetail sd				
		  	  WHERE								
				sm.college_id  = '".$_SESSION["VedantnrCollegeId"]."' 
				AND sm.student_id=sd.student_id
				AND sm.student_active=0
				AND sm.leave_type='c'
				AND sd.current_active = 0";
		
		$qry.=$where;
		$rsCount=dbQuery($qry);
		$total=dbRows($rsCount);
			
		 		
		$qry.=' Order By '.$order_field.'  '.$order_type.' Limit '.$start.','.$limit;
	//	echo $qry;
		$rs = dbQuery($qry);
		while($obj = dbObject($rs)) {
		
			$arr[$k] = $obj;   
		   $k++;
		}
	}
	echo '{"total":"'.$total.'","data":'.json_encode($arr).'}';
?>