action_justificar.php 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. <?
  2. header('Content-Type: application/json charset=utf-8');
  3. #return html
  4. $ruta = "../";
  5. require_once "../class/c_login.php";
  6. // check method
  7. if (!isset($_SESSION['user'])) {
  8. http_response_code(401);
  9. echo json_encode(['error' => 'unauthorized']);
  10. exit;
  11. }
  12. $user = unserialize($_SESSION['user']);
  13. try {
  14. if ($_SERVER['REQUEST_METHOD'] === 'PUT') {
  15. // check parameters
  16. $raw = file_get_contents('php://input');
  17. $post_data = json_decode($raw, true);
  18. // if it's a list
  19. // step 1: get subrutas
  20. if (empty($post_data)) {
  21. http_response_code(400);
  22. echo json_encode(['error' => 'No hay clases pendientes']);
  23. exit;
  24. }
  25. if (!(isset($post_data['fecha'], $post_data['bloques'], $post_data['justificacion']))) {
  26. http_response_code(400);
  27. echo json_encode(['error' => 'Faltan parametros']);
  28. exit;
  29. }
  30. $bloques = $db
  31. ->where('id', $post_data['bloques'])
  32. ->orderBy('hora_inicio')
  33. ->get('bloque_horario', null, 'hora_inicio, hora_fin');
  34. $min_hora_inicio = $bloques[0]['hora_inicio'];
  35. $max_hora_fin = $bloques[count($bloques) - 1]['hora_fin'];
  36. $pdo->beginTransaction();
  37. $data = $db->query(
  38. "INSERT INTO registro (horario_id, registro_fecha_ideal, profesor_id, justificador_id, justificacion, registro_fecha_justificacion, registro_justificada)
  39. SELECT DISTINCT
  40. horario_id, :fecha::DATE, profesor_id, :justificador_id::INT, :justificacion, NOW(), true
  41. from horario_view
  42. join horario_profesor using (horario_id)
  43. where
  44. (:hora_inicio::TIME, :hora_fin::TIME) OVERLAPS (horario_hora, horario_fin) AND
  45. horario_dia = EXTRACT(DOW FROM :fecha::DATE) AND
  46. periodo_id = :periodo_id AND
  47. (horario_view.facultad_id = :facultad_id OR :facultad_id IS NULL)
  48. ON CONFLICT (horario_id, registro_fecha_ideal, profesor_id) DO UPDATE SET
  49. justificador_id = :justificador_id,
  50. justificacion = :justificacion,
  51. registro_fecha_justificacion = NOW(),
  52. registro_justificada = true
  53. RETURNING *;",
  54. array(
  55. 'justificador_id' => $user->user['id'],
  56. 'justificacion' => empty($post_data['justificacion']) ? null : $post_data['justificacion'],
  57. 'fecha' => $post_data['fecha'],
  58. 'periodo_id' => $user->periodo_id,
  59. 'facultad_id' => $user->facultad['facultad_id'],
  60. 'hora_inicio' => $min_hora_inicio,
  61. 'hora_fin' => $max_hora_fin,
  62. )
  63. );
  64. $pdo->commit();
  65. echo json_encode($data, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
  66. } else {
  67. http_response_code(405);
  68. echo json_encode(['error' => 'method not allowed']);
  69. exit;
  70. }
  71. } catch (PDOException $th) {
  72. http_response_code(500);
  73. echo json_encode([
  74. 'error' => $th->getMessage(),
  75. 'query' => $db->getLastQuery(),
  76. 'post_data' => $post_data,
  77. ], JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
  78. $pdo->rollBack();
  79. exit;
  80. } catch (Exception $th) {
  81. http_response_code(500);
  82. echo json_encode([
  83. 'error' => $th->getMessage(),
  84. ], JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
  85. exit;
  86. }