justificar.php 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. <?php
  2. require_once "{$_SERVER['DOCUMENT_ROOT']}/class/c_login.php";
  3. header('Content-Type: application/json');
  4. if (!Login::is_logged()) {
  5. header('HTTP/1.1 401 Unauthorized');
  6. echo json_encode(['error' => 'No se ha iniciado sesión']);
  7. exit();
  8. }
  9. $user = Login::get_user();
  10. try {
  11. switch ($_SERVER['REQUEST_METHOD']) {
  12. case 'POST':
  13. // check parameters
  14. $raw = file_get_contents('php://input');
  15. $post_data = json_decode($raw, true);
  16. $data = $db->querySingle(
  17. 'WITH HORARIOS AS (
  18. SELECT *
  19. FROM horario
  20. JOIN horario_profesor USING (horario_id)
  21. WHERE horario.periodo_id = :periodo_id
  22. )
  23. INSERT INTO registro (profesor_id, horario_id, registro_fecha_ideal, registro_justificada, justificador_id, registro_fecha_justificacion, justificacion)
  24. VALUES (:profesor_id, :horario_id, :registro_fecha_ideal, :registro_justificada, :justificador_id, NOW(), :justificacion)
  25. ON CONFLICT (profesor_id, horario_id, registro_fecha_ideal)
  26. DO UPDATE SET registro_justificada = :registro_justificada, justificador_id = :justificador_id, registro_fecha_justificacion = NOW(), justificacion = :justificacion
  27. RETURNING *',
  28. array(
  29. 'periodo_id' => $user->periodo_id,
  30. 'profesor_id' => $post_data['profesor_id'],
  31. 'horario_id' => $post_data['horario_id'],
  32. 'registro_fecha_ideal' => $post_data['registro_fecha_ideal'],
  33. 'registro_justificada' => $post_data['registro_justificada'],
  34. 'justificador_id' => $user->user['id'],
  35. 'justificacion' => empty($post_data['justificacion']) ? null : $post_data['justificacion'],
  36. )
  37. );
  38. $data_justificador = $db->querySingle(
  39. "SELECT justificador.usuario_nombre as justificador_nombre,
  40. justificador.usuario_clave as justificador_clave,
  41. facultad.facultad_nombre as justificador_facultad, rol.rol_titulo as justificador_rol
  42. FROM USUARIO JUSTIFICADOR
  43. JOIN ROL on ROL.rol_id = justificador.rol_id
  44. LEFT JOIN facultad on facultad.facultad_id = justificador.facultad_id
  45. where justificador.usuario_id = :justificador_id",
  46. array(
  47. 'justificador_id' => $user->user['id'],
  48. )
  49. );
  50. // exit('exit');
  51. echo json_encode(array_merge($data, $data_justificador), JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
  52. break;
  53. default:
  54. header('HTTP/1.1 405 Method Not Allowed');
  55. echo json_encode(['error' => 'Método no permitido']);
  56. }
  57. } catch (PDOException $e) {
  58. echo json_encode([
  59. 'error' => $e->getMessage(),
  60. 'query' => $db->getLastQuery(),
  61. 'exception' => $e->getTraceAsString()
  62. ]);
  63. } catch (Exception $e) {
  64. echo json_encode([
  65. 'error' => $e->getMessage(),
  66. 'exception' => $e->getTraceAsString()
  67. ]);
  68. }