rutas_salón_horario.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. ->orderBy('horario_hora')
  77. ->orderBy('salon_view.salon')
  78. ->get(
  79. 'horario_view',
  80. columns: $columns
  81. ),
  82. // 'query' => $db->getLastQuery(),
  83. 'reposiciones' => $db->query(
  84. 'SELECT ' . implode(', ', $columns) . <<<SQL
  85. , reposicion_hora + horario_view.duracion as reposicion_fin
  86. FROM horario_view
  87. JOIN periodo USING (periodo_id)
  88. JOIN registro USING (horario_id)
  89. JOIN reposicion USING (reposicion_id)
  90. JOIN bloque_horario ON (bloque_horario.hora_inicio, bloque_horario.hora_fin) OVERLAPS (reposicion_hora, reposicion_hora + horario_view.duracion)
  91. JOIN profesor USING (profesor_id)
  92. JOIN salon_view as salon_reposicion ON (salon_reposicion.salon_id = reposicion.salon_id)
  93. WHERE
  94. CURRENT_DATE BETWEEN periodo.periodo_fecha_inicio
  95. AND periodo.periodo_fecha_fin
  96. AND reposicion_fecha = CURRENT_DATE
  97. AND bloque_horario.id = :bloque_horario_id
  98. AND salon_reposicion.id_espacio_padre = :id_espacio_sgu
  99. ORDER BY reposicion_hora
  100. SQL,
  101. [
  102. 'bloque_horario_id' => $_GET['bloque_horario_id'],
  103. 'id_espacio_sgu' => $ruta['id_espacio_sgu'],
  104. ]
  105. ),
  106. ],
  107. $ruta,
  108. ),
  109. $data
  110. );
  111. $data = array_filter(
  112. $data,
  113. fn($ruta) => count($ruta['horarios']) > 0 || count($ruta['reposiciones']) > 0
  114. );
  115. echo json_encode(array_values($data), JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
  116. } else {
  117. http_response_code(405);
  118. echo json_encode(['error' => 'method not allowed']);
  119. exit;
  120. }
  121. } catch (PDOException $th) {
  122. http_response_code(500);
  123. echo json_encode([
  124. 'error' => $th->getMessage(),
  125. 'query' => $db->getLastQuery(),
  126. ], JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
  127. exit;
  128. } catch (Exception $th) {
  129. http_response_code(500);
  130. echo json_encode([
  131. 'error' => $th->getMessage(),
  132. ], JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
  133. exit;
  134. }