rutas_salón_horario.php 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. <?
  2. #input $_GET['id_espacio_sgu']
  3. $information = [
  4. 'GET' => [
  5. 'id_espacio_sgu',
  6. 'bloque_horario_id',
  7. ],
  8. ];
  9. #output rutas: [ ...ruta, salones: [{...salon}] ]
  10. header('Content-Type: application/json charset=utf-8');
  11. $ruta = "../";
  12. require_once "../class/c_login.php";
  13. // check method
  14. try {
  15. if ($_SERVER['REQUEST_METHOD'] === 'GET') {
  16. // check parameters
  17. array_walk($information['GET'], function ($value) {
  18. if (!array_key_exists($value, $_GET)) {
  19. http_response_code(400);
  20. echo json_encode(['error' => "$value is required"]);
  21. exit;
  22. }
  23. });
  24. // step 1: get subrutas
  25. $data = $db
  26. ->where('tiene_salones')
  27. ->where("{$_GET['id_espacio_sgu']} = ANY(id_espacio_sgu_array)")
  28. ->get('salon_view');
  29. // step 3: get horarios
  30. $fecha = "'2023-10-13':DATE";
  31. $data = array_map(
  32. fn($ruta) => array_merge(
  33. [
  34. 'horarios' => $db
  35. ->join('periodo', 'periodo.periodo_id = horario_view.periodo_id')
  36. ->join('bloque_horario', '(bloque_horario.hora_inicio, bloque_horario.hora_fin) OVERLAPS (horario_view.horario_hora, horario_view.horario_hora + horario_view.duracion)')
  37. ->join('salon_view', 'salon_view.salon_id = horario_view.salon_id')
  38. ->join('horario_profesor', 'horario_profesor.horario_id = horario_view.horario_id')
  39. ->join('profesor', 'profesor.profesor_id = horario_profesor.profesor_id')
  40. ->join('registro', "(registro.profesor_id, registro.horario_id, registro.registro_fecha_ideal) = (profesor.profesor_id, horario_view.horario_id, $fecha)", 'LEFT')
  41. ->where("$fecha BETWEEN periodo.periodo_fecha_inicio AND periodo.periodo_fecha_fin")
  42. ->where("horario_dia = EXTRACT(DOW FROM $fecha)")
  43. ->where('bloque_horario.id', $_GET['bloque_horario_id'])
  44. ->where('id_espacio_padre', $ruta['id_espacio_sgu'])
  45. ->get('horario_view', null, '*, horario_view.horario_id, profesor.profesor_id'),
  46. ],
  47. $ruta
  48. ),
  49. $data
  50. );
  51. echo json_encode($data, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
  52. } else {
  53. http_response_code(405);
  54. echo json_encode(['error' => 'method not allowed']);
  55. exit;
  56. }
  57. } catch (PDOException $th) {
  58. http_response_code(500);
  59. echo json_encode([
  60. 'error' => $th->getMessage(),
  61. // 'query' => $db->getLastQuery(),
  62. ], JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
  63. exit;
  64. } catch (Exception $th) {
  65. http_response_code(500);
  66. echo json_encode([
  67. 'error' => $th->getMessage(),
  68. ], JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
  69. exit;
  70. }