From 8c7818ee407bc5aa04daef47ad8d53d07c975609 Mon Sep 17 00:00:00 2001 From: Patrick Niebeling Date: Fri, 13 Nov 2026 12:19:12 +0100 Subject: [PATCH] Let the dashboard follow the presenter The live marker in the slide list stayed on the last known slide when the presenter moved on in another window or with a clicker. The dashboard now polls a cheap version fingerprint (admin:live) and reloads the lists when it changes, but never under an open dialog, a pending request or a drag. Co-Authored-By: Claude Sonnet 5.5 --- AGENTS.md | 3 ++ config/routes.php | 1 + src/Controller/AdminController.php | 12 ++++++++ src/Service/PollStateService.php | 27 ++++++++++++++++++ templates/Admin/index.php | 5 +++- tests/TestCase/Controller/PollResetTest.php | 26 +++++++++++++++++ webroot/js/plenario.js | 31 +++++++++++++++++++++ 7 files changed, 104 insertions(+), 1 deletion(-) diff --git a/AGENTS.md b/AGENTS.md index 4ed77ca..57fb7f4 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -302,6 +302,9 @@ tests/ PHPUnit; PlenarioTestTrait erzeugt Testdaten, Fixtures `[data-refresh="head|slides|flash"]` (als DOM-Knoten, nie `innerHTML`) und stellt den Fokus wieder her. Bearbeiten/Anlegen: Links mit `data-modal` laden das Formular (`[data-modal-content]`) in den Dialog; Validierungsfehler kommen als Formular zurück (Dialog bleibt offen), Erfolg als Dashboard-Fragment (Dialog schließt). Ohne JS funktioniert alles als normale Seite. + Die Liste folgt auch dem Presenter (Live-Markierung, Status): `initAdminDashboard()` pollt `admin:live` (`AdminController::live()`, + nur `{version}` aus `PollStateService::dashboardVersion()`, billiger als der Presenter-State) und lädt `head`/`slides` nur bei geänderter Version + neu – nie bei offenem Dialog, laufender Anfrage oder Drag & Drop; der Fokus wird wiederhergestellt. Test: `PollResetTest::testDashboardLiveVersionFollowsThePresenter`. **Buttons mit Bestätigung** nutzen `Form->postButton()` + `data-confirm` (CakePHPs `postLink`/`confirm` erzeugt ein Inline-`onclick`, das die CSP blockiert); das JS fragt per `window.confirm()` vor dem Absenden. Tests: `AdminFragmentTest.php`. diff --git a/config/routes.php b/config/routes.php index f216427..e418efd 100644 --- a/config/routes.php +++ b/config/routes.php @@ -73,6 +73,7 @@ return function (RouteBuilder $routes): void { ->setPatterns($token)->setMethods(['POST']); $admin->connect('/present', 'Admin::present', ['_name' => 'admin:present'])->setPatterns($token); $admin->connect('/state', 'Admin::state', ['_name' => 'admin:state'])->setPatterns($token); + $admin->connect('/live', 'Admin::live', ['_name' => 'admin:live'])->setPatterns($token); $admin->connect('/control', 'Admin::control', ['_name' => 'admin:control']) ->setPatterns($token)->setMethods(['POST']); $admin->connect('/qr.svg', 'Admin::qr', ['_name' => 'admin:qr'])->setPatterns($token); diff --git a/src/Controller/AdminController.php b/src/Controller/AdminController.php index cea9232..a91c2d6 100644 --- a/src/Controller/AdminController.php +++ b/src/Controller/AdminController.php @@ -60,6 +60,7 @@ class AdminController extends AdminAppController $embedKey = $this->Polls->ensureEmbedKey($this->poll); $hasPrepared = $this->Responses->hasPrepared($this->poll->id); + $this->setPollInterval(); // Answers are not the only thing a reset undoes: a started quiz, closed slides or a running presentation too. $canReset = $responseCounts !== [] @@ -195,6 +196,17 @@ class AdminController extends AdminAppController return $this->json((new PollStateService())->forPresenter($this->poll)); } + /** + * Fingerprint of the dashboard content (JSON `{version}`), polled by the dashboard so the + * live marker follows the presenter. See PollStateService::dashboardVersion(). + * + * @return \Cake\Http\Response + */ + public function live(): Response + { + return $this->json(['version' => (new PollStateService())->dashboardVersion($this->poll)]); + } + /** * Presenter controls. Field `action`: * goto (+ slide_id), next, prev, stop (pause), resume (continue after a pause), and the slide actions of diff --git a/src/Service/PollStateService.php b/src/Service/PollStateService.php index 1669191..ecef281 100644 --- a/src/Service/PollStateService.php +++ b/src/Service/PollStateService.php @@ -56,6 +56,33 @@ class PollStateService $this->results = new ResultsService(); } + /** + * Cheap fingerprint of everything the admin dashboard shows (live/paused slide, slide settings + * and status, answer counts). The dashboard polls it and reloads its lists when it changes. + * + * @param \App\Model\Entity\Poll $poll Poll + * @return string + */ + public function dashboardVersion(Poll $poll): string + { + $slides = $this->Slides->find() + ->select([ + 'n' => $this->Slides->find()->func()->count('*'), + 'latest' => $this->Slides->find()->func()->max('modified'), + ]) + ->where(['poll_id' => $poll->id]) + ->disableHydration() + ->first(); + + return md5(json_encode([ + (int)$poll->is_active, + $poll->current_slide_id, + $poll->paused_slide_id, + $slides, + $this->Responses->countParticipantsPerSlide($poll->id), + ], JSON_THROW_ON_ERROR)); + } + /** * @param \App\Model\Entity\Poll $poll Poll * @return \App\Model\Entity\Slide|null diff --git a/templates/Admin/index.php b/templates/Admin/index.php index 75bd82c..623ed97 100644 --- a/templates/Admin/index.php +++ b/templates/Admin/index.php @@ -11,6 +11,7 @@ * @var array $typeDescriptions * @var string $joinUrl * @var string $embedKey + * @var int $pollIntervalMs */ $this->assign('title', $poll->title); $this->assign('noindex', '1'); @@ -52,7 +53,9 @@ $hasInternal = array_filter($slides, fn($slide) => !$slide->is_hidden && $slide- -
+

diff --git a/tests/TestCase/Controller/PollResetTest.php b/tests/TestCase/Controller/PollResetTest.php index c711c7f..83599ae 100644 --- a/tests/TestCase/Controller/PollResetTest.php +++ b/tests/TestCase/Controller/PollResetTest.php @@ -184,4 +184,30 @@ class PollResetTest extends TestCase $this->get("/admin/$token"); $this->assertResponseContains("/admin/$token/reset\""); } + + public function testDashboardLiveVersionFollowsThePresenter(): void + { + [$poll, $token] = $this->createPoll(); + $first = $this->createSlide($poll, Slide::TYPE_CHOICE); + $second = $this->createSlide($poll, Slide::TYPE_CHOICE); + $version = function () use ($token): string { + $this->configRequest(['headers' => ['Accept' => 'application/json']]); + $this->get("/admin/$token/live"); + $this->assertResponseOk(); + + return json_decode((string)$this->_response->getBody(), true)['version']; + }; + + $start = $version(); + $this->assertSame($start, $version(), 'stable while nothing happens'); + + $poll = $this->goLive($poll, $first); + $live = $version(); + $this->assertNotSame($start, $live, 'live slide changed'); + + $this->goLive($poll, $second); + $this->assertNotSame($live, $version(), 'live marker moved'); + + $this->assertStringNotContainsString($token, (string)$this->_response->getBody()); + } } diff --git a/webroot/js/plenario.js b/webroot/js/plenario.js index 3edb2ff..46346ca 100644 --- a/webroot/js/plenario.js +++ b/webroot/js/plenario.js @@ -2654,6 +2654,36 @@ }); } + // The presenter (other window, clicker) moves the live marker: poll a cheap fingerprint + // and reload the lists when it changes. Own actions already refresh, so they skip one round. + var liveRoot = document.querySelector('[data-dashboard-live]'); + var liveVersion = null; + if (liveRoot) { + poll(function () { + if ((dialog && dialog.open) || document.querySelector('[data-live] [aria-busy], .slide-item--dragging')) { + return null; // never replace the DOM under a dialog, a pending request or a drag + } + return getJson(liveRoot.getAttribute('data-dashboard-live')).then(function (state) { + var previous = liveVersion; + liveVersion = state.version; + if (previous === null || previous === state.version) { + return null; + } + var active = document.activeElement; + var item = active && active.closest ? active.closest('[data-slide-id]') : null; + var form = active && active.closest ? active.closest('form') : null; + var next = item ? item.nextElementSibling : null; + var target = item ? { + id: item.getAttribute('data-slide-id'), + action: form ? form.getAttribute('action') : null, + nextId: next ? next.getAttribute('data-slide-id') : null + } : null; + + return refreshList().then(function () { restoreFocus(target); }); + }); + }, Math.max(4000, parseInt(liveRoot.getAttribute('data-interval'), 10) * 2 || 4000)); + } + function openModal(doc) { var content = doc.querySelector('[data-modal-content]'); var flash = doc.querySelector('[data-refresh="flash"]'); @@ -2690,6 +2720,7 @@ return wasOpen ? null : refreshList(); } Fragments.apply(doc); + liveVersion = null; // the next poll only takes the new fingerprint if (dialog && dialog.open) { dirty = false; dialog.close();