<?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'));
	
	$nombre_grupo=$sesion->getVar('configuracion.nombres_grupos');
	
	
	$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
	$cdgr = $_REQUEST['cdgr'];
	$cdm = $_REQUEST['cdm'];
	
	$tipus=$sesion->get('ac_tipo_user');
	if($sesion->getVar('cod_cmps.cd_docen')==$tipus){
		$cd_docente=$sesion->getVar('info_doc.cod_profesor');
		$wh=" AND aca_logros.cod_grado=$cdgr and aca_logros.cod_asig=$cdm and aca_logros.cod_pro=$cd_docente " ;
	}else{
		$wh=" AND aca_logros.cod_grado=$cdgr and aca_logros.cod_asig=$cdm";
	}
	
	
	if(!$sidx) $sidx =1;
	if(!$sord) $sord ="asc";
	if(!$limit) $limit =10;

	$searchOn = Strip($_REQUEST['_search']);
	if($searchOn=='true') {
		$fld = Strip($_REQUEST['searchField']);
		$fldata = Strip($_REQUEST['searchString']);
		$foper = Strip($_REQUEST['searchOper']);
		$wh .= " AND ".$fld;
		$wh .=construcWhSimple($foper,$fldata);
	}

	$start = $limit*$page - $limit; // do not put $limit*($page - 1)
	if ($start<0) $start = 0;
	
	//$cons="SELECT COUNT(*) AS count FROM adm_administrativos WHERE 1=1 ".$wh;
	$SQL = "SELECT SQL_CALC_FOUND_ROWS aca_logros.cod_logro, LCASE(CONCAT_WS(' ',pro_profesores.ap1_pro, pro_profesores.ap2_pro,pro_profesores.no1_pro, pro_profesores.no2_pro)) AS nomape, aca_logros.per_cal, aca_logros.def_logro
		FROM aca_logros INNER JOIN pro_profesores  ON (aca_logros.cod_pro = pro_profesores.cod_profesor)
		WHERE 1=1".$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;	

	$responce->page = $page;
	$responce->total = $total_pages;
	$responce->records = $count;
	$mayus_nom=$sesion->getVar('configuracion.format_nom');
	$i=0;
	while($row=$academia->extraer_registro()){
		$responce->rows[$i]['id']=$row['cod_logro'];
		$responce->rows[$i]['cell']=array(formatoPalabras($row['nomape'],$mayus_nom),$row["per_cal"],parseString($row["def_logro"]));
		$i++;
	}
	$academia->liberar();
	echo json_encode($responce);
		
	$academia->cerrar();

?>
