Files
Plenario/src/Model/Entity/Slide.php
T
gnilebeinandClaude Opus 5.5 1c509e31c2
CI / Deploy / test (push) Successful in 46s
CI / Deploy / deploy (push) Skipped
Add YouTube video slide type
Admins paste a YouTube link (watch, youtu.be, shorts, live, embed).
The link is checked via oEmbed on save and with a "check link" button
(not found / not embeddable is rejected, unreachable saves with a
warning). Start time can be entered or is taken from the link's
timecode; autoplay is optional. The presenter embeds the video via
youtube-nocookie.com; participants get the link plus a share button
(Web Share API, falls back to copying).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-07 15:08:12 +01:00

277 lines
8.6 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 \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';
/**
* 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];
/**
* 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],
self::TYPE_OPEN => [],
self::TYPE_SCALE => ['scale_min' => 1, 'scale_max' => 5, 'min_label' => '', 'max_label' => ''],
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],
];
/**
* @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_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_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;
}
/**
* 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;
}
}