Files
paad/action/profesor_faltas.php
2024-10-07 19:31:30 +00:00

136 lines
6.0 KiB
PHP

<?
require_once "{$_SERVER['DOCUMENT_ROOT']}/class/c_login.php";
header('Content-Type: application/json');
if (!Login::is_logged()) {
header('HTTP/1.1 401 Unauthorized');
echo json_encode(['error' => 'No se ha iniciado sesión']);
exit();
}
$user = Login::get_user();
try {
switch ($_SERVER['REQUEST_METHOD']) {
case 'GET':
$facultad = $_GET['facultad'] ?? $user->facultad['facultad_id'] ?? null;
$porcentaje = $_GET['porcentaje'] ?? null;
$faltas = $_GET['faltas'] ?? null;
if (!isset($facultad) || !is_numeric($facultad)) {
$error = 'No se ha seleccionado una facultad';
} else if ((!isset($faltas) || !is_numeric($faltas)) && (!isset($porcentaje) || !is_numeric($porcentaje))) {
$error = 'Debe especificar las faltas o el porcentaje';
} else if (isset($faltas) && (!is_numeric($faltas) || $faltas <= 0)) {
$error = 'Las faltas deben ser un número mayor a 0';
} else if (isset($porcentaje) && (!is_numeric($porcentaje) || $porcentaje <= 0)) {
$error = 'El porcentaje debe ser un número mayor a 0';
} else if (isset($faltas) && isset($porcentaje)) {
$error = 'No se puede especificar las faltas y el porcentaje al mismo tiempo';
} else if (!isset($facultad) || !is_numeric($facultad)) {
$error = 'Debe especificar una facultad';
}
if (isset($error)) {
header('HTTP/1.1 400 Bad Request');
echo json_encode(['error' => $error]);
exit();
}
// Initialize the data array
$data = array();
// Check if 'profesor' or 'supervisor' is set and prepare the specific part of the SQL query accordingly.
if (isset($_GET['profesor']) || isset($_GET['supervisor'])) {
$condition = isset($_GET['profesor'])
? "r.registro_fecha IS NULL AND NOT COALESCE(r.registro_justificada, FALSE)"
: "estado_supervisor_id = 2";
$filter = isset($faltas)
? "afcp.faltas >= :faltas"
: "afcp.porcentaje >= :porcentaje";
// Prepare the SQL query with placeholders for parameters
$data = array_column($db->query(
"WITH fechas AS (
SELECT
h.horario_id,
fechas_clase(h.horario_id, true) AS registro_fecha_ideal,
hp.profesor_id
FROM horario h
JOIN horario_profesor hp USING (horario_id)
WHERE (h.PERIODO_ID, h.FACULTAD_ID) = (:periodo_id, :facultad_id) AND hp.profesor_id <> 0
),
asistencia_faltas AS (
SELECT
f.profesor_id,
COUNT(1) AS total,
COUNT(1) FILTER (WHERE $condition AND f.registro_fecha_ideal <= current_date) AS faltas
FROM fechas f
LEFT JOIN registro r USING (registro_fecha_ideal, horario_id, profesor_id)
GROUP BY f.profesor_id
),
asistencia_faltas_con_porcentaje AS (
SELECT
af.profesor_id,
af.faltas,
af.total,
CASE
WHEN af.total > 0 THEN ROUND((af.faltas::NUMERIC / af.total) * 100, 2)
ELSE NULL
END AS porcentaje
FROM asistencia_faltas af
WHERE af.faltas > 0
)
SELECT
json_build_object(
'profesor', json_build_object(
'profesor_nombre', p.profesor_nombre,
'profesor_clave', p.profesor_clave,
'profesor_correo', p.profesor_correo
),
'profesor_id', afcp.profesor_id,
'faltas', afcp.faltas,
'total', afcp.total,
'porcentaje', afcp.porcentaje
) AS result_json
FROM asistencia_faltas_con_porcentaje afcp
JOIN profesor p USING (profesor_id)
WHERE $filter
ORDER BY afcp.porcentaje DESC;
",
[
'periodo_id' => $user->periodo_id,
'facultad_id' => $facultad,
] + (isset($faltas)
? ['faltas' => $faltas]
: ['porcentaje' => $porcentaje])
), 'result_json');
} else {
// Send a 400 Bad Request header and an error message in JSON format
header('HTTP/1.1 400 Bad Request');
echo json_encode(['error' => 'Especifique si las faltas son de profesor o supervisor']);
exit();
}
if (empty($data)) {
header('HTTP/1.1 404 Not Found');
echo json_encode(['error' => 'No se encontraron faltas']);
} else {
echo json_encode(
array_map(fn($item) => json_decode($item), $data)
);
}
break;
default:
header('HTTP/1.1 405 Method Not Allowed');
echo json_encode(['error' => 'Método no permitido']);
break;
}
} catch (PDOException $e) {
echo json_encode([
'error' => $e->getMessage(),
'query' => $db->getLastQuery(),
]);
}