Let the dashboard follow the presenter #79

Merged
gnilebein merged 1 commits from feature/clicker-quiz-and-reset into main 2026-11-13 12:22:44 +01:00
7 changed files with 104 additions and 1 deletions

No files matched your search

+3
View File
@@ -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`.
+1
View File
@@ -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);
+12
View File
@@ -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
+27
View File
@@ -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
+4 -1
View File
@@ -11,6 +11,7 @@
* @var array<string, string> $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-
<?= __('Diese URL ist dein Zugang zur Umfrage. Wer sie kennt, kann alles bearbeiten.') ?>
</div>
<div class="grid grid--sidebar">
<div class="grid grid--sidebar"
data-dashboard-live="<?= $this->Url->build($url('admin:live')) ?>"
data-interval="<?= (int)$pollIntervalMs ?>">
<section>
<div class="section-head">
<h2><?= __('Folien') ?></h2>
@@ -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());
}
}
+31
View File
@@ -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();