94 lines
2.9 KiB
PHP
94 lines
2.9 KiB
PHP
<?php
|
|
|
|
// Endpoint JSON: nunca emitir HTML de avisos/errores.
|
|
@ini_set('display_errors', '0');
|
|
@ini_set('display_startup_errors', '0');
|
|
error_reporting(E_ALL & ~E_DEPRECATED & ~E_NOTICE);
|
|
if (ob_get_level() === 0) { @ob_start(); }
|
|
|
|
require_once("../config.php");
|
|
|
|
if (session_status() === PHP_SESSION_NONE) {
|
|
session_name($CFG->sessionname);
|
|
session_start();
|
|
}
|
|
|
|
while (ob_get_level() > 0) { @ob_end_clean(); }
|
|
|
|
header('Content-Type: application/json; charset=utf-8');
|
|
|
|
if (empty($_SESSION['usersession'])) {
|
|
http_response_code(401);
|
|
echo json_encode(array('code' => '0', 'msg' => 'Sesión no válida'), JSON_UNESCAPED_UNICODE);
|
|
exit;
|
|
}
|
|
|
|
$user = json_decode($_SESSION['usersession']);
|
|
if (!$user || empty($user->userid)) {
|
|
echo json_encode(array('code' => '0', 'msg' => 'Sesión no válida'), JSON_UNESCAPED_UNICODE);
|
|
exit;
|
|
}
|
|
|
|
|
|
|
|
$sql = "SELECT
|
|
|
|
asignacion.id,
|
|
|
|
asignacion.uid,
|
|
|
|
asignacion.catid,
|
|
|
|
categoria.clave,
|
|
|
|
categoria.nombre,
|
|
|
|
categoria.imagen,
|
|
|
|
categoria.descripcion
|
|
|
|
FROM
|
|
|
|
asignacion
|
|
|
|
INNER JOIN categoria ON asignacion.catid = categoria.id
|
|
|
|
WHERE
|
|
|
|
asignacion.uid = $user->userid
|
|
|
|
AND categoria.habilitado = 1
|
|
|
|
ORDER BY
|
|
|
|
categoria.order_desc asc
|
|
|
|
";
|
|
|
|
$return = new stdClass();
|
|
|
|
$return->user = $user;
|
|
|
|
global $link;
|
|
|
|
dbConnect();
|
|
|
|
$result = mysqli_query($link, $sql);
|
|
|
|
if ( mysqli_num_rows($result) <= 0 ) {
|
|
|
|
$return->code = "0";
|
|
|
|
$return->msg="Sin cursos asignados";
|
|
|
|
echo json_encode($return, JSON_UNESCAPED_UNICODE);
|
|
|
|
exit;
|
|
|
|
}
|
|
|
|
$return->code = "1";
|
|
|
|
$return->msg="Cursos asignados";
|
|
|
|
while ($row = mysqli_fetch_assoc ($result)){
|
|
|
|
$img = trim((string)$row["imagen"]);
|
|
if ($img !== '' && !preg_match('~^(https?:)?//~i', $img)) { $img = rtrim($CFG->wwwroot, '/') . '/' . ltrim($img, '/'); }
|
|
$row["imagen"] = $img;
|
|
|
|
$return->padre[] = $row;
|
|
|
|
}
|
|
|
|
$padreIds = array();
|
|
foreach ($return->padre as $value) {
|
|
if (isset($value["catid"])) { $padreIds[] = (int)$value["catid"]; }
|
|
}
|
|
$padreIds = array_values(array_unique($padreIds));
|
|
|
|
$hijosPorPadre = array();
|
|
if (count($padreIds)) {
|
|
$sql = "SELECT categoria.id, categoria.clave, categoria.padre, categoria.nombre, categoria.imagen, categoria.descripcion FROM categoria WHERE categoria.habilitado = 1 AND categoria.padre IN (" . implode(',', $padreIds) . ") ORDER BY categoria.order_desc ASC";
|
|
$resultHijos = mysqli_query($link, $sql);
|
|
while ($row = mysqli_fetch_assoc($resultHijos)) {
|
|
$img = trim((string)$row["imagen"]);
|
|
if ($img !== '' && !preg_match('~^(https?:)?//~i', $img)) { $img = rtrim($CFG->wwwroot, '/') . '/' . ltrim($img, '/'); }
|
|
$row["imagen"] = $img;
|
|
$hijosPorPadre[(int)$row["padre"]][] = $row;
|
|
}
|
|
}
|
|
|
|
foreach ($return->padre as $key => $value) {
|
|
$pid = isset($value["catid"]) ? (int)$value["catid"] : 0;
|
|
$return->padre[$key]["hijos"] = isset($hijosPorPadre[$pid]) ? $hijosPorPadre[$pid] : array();
|
|
}
|
|
|
|
|
|
|
|
echo json_encode($return, JSON_UNESCAPED_UNICODE);
|
|
|
|
exit;
|
|
|
|
|
|
|
|
?>
|