<?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;
    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'));

$configuracion = $sesion->get('configuracion');
$nombre_grupo = $configuracion['nombres_grupos'];

$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 = "";
$SQLTB = "FROM aca_grupos
    INNER JOIN aux_nombres_grupos 
        ON (aca_grupos.cod_nom_grupo = aux_nombres_grupos.cod_nom_grupo)
    INNER JOIN adm_sedes 
        ON (aca_grupos.cod_sede = adm_sedes.cod_sede)
    INNER JOIN aux_jornadas as j
        ON (aca_grupos.cod_jornada = j.cod_jornada)
    INNER JOIN aux_especialidades as e 
        ON (aca_grupos.cod_especialidad = e.cod_especialidad) 
    INNER JOIN aux_caracteres as c
	ON (e.cod_caracter=c.cod_caracter)
    INNER JOIN aux_metodologias as m 
        ON (aca_grupos.cod_metodologia = m.cod_metodologia) 
    INNER JOIN pro_profesores
	ON (aca_grupos.cod_director=pro_profesores.cod_profesor)";


$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;
}

$wh = str_replace('nom_profesor', 'pro_profesores.cod_profesor', $wh);
$wh = str_replace('nom_sede', 'adm_sedes.cod_sede', $wh);

$cons = "SELECT COUNT(*) AS count $SQLTB  WHERE cod_grado=" . $id . $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;



$SQL = "SELECT aca_grupos.finalizado,aca_grupos.cod_grupo, aux_nombres_grupos.$nombre_grupo as nom_nom_grupo, aca_grupos.num_aula, aca_grupos.dim_aula, adm_sedes.nom_sede, j.nom_jornada, 
	e.nom_especialidad, c.nom_caracter, m.nom_metodologia, CONCAT_WS(' ',ap1_pro,ap2_pro,no1_pro,no2_pro) AS nom_profesor, aca_grupos.num_aula,aca_grupos.dim_aula,aca_grupos.fecha_inicio,aca_grupos.fecha_fin $SQLTB  
	WHERE cod_grado=" . $id . $wh . " ORDER BY $sidx $sord LIMIT $start , $limit";


//	echo $SQL;
$academia->instruccion($SQL);

if (stristr($_SERVER["HTTP_ACCEPT"], "application/xhtml+xml")) {
    header("Content-type: application/xhtml+xml;charset=utf-8");
} else {
    header("Content-type: text/xml;charset=utf-8");
}
$et = ">";
echo "<?xml version='1.0' encoding='utf-8'?$et\n";
echo "<rows>";
echo "<page>" . $page . "</page>";
echo "<total>" . $total_pages . "</total>";
echo "<records>" . $count . "</records>";
// be sure to put text data in CDATA
while ($row = $academia->extraer_registro()) {
    echo "<row id='" . $row[cod_grupo] . "'>";
        echo "<cell><![CDATA[" . $row["finalizado"] . "]]></cell>";
        echo "<cell><![CDATA[" . $row[nom_nom_grupo] . "]]></cell>";
        echo "<cell><![CDATA[" . $row[nom_sede] . "]]></cell>";
        echo "<cell><![CDATA[" . $row[nom_jornada] . "]]></cell>";
        echo "<cell><![CDATA[" . $row[nom_especialidad] . "]]></cell>";
        echo "<cell><![CDATA[" . $row[nom_caracter] . "]]></cell>";
        echo "<cell><![CDATA[" . $row[nom_metodologia] . "]]></cell>";
        echo "<cell><![CDATA[" . $row[nom_profesor] . "]]></cell>";
        echo "<cell><![CDATA[" . $row[fecha_inicio] . "]]></cell>";
        echo "<cell><![CDATA[" . $row[fecha_fin] . "]]></cell>";
        echo "<cell><![CDATA[" . $row[num_aula] . "]]></cell>";
        echo "<cell><![CDATA[" . $row["dim_aula"] . "]]></cell>";
    echo "</row>";
}
echo "</rows>";
$academia->cerrar();
?>
