Files
Plenario/src/Model/Entity/Slide.php
T
gnilebeinandClaude Sonnet 5.5 4caff47a09
CI / Deploy / PHP 8.5 (push) Successful in 1m24s
CI / Deploy / PHP 8.4 (push) Successful in 1m27s
CI / Deploy / test (push) Successful in 2s
CI / Deploy / deploy (push) Skipped
Drop the image description field
Slide images are shown on the presenter's screen, so a description for
screen readers is not asked for any more. The column stays (always empty).

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-15 13:59:33 +01:00

605 lines
20 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 bool $is_hidden Prepared, but skipped in the presentation (never live)
* @property array<string, mixed>|null $settings
* @property string|null $image File name of the slide image in the poll's upload directory (see ImageService)
* @property string|null $image_alt Always '' (decorative): images are shown on the stage, no description is asked for
* @property string|null $image_layout top|side|full, null = default of the type (see imageLayout())
* @property int|null $image_width Pixels of the stored (scaled) image
* @property int|null $image_height
* @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 one participant may give a single question.
*/
public const QA_MAX_UPVOTES = 10;
/**
* Selectable shares (percent of the questions) that make up each participant's upvote budget.
*
* @var list<int>
*/
public const QA_UPVOTE_SHARES = [10, 20, 25, 33, 50];
/**
* Default settings per type. Unknown keys are dropped on save.
*
* @var array<string, array<string, mixed>>
*/
public const DEFAULT_SETTINGS = [
// comments: optional comment field (stored in text_value of the participant's first row),
// comments rotate in the presentation like those of a scale
self::TYPE_CHOICE => ['multiple' => false, 'comments' => 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],
// upvote_share: percent of the questions each participant may upvote in the voting stage, so the
// budget grows with the number of questions (qaBudget(); 0 = no voting stage, else QA_UPVOTE_SHARES);
// max_upvotes_per_question: own upvotes per question (0 = no limit besides the budget);
// allow_self_upvote: upvotes on own questions (for prioritizing topics, off by default)
self::TYPE_QA => ['upvote_share' => 25, '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 => [],
];
/**
* Where the image of a slide goes: above the results, next to the text (presenter only,
* participants' phones always show it on top) or - content slides - as large as possible.
*/
public const IMAGE_TOP = 'top';
public const IMAGE_SIDE = 'side';
public const IMAGE_FULL = 'full';
public const IMAGE_LAYOUTS = [self::IMAGE_TOP, self::IMAGE_SIDE, self::IMAGE_FULL];
/**
* @var array<string, bool>
*/
protected array $_accessible = [
'type' => true,
'question' => true,
'description' => true,
'settings' => true,
'is_voting_open' => true,
];
/**
* Whether slides of this type can carry an image (everything but YouTube, which has its own media).
*
* @param string $type Slide type
* @return bool
*/
public static function supportsImage(string $type): bool
{
return $type !== self::TYPE_YOUTUBE;
}
/**
* @return bool
*/
public function hasImage(): bool
{
return $this->image !== null && $this->image !== '';
}
/**
* Layout of the image: the chosen one, otherwise by type - next to the text on content slides,
* above the question's answers elsewhere. "full" only exists for content slides.
*
* @return string One of IMAGE_LAYOUTS
*/
public function imageLayout(): string
{
$layout = $this->image_layout;
if (!in_array($layout, self::IMAGE_LAYOUTS, true) || ($layout === self::IMAGE_FULL && $this->type !== self::TYPE_CONTENT)) {
return $this->type === self::TYPE_CONTENT ? self::IMAGE_SIDE : self::IMAGE_TOP;
}
return $layout;
}
/**
* @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 `upvote_share` > 0).
*
* @return bool
*/
public function hasQaUpvotes(): bool
{
return (int)$this->setting('upvote_share') > 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 each participant gets in the voting stage: `upvote_share` percent of the questions,
* rounded up and at least one (0 without questions or without a voting stage).
*
* @param int $questionCount Number of questions on the slide
* @return int
*/
public function qaBudget(int $questionCount): int
{
if ($questionCount <= 0 || !$this->hasQaUpvotes()) {
return 0;
}
return max(1, (int)ceil($questionCount * (int)$this->setting('upvote_share') / 100));
}
/**
* Upvotes one participant may give a single Q&A question (never more than the budget).
*
* @param int $budget Upvotes per participant, see qaBudget()
* @return int
*/
public function qaUpvotesPerQuestion(int $budget): int
{
$perQuestion = (int)$this->setting('max_upvotes_per_question');
return $perQuestion > 0 ? min($perQuestion, $budget) : $budget;
}
/**
* 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());
}
/**
* Sets the state of a slide that has not been presented yet (new slide, copy):
* a quiz waits for the presenter to start it, Q&A collects questions, others are open.
*
* @return void
*/
public function startFresh(): void
{
$this->is_voting_open = !$this->isQuiz();
$this->voting_started_at = null;
$this->voting_ends_at = null;
$this->stage = $this->isQa() ? self::QA_COLLECT : null;
$this->focus_response_id = null;
}
/**
* Whether the slide is in the state startFresh() sets (nothing started, closed or discussed yet).
* A reset has nothing to do for such a slide unless it has answers.
*
* @return bool
*/
public function isFresh(): bool
{
return $this->is_voting_open === !$this->isQuiz()
&& $this->voting_started_at === null
&& $this->voting_ends_at === null
&& $this->stage === ($this->isQa() ? self::QA_COLLECT : null)
&& $this->focus_response_id === null;
}
/**
* 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');
}
/**
* Multiple choice or scale with an optional comment field for participants.
*
* @return bool
*/
public function hasComments(): bool
{
return in_array($this->type, [self::TYPE_CHOICE, self::TYPE_SCALE], true) && (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);
}
}