<?php
// cms/api/moduls/list

    header("Content-Type:application/json");
    include("../../../settings/dbconn.php");
    include("../../../settings/utils.php");
    
    // parametros obligatorios
    $parmsob = array("sessionid","appid");
    if (!parametrosValidos($_GET, $parmsob))
        badEnd("400", array("msg"=>"Parametros obligatorios " . implode(", ", $parmsob)));
    
    //Declaramos e inicializamos nuestras variables
    $out = new stdClass();
    $sessionid = mysqli_real_escape_string($db, $_GET["sessionid"]);
    $appid = mysqli_real_escape_string($db, $_GET["appid"]);
    
    // 1 = CMS | 2 = APP
    $appid = $appid == 1 ? 'CMS' : 'APP';
    
    //Validamos que la session del usuario sea valida para continuar
    isSessionValidCMS($db, $sessionid);
        
    //Consultamos la cantidad de registros
    $sql = "SELECT COUNT(DISTINCT module) AS qty".
    "       FROM audit ".
    "       WHERE application = '".$appid."'".
    "       ORDER BY module ASC ";
    if (!$rs=$db->query($sql))
        badEnd("500", array("msg"=>determinateDBError($db)));
        
    $row = $rs->fetch_assoc();
    
    $out->numofrecords = (int)$row["qty"];
    
    //Consultamos los modulos
    $sql = "SELECT DISTINCT module ".
    "       FROM audit ".
    "       WHERE application = '".$appid."'".
    "       ORDER BY module ASC ";
    if (!$rs=$db->query($sql))
        badEnd("500", array("msg"=>determinateDBError($db)));
        
    while($row = $rs->fetch_assoc()){
        $record = new stdClass();
        $record->dsc = $row['module'];
        
        $records [] = $record;
    }
    
    $out->records = $records;
    
    header("HTTP/1.1 200");
    echo (json_encode($out));
    die();
?>
