action_justificar.php 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. $data = $db->querySingle(
  26. 'INSERT INTO registro (profesor_id, horario_id, registro_fecha_ideal, registro_justificada, justificador_id, registro_fecha_justificacion, justificacion)
  27. VALUES (:profesor_id, :horario_id, :registro_fecha_ideal, :registro_justificada, :justificador_id, NOW(), :justificacion)
  28. ON CONFLICT (profesor_id, horario_id, registro_fecha_ideal)
  29. DO UPDATE SET registro_justificada = :registro_justificada, justificador_id = :justificador_id, registro_fecha_justificacion = NOW(), justificacion = :justificacion
  30. RETURNING *',
  31. array(
  32. 'profesor_id' => $post_data['profesor_id'],
  33. 'horario_id' => $post_data['horario_id'],
  34. 'registro_fecha_ideal' => $post_data['registro_fecha_ideal'],
  35. 'registro_justificada' => $post_data['registro_justificada'],
  36. 'justificador_id' => $user->user['id'],
  37. 'justificacion' => empty($post_data['justificacion']) ? null : $post_data['justificacion'],
  38. )
  39. );
  40. $data_justificador = $db->querySingle(
  41. "SELECT justificador.usuario_nombre as justificador_nombre,
  42. justificador.usuario_clave as justificador_clave,
  43. facultad.facultad_nombre as justificador_facultad, rol.rol_titulo as justificador_rol
  44. FROM USUARIO JUSTIFICADOR
  45. JOIN ROL on ROL.rol_id = justificador.rol_id
  46. LEFT JOIN facultad on facultad.facultad_id = justificador.facultad_id
  47. where justificador.usuario_id = :justificador_id",
  48. array(
  49. 'justificador_id' => $user->user['id'],
  50. )
  51. );
  52. echo json_encode(array_merge($data, $data_justificador), JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
  53. } else {
  54. http_response_code(405);
  55. echo json_encode(['error' => 'method not allowed']);
  56. exit;
  57. }
  58. } catch (PDOException $th) {
  59. http_response_code(500);
  60. echo json_encode([
  61. 'error' => $th->getMessage(),
  62. 'query' => $db->getLastQuery(),
  63. 'post_data' => $post_data,
  64. ], JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
  65. exit;
  66. } catch (Exception $th) {
  67. http_response_code(500);
  68. echo json_encode([
  69. 'error' => $th->getMessage(),
  70. ], JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
  71. exit;
  72. }