From c1f9be2eefc879e7d36a23a1168f5166e13ced55 Mon Sep 17 00:00:00 2001 From: Patrick Niebeling Date: Wed, 11 Nov 2026 12:46:33 +0100 Subject: [PATCH] Add a star rating style to scale slides Scales can now be shown as stars (1 to n) instead of numbered steps: participants tap stars, the stage shows the average as partly filled stars and the distribution as bars from most to fewest stars, and the evaluation page and PDF export print ratings as stars. Co-Authored-By: Claude Opus 5.5 --- AGENTS.md | 5 + src/Model/Entity/Slide.php | 35 +++++ src/Model/Table/SlidesTable.php | 5 +- src/Service/ResultsService.php | 5 +- src/Service/VoteService.php | 4 +- templates/Admin/export.php | 10 +- templates/Participate/view.php | 31 ++-- templates/Slides/answers.php | 8 +- templates/Slides/form.php | 13 +- templates/element/rating.php | 14 ++ templates/layout/pdf.php | 1 + tests/TestCase/Controller/StarRatingTest.php | 156 +++++++++++++++++++ webroot/css/plenario.css | 46 +++++- webroot/js/plenario.js | 98 +++++++++++- 14 files changed, 400 insertions(+), 31 deletions(-) create mode 100644 templates/element/rating.php create mode 100644 tests/TestCase/Controller/StarRatingTest.php diff --git a/AGENTS.md b/AGENTS.md index 89d3bb5..9e048d4 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -226,6 +226,11 @@ tests/ PHPUnit; PlenarioTestTrait erzeugt Testdaten, Fixtures vor/zurück/Pause, Laufbalken bis zum Wechsel (`Comments.progress()`, setzt nach dem Neu-Rendern per negativem `animation-delay` fort, bei Pause unsichtbar), Zustand übersteht das Neu-Rendern beim Polling). Bei internem Feedback erscheinen sie erst, wenn der Presenter die Ergebnisse einblendet. +- **Sterne-Bewertung** (Skala mit Setting `style = stars`, `Slide::isStarRating()`, Standard `numbers`): immer 1 bis `scale_max` + Sterne (`SlidesTable::sanitizeSettings()` erzwingt `scale_min = 1`), gespeichert wie jede Skala als `numeric_value`. + Teilnehmende wählen Sterne (Radios in DOM-Reihenfolge 1..n, Füllung per CSS `:has()`), der State enthält `results.stars`; + `Results.stars` in `plenario.js` zeigt den Durchschnitt als teilweise gefüllte Sterne und die Verteilung als Balken 5 → 1. + Text-Ausgabe (Auswertung, PDF) über `Slide::ratingText()` (`★★★★☆`), barrierearm über `element/rating`. - **Antwort nachträglich bearbeiten** (gegen Tippfehler, `VoteService::hasEditableText()`): Antwort einer offenen Frage (darf nicht leer werden) bzw. Kommentar einer Skala (ändern, ergänzen, leeren; die Bewertung bleibt unveränderlich), nur solange die Abstimmung offen ist. `participate:edit` → `VoteService::editText()`, gleiche Vorbedingungen wie diff --git a/src/Model/Entity/Slide.php b/src/Model/Entity/Slide.php index bca72f8..608860d 100644 --- a/src/Model/Entity/Slide.php +++ b/src/Model/Entity/Slide.php @@ -55,6 +55,12 @@ class Slide extends Entity */ public const QUIZ_TIME_LIMITS = [0, 10, 15, 20, 30, 45, 60, 90, 120, 180, 300]; + /** + * Display styles of a scale: numbered steps or a star rating (always starts at 1 star). + */ + public const SCALE_NUMBERS = 'numbers'; + public const SCALE_STARS = 'stars'; + /** * Q&A stages, see qaStage(): participants submit questions, then upvote * them, then the presenter discusses them one by one (most upvoted first). @@ -88,7 +94,9 @@ class Slide extends Entity // comments: optional comment field, comments rotate in the presentation (unless private) // comment_ratings: show the rating next to each comment in the presentation // (time per comment depends on its length, see Comments.delay() in plenario.js) + // style: numbers or stars (SCALE_*), stars always start at scale_min = 1 self::TYPE_SCALE => [ + 'style' => self::SCALE_NUMBERS, 'scale_min' => 1, 'scale_max' => 5, 'min_label' => '', 'max_label' => '', 'private' => false, 'comments' => false, 'comment_ratings' => false, ], @@ -449,4 +457,31 @@ class Slide extends Entity { return $this->type === self::TYPE_SCALE && (bool)$this->setting('comments'); } + + /** + * Scale shown as a star rating (1 to scale_max stars) instead of numbered steps. + * + * @return bool + */ + public function isStarRating(): bool + { + return $this->type === self::TYPE_SCALE && $this->setting('style') === self::SCALE_STARS; + } + + /** + * Rating of a scale as plain text: the number, or filled and empty stars (e.g. ★★★★☆). + * + * @param int $value Rating + * @return string + */ + public function ratingText(int $value): string + { + if (!$this->isStarRating()) { + return (string)$value; + } + $max = (int)$this->setting('scale_max'); + $value = max(0, min($max, $value)); + + return str_repeat('★', $value) . str_repeat('☆', $max - $value); + } } diff --git a/src/Model/Table/SlidesTable.php b/src/Model/Table/SlidesTable.php index 1321511..55bd79a 100644 --- a/src/Model/Table/SlidesTable.php +++ b/src/Model/Table/SlidesTable.php @@ -150,7 +150,10 @@ class SlidesTable extends Table $settings['private'] = filter_var($input['private'] ?? false, FILTER_VALIDATE_BOOLEAN); break; case Slide::TYPE_SCALE: - $min = max(0, min(1, (int)($input['scale_min'] ?? 1))); + $stars = ($input['style'] ?? null) === Slide::SCALE_STARS; + $settings['style'] = $stars ? Slide::SCALE_STARS : Slide::SCALE_NUMBERS; + // Stars always start at one star (zero stars is no rating). + $min = $stars ? 1 : max(0, min(1, (int)($input['scale_min'] ?? 1))); $max = max(2, min(10, (int)($input['scale_max'] ?? 5))); $settings['scale_min'] = $min; $settings['scale_max'] = $max; diff --git a/src/Service/ResultsService.php b/src/Service/ResultsService.php index a418cd6..f42a66a 100644 --- a/src/Service/ResultsService.php +++ b/src/Service/ResultsService.php @@ -18,7 +18,7 @@ use Cake\ORM\Locator\LocatorAwareTrait; * weight follows count) * - open: 'answers' => [['text', 'created']] newest first, 'private' (bool); * - scale: 'buckets' => [['value', 'count', 'percent']], 'average' => float|null, - * 'min', 'max', 'minLabel', 'maxLabel', 'private' (bool); + * 'min', 'max', 'minLabel', 'maxLabel', 'private' (bool), 'stars' (bool: star rating, min = 1); * 'comments' => [['id', 'text', 'value' (int|null)]] oldest first (only with the comment field, else []; * 'value' is null unless the setting comment_ratings is on; * presentation only: participants never get results of scale slides, see PollStateService); @@ -394,7 +394,7 @@ class ResultsService * Distribution and average of a scale. * * @param \App\Model\Entity\Slide $slide Scale slide - * @return array{buckets: list, average: float|null, min: int, max: int, minLabel: string, maxLabel: string} + * @return array{buckets: list, average: float|null, min: int, max: int, minLabel: string, maxLabel: string, stars: bool} */ public function scaleResults(Slide $slide): array { @@ -432,6 +432,7 @@ class ResultsService 'max' => $max, 'minLabel' => (string)$slide->setting('min_label'), 'maxLabel' => (string)$slide->setting('max_label'), + 'stars' => $slide->isStarRating(), ]; } } diff --git a/src/Service/VoteService.php b/src/Service/VoteService.php index 3fbf8b9..6076b78 100644 --- a/src/Service/VoteService.php +++ b/src/Service/VoteService.php @@ -665,7 +665,9 @@ class VoteService $min = (int)$slide->setting('scale_min'); $max = (int)$slide->setting('scale_max'); if ($value === false || $value < $min || $value > $max) { - throw new VoteException(__('Bitte wähle einen Wert zwischen {0} und {1}.', $min, $max)); + throw new VoteException($slide->isStarRating() + ? __('Bitte vergib zwischen {0} und {1} Sternen.', $min, $max) + : __('Bitte wähle einen Wert zwischen {0} und {1}.', $min, $max)); } // The optional comment is stored in the same row; ignored if the slide has no comment field. return [['numeric_value' => $value, 'text_value' => $slide->hasComments() ? self::comment($data) : null]]; diff --git a/templates/Admin/export.php b/templates/Admin/export.php index fc03efc..213ea9b 100644 --- a/templates/Admin/export.php +++ b/templates/Admin/export.php @@ -171,10 +171,12 @@ $bar = function (float $percent, bool $ok = false): string {

: - +

- + $bucket) : $label = match (true) { @@ -183,7 +185,7 @@ $bar = function (float $percent, bool $ok = false): string { default => '', }; ?> - + >ratingText((int)$bucket['value'])) ?> () @@ -200,7 +202,7 @@ $bar = function (float $percent, bool $ok = false): string {
  • - () + (ratingText((int)$comment['value']))) ?>)
  • diff --git a/templates/Participate/view.php b/templates/Participate/view.php index 3d20416..62da0d5 100644 --- a/templates/Participate/view.php +++ b/templates/Participate/view.php @@ -280,15 +280,28 @@ $countdown = function (?int $remaining): string {

    - type === 'scale') : ?> -
    isPrivate() ? ' aria-describedby="private-hint"' : '' ?>> - setting('scale_min'); $v <= (int)$slide->setting('scale_max'); $v++) : ?> - - -
    + type === 'scale') : + $maxValue = (int)$slide->setting('scale_max'); ?> + isStarRating()) : ?> +
    isPrivate() ? ' aria-describedby="private-hint"' : '' ?>> + + + +
    + +
    isPrivate() ? ' aria-describedby="private-hint"' : '' ?>> + setting('scale_min'); $v <= $maxValue; $v++) : ?> + + +
    +
    setting('min_label')) ?> setting('max_label')) ?> diff --git a/templates/Slides/answers.php b/templates/Slides/answers.php index 7872c34..c969813 100644 --- a/templates/Slides/answers.php +++ b/templates/Slides/answers.php @@ -43,12 +43,12 @@ $count = $scale !== null ? array_sum(array_column($scale['buckets'], 'count')) :

    : Number->format((float)$scale['average'], ['places' => 1, 'precision' => 1, 'locale' => 'de_DE'])) ?> - / + isStarRating() ? __('von {0} Sternen', (int)$scale['max']) : '/ ' . (int)$scale['max'] ?>

    () %
    - + $bucket) : @@ -58,7 +58,7 @@ $count = $scale !== null ? array_sum(array_column($scale['buckets'], 'count')) : default => '', }; ?> - +
    isStarRating() ? __('Sterne') : __('Wert') ?>
    ()element('rating', ['slide' => $slide, 'value' => (int)$bucket['value']]) ?> () @@ -79,7 +79,7 @@ $count = $scale !== null ? array_sum(array_column($scale['buckets'], 'count')) : $created = new DateTime($comment['created']); ?>
  • - +
  • diff --git a/templates/Slides/form.php b/templates/Slides/form.php index fd1095a..536968f 100644 --- a/templates/Slides/form.php +++ b/templates/Slides/form.php @@ -148,14 +148,25 @@ $optionsError = $slide->getError('options_text'); 'checked' => (bool)$settings['allow_downvotes'], ]) ?> type === 'scale') : ?> + +
    + + Form->radio('settings.style', [ + ['value' => \App\Model\Entity\Slide::SCALE_NUMBERS, 'text' => __('Zahlen (z. B. 1 bis 10)')], + ['value' => \App\Model\Entity\Slide::SCALE_STARS, 'text' => __('Sterne (wie bei einer Produktbewertung)')], + ], ['value' => $settings['style']]) ?> +
    Form->control('settings.scale_min', [ 'type' => 'select', 'options' => [0 => '0', 1 => '1'], 'label' => __('Von'), 'value' => $settings['scale_min'], + 'templateVars' => ['attrs' => $stars ? ' hidden' : ''], + 'templates' => ['inputContainer' => '
    {{content}}
    '], ]) ?> Form->control('settings.scale_max', [ 'type' => 'select', 'options' => array_combine(range(2, 10), range(2, 10)), - 'label' => __('Bis'), 'value' => $settings['scale_max'], + 'label' => $stars ? __('Anzahl Sterne') : __('Bis'), 'value' => $settings['scale_max'], + 'data-label-numbers' => __('Bis'), 'data-label-stars' => __('Anzahl Sterne'), ]) ?> Form->control('settings.min_label', [ 'label' => __('Beschriftung links (optional)'), 'maxlength' => 60, diff --git a/templates/element/rating.php b/templates/element/rating.php new file mode 100644 index 0000000..3b0769b --- /dev/null +++ b/templates/element/rating.php @@ -0,0 +1,14 @@ +isStarRating()) { + echo (int)$value; + + return; +} +?>setting('scale_max')) ?> diff --git a/templates/layout/pdf.php b/templates/layout/pdf.php index f2b8547..eef91cd 100644 --- a/templates/layout/pdf.php +++ b/templates/layout/pdf.php @@ -46,6 +46,7 @@ ol.list, ul.list { margin: 1mm 0 0; padding-left: 6mm; } ol.list li, ul.list li { margin-bottom: 1.2mm; } .stat { font-size: 12pt; font-weight: bold; } + td.stars { color: #c27c00; letter-spacing: 1pt; } .empty { color: #5d6280; font-style: italic; } diff --git a/tests/TestCase/Controller/StarRatingTest.php b/tests/TestCase/Controller/StarRatingTest.php new file mode 100644 index 0000000..b90942a --- /dev/null +++ b/tests/TestCase/Controller/StarRatingTest.php @@ -0,0 +1,156 @@ + + */ + protected array $fixtures = ['app.Polls', 'app.Slides', 'app.SlideOptions', 'app.Responses']; + + protected function setUp(): void + { + parent::setUp(); + $this->enableCsrfToken(); + } + + /** + * @param array $settings Extra settings + * @return array{0: \App\Model\Entity\Poll, 1: string, 2: \App\Model\Entity\Slide} + */ + protected function starSlide(array $settings = []): array + { + [$poll, $token] = $this->createPoll(); + $slide = $this->createSlide($poll, Slide::TYPE_SCALE, [ + 'settings' => $settings + ['style' => Slide::SCALE_STARS, 'scale_max' => 5], + ]); + + return [$this->goLive($poll, $slide), $token, $slide]; + } + + protected function hash(string $char, Poll $poll): string + { + return VoteService::participantHash(str_repeat($char, 32), $poll->id); + } + + public function testSettingIsSanitized(): void + { + $stars = SlidesTable::sanitizeSettings(Slide::TYPE_SCALE, ['style' => 'stars', 'scale_min' => '0', 'scale_max' => '5']); + $this->assertSame(Slide::SCALE_STARS, $stars['style']); + $this->assertSame(1, $stars['scale_min'], 'Stars always start at one star'); + + $this->assertSame(Slide::SCALE_NUMBERS, SlidesTable::sanitizeSettings(Slide::TYPE_SCALE, [])['style']); + $this->assertSame(Slide::SCALE_NUMBERS, SlidesTable::sanitizeSettings(Slide::TYPE_SCALE, ['style' => 'hearts'])['style']); + $this->assertSame(0, SlidesTable::sanitizeSettings(Slide::TYPE_SCALE, ['scale_min' => '0'])['scale_min']); + } + + public function testExistingScalesStayNumbers(): void + { + [$poll] = $this->createPoll(); + $slide = $this->createSlide($poll, Slide::TYPE_SCALE, ['settings' => ['scale_min' => 1, 'scale_max' => 5]]); + + $this->assertFalse($slide->isStarRating()); + $this->assertSame('4', $slide->ratingText(4)); + } + + public function testRatingText(): void + { + [, , $slide] = $this->starSlide(); + + $this->assertTrue($slide->isStarRating()); + $this->assertSame('★★★★☆', $slide->ratingText(4)); + $this->assertSame('★☆☆☆☆', $slide->ratingText(1)); + } + + public function testFormSavesStarStyle(): void + { + [$poll, $token] = $this->createPoll(); + $this->post("/admin/$token/slides/add?type=scale", [ + 'type' => 'scale', + 'question' => 'Wie gefällt dir das Produkt?', + 'settings' => ['style' => 'stars', 'scale_min' => '0', 'scale_max' => '5'], + ]); + + $slide = $this->slidesTable()->find()->where(['poll_id' => $poll->id])->firstOrFail(); + $this->assertTrue($slide->isStarRating()); + $this->assertSame(1, $slide->setting('scale_min')); + } + + public function testEditFormOffersStyle(): void + { + [, $token, $slide] = $this->starSlide(); + + $this->get("/admin/$token/slides/{$slide->id}/edit"); + $this->assertResponseOk(); + $this->assertResponseContains('data-scale-style'); + $this->assertResponseContains('Anzahl Sterne'); + } + + public function testParticipantSeesStars(): void + { + [$poll] = $this->starSlide(); + + $this->get('/p/' . $poll->join_code); + $this->assertResponseContains('class="stars"'); + $this->assertResponseContains('1 von 5 Sternen'); + $this->assertResponseContains('5 von 5 Sternen'); + $this->assertResponseNotContains('value="0"'); + } + + public function testZeroStarsAreRejected(): void + { + [$poll, , $slide] = $this->starSlide(); + + $this->expectException(VoteException::class); + $this->expectExceptionMessage('Sternen'); + (new VoteService())->submit($poll, $slide, $this->hash('a', $poll), ['value' => 0]); + } + + public function testPresenterGetsStarFlag(): void + { + [$poll, $token, $slide] = $this->starSlide(); + $votes = new VoteService(); + $votes->submit($poll, $slide, $this->hash('a', $poll), ['value' => 5]); + $votes->submit($poll, $slide, $this->hash('b', $poll), ['value' => 4]); + + $this->get("/admin/$token/state"); + $results = json_decode((string)$this->_response->getBody(), true)['results']; + + $this->assertTrue($results['stars']); + $this->assertSame(1, $results['min']); + $this->assertEquals(4.5, $results['average']); + $this->assertSame([1, 2, 3, 4, 5], array_column($results['buckets'], 'value')); + } + + public function testEvaluationAndExportShowStars(): void + { + [$poll, $token, $slide] = $this->starSlide(['comments' => true, 'comment_ratings' => true]); + (new VoteService())->submit($poll, $slide, $this->hash('a', $poll), ['value' => 4, 'comment' => 'Gut']); + + $this->get("/admin/$token/slides/{$slide->id}/answers"); + $this->assertResponseOk(); + $this->assertResponseContains('★★★★☆'); + $this->assertResponseContains('4 von 5 Sternen'); + + $html = $this->exportHtml($token); + $this->assertStringContainsString('von 5 Sternen', $html); + $this->assertStringContainsString('Bewertung ★★★★☆', $html); + } +} diff --git a/webroot/css/plenario.css b/webroot/css/plenario.css index 679be68..5a80bc2 100644 --- a/webroot/css/plenario.css +++ b/webroot/css/plenario.css @@ -30,6 +30,8 @@ --qa: #1f6bb0; /* Q&A accent, AA contrast with white text */ --notice-bg: #fff6e0; --notice-ink: #6b4a00; + --star: #d48500; /* star rating, 3:1 against light surfaces */ + --star-empty: #c3c8da; color-scheme: light; } @@ -50,6 +52,8 @@ --shadow: none; --notice-bg: #3a2f12; --notice-ink: #f3d98f; + --star: #ffc233; + --star-empty: #464a66; color-scheme: dark; } @@ -67,6 +71,8 @@ --shadow: none; --notice-bg: #3a2f12; --notice-ink: #f3d98f; + --star: #ffc233; + --star-empty: #464a66; color-scheme: dark; } } @@ -298,12 +304,12 @@ textarea { resize: vertical; } .order-option__pos { min-width: 2em; text-align: right; font-weight: 700; color: var(--muted); font-variant-numeric: tabular-nums; } .order-options.error .order-options__list input[type=text] { border-color: var(--danger); } -/* dashboard: PDF export, choice between public and complete version */ -.export-choice { border: 0; padding: 0; margin: 0 0 12px; display: grid; gap: 8px; } -.export-choice legend { font-weight: 600; margin-bottom: 4px; } -.export-choice label { display: flex; gap: 10px; align-items: flex-start; padding: 10px 12px; border: 2px solid var(--border); border-radius: 8px; cursor: pointer; } -.export-choice label:has(input:checked) { border-color: var(--primary); background: var(--primary-soft); } -.export-choice input { width: 18px; height: 18px; margin: 2px 0 0; flex: none; accent-color: var(--primary); } +/* radio cards: PDF export (public/complete version), scale style (numbers/stars) */ +.export-choice, .scale-style { border: 0; padding: 0; margin: 0 0 12px; display: grid; gap: 8px; } +.export-choice legend, .scale-style legend { font-weight: 600; margin-bottom: 4px; } +.export-choice label, .scale-style label { display: flex; gap: 10px; align-items: flex-start; padding: 10px 12px; border: 2px solid var(--border); border-radius: 8px; cursor: pointer; } +.export-choice label:has(input:checked), .scale-style label:has(input:checked) { border-color: var(--primary); background: var(--primary-soft); } +.export-choice input, .scale-style input { width: 18px; height: 18px; margin: 2px 0 0; flex: none; accent-color: var(--primary); } .export-choice strong { display: block; } .export-choice .hint { display: block; margin: 2px 0 0; } @@ -352,6 +358,20 @@ textarea { resize: vertical; } .scale__step input:checked + span { border-color: var(--primary); background: var(--primary); color: #fff; } .scale__step input:focus-visible + span { outline: 3px solid var(--primary-soft); } .scale__labels { display: flex; justify-content: space-between; font-size: .85rem; color: var(--muted); } +/* star rating: stars up to the checked (or hovered) one are filled; :has() keeps the DOM order 1..n for arrow keys */ +.stars { display: flex; justify-content: center; margin: 20px 0 6px; } +.stars__star { position: relative; padding: 0 4px; cursor: pointer; font-size: clamp(2.4rem, 13vw, 3.4rem); line-height: 1; color: var(--star); } +.stars--many .stars__star { padding: 0 1px; font-size: clamp(1.5rem, 7.5vw, 2.4rem); } +.stars__star input { position: absolute; inset: 0; width: 100%; height: 100%; margin: 0; opacity: 0; cursor: pointer; } +.stars__icon { display: block; transition: color .15s, transform .15s; } +/* :where() keeps these weaker than the hover preview below */ +:where(.stars:not(:has(input:checked))) .stars__star, :where(.stars__star:has(input:checked)) ~ .stars__star { color: var(--star-empty); } +.stars__star:has(input:focus-visible) .stars__icon { outline: 3px solid var(--primary); outline-offset: 2px; border-radius: 6px; } +@media (hover: hover) { + .stars:hover .stars__star { color: var(--star); } + .stars .stars__star:hover ~ .stars__star { color: var(--star-empty); } + .stars__star:hover .stars__icon { transform: scale(1.12); } +} .done { font-size: 1.1rem; font-weight: 600; color: var(--ok); } .waiting { padding: 48px 24px; } .pulse { width: 18px; height: 18px; border-radius: 50%; background: var(--primary); margin: 0 auto 24px; animation: pulse 1.6s infinite; } @@ -432,6 +452,12 @@ textarea { resize: vertical; } .average__track { position: relative; height: 12px; border-radius: 99px; background: linear-gradient(90deg, var(--bg), var(--c1)); box-shadow: inset 0 0 0 1px var(--border); } .average__marker { position: absolute; top: 50%; width: 1.6em; height: 1.6em; margin: -.8em 0 0 -.8em; border-radius: 50%; background: var(--c1); border: .25em solid var(--bg); box-shadow: 0 1px 4px rgba(0, 0, 0, .35); transition: left .6s ease; } .average__ends { display: flex; justify-content: space-between; margin-top: 8px; font-size: .85rem; font-weight: 700; color: var(--muted); } +/* star rating results: average as partly filled stars, bars from most to fewest stars */ +.star-meter { position: relative; display: inline-block; font-size: 2.6rem; line-height: 1; letter-spacing: .08em; white-space: nowrap; } +.star-meter__base { color: var(--star-empty); } +.star-meter__fill { position: absolute; top: 0; left: 0; overflow: hidden; color: var(--star); transition: width .6s ease; } +.bar--star .bar__fill { background: var(--star); } +.stars-legend { margin: 10px 0 0; font-size: .85rem; color: var(--muted); } .cloud { display: flex; flex-wrap: wrap; justify-content: center; align-items: center; gap: .2em .6em; line-height: 1.15; padding: 8px; } .cloud__word { font-weight: 800; color: var(--c0); transition: font-size .6s ease; } @@ -576,6 +602,7 @@ button.qa-steps__step:hover:not(:disabled) { color: var(--text); border-color: v .scale-table th, .scale-table td { text-align: left; padding: 8px 10px; border-bottom: 1px solid var(--border); vertical-align: middle; } .scale-table td:last-child { width: 50%; } .scale-table__bar { display: block; height: 6px; min-width: 2px; margin-bottom: 4px; border-radius: 3px; background: var(--c2); } +.rating-stars { color: var(--star); letter-spacing: .05em; white-space: nowrap; } /* ---------------------------------------------------------------- presenter */ /* Dark by default (beamer); the presenter's own switch can make it light (data-theme="light"). */ @@ -588,6 +615,8 @@ body.present { --stage-glow: #2a1f6e; --stage-ok: #3ddc97; /* brighter green for contrast on the dark stage */ --overlay-bg: rgba(8, 9, 20, .92); + --star: #ffc233; + --star-empty: #3a3d60; background: radial-gradient(1200px 600px at 10% -10%, var(--stage-glow) 0%, transparent 60%), var(--bg); color: var(--text); overflow: hidden; @@ -602,6 +631,8 @@ body.present { --stage-glow: #dcd8ff; --stage-ok: var(--ok); --overlay-bg: rgba(245, 246, 251, .95); + --star: #d48500; + --star-empty: #c3c8da; } .stage__top { display: flex; justify-content: space-between; align-items: center; gap: 16px; font-size: clamp(1rem, 1.6vw, 1.4rem); color: var(--muted); } .stage__top strong { color: var(--text); } @@ -627,6 +658,9 @@ body.present { .stage .average__label, .stage .average__count, .stage .average__ends { font-size: clamp(.85rem, 1.2vw, 1.1rem); } .stage .average__track { height: .7em; box-shadow: none; background: linear-gradient(90deg, var(--surface-2), var(--c1)); } .stage .average__ends { margin-top: 4px; } +.stage .star-meter { font-size: clamp(2.4rem, 5.5vw, 5rem); } +.stage .bars--stars { gap: 1.2vh; } +.stage .stars-legend { font-size: clamp(.85rem, 1.2vw, 1.1rem); } .stage .cloud { font-size: clamp(1.4rem, 2.6vw, 2.6rem); } .stage .answers { columns: 340px; } .stage .quiz-timer { font-size: clamp(3rem, 7vw, 6rem); } diff --git a/webroot/js/plenario.js b/webroot/js/plenario.js index 4bb358e..95fc7c3 100644 --- a/webroot/js/plenario.js +++ b/webroot/js/plenario.js @@ -302,6 +302,7 @@ paused: false, timer: null, items: [], + format: String, node: null, // Countdown of the comment shown (drives the progress bar across re-renders). startedAt: 0, @@ -315,8 +316,10 @@ /** * @param {Array} items comments, oldest first + * @param {Function} format turns a rating into display text (number or stars) */ - render: function (items) { + render: function (items, format) { + Comments.format = format || String; // A different first comment means a different slide (or a reset): start over. var key = String(items[0].id); if (key !== Comments.key) { @@ -443,7 +446,7 @@ 'Kommentar ' + (Comments.index + 1) + ' von ' + Comments.items.length; text.textContent = item.text; var value = node.querySelector('[data-comment-value]'); - value.textContent = item.value === null || item.value === undefined ? '' : 'Bewertung: ' + item.value; + value.textContent = item.value === null || item.value === undefined ? '' : 'Bewertung: ' + Comments.format(item.value); value.hidden = value.textContent === ''; // Only announce changes the viewer triggered, not every automatic rotation. node.setAttribute('aria-live', Comments.paused ? 'polite' : 'off'); @@ -529,6 +532,9 @@ }, scale: function (results) { + if (results.stars) { + return Results.stars(results); + } var max = Math.max.apply(null, results.buckets.map(function (b) { return b.count; }).concat([1])); var total = results.buckets.reduce(function (sum, b) { return sum + b.count; }, 0); var hasAverage = results.average !== null; @@ -575,10 +581,68 @@ ]), gauge ]); - var comments = results.comments && results.comments.length ? Comments.render(results.comments) : null; + var comments = results.comments && results.comments.length ? Comments.render(results.comments, String) : null; return el('div', { class: 'scale-results' }, [average, columns, labels, comments]); }, + /** + * Star rating (scale with stars): average as partly filled stars, distribution as + * horizontal bars from most to fewest stars, like a product rating. + */ + stars: function (results) { + var max = Math.max.apply(null, results.buckets.map(function (b) { return b.count; }).concat([1])); + var total = results.buckets.reduce(function (sum, b) { return sum + b.count; }, 0); + var hasAverage = results.average !== null; + var averageText = hasAverage + ? Number(results.average).toLocaleString('de-DE', { minimumFractionDigits: 1, maximumFractionDigits: 1 }) + : '–'; + var row = Array(results.max + 1).join('★'); + var meter = el('div', { + class: 'star-meter', + role: 'img', + 'aria-label': 'Durchschnitt ' + averageText + ' von ' + results.max + ' Sternen' + }, [ + // Same glyphs twice: the filled layer is clipped to the average. + el('span', { class: 'star-meter__base', 'aria-hidden': 'true', text: row }), + el('span', { + class: 'star-meter__fill', + 'aria-hidden': 'true', + style: 'width:' + (hasAverage ? results.average / results.max * 100 : 0) + '%', + text: row + }) + ]); + var average = el('div', { class: 'average average--stars' }, [ + el('div', { class: 'average__figure' }, [ + el('span', { class: 'average__label', text: 'Durchschnitt' }), + el('span', { class: 'average__value' }, [ + el('strong', { text: averageText }), + el('span', { class: 'average__of', text: ' von ' + results.max }) + ]), + el('span', { class: 'average__count', text: total + (total === 1 ? ' Bewertung' : ' Bewertungen') }) + ]), + meter + ]); + var bars = el('div', { class: 'bars bars--stars' }, results.buckets.slice().reverse().map(function (bucket) { + return el('div', { class: 'bar bar--star' }, [ + el('div', { class: 'bar__label', text: bucket.value + (bucket.value === 1 ? ' Stern' : ' Sterne') }), + el('div', { class: 'bar__track' }, [ + el('div', { class: 'bar__fill', style: 'width:' + (bucket.count / max * 100) + '%' }) + ]), + el('div', { class: 'bar__value', text: formatNumber(bucket.percent) + ' % (' + bucket.count + ')' }) + ]); + })); + var labels = results.minLabel || results.maxLabel + ? el('p', { class: 'stars-legend', text: '1 Stern: ' + (results.minLabel || '–') + ' · ' + results.max + ' Sterne: ' + (results.maxLabel || '–') }) + : null; + var comments = results.comments && results.comments.length + ? Comments.render(results.comments, function (value) { + return row.slice(0, value) + Array(results.max - value + 1).join('☆') + + ' (' + value + ' von ' + results.max + ')'; + }) + : null; + return el('div', { class: 'scale-results' }, [average, bars, labels, comments]); + }, + /** * options: {mine: [keys], down: [keys], onUpvote: fn(word)|null, onDownvote: fn(word)|null} * Counts are shown as "+up" and "−down" (down only when > 0 or clickable). @@ -2196,6 +2260,33 @@ }); } + // ------------------------------------------------------------------ scale style (slide form) + + /** + * Stars always start at one star: hide "Von" and call "Bis" the number of stars. + * Without JS both fields stay visible and the server ignores "Von" for stars. + */ + function initScaleStyle(fieldset) { + var form = fieldset.form || fieldset.closest('form'); + var min = form.querySelector('[data-scale-min]'); + var max = form.querySelector('[data-label-stars]'); + var maxLabel = max && form.querySelector('label[for="' + max.id + '"]'); + + function update() { + var checked = fieldset.querySelector('input[type=radio]:checked'); + var stars = checked !== null && checked.value === 'stars'; + if (min) { + min.hidden = stars; + } + if (maxLabel) { + maxLabel.textContent = max.getAttribute(stars ? 'data-label-stars' : 'data-label-numbers'); + } + } + + fieldset.addEventListener('change', update); + update(); + } + // ------------------------------------------------------------------ theme switch var THEME_ICONS = { auto: '◐', light: '☀', dark: '☾' }; @@ -2264,6 +2355,7 @@ document.querySelectorAll('[data-sortable]').forEach(initSortable); document.querySelectorAll('[data-order-options]').forEach(initOrderOptions); document.querySelectorAll('[data-order-form]').forEach(initOrderForm); + document.querySelectorAll('[data-scale-style]').forEach(initScaleStyle); // Prevent double submits of vote forms. document.querySelectorAll('.vote-form').forEach(function (form) {