profesor_faltas.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <?
  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 'GET':
  13. $facultad = $_GET['facultad'] ?? $user->facultad['facultad_id'] ?? null;
  14. $porcentaje = $_GET['porcentaje'] ?? null;
  15. $faltas = $_GET['faltas'] ?? null;
  16. if (!isset($facultad) || !is_numeric($facultad)) {
  17. $error = 'No se ha seleccionado una facultad';
  18. } else if ((!isset($faltas) || !is_numeric($faltas)) && (!isset($porcentaje) || !is_numeric($porcentaje))) {
  19. $error = 'Debe especificar las faltas o el porcentaje';
  20. } else if (isset($faltas) && (!is_numeric($faltas) || $faltas <= 0)) {
  21. $error = 'Las faltas deben ser un número mayor a 0';
  22. } else if (isset($porcentaje) && (!is_numeric($porcentaje) || $porcentaje <= 0)) {
  23. $error = 'El porcentaje debe ser un número mayor a 0';
  24. } else if (isset($faltas) && isset($porcentaje)) {
  25. $error = 'No se puede especificar las faltas y el porcentaje al mismo tiempo';
  26. } else if (!isset($facultad) || !is_numeric($facultad)) {
  27. $error = 'Debe especificar una facultad';
  28. }
  29. if (isset($error)) {
  30. header('HTTP/1.1 400 Bad Request');
  31. echo json_encode(['error' => $error]);
  32. exit();
  33. }
  34. // Initialize the data array
  35. $data = array();
  36. // Check if 'profesor' or 'supervisor' is set and prepare the specific part of the SQL query accordingly.
  37. if (isset($_GET['profesor']) || isset($_GET['supervisor'])) {
  38. $condition = isset($_GET['profesor'])
  39. ? "r.registro_fecha IS NULL AND NOT COALESCE(r.registro_justificada, FALSE)"
  40. : "estado_supervisor_id = 2";
  41. $filter = isset($faltas)
  42. ? "afcp.faltas >= :faltas"
  43. : "afcp.porcentaje >= :porcentaje";
  44. // Prepare the SQL query with placeholders for parameters
  45. $data = array_column($db->query(
  46. "WITH fechas AS (
  47. SELECT
  48. fcc.registro_fecha_ideal,
  49. fcc.horario_id,
  50. hp.profesor_id
  51. FROM fechas_clase_cache fcc
  52. JOIN horario_profesor hp USING (horario_id)
  53. JOIN horario h USING (horario_id)
  54. WHERE (h.PERIODO_ID, h.FACULTAD_ID) = (:periodo_id, :facultad_id) and profesor_id <> 0
  55. ),
  56. asistencia_faltas AS (
  57. SELECT
  58. f.profesor_id,
  59. COUNT(1) AS total,
  60. COUNT(1) FILTER (WHERE $condition AND f.registro_fecha_ideal <= current_date) AS faltas
  61. FROM fechas f
  62. LEFT JOIN registro r USING (registro_fecha_ideal, horario_id, profesor_id)
  63. GROUP BY f.profesor_id
  64. ),
  65. asistencia_faltas_con_porcentaje AS (
  66. SELECT
  67. af.profesor_id,
  68. af.faltas,
  69. af.total,
  70. CASE
  71. WHEN af.total > 0 THEN ROUND((af.faltas::NUMERIC / af.total) * 100, 2)
  72. ELSE NULL
  73. END AS porcentaje
  74. FROM asistencia_faltas af
  75. WHERE af.faltas > 0
  76. )
  77. SELECT
  78. json_build_object(
  79. 'profesor', json_build_object(
  80. 'profesor_nombre', p.profesor_nombre,
  81. 'profesor_clave', p.profesor_clave,
  82. 'profesor_correo', p.profesor_correo
  83. ),
  84. 'profesor_id', afcp.profesor_id,
  85. 'faltas', afcp.faltas,
  86. 'total', afcp.total,
  87. 'porcentaje', afcp.porcentaje
  88. ) AS result_json
  89. FROM asistencia_faltas_con_porcentaje afcp
  90. JOIN profesor p USING (profesor_id)
  91. WHERE $filter
  92. ORDER BY afcp.porcentaje DESC",
  93. [
  94. 'periodo_id' => $user->periodo_id,
  95. 'facultad_id' => $facultad,
  96. ] + (isset($faltas)
  97. ? ['faltas' => $faltas]
  98. : ['porcentaje' => $porcentaje])
  99. ), 'result_json');
  100. } else {
  101. // Send a 400 Bad Request header and an error message in JSON format
  102. header('HTTP/1.1 400 Bad Request');
  103. echo json_encode(['error' => 'Especifique si las faltas son de profesor o supervisor']);
  104. exit();
  105. }
  106. if (empty($data)) {
  107. header('HTTP/1.1 404 Not Found');
  108. echo json_encode(['error' => 'No se encontraron faltas']);
  109. } else {
  110. echo json_encode(
  111. array_map(fn($item) => json_decode($item), $data)
  112. );
  113. }
  114. break;
  115. default:
  116. header('HTTP/1.1 405 Method Not Allowed');
  117. echo json_encode(['error' => 'Método no permitido']);
  118. break;
  119. }
  120. } catch (PDOException $e) {
  121. echo json_encode([
  122. 'error' => $e->getMessage(),
  123. 'query' => $db->getLastQuery(),
  124. ]);
  125. }