rutas_salón_horario.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. <?
  2. ini_set('display_errors', 1);
  3. ini_set('display_startup_errors', 1);
  4. error_reporting(E_ALL);
  5. #input $_GET['id_espacio_sgu']
  6. $information = [
  7. 'GET' => [
  8. 'id_espacio_sgu',
  9. 'bloque_horario_id',
  10. ],
  11. ];
  12. #output rutas: [ ...ruta, salones: [{...salon}] ]
  13. header('Content-Type: application/json charset=utf-8');
  14. $ruta = "../";
  15. require_once "../class/c_login.php";
  16. // check method
  17. try {
  18. global $db;
  19. if ($_SERVER['REQUEST_METHOD'] === 'GET') {
  20. // check parameters
  21. array_walk($information['GET'], function ($value) {
  22. if (!array_key_exists($value, $_GET)) {
  23. http_response_code(400);
  24. echo json_encode(['error' => "$value is required"]);
  25. exit;
  26. }
  27. });
  28. // step 1: get subrutas
  29. $data = $db
  30. ->where('tiene_salones')
  31. ->where("{$_GET['id_espacio_sgu']} = ANY(id_espacio_sgu_array)")
  32. ->get('salon_view', columns: 'id_espacio_sgu, salon, salon_id, salon_array');
  33. $columns = [
  34. // horario
  35. 'horario_view.horario_id',
  36. 'horario_hora',
  37. 'horario_grupo',
  38. 'horario_fin',
  39. 'materia',
  40. 'carrera',
  41. 'nivel',
  42. 'horario_view.salon',
  43. 'facultad',
  44. // profesor
  45. 'profesor.profesor_id',
  46. 'profesor_clave',
  47. 'profesor_nombre',
  48. 'profesor_correo',
  49. // registro
  50. 'registro_fecha',
  51. 'registro_retardo',
  52. 'registro.reposicion_id',
  53. 'estado_supervisor_id',
  54. 'comentario',
  55. // reposicion
  56. 'reposicion_fecha',
  57. 'reposicion_hora',
  58. 'salon_reposicion.salon as reposicion_salon',
  59. ];
  60. $data = array_map(
  61. fn($ruta) => array_merge(
  62. [
  63. 'horarios' => $db
  64. ->join('periodo', 'periodo.periodo_id = horario_view.periodo_id')
  65. ->join('bloque_horario', '(bloque_horario.hora_inicio, bloque_horario.hora_fin) OVERLAPS (horario_view.horario_hora, horario_view.horario_hora + horario_view.duracion)')
  66. ->join('salon_view', 'salon_view.salon_id = horario_view.salon_id')
  67. ->join('horario_profesor', 'horario_profesor.horario_id = horario_view.horario_id')
  68. ->join('profesor', 'profesor.profesor_id = horario_profesor.profesor_id')
  69. ->join('registro', '(registro.profesor_id, registro.horario_id, registro.registro_fecha_ideal) = (profesor.profesor_id, horario_view.horario_id, CURRENT_DATE)', 'LEFT')
  70. ->join('reposicion', 'reposicion.reposicion_id = registro.reposicion_id', 'LEFT')
  71. ->join('salon as salon_reposicion', 'salon_reposicion.salon_id = reposicion.salon_id', 'LEFT')
  72. ->where('CURRENT_DATE BETWEEN periodo.periodo_fecha_inicio AND periodo.periodo_fecha_fin')
  73. ->where('horario_dia = EXTRACT(DOW FROM CURRENT_DATE)')
  74. ->where('bloque_horario.id', $_GET['bloque_horario_id'])
  75. ->where('salon_view.id_espacio_padre', $ruta['id_espacio_sgu'])
  76. ->get(
  77. 'horario_view',
  78. columns: $columns
  79. ),
  80. // 'query' => $db->getLastQuery(),
  81. 'reposiciones' => $db->query(
  82. 'SELECT ' . implode(', ', $columns) . <<<SQL
  83. , reposicion_hora + horario_view.duracion as reposicion_fin
  84. FROM horario_view
  85. JOIN periodo USING (periodo_id)
  86. JOIN registro USING (horario_id)
  87. JOIN reposicion USING (reposicion_id)
  88. JOIN bloque_horario ON (bloque_horario.hora_inicio, bloque_horario.hora_fin) OVERLAPS (reposicion_hora, reposicion_hora + horario_view.duracion)
  89. JOIN profesor USING (profesor_id)
  90. JOIN salon_view as salon_reposicion ON (salon_reposicion.salon_id = reposicion.salon_id)
  91. WHERE
  92. CURRENT_DATE BETWEEN periodo.periodo_fecha_inicio
  93. AND periodo.periodo_fecha_fin
  94. AND reposicion_fecha = CURRENT_DATE
  95. AND bloque_horario.id = :bloque_horario_id
  96. AND salon_reposicion.id_espacio_padre = :id_espacio_sgu
  97. ORDER BY reposicion_hora
  98. SQL,
  99. [
  100. 'bloque_horario_id' => $_GET['bloque_horario_id'],
  101. 'id_espacio_sgu' => $ruta['id_espacio_sgu'],
  102. ]
  103. ),
  104. ],
  105. $ruta,
  106. ),
  107. $data
  108. );
  109. $data = array_filter(
  110. $data,
  111. fn($ruta) => count($ruta['horarios']) > 0 || count($ruta['reposiciones']) > 0
  112. );
  113. echo json_encode(array_values($data), JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
  114. } else {
  115. http_response_code(405);
  116. echo json_encode(['error' => 'method not allowed']);
  117. exit;
  118. }
  119. } catch (PDOException $th) {
  120. http_response_code(500);
  121. echo json_encode([
  122. 'error' => $th->getMessage(),
  123. 'query' => $db->getLastQuery(),
  124. ], JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
  125. exit;
  126. } catch (Exception $th) {
  127. http_response_code(500);
  128. echo json_encode([
  129. 'error' => $th->getMessage(),
  130. ], JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
  131. exit;
  132. }