index.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <?
  2. ini_set('display_errors', 1);
  3. ini_set('display_startup_errors', 1);
  4. error_reporting(E_ALL);
  5. $ruta = '../../';
  6. require_once $_SERVER['DOCUMENT_ROOT'] . '/include/bd_pdo.php';
  7. header('Content-Type: application/json charset=utf-8');
  8. if (
  9. (!isset($_SERVER['PHP_AUTH_USER']) || $_SERVER['PHP_AUTH_USER'] !== $_ENV['API_USER']) ||
  10. (!isset($_SERVER['PHP_AUTH_PW']) || $_SERVER['PHP_AUTH_PW'] !== $_ENV['API_PASS'])
  11. ) {
  12. http_response_code(400);
  13. echo json_encode(['error' => 'Usuario no autorizado']);
  14. exit();
  15. }
  16. $input_raw = file_get_contents('php://input');
  17. $input = json_decode($input_raw, true);
  18. if (!isset($input['clave'])) {
  19. http_response_code(400);
  20. echo json_encode(['error' => 'clave no especificada']);
  21. exit();
  22. } else if (!isset($input['fecha']) and (!isset($input['fecha_inicio']) and !isset($input['fecha_fin']))) {
  23. http_response_code(400);
  24. echo json_encode(['error' => 'fecha no especificada']);
  25. exit();
  26. }
  27. try {
  28. if ($_SERVER['REQUEST_METHOD'] !== 'POST')
  29. throw new Exception('method not allowed');
  30. if (!$db->where('profesor_clave', $input['clave'])->has('profesor'))
  31. throw new Exception('clave no válida');
  32. $profesor = $db->where('profesor_clave', $input['clave'])->getOne('profesor');
  33. $data = $db->query(
  34. "WITH horarios AS (
  35. SELECT *,
  36. CASE
  37. WHEN horario_dia = 1 THEN 'Lunes'
  38. WHEN horario_dia = 2 THEN 'Martes'
  39. WHEN horario_dia = 3 THEN 'Miércoles'
  40. WHEN horario_dia = 4 THEN 'Jueves'
  41. WHEN horario_dia = 5 THEN 'Viernes'
  42. WHEN horario_dia = 6 THEN 'Sábado'
  43. WHEN horario_dia = 7 THEN 'Domingo'
  44. END as dia,
  45. horario_hora + duracion_interval as horario_fin
  46. FROM horario
  47. JOIN horario_profesor USING (horario_id)
  48. JOIN materia USING (materia_id)
  49. JOIN carrera USING (carrera_id)
  50. JOIN facultad ON facultad.facultad_id = carrera.facultad_id
  51. JOIN periodo_carrera USING (carrera_id)
  52. JOIN periodo_view USING (periodo_id)
  53. JOIN duracion USING (duracion_id)
  54. WHERE :profesor_id = profesor_id and periodo_view.activo
  55. ),
  56. fechas AS (
  57. SELECT fechas_clase(h.horario_id) as registro_fecha_ideal, h.horario_id
  58. FROM horarios h
  59. )
  60. SELECT distinct
  61. materia_nombre as materia,
  62. facultad_nombre as facultad,
  63. carrera_nombre as carrera,
  64. registro_fecha_ideal as fecha_clase,
  65. horarios.horario_hora as hora_clase,
  66. horarios.horario_fin as fin_clase,
  67. horarios.dia as dia_clase,
  68. CASE
  69. when registro_justificada THEN 'Justificada'
  70. WHEN registro_retardo THEN 'Retardo'
  71. WHEN registro_reposicion THEN 'Respuesta'
  72. WHEN registro_fecha IS NOT NULL THEN 'Registrado'
  73. ELSE 'Sin registro' END as registro,
  74. TO_CHAR(registro_fecha::TIME, 'HH24:MI:SS') as hora_registro
  75. FROM horarios
  76. JOIN fechas using (horario_id)
  77. LEFT JOIN registro USING (horario_id, registro_fecha_ideal, profesor_id)
  78. WHERE registro.registro_fecha_ideal BETWEEN :fecha_inicio AND :fecha_fin
  79. ORDER BY fecha_clase DESC, materia_nombre",
  80. [
  81. ':fecha_inicio' => $input['fecha'] ?? $input['fecha_inicio'] ?? null,
  82. ':fecha_fin' => $input['fecha'] ?? $input['fecha_fin'] ?? null,
  83. ':profesor_id' => $profesor['profesor_id'],
  84. ]
  85. );
  86. echo json_encode($data, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
  87. } catch (PDOException $th) {
  88. http_response_code(500);
  89. echo json_encode([
  90. 'error' => $th->getMessage(),
  91. 'query' => $db->getLastQuery(),
  92. ], JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_PARTIAL_OUTPUT_ON_ERROR);
  93. exit;
  94. } catch (Exception $th) {
  95. http_response_code(500);
  96. echo json_encode([
  97. 'error' => $th->getMessage(),
  98. ], JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
  99. exit;
  100. }