123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108 |
- <?
- ini_set('display_errors', 1);
- ini_set('display_startup_errors', 1);
- error_reporting(E_ALL);
- $ruta = '../../';
- require_once $_SERVER['DOCUMENT_ROOT'] . '/include/bd_pdo.php';
- header('Content-Type: application/json charset=utf-8');
- 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_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 *,
- CASE
- WHEN horario_dia = 1 THEN 'Lunes'
- WHEN horario_dia = 2 THEN 'Martes'
- WHEN horario_dia = 3 THEN 'Miércoles'
- WHEN horario_dia = 4 THEN 'Jueves'
- WHEN horario_dia = 5 THEN 'Viernes'
- WHEN horario_dia = 6 THEN 'Sábado'
- WHEN horario_dia = 7 THEN 'Domingo'
- END as dia,
- horario_hora + duracion_interval as horario_fin
- FROM horario
- JOIN horario_profesor USING (horario_id)
- JOIN materia USING (materia_id)
- JOIN carrera USING (carrera_id)
- JOIN facultad ON facultad.facultad_id = carrera.facultad_id
- JOIN periodo_carrera USING (carrera_id)
- JOIN periodo_view USING (periodo_id)
- JOIN duracion USING (duracion_id)
- WHERE :profesor_id = profesor_id and periodo_view.activo
- ),
- fechas AS (
- SELECT fechas_clase(h.horario_id) as registro_fecha_ideal, h.horario_id
- FROM horarios h
- )
- SELECT distinct
- materia_nombre as materia,
- facultad_nombre as facultad,
- carrera_nombre as carrera,
- registro_fecha_ideal as fecha_clase,
- horarios.horario_hora as hora_clase,
- horarios.horario_fin as fin_clase,
- horarios.dia as dia_clase,
- CASE
- when registro_justificada THEN 'Justificada'
- WHEN registro_retardo THEN 'Retardo'
- WHEN registro_reposicion THEN 'Respuesta'
- WHEN registro_fecha IS NOT NULL THEN 'Registrado'
- ELSE 'Sin registro' END as registro,
- TO_CHAR(registro_fecha::TIME, 'HH24:MI:SS') as hora_registro
- FROM horarios
- JOIN fechas using (horario_id)
- LEFT JOIN registro USING (horario_id, registro_fecha_ideal, profesor_id)
- WHERE registro.registro_fecha_ideal BETWEEN :fecha_inicio AND :fecha_fin
- ORDER BY fecha_clase DESC, materia_nombre",
- [
- ':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;
- }
|