From 79613cadfef41fb9bdcf57417595d2a809f8a3be Mon Sep 17 00:00:00 2001 From: solocla Date: Sat, 3 Oct 2026 14:18:28 +0200 Subject: [PATCH] SaaS fase 1.2: SchoolContext e scuola corrente nella headscript --- public/class/SchoolContext.php | 111 +++++++++++++++++++++++++++++++++ public/include/headscript.php | 6 ++ 2 files changed, 117 insertions(+) create mode 100644 public/class/SchoolContext.php diff --git a/public/class/SchoolContext.php b/public/class/SchoolContext.php new file mode 100644 index 0000000..f9e5987 --- /dev/null +++ b/public/class/SchoolContext.php @@ -0,0 +1,111 @@ +getConnection(); + + $schools = self::userSchools($pdo, $userId); + + // TEMPORANEO (fase monoscuola): utente senza scuola -> agganciato a Seregno + if (empty($schools)) { + $stmt = $pdo->prepare( + "INSERT IGNORE INTO school_user (user_id, idschool, school_role) + VALUES (?, ?, 'student')" + ); + $stmt->execute([$userId, self::DEFAULT_SCHOOL]); + $schools = self::userSchools($pdo, $userId); + } + + if (empty($schools)) { + http_response_code(403); + die('Nessuna scuola attiva associata a questo account.'); + } + + // Scuola richiesta in sessione: accettata solo se l'utente ne fa parte + $requested = isset($_SESSION['idschool']) ? (int) $_SESSION['idschool'] : 0; + + $selected = $schools[0]; + foreach ($schools as $row) { + if ((int) $row['idschool'] === $requested) { + $selected = $row; + break; + } + } + + $selected['idschool'] = (int) $selected['idschool']; + $_SESSION['idschool'] = $selected['idschool']; + + self::$school = $selected; + return self::$school; + } + + /** ID della scuola corrente */ + public static function id(): int + { + if (self::$school === null) { + throw new RuntimeException('SchoolContext::resolve() non ancora chiamato'); + } + return self::$school['idschool']; + } + + /** Dati completi della scuola corrente */ + public static function get(): array + { + if (self::$school === null) { + throw new RuntimeException('SchoolContext::resolve() non ancora chiamato'); + } + return self::$school; + } + + /** Ruolo dell'utente nella scuola corrente: student|teacher|admin|owner */ + public static function role(): string + { + return self::get()['school_role']; + } + + /** Scuole attive a cui l'utente รจ iscritto */ + private static function userSchools(PDO $pdo, int $userId): array + { + $stmt = $pdo->prepare( + "SELECT s.*, su.school_role + FROM school_user su + JOIN schools s ON s.idschool = su.idschool + WHERE su.user_id = ? + AND su.active = 'Y' + AND s.status <> 'suspended' + ORDER BY su.idschool" + ); + $stmt->execute([$userId]); + return $stmt->fetchAll(); + } +} diff --git a/public/include/headscript.php b/public/include/headscript.php index 7994e7c..39cfcca 100644 --- a/public/include/headscript.php +++ b/public/include/headscript.php @@ -53,6 +53,12 @@ if (!isset($_SESSION["idowneruser"])) { $iduserlog = $_SESSION["iduserlogin"]; $_SESSION["nameuser"] = $nameuser; $_SESSION["emailuser"] = $emailuser; +// --- Scuola corrente (SaaS) --- +require_once('class/SchoolContext.php'); +$schoolData = SchoolContext::resolve((int) $iduserlogin); +$idschool = SchoolContext::id(); +$schoolname = $schoolData['name']; +// ------------------------------ if (!isset($_SESSION["tempcode"])) { $timestampnow = time(); $temporarycode = $iduserlog . "-" . $timestampnow;