Programación Web ITI-07

Práctica: Mini Sistema Web Universitario - 2


Práctica: Mini Sistema Web Universitario - 2

Dashboard y estilos generales

Dashboard y estilos generales

dashboard.php

<?php

session_start();

// Verificamos si el usuario esta autenticado
/*
if (!isset($_SESSION["usuario_id"])) {
    header("Location: index.php");
    exit();
}
*/    
// Incluimos la concexion a la Basae de Datos

require_once "config/conexion.php";

// Esto sirve para obtener las tareas del usuario actual
$usuario_id = $_SESSION["usuario_id"];
$stmt = $conexion->prepare(
    "SELECT id, nombre_archivo, nombre_original, fecha, tamano_bytes FROM tareas WHERE usuario_id = ? ORDER BY fecha DESC",
);
$stmt->bind_param("i", $usuario_id);
$stmt->execute();
$resultado = $stmt->get_result();
$tareas = $resultado->fetch_all(MYSQLI_ASSOC);
$stmt->close();

// Esta es una funcion para formatear el tamaño del archivo
function formatBytes($bytes, $precision = 2)
{
    $units = ["B", "KB", "MB", "GB", "TB"];
    $bytes = max($bytes, 0);
    $pow = floor(($bytes ? log($bytes) : 0) / log(1024));
    $pow = min($pow, count($units) - 1);
    $bytes /= 1 << 10 * $pow;
    return round($bytes, $precision) . " " . $units[$pow];
}
?>
<!DOCTYPE html>
<html lang="es">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Dashboard</title>
    <link rel="stylesheet" href="css/style.css">
</head>

<body>
    <!-- Header -->
    <div class="header">
        <div class="header-content">
            <h1>Sistema Universitario - CLA</h1>
            <div class="user-info">
                <span><?php echo htmlspecialchars($_SESSION['nombre_completo'] ?? $_SESSION['usuario']); ?></span>
                <a href="logout.php" class="logout-btn">Cerrar Sesión</a>
            </div>
        </div>
    </div>
    
    <!-- Main Container -->
    <div class="container">
        <!-- Upload Section -->
        <div class="card">
            <h2 class="card-title">Subir Nueva Tarea</h2>
            
            <div id="alertContainer"></div>
            
            <form action="upload.php" method="POST" enctype="multipart/form-data" class="upload-form" id="uploadForm">
                <div class="file-input-wrapper">
                    <label for="archivo" class="file-input-label">
                        <div>
                            <strong>Selecciona un archivo PDF</strong>
                            <br>
                            <small>Tamaño máximo: 5MB</small>
                        </div>
                    </label>
                    <input type="file" id="archivo" name="archivo" accept=".pdf" required>
                    <div class="file-name" id="fileName"></div>
                </div>
                
                <div class="alert alert-info">
                    <strong>Requisitos:</strong> Solo archivos PDF, tamaño máximo 5MB
                </div>
                
                <button type="submit" class="upload-btn" id="submitBtn">
                    Subir Tarea
                </button>
            </form>
        </div>
        
        <!-- Tasks List Section -->
        <div class="card">
            <h2 class="card-title">Mis Tareas Enviadas</h2>
            
            <?php if (count($tareas) > 0): ?>
                <table class="tasks-table">
                    <thead>
                        <tr>
                            <th>#</th>
                            <th>Nombre del Archivo</th>
                            <th>Tamaño</th>
                            <th>Fecha de Envío</th>
                            <th>Acción</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php foreach ($tareas as $index => $tarea): ?>
                            <tr>
                                <td><?php echo $index + 1; ?></td>
                                <td><?php echo htmlspecialchars($tarea['nombre_original']); ?></td>
                                <td><?php echo formatBytes($tarea['tamano_bytes']); ?></td>
                                <td><?php echo date('d/m/Y H:i', strtotime($tarea['fecha'])); ?></td>
                                <td>
                                    <a href="uploads/<?php echo htmlspecialchars($tarea['nombre_archivo']); ?>" 
                                       class="download-link" 
                                       download="<?php echo htmlspecialchars($tarea['nombre_original']); ?>"
                                       target="_blank">
                                        Descargar
                                    </a>
                                </td>
                            </tr>
                        <?php endforeach; ?>
                    </tbody>
                </table>
            <?php else: ?>
                <div class="empty-state">
                    <div class="empty-state-icon"></div>
                    <h3>No hay tareas enviadas</h3>
                    <p>Sube tu primera tarea usando el formulario de arriba</p>
                </div>
            <?php endif; ?>
        </div>
    </div>
    
    <script>
        // Mostrar nombre del archivo seleccionado
        document.getElementById('archivo').addEventListener('change', function(e) {
            const fileName = document.getElementById('fileName');
            const submitBtn = document.getElementById('submitBtn');
            
            if (this.files.length > 0) {
                const file = this.files[0];
                const size = (file.size / (1024 * 1024)).toFixed(2);
                
                fileName.innerHTML = `<strong>Archivo seleccionado:</strong> ${file.name} (${size} MB)`;
                
                // Validar tamaño
                if (file.size > 5 * 1024 * 1024) {
                    fileName.innerHTML += '<br><span style="color: red;">El archivo supera los 5MB</span>';
                    submitBtn.disabled = true;
                } else {
                    submitBtn.disabled = false;
                }
            } else {
                fileName.innerHTML = '';
                submitBtn.disabled = false;
            }
        });
        
        // Mostrar mensaje de éxito/error desde URL
        window.addEventListener('DOMContentLoaded', function() {
            const urlParams = new URLSearchParams(window.location.search);
            const mensaje = urlParams.get('mensaje');
            const error = urlParams.get('error');
            const alertContainer = document.getElementById('alertContainer');
            
            if (mensaje) {
                alertContainer.innerHTML = `<div class="alert alert-success">${decodeURIComponent(mensaje)}</div>`;
                // Limpiar URL
                window.history.replaceState({}, document.title, window.location.pathname);
            }
            
            if (error) {
                alertContainer.innerHTML = `<div class="alert alert-error">${decodeURIComponent(error)}</div>`;
                // Limpiar URL
                window.history.replaceState({}, document.title, window.location.pathname);
            }
        });
    </script>
</body>
</html>

css/style.css

:root {
    --primary-dark-blue: #061856;
    --primary-bold-blue: #134074;
    --primary-blue: #0054A2;
    --primary-light-blue: #5590FF;
    --primary-light: #F2F9FF;
    --neutral-white: #FFFFFF;
    --neutral-black: #000000;
    --neutral-gray: #D8D8D8;
    --neutral-dark-gray: #545454;
    --neutral-light-gray: #EEEEEE;
}

* {
    box-sizing: border-box;
    font-family: Arial, Helvetica, sans-serif;
}

body {
    margin: 0;
    background: #f7f8fa;
    color: #1f2937;
}

h1,
h2,
h3,
p {
    margin: 0;
}

.header {
    width: 100%;
    background: var(--neutral-white);
    border-bottom: 1px solid #e5e7eb;
}

.header-content {
    max-width: 980px;
    margin: 0 auto;
    padding: 14px 20px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
}

.header-content h1 {
    font-size: 20px;
    font-weight: 600;
    color: #111827;
}

.user-info {
    display: flex;
    align-items: center;
    gap: 12px;
    font-size: 14px;
    color: #4b5563;
}

.logout-btn {
    text-decoration: none;
    color: #374151;
    border: 1px solid #d1d5db;
    padding: 6px 10px;
    border-radius: 8px;
    transition: 0.2s ease;
}

.logout-btn:hover {
    background: #f3f4f6;
}

.container {
    max-width: 980px;
    margin: 24px auto;
    padding: 0 20px 24px;
    display: grid;
    gap: 18px;
}

.card {
    background: var(--neutral-white);
    border: 1px solid #e5e7eb;
    border-radius: 12px;
    padding: 18px;
}

.card-title {
    font-size: 18px;
    font-weight: 600;
    margin-bottom: 14px;
    color: #111827;
}

.file-input-wrapper {
    border: 1px dashed #d1d5db;
    border-radius: 10px;
    padding: 16px;
    background: #fafafa;
}

.file-input-label {
    display: flex;
    align-items: center;
    gap: 12px;
    margin-bottom: 10px;
    color: #374151;
    cursor: pointer;
}

input[type="file"] {
    width: 100%;
}

.file-name {
    margin-top: 10px;
    font-size: 14px;
    color: #4b5563;
}

.upload-form {
    display: grid;
    gap: 12px;
}

.upload-btn {
    width: fit-content;
    border: 1px solid #d1d5db;
    background: #111827;
    color: #ffffff;
    padding: 10px 14px;
    border-radius: 8px;
    cursor: pointer;
    font-weight: 600;
}

.upload-btn:hover {
    background: #1f2937;
}

.upload-btn:disabled {
    cursor: not-allowed;
    opacity: 0.6;
}

.alert {
    border-radius: 8px;
    padding: 10px 12px;
    font-size: 14px;
    border: 1px solid;
}

.alert-info {
    background: #eff6ff;
    border-color: #bfdbfe;
    color: #1e3a8a;
}

.alert-success {
    background: #f0fdf4;
    border-color: #bbf7d0;
    color: #166534;
}

.alert-error {
    background: #fef2f2;
    border-color: #fecaca;
    color: #991b1b;
}

.tasks-table {
    width: 100%;
    border-collapse: collapse;
    font-size: 14px;
}

.tasks-table th,
.tasks-table td {
    text-align: left;
    padding: 10px;
    border-bottom: 1px solid #e5e7eb;
}

.tasks-table th {
    font-weight: 600;
    color: #374151;
    background: #fafafa;
}

.download-link {
    text-decoration: none;
    color: #1d4ed8;
    font-weight: 600;
}

.download-link:hover {
    text-decoration: underline;
}

.empty-state {
    text-align: center;
    padding: 24px 10px;
    color: #4b5563;
}

.empty-state-icon {
    font-size: 28px;
    margin-bottom: 10px;
}

.empty-state p {
    margin-top: 6px;
}

@media (max-width: 768px) {
    .header-content {
        flex-direction: column;
        align-items: flex-start;
    }

    .user-info {
        width: 100%;
        justify-content: space-between;
    }

    .tasks-table {
        display: block;
        overflow-x: auto;
        white-space: nowrap;
    }
}

body.login-page {
    min-height: 100vh;
    display: grid;
    place-items: center;
    padding: 20px;
}

.login-container {
    width: 100%;
    max-width: 420px;
    background: #ffffff;
    border: 1px solid #e5e7eb;
    border-radius: 12px;
    padding: 24px;
}

.logo {
    margin-bottom: 18px;
}

.logo h1 {
    font-size: 22px;
    margin-bottom: 6px;
    color: #111827;
}

.logo p {
    color: #6b7280;
    font-size: 14px;
}

.form-group {
    display: grid;
    gap: 6px;
    margin-bottom: 12px;
}

.form-group label {
    font-size: 14px;
    color: #374151;
}

.form-group input {
    width: 100%;
    border: 1px solid #d1d5db;
    border-radius: 8px;
    padding: 10px 12px;
    font-size: 14px;
}

.form-group input:focus {
    outline: none;
    border-color: #9ca3af;
}

.btn-login {
    width: 100%;
    margin-top: 4px;
    border: 1px solid #d1d5db;
    background: #111827;
    color: #ffffff;
    padding: 10px 14px;
    border-radius: 8px;
    cursor: pointer;
    font-weight: 600;
}

.btn-login:hover {
    background: #1f2937;
}

.info-box {
    margin-top: 14px;
    padding-top: 12px;
    border-top: 1px solid #e5e7eb;
    color: #4b5563;
    font-size: 14px;
}

.info-box h3 {
    font-size: 14px;
    color: #111827;
    margin-bottom: 6px;
    text-transform: capitalize;
}