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

    header("Content-Type:application/json");
    include("../dbconn.php");
    include("../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 = $_GET["sessionid"];
    $appid = $_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"=>$db->error));
        
    $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"=>$db->error));
        
    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();
?>
