New service

This commit is contained in:
2023-08-28 18:56:53 +00:00
parent df135c1147
commit 95abd62d55

View File

@@ -0,0 +1,89 @@
<?
$ruta = '../../';
require_once $_SERVER['DOCUMENT_ROOT'] . '/include/bd_pdo.php';
header('Content-Type: application/json charset=utf-8');
if (isset($_ENV['DEBUG']) and $_ENV['DEBUG']) {
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
}
if (
(!isset($_SERVER['PHP_AUTH_USER']) || $_SERVER['PHP_AUTH_USER'] !== $_ENV['API_USER']) ||
(!isset($_SERVER['PHP_AUTH_PW']) || $_SERVER['PHP_AUTH_PW'] !== $_ENV['API_PASS'])
) {
http_response_code(400);
echo json_encode(['error' => 'Usuario no autorizado']);
exit();
}
// input
$input_raw = file_get_contents('php://input');
$input = json_decode($input_raw, true);
if (!isset($input['clave'])) {
http_response_code(400);
echo json_encode(['error' => 'clave no especificada']);
exit();
} else if (!isset($input['fecha']) and (!isset($input['fecha_inicio']) and !isset($input['fecha_fin']))) {
http_response_code(400);
echo json_encode(['error' => 'fecha no especificada']);
exit();
}
try {
if ($_SERVER['REQUEST_METHOD'] !== 'POST')
throw new Exception('method not allowed');
if (!$db->where('profesor_clave', $input['clave'])->has('profesor'))
throw new Exception('clave no válida');
$profesor = $db->where('profesor_clave', $input['clave'])->getOne('profesor');
$data = $db->query(
"WITH horarios AS (
SELECT * FROM horario_view JOIN horario_profesor USING (horario_id) WHERE :profesor_id = profesor_id
),
fechas AS (
SELECT fechas_clase(h.horario_id) as registro_fecha_ideal, h.horario_id
FROM horarios h
)
SELECT
materia,
facultad,
carrera,
registro_fecha_ideal as fecha_clase,
horarios.horario_hora as hora_clase,
horarios.dia as dia_clase,
COALESCE(
TO_CHAR(registro_fecha::TIME, 'HH24:MI:SS'),
'Sin registro'
) as hora_registro
FROM horarios
JOIN fechas using (horario_id)
LEFT JOIN registro USING (horario_id, registro_fecha_ideal, profesor_id)
WHERE fechas.registro_fecha_ideal BETWEEN :fecha_inicio AND :fecha_fin
ORDER BY fechas.registro_fecha_ideal DESC, horarios.horario_id",
[
':fecha_inicio' => $input['fecha'] ?? $input['fecha_inicio'] ?? null,
':fecha_fin' => $input['fecha'] ?? $input['fecha_fin'] ?? null,
':profesor_id' => $profesor['profesor_id'],
]
);
echo json_encode($data, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
} catch (PDOException $th) {
http_response_code(500);
echo json_encode([
'error' => $th->getMessage(),
'query' => $db->getLastQuery(),
], JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_PARTIAL_OUTPUT_ON_ERROR);
exit;
} catch (Exception $th) {
http_response_code(500);
echo json_encode([
'error' => $th->getMessage(),
], JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
exit;
}