diff --git a/public/phototeachers/qrcodes/7-5d516ce9cdb43b39.png b/public/phototeachers/qrcodes/7-5d516ce9cdb43b39.png new file mode 100644 index 0000000..037682e Binary files /dev/null and b/public/phototeachers/qrcodes/7-5d516ce9cdb43b39.png differ diff --git a/public/userarea/include/headscript.php b/public/userarea/include/headscript.php index 693ec61..5738c9a 100644 --- a/public/userarea/include/headscript.php +++ b/public/userarea/include/headscript.php @@ -9,8 +9,9 @@ error_reporting(E_ALL | E_STRICT); include('../../extra/auth.php'); -if (! Auth::check()) { +if (!Auth::check()) { redirectTo('../../public/login'); + exit; } $user = Auth::user(); @@ -20,42 +21,65 @@ $nameuser = $user->present()->first_name; $surnameuser = $user->present()->last_name; $emailuser = $user->present()->email; $avatar = $user->present()->avatar; -$kindofrole = $user->present()->role_id; // <-- Questo è il ruolo (es. 1=admin, 2=teacher, 3=student, ecc.) +$kindofrole = $user->present()->role_id; +$kindofrole = (int)$user->present()->role_id; -// --- INIZIO: Reindirizzamento intelligente per studenti senza profilo --- -if (session_status() == PHP_SESSION_NONE) { + +// Definisci ruolo studente (conferma che sia 2!) +define('ROLE_STUDENTE', 2); + +// Avvia sessione se non attiva +if (session_status() === PHP_SESSION_NONE) { session_start(); } -// Definisci qui l'ID del ruolo STUDENTE (cambialo se è diverso!) -define('ROLE_STUDENTE', 2); // Cambia 3 con il ruolo corretto del tuo studente - -// Escludi alcune pagine dove NON vuoi il redirect (es. login, logout, profile) $current_page = basename($_SERVER['PHP_SELF']); -$no_redirect_pages = ['login.php', 'logout.php', 'student_profile.php', 'register.php']; -if ( - $kindofrole == ROLE_STUDENTE && - !in_array($current_page, $no_redirect_pages) && - !isset($_SESSION['student_profile_completed']) -) { - // Controlla se esiste il record in tabella students - $stmt = $db->prepare("SELECT id FROM students WHERE user_id = ? LIMIT 1"); +// Pagine escluse da TUTTI i redirect/redirect automatici +$excluded_pages = [ + 'login.php', + 'logout.php', + 'register.php', + 'forgot-password.php', // se esiste + 'student_profile.php', + 'select_school.php' +]; + +// ================================================ +// 1. CREAZIONE AUTOMATICA PROFILO STUDENTE (se manca) +// ================================================ +if ($kindofrole === ROLE_STUDENTE && !in_array($current_page, $excluded_pages)) { + + $stmt = $db->prepare("SELECT 1 FROM students WHERE user_id = ? LIMIT 1"); $stmt->execute([$iduserlogin]); - $student_exists = $stmt->fetch(); + $profile_exists = $stmt->fetchColumn(); - if (!$student_exists) { - // Non ha completato il profilo → reindirizza - $_SESSION['student_profile_pending'] = true; - header("Location: student_profile.php"); - exit; + if (!$profile_exists) { + // Crea record minimo obbligatorio + $stmt_insert = $db->prepare(" + INSERT INTO students ( + user_id, + billing_country, + shipping_same_as_billing, + privacy_consent, + created_at, + updated_at + ) VALUES ( + ?, 'Italia', 1, 1, NOW(), NOW() + ) + "); + $stmt_insert->execute([$iduserlogin]); + + // Imposta flag per non rifare controlli inutili + $_SESSION['student_profile_completed'] = true; } else { - // Ha già completato → segna per non controllare più $_SESSION['student_profile_completed'] = true; } } -// --- FINE: Reindirizzamento intelligente --- +// ================================================ +// 2. SALVA DATI UTENTE IN SESSIONE +// ================================================ $_SESSION["iduserlogin"] = $iduserlogin; $_SESSION["nameuser"] = $nameuser; $_SESSION["surnameuser"] = $surnameuser; @@ -64,12 +88,44 @@ $_SESSION["photouser"] = $avatar; $photouser = $_SESSION["photouser"]; +if (defined('SKIP_SCHOOL_CONTEXT') && SKIP_SCHOOL_CONTEXT === true) { + return; // oppure salta SOLO i redirect scuola +} +// ================================================ +// 3. LOGICA SCUOLA (solo se profilo base esiste) +// ================================================ +$has_school_association = false; +$stmt_school_check = $db->prepare("SELECT 1 FROM user_schools WHERE user_id = ? LIMIT 1"); +$stmt_school_check->execute([$iduserlogin]); +$has_school_association = (bool) $stmt_school_check->fetchColumn(); -// include school settings -include('schoolid_select.php'); +if ($has_school_association) { + // include('schoolid_select.php'); +} +// Carica impostazioni scuola solo se c'è school_id valida +$pages_allow_no_school = ['school_profile.php']; -// include school settings -include('school_settings_loader.php'); +if (isset($_SESSION['school_id']) && (int)$_SESSION['school_id'] > 0) { + include('school_settings_loader.php'); +} else { + // Per studenti: se non ha scuola selezionata → vai a select_school + if ($kindofrole === ROLE_STUDENTE && !in_array($current_page, $excluded_pages)) { + header("Location: select_school.php"); + exit; + } + + // Per owner/admin: vai a school_profile se non è una pagina permessa + elseif (!in_array($current_page, $pages_allow_no_school)) { + header("Location: school_profile.php"); + exit; + } + + // Default impostazioni minime + $schoolSettings = [ + 'timezone' => 'Europe/Rome', + 'locale' => 'it', + ]; +} diff --git a/public/userarea/include/navbar.php b/public/userarea/include/navbar.php index ec3ca73..a8b6660 100644 --- a/public/userarea/include/navbar.php +++ b/public/userarea/include/navbar.php @@ -9,8 +9,7 @@ if (!empty($_SESSION['school_id'])) { $stmt_school = $pdo->prepare("SELECT name, logo FROM schools WHERE id = ?"); $stmt_school->execute([$school_id]); $current_school = $stmt_school->fetch(PDO::FETCH_ASSOC); - echo $current_school['name']; - echo "Ciao"; + if ($current_school) { $school_display_name = $current_school['name']; @@ -24,6 +23,14 @@ if (!empty($_SESSION['school_id'])) { } } ?> +hasRole('Admin') || Auth::user()->hasRole('User')) + || (Auth::user()->hasRole('school_owner') && !empty($hasUserSchools)); +?> + @@ -268,12 +311,20 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') {
+
+