<?php 
require_once("../includes/bootstrap2.php");
$sesion=new sesion(); // inicio la sesion

if(!comprobar_sesion()){
	$responce->page = 0;
	$responce->total = 0;
	$responce->records = 0;
	echo json_encode($responce);
	exit();
}

$academia = bootstrap::initBd($sesion->get('anio'));


$page = $_REQUEST['page']; // get the requested page
$limit = $_REQUEST['rows']; // get how many rows we want to have into the grid
$sidx = $_REQUEST['sidx']; // get index row - i.e. user click to sort
$sord = $_REQUEST['sord']; // get the direction

$id = $_POST['codigo'];

if(!$sidx) $sidx =1;
if(!$sord) $sord ="asc";
if(!$limit) $limit =10;

// search options
// IMPORTANT NOTE!!!!!!!!!!!!!!!!!!!!!!!!!!!!
// this type of constructing is not recommendet
// it is only for demonstration
//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
$wh = "";
$searchOn = Strip($_REQUEST['_search']);
if($searchOn=='true') {
	$searchstr = Strip($_REQUEST['filters']);
	//$wh= constructWhere($searchstr);
	$jsona = json_decode($searchstr,true);
	$wh =  " AND ".getStringForGroup($jsona);
}

		$start = $limit*$page - $limit; // do not put $limit*($page - 1)
		if ($start<0) $start = 0;
		
		$SQL = "SELECT SQL_CALC_FOUND_ROWS  cod_nota, nombre_nota as nombre, porc_nota, fec_nota, desc_nota FROM conf_detnotasxsaber WHERE cod_saber='$id' ".$wh." ORDER BY "."$sidx $sord LIMIT $start , $limit ";
		
		$academia->instruccion($SQL,1);
			
		$count = $academia->totReg();
		
		
		if( $count >0 ) {
			$total_pages = ceil($count/$limit);
		} else {
			$total_pages = 0;
		}
		
        if ($page > $total_pages) $page=$total_pages;	
		
        //echo $SQL;
        $responce->page = $page;
        $responce->total = $total_pages;
        $responce->records = $count;
		
        $i=0;
		$aux=0;
		while($row=$academia->extraer_registro()){
			$responce->rows[$i]['id']=$row['cod_nota'];
			$responce->rows[$i]['cell']=array($row["nombre"],$row["porc_nota"], $row["fec_nota"],$row["desc_nota"]);
			$i++;
		}
		$academia->liberar();
        echo json_encode($responce);
		$academia->cerrar();

?>
