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

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

require_once("../includes/MySQL.php");
require_once("../includes/funciones_buscador.php");
                                                                          
$academia = new Servidor_Base_Datos(AC_CONN_HOST,AC_CONN_USER,AC_CONN_PASS,AC_CONN_DB."".$sesion->get('anio'));

$examp = $_REQUEST["q"]; //query number

$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 = $_REQUEST['id'];
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);
	//var_dump($wh);
	//echo $ss;
}

		$cons="SELECT COUNT(*) AS count FROM `pro_profesores` WHERE 1=1 ".$wh;
		$academia->instruccion($cons);
		
		$row = $academia->extraer_registro();
		$count = $row['count'];
		$academia->liberar();
		if( $count >0 ) {
			$total_pages = ceil($count/$limit);
		} else {
			$total_pages = 0;
		}
        if ($page > $total_pages) $page=$total_pages;
		$start = $limit*$page - $limit; // do not put $limit*($page - 1)
		if ($start<0) $start = 0;
		
//*************************************
 $sidx=$sidx=='nom_ape' ? 'ap1_pro' : $sidx;
//**************************************		
		
        $SQL = "SELECT
    pro_profesores.cod_profesor
    , pro_profesores.ide_profesor
    , aux_tipos_identificacion.nom_tipoiden
    , pro_profesores.no1_pro
    , pro_profesores.no2_pro
    , pro_profesores.ap1_pro
    , pro_profesores.ap2_pro
    , pro_profesores.dir_pro
    , pro_profesores.cel_pro
    , pro_profesores.mail_pro
FROM
    pro_profesores
    INNER JOIN aux_tipos_identificacion 
        ON (pro_profesores.ide_tipoiden = aux_tipos_identificacion.ide_tipoiden)".$wh." ORDER BY $sidx $sord LIMIT $start , $limit";
		
        $responce->page = $page;
        $responce->total = $total_pages;
        $responce->records = $count;
		
		$academia->instruccion($SQL);
        $i=0;
		while($row = $academia->extraer_registro()) {
            $responce->rows[$i]['id']=$row[cod_profesor];
			$nom_ape=$row[ap1_pro].' '.$row[ap2_pro].' '.$row[no1_pro].' '.$row[no2_pro];
			$responce->rows[$i]['cell']=array($row[ide_profesor],$row[nom_tipoiden],trim($nom_ape),$row[ap1_pro],$row[ap2_pro],$row[no1_pro],$row[no2_pro],$row[dir_pro],$row[cel_pro],$row[mail_pro]);
            $i++;
		} 
        echo json_encode($responce);
$academia->cerrar();

?>
