|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 */ public const QUIZ_TIME_LIMITS = [0, 10, 15, 20, 30, 45, 60, 90, 120, 180, 300]; /** * Selectable seconds per comment when the comments of a scale rotate in the presentation. * * @var list */ public const COMMENT_INTERVALS = [3, 5, 8, 10, 15, 20, 30, 45, 60]; /** * 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 */ 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> */ 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 // comment_interval: seconds per comment while they rotate (one of COMMENT_INTERVALS) self::TYPE_SCALE => [ 'scale_min' => 1, 'scale_max' => 5, 'min_label' => '', 'max_label' => '', 'private' => false, 'comments' => false, 'comment_ratings' => false, 'comment_interval' => 8, ], 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], // upvotes per participant in the voting stage (1..QA_MAX_UPVOTES); // allow_self_upvote: upvotes on own questions (for prioritizing topics, off by default) self::TYPE_QA => ['max_upvotes' => 3, 'allow_self_upvote' => false], // The correct order is the order of the slide_options (position), revealed when voting closes. self::TYPE_ORDER => [], ]; /** * @var array */ protected array $_accessible = [ 'type' => true, 'question' => true, 'description' => true, 'settings' => true, 'is_voting_open' => true, ]; /** * @return array 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 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 */ 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 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 */ 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'); } }