Files
paad/action/rutas_salón_horario.php
Alejandro Rosales 047554636d
Some checks are pending
Deploy Pruebas / deploy (push) Waiting to run
Query final
2024-10-08 12:35:47 -06:00

152 lines
6.6 KiB
PHP

<?
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
#input $_GET['id_espacio_sgu']
$information = [
'GET' => [
'id_espacio_sgu',
'bloque_horario_id',
],
];
#output rutas: [ ...ruta, salones: [{...salon}] ]
header('Content-Type: application/json charset=utf-8');
$ruta = "../";
require_once "../class/c_login.php";
// check method
try {
global $db;
if ($_SERVER['REQUEST_METHOD'] === 'GET') {
// check parameters
array_walk($information['GET'], function ($value) {
if (!array_key_exists($value, $_GET)) {
http_response_code(400);
echo json_encode(['error' => "$value is required"]);
exit;
}
});
// step 1: get subrutas
$data = $db
->where('tiene_salones')
->where("{$_GET['id_espacio_sgu']} = ANY(id_espacio_sgu_array)")
->get('salon_view_mat', columns: 'id_espacio_sgu, salon, salon_id, salon_array');
$columns = [
// horario
'horario_view.horario_id',
'horario_hora',
'horario_grupo',
'horario_fin',
'materia',
'carrera',
'nivel',
'horario_view.salon',
'facultad',
// profesor
'profesor.profesor_id',
'profesor_clave',
'profesor_nombre',
'profesor_correo',
// registro
'registro_fecha',
'registro_retardo',
'registro.reposicion_id',
'estado_supervisor_id',
'comentario',
// reposicion
'reposicion_fecha',
'reposicion_hora',
'reposicion_hora_fin',
'salon_reposicion.salon as reposicion_salon',
];
$fecha = ($_GET['fecha'] != 'null') ? ("'{$_GET['fecha']}'" ?: 'CURRENT_DATE') : 'CURRENT_DATE';
$horarios_y_reposiciones = $db->query(
"SELECT " . implode(', ', $columns) . <<<SQL
, NULL as reposicion_fin, NULL as registro_fecha_ideal, salon_view_mat.id_espacio_padre, 0 AS tipo
FROM horario_view
inner JOIN periodo using(periodo_id)
JOIN bloque_horario ON (bloque_horario.hora_inicio, bloque_horario.hora_fin) OVERLAPS (horario_view.horario_hora, horario_view.horario_hora + horario_view.duracion)
NATURAL JOIN salon_view_mat
NATURAL JOIN horario_profesor
NATURAL JOIN profesor
LEFT JOIN registro ON (registro.profesor_id, registro.horario_id, registro.registro_fecha_ideal) = (profesor.profesor_id, horario_view.horario_id, $fecha::DATE)
NATURAL LEFT JOIN reposicion
LEFT JOIN salon AS salon_reposicion ON salon_reposicion.salon_id = reposicion.salon_id
WHERE $fecha::DATE BETWEEN periodo.periodo_fecha_inicio AND periodo.periodo_fecha_fin
AND horario_dia = EXTRACT(DOW FROM $fecha::DATE)
AND bloque_horario.id = :bloque_horario_id
AND salon_view_mat.id_espacio_padre IN (
SQL . implode(', ', array_map(fn($s) => $s['id_espacio_sgu'], $data)) . <<<SQL
)
SQL . ' UNION ALL ' . 'SELECT ' . implode(', ', $columns) . <<<SQL
, reposicion_hora + horario_view.duracion as reposicion_fin, registro_fecha_ideal, salon_reposicion.id_espacio_padre, 1 AS tipo
FROM horario_view
inner JOIN periodo using(periodo_id)
NATURAL JOIN registro
NATURAL JOIN reposicion
JOIN bloque_horario ON (bloque_horario.hora_inicio, bloque_horario.hora_fin) OVERLAPS (reposicion_hora, reposicion_hora + horario_view.duracion)
NATURAL JOIN profesor
JOIN salon_view_mat as salon_reposicion ON (salon_reposicion.salon_id = reposicion.salon_id)
WHERE
$fecha::DATE BETWEEN periodo.periodo_fecha_inicio
AND periodo.periodo_fecha_fin
AND reposicion_fecha = $fecha::DATE
AND bloque_horario.id = :bloque_horario_id
AND salon_reposicion.id_espacio_padre IN (
SQL . implode(', ', array_map(fn($s) => $s['id_espacio_sgu'], $data)) . <<<SQL
)
SQL,
[
'bloque_horario_id' => $_GET['bloque_horario_id'],
]
);
// echo '<pre>' . json_encode($horarios_y_reposiciones, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE) . '</pre>'; exit();
$data = array_map(
fn($ruta) => array_merge(
[
'horarios' => array_values(array_filter(
$horarios_y_reposiciones,
fn($horario) => $horario['id_espacio_padre'] == $ruta['id_espacio_sgu'] && $horario['tipo'] == 0
)),
// 'query' => $db->getLastQuery(),
'reposiciones' => array_values(array_filter(
$horarios_y_reposiciones,
fn($horario) => $horario['id_espacio_padre'] == $ruta['id_espacio_sgu'] && $horario['tipo'] == 1
)),
],
$ruta,
),
$data
);
$data = array_filter(
$data,
fn($ruta) => count($ruta['horarios']) > 0 || count($ruta['reposiciones']) > 0
);
echo json_encode(array_values($data), JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
} else {
http_response_code(405);
echo json_encode(['error' => 'method not allowed']);
exit;
}
} catch (PDOException $th) {
http_response_code(500);
echo json_encode([
'error' => $th->getMessage(),
'query' => $db->getLastQuery(),
], JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
exit;
} catch (Exception $th) {
http_response_code(500);
echo json_encode([
'error' => $th->getMessage(),
], JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
exit;
}