Files
geograsim/App/php/templates/_scripts.php
T
Adminator 33fb423967 Atlas: Live-View-Feature + Submit-Bug-Fixes + Admin-Styleguide
- Neuer API-Endpoint /api/live (heartbeat / spectator) + DB-Tabelle
  live_sessions + class_modules.paused-Spalte
- Plattform-Live-Client (assets/js/live-client.js): Heartbeat,
  Pause-Overlay, Spectator-Mode bei ?view=teacher
- ggs_inject_live() in _scripts.php als Helper, in alle 11 Sim-Wrapper
  integriert
- Lehrer-Cockpit: tabellarische Klassen-Live-Ansicht pro Sim,
  Pause-Toggle, blinkender Tab-Indikator wenn aktive Sessions
- Submit-Bug erschlagen: progress.php hat jetzt submit_assessment-
  und reflection-Action; saves.php akzeptiert beide Key-Konventionen;
  alle Direkt-Pfad-API-Files mit require_once-Bootstrap
- Avatar-Default: zufaelliger DALL-E-Avatar fuer neue Schueler:innen
- Admin-Styleguide-Seite mit Bildstil, Farb-Tokens, Layout, iPad-Pattern
- Klima 2D + 3D: GGS_LIVE_STATE-Hook, Klima 2D zusaetzlich skipResume
  fuer Klassenaufgaben-Reset
- Test-Lehrer Jakob + 3 Schueler:innen lokal + auf Live angelegt
- Rundbriefe an alle Sim-Instanzen + Submit-Briefe an sonnensystem,
  busfahrt, energiemanager + Glossar-Anfrage zu Grundriess
- Status-Uebergabe in _inbox/zentrale/_status.md

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-07 01:20:53 +02:00

108 lines
4.2 KiB
PHP

<?php
/**
* Vite Manifest Reader
* Liest dist/.vite/manifest.json und gibt die korrekten Script-Tags aus.
* Im Dev-Modus (kein manifest vorhanden) wird direkt auf die TS-Quellen verwiesen.
*/
function viteAssets(string $entry): void {
$manifestPath = APP_ROOT . '/dist/.vite/manifest.json';
// Production: Lese Manifest
if (file_exists($manifestPath)) {
$manifest = json_decode(file_get_contents($manifestPath), true);
$chunk = $manifest[$entry] ?? null;
if (!$chunk) return;
// CSS
foreach ($chunk['css'] ?? [] as $css) {
echo '<link rel="stylesheet" href="' . BASE_PATH . '/dist/' . $css . '">' . "\n";
}
// Preload imports
foreach ($chunk['imports'] ?? [] as $importKey) {
$imp = $manifest[$importKey] ?? null;
if ($imp) {
echo '<link rel="modulepreload" crossorigin href="' . BASE_PATH . '/dist/' . $imp['file'] . '">' . "\n";
}
}
// Main entry
echo '<script type="module" crossorigin src="' . BASE_PATH . '/dist/' . $chunk['file'] . '"></script>' . "\n";
}
// Dev fallback: keine Manifest-Datei → direkte TS-Referenz (Vite Dev Server)
}
/** Session-Kontext als JS-Variable injizieren */
function injectSessionContext(): void {
$ctx = [
'sessionId' => Session::studentId(),
'teacherId' => Session::teacherId(),
'baseUrl' => BASE_URL,
'basePath' => BASE_PATH,
];
echo '<script>window.__GGS__ = ' . json_encode($ctx, JSON_UNESCAPED_UNICODE) . ';</script>' . "\n";
}
/**
* Plattform-Live-Client in eine Sim-Render-HTML einfügen.
*
* Stellt sicher, dass `window.__GGS__` mit `sessionId`, `simId`, `baseUrl`,
* `basePath`, `apiUrl` belegt ist (Sim-eigene Werte gewinnen) und lädt
* danach `assets/js/live-client.js`. Der Live-Client kümmert sich dann um
* Heartbeat / Pause-Polling / Spectator-Mode.
*
* In jedem Sim-Wrapper kurz vor `echo $html` aufrufen:
* $html = ggs_inject_live($html, 'logistik');
*/
function ggs_inject_live(string $html, string $simId): string {
$bp = BASE_PATH;
$bu = BASE_URL;
$sid = Session::studentId() ?? '';
$ctx = [
'sessionId' => $sid,
'simId' => $simId,
'baseUrl' => $bu,
'basePath' => $bp,
'apiUrl' => $bu . '/php/api',
];
$ctxJson = json_encode($ctx, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
// Sim-eigene __GGS__-Felder gewinnen — wir ergänzen nur Plattform-Felder.
$bootstrap = '<script>window.__GGS__=Object.assign(' . $ctxJson . ',window.__GGS__||{});</script>';
$tag = '<script src="' . htmlspecialchars($bp . '/assets/js/live-client.js', ENT_QUOTES) . '" defer></script>';
$injection = $bootstrap . "\n" . $tag . "\n";
// Sicheres Inject: vor dem letzten </body>. Falls keines, einfach anhängen.
$pos = strrpos($html, '</body>');
if ($pos === false) return $html . "\n" . $injection;
return substr($html, 0, $pos) . $injection . substr($html, $pos);
}
/** Seite rendern: HTML laden, Session-Kontext + Favicon injizieren, ausgeben */
function renderPage(string $htmlFile): void {
$html = file_get_contents(APP_ROOT . '/' . $htmlFile);
// Favicon-Block (ersetzt alte logo.png Referenz)
$bp = BASE_PATH;
$favicon = <<<HTML
<link rel="icon" type="image/png" href="{$bp}/favicon-96x96.png" sizes="96x96">
<link rel="icon" type="image/svg+xml" href="{$bp}/favicon.svg">
<link rel="shortcut icon" href="{$bp}/favicon.ico">
<link rel="apple-touch-icon" sizes="180x180" href="{$bp}/apple-touch-icon.png">
<link rel="manifest" href="{$bp}/site.webmanifest">
HTML;
// Alten Favicon-Link ersetzen oder vor </head> einfuegen
if (preg_match('/<link[^>]*rel=["\']icon["\'][^>]*>/', $html)) {
$html = preg_replace('/<link[^>]*rel=["\']icon["\'][^>]*>\s*/', '', $html);
}
$html = str_replace('</head>', $favicon . "\n</head>", $html);
// Session-Kontext
$ctx = '<script>window.__GGS__ = ' . json_encode([
'sessionId' => Session::studentId(),
'teacherId' => Session::teacherId(),
'baseUrl' => BASE_URL,
'basePath' => BASE_PATH,
], JSON_UNESCAPED_UNICODE) . ';</script>';
echo str_replace('</body>', $ctx . "\n</body>", $html);
}