<?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'));
$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
if (!$sidx)
    $sidx = 1;

$totalrows = isset($_REQUEST['totalrows']) ? $_REQUEST['totalrows'] : false;
if ($totalrows) {
    $limit = $totalrows;
}

$tbls = "FROM aca_observacion_seguimiento ";

$filtro = $_GET['id'];
$periodo = escapar($_GET['per_cal'], "int");

$wh = " AND cod_estudiantes_grupos='" . $filtro . "'";

$cons = "SELECT COUNT(*) AS count $tbls  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;

/* $sql = "SELECT
  aca_asignaturas.nom_asi,
  id_inasistencia as id_inasis, fecha_falta as fecha, justificada as cod_tipo_in, if(justificada=0, 'Justificada','Injustificada') as tipo, obser_falta as obs ,
  aca_estxasigxgrupos.cod_estxasigxgrupos as id_materia $tbls WHERE 1=1".$wh. " ORDER BY fecha DESC"; */
$sql = "SELECT 
  cod_observacion_seguimiento, cod_estudiantes_grupos, fecha_observacion, 
  tipo_observacion, observacion, accion_reparadora 
			 $tbls WHERE 1=1" . $wh . " ORDER BY " . $sidx . " " . $sord . " LIMIT " . $start . " , " . $limit;
;


$academia->instruccion($sql);
$i = 0;
$responce->page = $page;
$responce->total = $total_pages;
$responce->records = $count;
$mayus_mat = $sesion->getVar('configuracion.format_mat');
$i = 0;
while ($row = $academia->extraer_registro()) {
    $responce->rows[$i]['id'] = $row['cod_observacion_seguimiento'];
    $responce->rows[$i]['cell'] = array(
        $row['fecha_observacion'],
        $row['tipo_observacion']==1 ? 'Aspectos Académicos' : 'Aspectos de convivencia',
        $row['observacion'],
        $row['accion_reparadora'],
        $row['tipo_observacion']
    );

    $i++;
}


header("Content-type: application/json");
echo json_encode($responce);
$academia->cerrar();
?>
        