rutas_salón_horario.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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_mat', 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. 'reposicion_hora_fin',
  59. 'salon_reposicion.salon as reposicion_salon',
  60. ];
  61. $fecha = ($_GET['fecha'] != 'null') ? ("'{$_GET['fecha']}'" ?: 'CURRENT_DATE') : 'CURRENT_DATE';
  62. $data = array_map(
  63. fn($ruta) => array_merge(
  64. [
  65. 'horarios' => $db->query(
  66. "SELECT " . implode(', ', $columns) . <<<SQL
  67. FROM horario_view
  68. NATURAL JOIN periodo
  69. JOIN bloque_horario ON (bloque_horario.hora_inicio, bloque_horario.hora_fin) OVERLAPS (horario_view.horario_hora, horario_view.horario_hora + horario_view.duracion)
  70. NATURAL JOIN salon_view_mat
  71. NATURAL JOIN horario_profesor
  72. NATURAL JOIN profesor
  73. LEFT JOIN registro ON (registro.profesor_id, registro.horario_id, registro.registro_fecha_ideal) = (profesor.profesor_id, horario_view.horario_id, $fecha::DATE)
  74. NATURAL LEFT JOIN reposicion
  75. LEFT JOIN salon AS salon_reposicion ON salon_reposicion.salon_id = reposicion.salon_id
  76. WHERE $fecha::DATE BETWEEN periodo.periodo_fecha_inicio AND periodo.periodo_fecha_fin
  77. AND horario_dia = EXTRACT(DOW FROM $fecha::DATE)
  78. AND bloque_horario.id = :bloque_horario_id
  79. AND salon_view_mat.id_espacio_padre = :id_espacio_sgu
  80. ORDER BY horario_hora, salon_view_mat.salon;
  81. SQL,
  82. [
  83. 'bloque_horario_id' => $_GET['bloque_horario_id'],
  84. 'id_espacio_sgu' => $ruta['id_espacio_sgu'],
  85. ]
  86. ),
  87. // 'query' => $db->getLastQuery(),
  88. 'reposiciones' => /*$db->query(
  89. 'SELECT ' . implode(', ', $columns) . <<<SQL
  90. , reposicion_hora + horario_view.duracion as reposicion_fin, registro_fecha_ideal
  91. FROM horario_view
  92. NATURAL JOIN periodo
  93. NATURAL JOIN registro
  94. NATURAL JOIN reposicion
  95. JOIN bloque_horario ON (bloque_horario.hora_inicio, bloque_horario.hora_fin) OVERLAPS (reposicion_hora, reposicion_hora + horario_view.duracion)
  96. NATURAL JOIN profesor
  97. JOIN salon_view_mat as salon_reposicion ON (salon_reposicion.salon_id = reposicion.salon_id)
  98. WHERE
  99. $fecha::DATE BETWEEN periodo.periodo_fecha_inicio
  100. AND periodo.periodo_fecha_fin
  101. AND reposicion_fecha = $fecha::DATE
  102. AND bloque_horario.id = :bloque_horario_id
  103. AND salon_reposicion.id_espacio_padre = :id_espacio_sgu
  104. ORDER BY reposicion_hora
  105. SQL,
  106. [
  107. 'bloque_horario_id' => $_GET['bloque_horario_id'],
  108. 'id_espacio_sgu' => $ruta['id_espacio_sgu'],
  109. ]
  110. )*/ [],
  111. ],
  112. $ruta,
  113. ),
  114. $data
  115. );
  116. $data = array_filter(
  117. $data,
  118. fn($ruta) => count($ruta['horarios']) > 0 || count($ruta['reposiciones']) > 0
  119. );
  120. echo json_encode(array_values($data), JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
  121. } else {
  122. http_response_code(405);
  123. echo json_encode(['error' => 'method not allowed']);
  124. exit;
  125. }
  126. } catch (PDOException $th) {
  127. http_response_code(500);
  128. echo json_encode([
  129. 'error' => $th->getMessage(),
  130. 'query' => $db->getLastQuery(),
  131. ], JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
  132. exit;
  133. } catch (Exception $th) {
  134. http_response_code(500);
  135. echo json_encode([
  136. 'error' => $th->getMessage(),
  137. ], JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
  138. exit;
  139. }