rutas_salón_horario.php 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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. $data = array_map(
  31. fn($ruta) => array_merge(
  32. [
  33. 'horarios' => $db
  34. ->join('periodo', 'periodo.periodo_id = horario_view.periodo_id')
  35. ->join('bloque_horario', '(bloque_horario.hora_inicio, bloque_horario.hora_fin) OVERLAPS (horario_view.horario_hora, horario_view.horario_hora + horario_view.duracion)')
  36. ->join('salon_view', 'salon_view.salon_id = horario_view.salon_id')
  37. ->join('horario_profesor', 'horario_profesor.horario_id = horario_view.horario_id')
  38. ->join('profesor', 'profesor.profesor_id = horario_profesor.profesor_id')
  39. ->join('registro', '(registro.profesor_id, registro.horario_id, registro.registro_fecha_ideal) = (profesor.profesor_id, horario_view.horario_id, CURRENT_DATE)', 'LEFT')
  40. ->where('CURRENT_DATE BETWEEN periodo.periodo_fecha_inicio AND periodo.periodo_fecha_fin')
  41. ->where('horario_dia = EXTRACT(DOW FROM CURRENT_DATE)')
  42. ->where('bloque_horario.id', $_GET['bloque_horario_id'])
  43. ->where('id_espacio_padre', $ruta['id_espacio_sgu'])
  44. ->get('horario_view', null, '*, horario_view.horario_id, profesor.profesor_id'),
  45. ],
  46. $ruta
  47. ),
  48. $data
  49. );
  50. echo json_encode($data, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
  51. } else {
  52. http_response_code(405);
  53. echo json_encode(['error' => 'method not allowed']);
  54. exit;
  55. }
  56. } catch (PDOException $th) {
  57. http_response_code(500);
  58. echo json_encode([
  59. 'error' => $th->getMessage(),
  60. 'query' => $db->getLastQuery(),
  61. ], JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
  62. exit;
  63. } catch (Exception $th) {
  64. http_response_code(500);
  65. echo json_encode([
  66. 'error' => $th->getMessage(),
  67. ], JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
  68. exit;
  69. }