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

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

$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 = " and cod_sede=1 and cod_jornada=2 ";
$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;
$cmnomgrup = $sesion->getVar("configuracion.nombres_grupos");
//$cons="SELECT COUNT(*) AS count FROM adm_administrativos WHERE 1=1 ".$wh;

$node = isset($_REQUEST["nodeid"]) ? $_REQUEST["nodeid"] : false;
// detect if here we post the data from allready loaded tree
// we can make here other checks
if ($node) {
    $codGrupo = substr($node, 1);

    $wh = " and cod_grupo=$codGrupo ";

    $SQL = 'select SQL_CALC_FOUND_ROWS cod_asigxgrupo AS id, cod_asi, nom_asi as nom, nom_ape_pro as nom_pro, cod_grupo '
            . 'from view_info_asignaturas '
            . 'where 1=1 ' . $wh . ' ORDER BY nom asc';
    $level = 1;
    $leaf = 'false';
} else {
    // initial grid
    $SQL = 'select SQL_CALC_FOUND_ROWS cod_grupo as id,concat_ws("",nom_grado," ", ' . $cmnomgrup . ') as nom, nom_ape  as nom_pro '
            . 'from view_datos_grupos '
            . 'where 1=1 ' . $wh . ' ORDER BY orden';
    $level = 0;
    $leaf = 'true';
}

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

$i = 0;
$aux = 0;
$formato_nombres = $sesion->getVar('configuracion.format_nom');
$formato_materias = $sesion->getVar('configuracion.format_mat');


while ($row = $academia->extraer_registro()) {

    $nom = ($level == 1) ? formatoPalabras($row['nom'], $formato_materias) : $row['nom'];
    $id = ($level == 1) ? 'a-' . $row['id'] : 'g' . $row['id'];

    $responce->rows[$i]['cell'] = array(
        $id,
        $nom,
        formatoPalabras($row['nom_pro'], $formato_nombres),
        $level,
        $row['id'],
        $row[rgt],
        $leaf,
        'false'
    );

    $i++;
}

if ($sesion->get('ac_usuario') == 'prointel')
    $responce->sql = $SQL;


$academia->liberar();

echo json_encode($responce);

$academia->cerrar();
?>

