Facultades propias

This commit is contained in:
2025-02-18 01:33:57 +00:00
parent dbc73c02be
commit d83f7599a3

View File

@@ -1,222 +1,222 @@
<?php <?php
$fecha = date('d_m_Y'); $fecha = date('d_m_Y');
/* header("Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"); /* header("Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
header("Content-Disposition: attachment;filename=horario_$fecha.xlsx"); header("Content-Disposition: attachment;filename=horario_$fecha.xlsx");
header("Cache-Control: max-age=0"); */ header("Cache-Control: max-age=0"); */
$ruta = ".."; $ruta = "..";
require_once "$ruta/class/c_login.php"; require_once "$ruta/class/c_login.php";
if (!isset($_SESSION['user'])) { if (!isset($_SESSION['user'])) {
http_response_code(401); http_response_code(401);
die(json_encode(['error' => 'unauthorized'])); die(json_encode(['error' => 'unauthorized']));
} }
$user = unserialize($_SESSION['user']); $user = unserialize($_SESSION['user']);
$json = file_get_contents('php://input'); $json = file_get_contents('php://input');
$params = json_decode($json, true); $params = json_decode($json, true);
$profesor_clave = isset($params['profesor']) $profesor_clave = isset($params['profesor'])
? preg_replace('/[^\d]/', '', $params['profesor']) ? preg_replace('/[^\d]/', '', $params['profesor'])
: null; : null;
$data = $db->query( $data = $db->query(
"SELECT * FROM auditoria_mat "SELECT * FROM auditoria_mat
WHERE facultad_id = COALESCE(:facultad_id, facultad_id) WHERE facultad_id = COALESCE(:facultad_id, facultad_id)
AND profesor_clave = COALESCE(:profesor_clave, profesor_clave) AND profesor_clave = COALESCE(:profesor_clave, profesor_clave)
AND REGISTRO_FECHA_IDEAL + HORARIO_HORA between :fecha_inicio AND :fecha_fin AND REGISTRO_FECHA_IDEAL + HORARIO_HORA between :fecha_inicio AND :fecha_fin
ORDER BY REGISTRO_FECHA_IDEAL, HORARIO_HORA", ORDER BY REGISTRO_FECHA_IDEAL, HORARIO_HORA",
[ [
'facultad_id' => $params['facultad_id'], 'facultad_id' => $user->facultad['facultad_id'],
'profesor_clave' => $profesor_clave, 'profesor_clave' => $profesor_clave,
'fecha_inicio' => $_SESSION['fecha_inicio'], 'fecha_inicio' => $_SESSION['fecha_inicio'],
'fecha_fin' => $_SESSION['fecha_fin'], 'fecha_fin' => $_SESSION['fecha_fin'],
] ]
); );
$estados = empty($params['estados']) ? null : $params['estados']; $estados = empty($params['estados']) ? null : $params['estados'];
$data = array_filter( $data = array_filter(
$data, $data,
fn($registro) => in_array($registro['estado_supervisor_id'], $estados ?? [$registro['estado_supervisor_id']]) fn($registro) => in_array($registro['estado_supervisor_id'], $estados ?? [$registro['estado_supervisor_id']])
); );
$data = array_values($data); $data = array_values($data);
empty($data) and die(json_encode(['error' => 'No se recibieron datos', 'data' => $data])); empty($data) and die(json_encode(['error' => 'No se recibieron datos', 'data' => $data]));
use PhpOffice\PhpSpreadsheet\Spreadsheet; use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Style\Border; use PhpOffice\PhpSpreadsheet\Style\Border;
use PhpOffice\PhpSpreadsheet\Style\Color; use PhpOffice\PhpSpreadsheet\Style\Color;
use PhpOffice\PhpSpreadsheet\Style\Fill; use PhpOffice\PhpSpreadsheet\Style\Fill;
use PhpOffice\PhpSpreadsheet\Style\Alignment; use PhpOffice\PhpSpreadsheet\Style\Alignment;
use PhpOffice\PhpSpreadsheet\IOFactory; use PhpOffice\PhpSpreadsheet\IOFactory;
$spreadsheet = new Spreadsheet(); $spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet(); $sheet = $spreadsheet->getActiveSheet();
// Image settings // Image settings
$drawing = new \PhpOffice\PhpSpreadsheet\Worksheet\Drawing(); $drawing = new \PhpOffice\PhpSpreadsheet\Worksheet\Drawing();
$drawing->setName('La Salle') $drawing->setName('La Salle')
->setDescription('La Salle') ->setDescription('La Salle')
->setPath('../imagenes/logo.png') ->setPath('../imagenes/logo.png')
->setCoordinates('B1') ->setCoordinates('B1')
->setHeight(100) ->setHeight(100)
->setOffsetX(10) ->setOffsetX(10)
->setWorksheet($spreadsheet->getActiveSheet()); ->setWorksheet($spreadsheet->getActiveSheet());
$data_excel = array( $data_excel = array(
"FECHA" => 'registro_fecha_ideal', "FECHA" => 'registro_fecha_ideal',
"CLAVE" => 'profesor_clave', "CLAVE" => 'profesor_clave',
"PROFESOR" => 'profesor_nombre', "PROFESOR" => 'profesor_nombre',
"CORREO" => 'profesor_correo', "CORREO" => 'profesor_correo',
"FACULTAD" => 'facultad', "FACULTAD" => 'facultad',
"MATERIA" => 'materia', "MATERIA" => 'materia',
"CARRERA / PROGRAMA" => 'carrera', "CARRERA / PROGRAMA" => 'carrera',
"GRUPO" => 'horario_grupo', "GRUPO" => 'horario_grupo',
"HORARIO" => 'horario_hora_completa', "HORARIO" => 'horario_hora_completa',
"SALÓN" => 'salon', "SALÓN" => 'salon',
"UNIDAD" => 'salon_array', "UNIDAD" => 'salon_array',
"REGISTRO PROFESOR" => 'asistencia', "REGISTRO PROFESOR" => 'asistencia',
"HORA DE REGISTRO" => 'registro_fecha', "HORA DE REGISTRO" => 'registro_fecha',
"NOMBRE SUPERVISOR" => 'usuario_nombre', "NOMBRE SUPERVISOR" => 'usuario_nombre',
"REGISTRO SUPERVISOR" => 'nombre', "REGISTRO SUPERVISOR" => 'nombre',
"HORA DE SUPERVISIÓN" => 'registro_fecha_supervisor', "HORA DE SUPERVISIÓN" => 'registro_fecha_supervisor',
"OBSERVACIONES" => 'comentario', "OBSERVACIONES" => 'comentario',
"JUSTIFICACIÓN" => 'justificacion', "JUSTIFICACIÓN" => 'justificacion',
); // Same as before ); // Same as before
const ROW = 6; const ROW = 6;
// Merge cells from A1 to C+ ROW // Merge cells from A1 to C+ ROW
$sheet->mergeCells('A1:C' . (ROW - 1)); $sheet->mergeCells('A1:C' . (ROW - 1));
// Merge cells from D1 to size of $data_excel + 1 // Merge cells from D1 to size of $data_excel + 1
$sheet->mergeCells('D1:' . chr(65 + count($data_excel) - 1) . (ROW - 1)); $sheet->mergeCells('D1:' . chr(65 + count($data_excel) - 1) . (ROW - 1));
// Set the title in D1 Sistema de Auditoría de Asistencia // Set the title in D1 Sistema de Auditoría de Asistencia
$sheet->setCellValue('D1', 'Sistema de Auditoría de Asistencia'); $sheet->setCellValue('D1', 'Sistema de Auditoría de Asistencia');
$sheet->getStyle('D1')->applyFromArray([ $sheet->getStyle('D1')->applyFromArray([
'font' => [ 'font' => [
'bold' => true, 'bold' => true,
'size' => 30, 'size' => 30,
'name' => 'Indivisa Text Sans', 'name' => 'Indivisa Text Sans',
'color' => ['argb' => '001d68'], 'color' => ['argb' => '001d68'],
], ],
'alignment' => [ 'alignment' => [
'vertical' => Alignment::VERTICAL_CENTER, 'vertical' => Alignment::VERTICAL_CENTER,
], ],
]); ]);
$lastColumnLetter = chr(65 + count($data_excel) - 1); $lastColumnLetter = chr(65 + count($data_excel) - 1);
$headers_range = 'A' . ROW . ':' . $lastColumnLetter . ROW; $headers_range = 'A' . ROW . ':' . $lastColumnLetter . ROW;
$keys = array_keys($data_excel); $keys = array_keys($data_excel);
array_walk($keys, function ($key, $index) use ($sheet) { array_walk($keys, function ($key, $index) use ($sheet) {
$sheet->setCellValue(chr(65 + $index) . ROW, $key); $sheet->setCellValue(chr(65 + $index) . ROW, $key);
}); });
// Apply the header styles // Apply the header styles
$sheet->getStyle($headers_range)->applyFromArray([ $sheet->getStyle($headers_range)->applyFromArray([
'font' => [ 'font' => [
'bold' => true, 'bold' => true,
'size' => 12, 'size' => 12,
'name' => 'Indivisa Text Sans', 'name' => 'Indivisa Text Sans',
'color' => ['argb' => Color::COLOR_WHITE], 'color' => ['argb' => Color::COLOR_WHITE],
], ],
'alignment' => [ 'alignment' => [
'horizontal' => Alignment::HORIZONTAL_CENTER, 'horizontal' => Alignment::HORIZONTAL_CENTER,
'vertical' => Alignment::VERTICAL_CENTER, 'vertical' => Alignment::VERTICAL_CENTER,
], ],
'fill' => [ 'fill' => [
'fillType' => Fill::FILL_SOLID, 'fillType' => Fill::FILL_SOLID,
'startColor' => ['argb' => '001d68'], 'startColor' => ['argb' => '001d68'],
] ]
]); ]);
// set filters // set filters
$sheet->setAutoFilter($headers_range); $sheet->setAutoFilter($headers_range);
// Styles that are common for all rows can be set outside the loop // Styles that are common for all rows can be set outside the loop
const DEFAULT_FONT = [ const DEFAULT_FONT = [
'size' => 10, 'size' => 10,
'name' => 'Indivisa Text Sans', 'name' => 'Indivisa Text Sans',
'color' => ['argb' => '001d68'] 'color' => ['argb' => '001d68']
]; ];
const DEFAULT_STYLE = [ const DEFAULT_STYLE = [
'alignment' => [ 'alignment' => [
'vertical' => Alignment::VERTICAL_CENTER, 'vertical' => Alignment::VERTICAL_CENTER,
'wrapText' => true, 'wrapText' => true,
], ],
'font' => DEFAULT_FONT, 'font' => DEFAULT_FONT,
'borders' => [ 'borders' => [
'outline' => [ 'outline' => [
'borderStyle' => Border::BORDER_THIN, 'borderStyle' => Border::BORDER_THIN,
'color' => ['argb' => Color::COLOR_WHITE], 'color' => ['argb' => Color::COLOR_WHITE],
] ]
] ]
]; ];
function getFormattedValue($key, $registro) function getFormattedValue($key, $registro)
{ {
return match ($key) { return match ($key) {
'asistencia' => $registro['registro_fecha'] === null 'asistencia' => $registro['registro_fecha'] === null
? "Sin registro" ? "Sin registro"
: ($registro['registro_retardo'] ? "Retardo " : "Asistencia "), : ($registro['registro_retardo'] ? "Retardo " : "Asistencia "),
'registro_fecha', 'registro_fecha',
'registro_fecha_supervisor' => $registro[$key] === null 'registro_fecha_supervisor' => $registro[$key] === null
? 'Sin registro' ? 'Sin registro'
: date('H:i', strtotime($registro[$key])), : date('H:i', strtotime($registro[$key])),
'nombre' => $registro[$key] ?? "Sin registro", 'nombre' => $registro[$key] ?? "Sin registro",
'horario_hora_completa' => "{$registro['horario_hora']} - {$registro['horario_fin']}", 'horario_hora_completa' => "{$registro['horario_hora']} - {$registro['horario_fin']}",
'usuario_nombre' => $registro[$key] ?? "Sin registro", 'usuario_nombre' => $registro[$key] ?? "Sin registro",
'salon_array' => $registro[$key], 'salon_array' => $registro[$key],
'justificacion', 'justificacion',
'comentario' => $registro[$key] ?? "Sin registro", 'comentario' => $registro[$key] ?? "Sin registro",
'horario_grupo' => $registro[$key] ?? "Sin registro", 'horario_grupo' => $registro[$key] ?? "Sin registro",
'profesor_correo' => $registro[$key] ?? "Sin correo", 'profesor_correo' => $registro[$key] ?? "Sin correo",
default => $registro[$key] default => $registro[$key]
}; };
} }
foreach ($data as $index => $registro) { foreach ($data as $index => $registro) {
$pair = $index % 2 == 0; $pair = $index % 2 == 0;
$cellRange = 'A' . (ROW + $index + 1) . ':' . $lastColumnLetter . (ROW + $index + 1); $cellRange = 'A' . (ROW + $index + 1) . ':' . $lastColumnLetter . (ROW + $index + 1);
$styleArray = DEFAULT_STYLE; $styleArray = DEFAULT_STYLE;
$styleArray['fill'] = [ $styleArray['fill'] = [
'fillType' => Fill::FILL_SOLID, 'fillType' => Fill::FILL_SOLID,
'startColor' => ['argb' => $pair ? 'd4d9dd' : 'f6f7f8'], 'startColor' => ['argb' => $pair ? 'd4d9dd' : 'f6f7f8'],
]; ];
$sheet->getStyle($cellRange)->applyFromArray($styleArray); $sheet->getStyle($cellRange)->applyFromArray($styleArray);
$values = array_values($data_excel); $values = array_values($data_excel);
array_walk($values, function ($row, $column_index) use ($sheet, $index, $registro) { array_walk($values, function ($row, $column_index) use ($sheet, $index, $registro) {
$value = getFormattedValue($row, $registro); $value = getFormattedValue($row, $registro);
$cellLocation = chr(65 + $column_index) . (ROW + $index + 1); $cellLocation = chr(65 + $column_index) . (ROW + $index + 1);
$sheet->setCellValue($cellLocation, $value); $sheet->setCellValue($cellLocation, $value);
if (in_array($value, ["Sin registro", "Sin asignar", "Sin correo"])) { if (in_array($value, ["Sin registro", "Sin asignar", "Sin correo"])) {
$sheet->getStyle($cellLocation)->applyFromArray(['font' => ['color' => ['argb' => 'ea0029']]]); $sheet->getStyle($cellLocation)->applyFromArray(['font' => ['color' => ['argb' => 'ea0029']]]);
} }
}); });
} }
foreach ($sheet->getColumnIterator() as $column) { foreach ($sheet->getColumnIterator() as $column) {
$colIndex = $column->getColumnIndex(); $colIndex = $column->getColumnIndex();
if (in_array($colIndex, ['Q', 'R'])) { if (in_array($colIndex, ['Q', 'R'])) {
$sheet->getColumnDimension($colIndex)->setWidth(100); // Ajusta el valor según el tamaño deseado $sheet->getColumnDimension($colIndex)->setWidth(100); // Ajusta el valor según el tamaño deseado
} else { } else {
$sheet->getColumnDimension($colIndex)->setAutoSize(true); $sheet->getColumnDimension($colIndex)->setAutoSize(true);
} }
} }
$writer = IOFactory::createWriter($spreadsheet, 'Xlsx'); $writer = IOFactory::createWriter($spreadsheet, 'Xlsx');
$writer->save('php://output'); $writer->save('php://output');