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>
This commit is contained in:
1 parent
93aeae1884
commit
1c509e31c2
14 files changed
+972
-24
No files matched your search
@@ -5,6 +5,7 @@ namespace App\Controller;
|
||||
|
||||
use App\Model\Entity\Slide;
|
||||
use App\Model\Table\SlidesTable;
|
||||
use App\Service\YouTubeService;
|
||||
use Cake\Http\Exception\NotFoundException;
|
||||
use Cake\Http\Response;
|
||||
|
||||
@@ -208,7 +209,11 @@ class SlidesController extends AdminAppController
|
||||
// Quiz voting is controlled by the presenter only.
|
||||
$fields = array_values(array_diff($fields, ['is_voting_open']));
|
||||
}
|
||||
$youtube = $slide->isYouTube() ? $this->prepareYouTube($data) : ['errors' => [], 'warning' => null];
|
||||
$this->Slides->patchEntity($slide, $data, ['fields' => $fields, 'associated' => []]);
|
||||
foreach ($youtube['errors'] as $field => $message) {
|
||||
$slide->setError($field, $message);
|
||||
}
|
||||
|
||||
$labels = SlidesTable::parseOptionLines($optionsText);
|
||||
$correct = [];
|
||||
@@ -231,15 +236,106 @@ class SlidesController extends AdminAppController
|
||||
return false;
|
||||
}
|
||||
|
||||
return (bool)$this->Slides->getConnection()->transactional(function () use ($slide, $labels, $correct): bool {
|
||||
return (bool)$this->Slides->getConnection()->transactional(function () use ($slide, $labels, $correct, $youtube): bool {
|
||||
if (!$this->Slides->save($slide)) {
|
||||
return false;
|
||||
}
|
||||
if ($slide->type === Slide::TYPE_CHOICE || $slide->isQuiz()) {
|
||||
$this->Slides->syncOptions($slide, $labels, $correct);
|
||||
}
|
||||
if ($youtube['warning'] !== null) {
|
||||
$this->Flash->warning($youtube['warning']);
|
||||
}
|
||||
|
||||
return true;
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks a YouTube link (AJAX, "Link prüfen" in the slide form). Expects `url`
|
||||
* and optionally `start`, answers with JSON.
|
||||
*
|
||||
* @return \Cake\Http\Response
|
||||
*/
|
||||
public function youtubeCheck(): Response
|
||||
{
|
||||
$this->request->allowMethod(['post']);
|
||||
$url = trim((string)$this->request->getData('url', ''));
|
||||
$parsed = YouTubeService::parseUrl($url);
|
||||
if ($parsed === null) {
|
||||
return $this->json([
|
||||
'ok' => false,
|
||||
'message' => $url === '' ? __('Bitte gib einen YouTube-Link ein.') : $this->invalidYouTubeMessage(),
|
||||
]);
|
||||
}
|
||||
|
||||
$check = (new YouTubeService())->check($parsed['videoId']);
|
||||
$ok = $check['status'] === YouTubeService::STATUS_OK || $check['status'] === YouTubeService::STATUS_UNREACHABLE;
|
||||
$settings = SlidesTable::sanitizeSettings(Slide::TYPE_YOUTUBE, [
|
||||
'url' => $url,
|
||||
'start' => (string)$this->request->getData('start', ''),
|
||||
]);
|
||||
|
||||
return $this->json([
|
||||
'ok' => $ok,
|
||||
'status' => $check['status'],
|
||||
'message' => YouTubeService::statusMessage($check['status']),
|
||||
'title' => $check['title'],
|
||||
'linkStart' => $parsed['start'] !== null ? YouTubeService::formatTime($parsed['start']) : null,
|
||||
'start' => YouTubeService::formatTime($settings['start']),
|
||||
'embedUrl' => $ok ? YouTubeService::embedUrl($parsed['videoId'], $settings['start']) : null,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Validates the YouTube settings of the posted slide data and asks YouTube
|
||||
* whether the video can be embedded. An empty title is filled with the video title.
|
||||
*
|
||||
* @param array<string, mixed> $data Request data (question may be filled in)
|
||||
* @return array{errors: array<string, string>, warning: string|null}
|
||||
*/
|
||||
protected function prepareYouTube(array &$data): array
|
||||
{
|
||||
$settings = is_array($data['settings'] ?? null) ? $data['settings'] : [];
|
||||
$url = trim((string)($settings['url'] ?? ''));
|
||||
$start = trim((string)($settings['start'] ?? ''));
|
||||
$errors = [];
|
||||
$warning = null;
|
||||
|
||||
if ($start !== '' && YouTubeService::parseStartInput($start) === null) {
|
||||
$errors['youtube_start'] = __('Bitte gib die Startzeit als Minute:Sekunde an, z. B. 1:30 (oder nur die Minute).');
|
||||
}
|
||||
|
||||
$parsed = YouTubeService::parseUrl($url);
|
||||
if ($parsed === null) {
|
||||
$errors['youtube_url'] = $url === '' ? __('Bitte gib einen YouTube-Link ein.') : $this->invalidYouTubeMessage();
|
||||
|
||||
return ['errors' => $errors, 'warning' => null];
|
||||
}
|
||||
|
||||
$check = (new YouTubeService())->check($parsed['videoId']);
|
||||
switch ($check['status']) {
|
||||
case YouTubeService::STATUS_OK:
|
||||
if (trim((string)($data['question'] ?? '')) === '' && $check['title'] !== null) {
|
||||
$data['question'] = mb_substr($check['title'], 0, 500);
|
||||
}
|
||||
break;
|
||||
case YouTubeService::STATUS_UNREACHABLE:
|
||||
$warning = YouTubeService::statusMessage($check['status'])
|
||||
. ' ' . __('Bitte teste das Video vor deiner Präsentation.');
|
||||
break;
|
||||
default:
|
||||
$errors['youtube_url'] = YouTubeService::statusMessage($check['status']);
|
||||
}
|
||||
|
||||
return ['errors' => $errors, 'warning' => $warning];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
protected function invalidYouTubeMessage(): string
|
||||
{
|
||||
return __('Das ist kein gültiger YouTube-Link. Kopiere die Adresse des Videos, z. B. https://www.youtube.com/watch?v=dQw4w9WgXcQ oder https://youtu.be/dQw4w9WgXcQ.');
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user