Files
Plenario/src/Model/Entity/Slide.php
T
gnilebeinandClaude Opus 5.5 b3c80ab79f
CI / Deploy / test (push) Successful in 1m8s
CI / Deploy / deploy (push) Skipped
Show scale results as average next to a donut chart
Average and distribution now sit side by side: a big average figure with
its gauge or stars, and the distribution as a donut (CSS conic-gradient,
shades of one colour, legend with percent and count). A single dropdown
setting scale_view (both, average only, chart only) replaces the two
checkboxes; a single part is centred.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-11 13:38:58 +01:00

499 lines
16 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Model\Entity;
use App\Service\YouTubeService;
use Cake\I18n\DateTime;
use Cake\ORM\Entity;
/**
* A slide of a poll. `type` decides how participants answer and how results
* are aggregated (see \App\Service\ResultsService and \App\Service\VoteService).
*
* @property int $id
* @property int $poll_id
* @property string $type
* @property string $question
* @property string|null $description
* @property int $position
* @property array<string, mixed>|null $settings
* @property bool $is_voting_open
* @property \Cake\I18n\DateTime|null $voting_started_at Quiz: start of the voting window
* @property \Cake\I18n\DateTime|null $voting_ends_at Quiz: deadline (null = closed manually)
* @property string|null $stage Q&A: collect|vote|discuss (null = collect), see qaStage()
* @property int|null $focus_response_id Q&A: question currently being discussed
* @property \Cake\I18n\DateTime $created
* @property \Cake\I18n\DateTime $modified
* @property \App\Model\Entity\Poll $poll
* @property array<\App\Model\Entity\SlideOption> $slide_options
* @property array<\App\Model\Entity\Response> $responses
*/
class Slide extends Entity
{
public const TYPE_CHOICE = 'choice';
public const TYPE_WORDCLOUD = 'wordcloud';
public const TYPE_OPEN = 'open';
public const TYPE_SCALE = 'scale';
public const TYPE_CONTENT = 'content';
public const TYPE_QUIZ = 'quiz';
public const TYPE_YOUTUBE = 'youtube';
public const TYPE_QA = 'qa';
public const TYPE_ORDER = 'order';
/**
* Quiz phases, see quizPhase().
*/
public const QUIZ_READY = 'ready';
public const QUIZ_RUNNING = 'running';
public const QUIZ_FINISHED = 'finished';
/**
* Selectable quiz time limits in seconds (0 = presenter ends the quiz manually).
*
* @var list<int>
*/
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';
/**
* What a scale shows in the presentation: average and distribution (chart), or only one of them.
*/
public const SCALE_VIEW_BOTH = 'both';
public const SCALE_VIEW_AVERAGE = 'average';
public const SCALE_VIEW_CHART = 'chart';
public const SCALE_VIEWS = [self::SCALE_VIEW_BOTH, self::SCALE_VIEW_AVERAGE, self::SCALE_VIEW_CHART];
/**
* Q&A stages, see qaStage(): participants submit questions, then upvote
* them, then the presenter discusses them one by one (most upvoted first).
*/
public const QA_COLLECT = 'collect';
public const QA_VOTE = 'vote';
public const QA_DISCUSS = 'discuss';
/**
* @var list<string>
*/
public const QA_STAGES = [self::QA_COLLECT, self::QA_VOTE, self::QA_DISCUSS];
/**
* Highest selectable number of upvotes per participant on a Q&A slide.
*/
public const QA_MAX_UPVOTES = 10;
/**
* Default settings per type. Unknown keys are dropped on save.
*
* @var array<string, array<string, mixed>>
*/
public const DEFAULT_SETTINGS = [
self::TYPE_CHOICE => ['multiple' => false],
// allow_downvotes only takes effect together with allow_upvotes
self::TYPE_WORDCLOUD => ['allow_upvotes' => true, 'allow_downvotes' => true],
// private: anonymous feedback, answers only visible in the admin area (never on the stage)
self::TYPE_OPEN => ['private' => false],
// private: internal feedback, ratings only visible in the admin area (never on the stage)
// 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
// scale_view: parts of the result shown in the presentation (presenter/embed, SCALE_VIEWS);
// the evaluation in the admin area and the PDF export always contain both
self::TYPE_SCALE => [
'style' => self::SCALE_NUMBERS,
'scale_min' => 1, 'scale_max' => 5, 'min_label' => '', 'max_label' => '', 'private' => false,
'comments' => false, 'comment_ratings' => false,
'scale_view' => self::SCALE_VIEW_BOTH,
],
self::TYPE_CONTENT => [],
// time_limit in seconds, 0 = no limit (presenter ends the quiz)
self::TYPE_QUIZ => ['time_limit' => 30],
// url as entered, video_id parsed from it, start in seconds
self::TYPE_YOUTUBE => ['url' => '', 'video_id' => '', 'start' => 0, 'autoplay' => false],
// max_upvotes: upvotes per participant in the voting stage (0..QA_MAX_UPVOTES, 0 = no voting stage);
// max_upvotes_per_question: own upvotes per question (0 = no limit besides max_upvotes);
// allow_self_upvote: upvotes on own questions (for prioritizing topics, off by default)
self::TYPE_QA => ['max_upvotes' => 3, 'max_upvotes_per_question' => 0, 'allow_self_upvote' => false],
// The correct order is the order of the slide_options (position), revealed when voting closes.
self::TYPE_ORDER => [],
];
/**
* @var array<string, bool>
*/
protected array $_accessible = [
'type' => true,
'question' => true,
'description' => true,
'settings' => true,
'is_voting_open' => true,
];
/**
* @return array<string, string> type => human readable label
*/
public static function typeLabels(): array
{
return [
self::TYPE_CHOICE => __('Multiple Choice'),
self::TYPE_WORDCLOUD => __('Wortwolke'),
self::TYPE_OPEN => __('Offene Frage'),
self::TYPE_SCALE => __('Skala'),
self::TYPE_QUIZ => __('Quiz'),
self::TYPE_ORDER => __('Reihenfolge'),
self::TYPE_QA => __('Fragen & Antworten'),
self::TYPE_CONTENT => __('Inhaltsfolie'),
self::TYPE_YOUTUBE => __('YouTube-Video'),
];
}
/**
* @return array<string, string> type => short description
*/
public static function typeDescriptions(): array
{
return [
self::TYPE_CHOICE => __('Antwortoptionen vorgeben, Ergebnis als Balkendiagramm.'),
self::TYPE_WORDCLOUD => __('Teilnehmende geben Begriffe ein oder unterstützen vorhandene, häufige werden größer.'),
self::TYPE_OPEN => __('Freitext-Antworten, live als Liste.'),
self::TYPE_SCALE => __('Bewertung auf einer Skala, z. B. 1 bis 5.'),
self::TYPE_QUIZ => __('Frage mit richtiger Antwort und Zeitlimit, aufgelöst wird erst am Ende.'),
self::TYPE_ORDER => __('Einträge in die richtige Reihenfolge bringen, aufgelöst wird beim Schließen der Abstimmung.'),
self::TYPE_QA => __('Teilnehmende reichen Fragen ein und wählen per Upvote, welche zuerst beantwortet werden.'),
self::TYPE_CONTENT => __('Nur Text anzeigen, ohne Abstimmung.'),
self::TYPE_YOUTUBE => __('Video in der Präsentation abspielen, Teilnehmende bekommen den Link zum Teilen.'),
];
}
/**
* @return string
*/
public function typeLabel(): string
{
return static::typeLabels()[$this->type] ?? $this->type;
}
/**
* Whether participants can answer this slide at all.
*
* @return bool
*/
public function isInteractive(): bool
{
return !in_array($this->type, [self::TYPE_CONTENT, self::TYPE_YOUTUBE], true);
}
/**
* @return bool
*/
public function isYouTube(): bool
{
return $this->type === self::TYPE_YOUTUBE;
}
/**
* Link to the video on YouTube incl. start time, null without valid video.
*
* @return string|null
*/
public function youtubeWatchUrl(): ?string
{
$id = (string)$this->setting('video_id');
return $this->isYouTube() && $id !== '' ? YouTubeService::watchUrl($id, (int)$this->setting('start')) : null;
}
/**
* Player URL for the presenter screen, null without valid video.
*
* @return string|null
*/
public function youtubeEmbedUrl(): ?string
{
$id = (string)$this->setting('video_id');
return $this->isYouTube() && $id !== ''
? YouTubeService::embedUrl($id, (int)$this->setting('start'), (bool)$this->setting('autoplay'))
: null;
}
/**
* Whether answers are accepted right now: voting is open and the deadline
* of a timed quiz has not passed.
*
* @param \Cake\I18n\DateTime|null $now Reference time (default: now)
* @return bool
*/
public function isVotingOpen(?DateTime $now = null): bool
{
if (!$this->is_voting_open) {
return false;
}
return $this->voting_ends_at === null || ($now ?? DateTime::now())->lessThan($this->voting_ends_at);
}
/**
* Whether a timed quiz is still flagged open although its deadline has passed.
*
* @param \Cake\I18n\DateTime|null $now Reference time (default: now)
* @return bool
*/
public function isExpired(?DateTime $now = null): bool
{
return $this->is_voting_open && !$this->isVotingOpen($now);
}
/**
* @return bool
*/
public function isQuiz(): bool
{
return $this->type === self::TYPE_QUIZ;
}
/**
* @return bool
*/
public function isQa(): bool
{
return $this->type === self::TYPE_QA;
}
/**
* @return bool
*/
public function isOrder(): bool
{
return $this->type === self::TYPE_ORDER;
}
/**
* Ordering slide whose solution may be shown: voting is closed. While voting
* is open, neither the correct order nor any counts may leave the server.
*
* @return bool
*/
public function isOrderRevealed(): bool
{
return $this->isOrder() && !$this->isVotingOpen();
}
/**
* Ids of the answer options in their stored order (choice, quiz: display order;
* ordering slides: the correct order).
*
* @return list<int>
*/
public function optionIds(): array
{
return array_map(fn($option) => (int)$option->id, array_values($this->slide_options ?? []));
}
/**
* Options of an ordering slide in a stable pseudo-random order (same seed =
* same order, e.g. per participant across reloads). With two or more options
* the result never equals the correct order.
*
* @param string $seed Seed, e.g. participant hash or "slide:{id}"
* @return list<\App\Model\Entity\SlideOption>
*/
public function shuffledOptions(string $seed): array
{
$options = array_values($this->slide_options ?? []);
$correct = $this->optionIds();
usort($options, fn($a, $b) => strcmp(
hash('sha256', $seed . '|' . $a->id),
hash('sha256', $seed . '|' . $b->id),
));
if (count($options) > 1 && array_map(fn($option) => $option->id, $options) === $correct) {
$options[] = array_shift($options);
}
return $options;
}
/**
* Stage of a Q&A slide (one of the QA_* constants), see QaService.
*
* @return string
*/
public function qaStage(): string
{
return in_array($this->stage, self::QA_STAGES, true) ? (string)$this->stage : self::QA_COLLECT;
}
/**
* Whether a Q&A slide has a voting stage (setting `max_upvotes` > 0).
*
* @return bool
*/
public function hasQaUpvotes(): bool
{
return (int)$this->setting('max_upvotes') > 0;
}
/**
* Stages of a Q&A slide in order: without upvotes the voting stage is skipped.
*
* @return list<string>
*/
public function qaStages(): array
{
return $this->hasQaUpvotes() ? self::QA_STAGES : [self::QA_COLLECT, self::QA_DISCUSS];
}
/**
* Upvotes one participant may give a single Q&A question (never more than `max_upvotes`).
*
* @return int
*/
public function qaUpvotesPerQuestion(): int
{
$max = (int)$this->setting('max_upvotes');
$perQuestion = (int)$this->setting('max_upvotes_per_question');
return $perQuestion > 0 ? min($perQuestion, $max) : $max;
}
/**
* Whether a participant may answer the slide several times (word cloud: one
* row per word, Q&A: questions and upvotes).
*
* @return bool
*/
public function allowsMultipleResponses(): bool
{
return in_array($this->type, [self::TYPE_WORDCLOUD, self::TYPE_QA], true);
}
/**
* Phase of a quiz: ready (not started yet), running, finished (time up or
* closed by the presenter). Results and correct answers are only revealed
* when finished.
*
* @param \Cake\I18n\DateTime|null $now Reference time (default: now)
* @return string One of the QUIZ_* constants
*/
public function quizPhase(?DateTime $now = null): string
{
if ($this->isVotingOpen($now)) {
return self::QUIZ_RUNNING;
}
return $this->voting_started_at === null ? self::QUIZ_READY : self::QUIZ_FINISHED;
}
/**
* Seconds left of a running timed quiz, null without time limit.
*
* @param \Cake\I18n\DateTime|null $now Reference time (default: now)
* @return int|null
*/
public function remainingSeconds(?DateTime $now = null): ?int
{
if ($this->voting_ends_at === null) {
return null;
}
if (!$this->isVotingOpen($now)) {
return 0;
}
return max(0, $this->voting_ends_at->getTimestamp() - ($now ?? DateTime::now())->getTimestamp());
}
/**
* Opens or closes voting. Opening a quiz that is not running (re)starts its
* voting window and timer; opening a running quiz changes nothing.
*
* @param bool $open Open or close
* @param \Cake\I18n\DateTime|null $now Reference time (default: now)
* @return void
*/
public function setVotingOpen(bool $open, ?DateTime $now = null): void
{
$now ??= DateTime::now();
if ($open && $this->isQuiz() && !$this->isVotingOpen($now)) {
$limit = (int)$this->setting('time_limit');
$this->voting_started_at = $now;
$this->voting_ends_at = $limit > 0 ? $now->addSeconds($limit) : null;
}
$this->is_voting_open = $open;
}
/**
* Settings merged with the type defaults.
*
* @return array<string, mixed>
*/
public function getSettings(): array
{
$defaults = self::DEFAULT_SETTINGS[$this->type] ?? [];
return array_intersect_key(array_merge($defaults, (array)($this->settings ?? [])), $defaults);
}
/**
* @param string $key Setting name
* @return mixed
*/
public function setting(string $key): mixed
{
return $this->getSettings()[$key] ?? null;
}
/**
* Open question or scale collecting anonymous/internal feedback: answers and ratings are
* never shown on the presenter screen or to participants, only in the admin area and the complete version of the PDF export.
*
* @return bool
*/
public function isPrivate(): bool
{
return in_array($this->type, [self::TYPE_OPEN, self::TYPE_SCALE], true) && (bool)$this->setting('private');
}
/**
* Scale with an optional comment field for participants.
*
* @return bool
*/
public function hasComments(): bool
{
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);
}
}