index.php 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. <?
  2. $ruta = '../../';
  3. require_once $_SERVER['DOCUMENT_ROOT'] . '/include/bd_pdo.php';
  4. header('Content-Type: application/json charset=utf-8');
  5. if (isset($_ENV['DEBUG']) and $_ENV['DEBUG']) {
  6. ini_set('display_errors', 1);
  7. ini_set('display_startup_errors', 1);
  8. error_reporting(E_ALL);
  9. }
  10. if (
  11. (!isset($_SERVER['PHP_AUTH_USER']) || $_SERVER['PHP_AUTH_USER'] !== $_ENV['API_USER']) ||
  12. (!isset($_SERVER['PHP_AUTH_PW']) || $_SERVER['PHP_AUTH_PW'] !== $_ENV['API_PASS'])
  13. ) {
  14. http_response_code(400);
  15. echo json_encode(['error' => 'Usuario no autorizado']);
  16. exit();
  17. }
  18. // input
  19. $input_raw = file_get_contents('php://input');
  20. $input = json_decode($input_raw, true);
  21. if (!isset($input['clave'])) {
  22. http_response_code(400);
  23. echo json_encode(['error' => 'clave no especificada']);
  24. exit();
  25. } else if (!isset($input['fecha']) and (!isset($input['fecha_inicio']) and !isset($input['fecha_fin']))) {
  26. http_response_code(400);
  27. echo json_encode(['error' => 'fecha no especificada']);
  28. exit();
  29. }
  30. try {
  31. if ($_SERVER['REQUEST_METHOD'] !== 'POST')
  32. throw new Exception('method not allowed');
  33. if (!$db->where('profesor_clave', $input['clave'])->has('profesor'))
  34. throw new Exception('clave no válida');
  35. $profesor = $db->where('profesor_clave', $input['clave'])->getOne('profesor');
  36. $data = $db->query(
  37. "WITH horarios AS (
  38. SELECT * FROM horario_view JOIN horario_profesor USING (horario_id) WHERE :profesor_id = profesor_id
  39. ),
  40. fechas AS (
  41. SELECT fechas_clase(h.horario_id) as registro_fecha_ideal, h.horario_id
  42. FROM horarios h
  43. )
  44. SELECT
  45. materia,
  46. facultad,
  47. carrera,
  48. registro_fecha_ideal as fecha_clase,
  49. horarios.horario_hora as hora_clase,
  50. horarios.dia as dia_clase,
  51. COALESCE(
  52. TO_CHAR(registro_fecha::TIME, 'HH24:MI:SS'),
  53. 'Sin registro'
  54. ) as hora_registro
  55. FROM horarios
  56. JOIN fechas using (horario_id)
  57. LEFT JOIN registro USING (horario_id, registro_fecha_ideal, profesor_id)
  58. WHERE fechas.registro_fecha_ideal BETWEEN :fecha_inicio AND :fecha_fin
  59. ORDER BY fechas.registro_fecha_ideal DESC, horarios.horario_id",
  60. [
  61. ':fecha_inicio' => $input['fecha'] ?? $input['fecha_inicio'] ?? null,
  62. ':fecha_fin' => $input['fecha'] ?? $input['fecha_fin'] ?? null,
  63. ':profesor_id' => $profesor['profesor_id'],
  64. ]
  65. );
  66. echo json_encode($data, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
  67. } catch (PDOException $th) {
  68. http_response_code(500);
  69. echo json_encode([
  70. 'error' => $th->getMessage(),
  71. 'query' => $db->getLastQuery(),
  72. ], JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_PARTIAL_OUTPUT_ON_ERROR);
  73. exit;
  74. } catch (Exception $th) {
  75. http_response_code(500);
  76. echo json_encode([
  77. 'error' => $th->getMessage(),
  78. ], JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
  79. exit;
  80. }