Add admin menu drawer and poll cloning, drop participant screen tools
- Remove fullscreen and wake lock buttons from the participant view. - Turn the admin sidebar into a drawer below 900px, opened from a header menu button next to the theme switch (progressive enhancement). - Add "Umfrage kopieren": clone a poll with all slides, options and images but without responses, with its own join code and admin link. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
0f91377de3
commit
bd57f23be6
15 files changed
+374
-138
No files matched your search
@@ -20,6 +20,7 @@ use Cake\Routing\Router;
|
||||
use chillerlan\QRCode\Output\QRMarkupSVG;
|
||||
use chillerlan\QRCode\QRCode;
|
||||
use chillerlan\QRCode\QROptions;
|
||||
use Throwable;
|
||||
|
||||
/**
|
||||
* Poll level admin actions: dashboard, settings, presenter mode, export.
|
||||
@@ -141,6 +142,43 @@ class AdminController extends AdminAppController
|
||||
return $this->redirect($this->adminUrl());
|
||||
}
|
||||
|
||||
/**
|
||||
* Clones the whole presentation (slides, settings, images) without any responses into a new poll with
|
||||
* its own admin link, which is mailed to the owner like after creating a poll.
|
||||
*
|
||||
* @return \Cake\Http\Response|null
|
||||
*/
|
||||
public function cloneAll(): ?Response
|
||||
{
|
||||
$this->request->allowMethod(['post']);
|
||||
if (!$this->throttle('create', 'Plenario.throttle.createPerHour')) {
|
||||
$this->Flash->error(__('Zu viele Umfragen in kurzer Zeit. Bitte versuche es später noch einmal.'));
|
||||
|
||||
return $this->redirect($this->adminUrl());
|
||||
}
|
||||
|
||||
try {
|
||||
[$copy, $token] = $this->Polls->clonePoll($this->poll, $this->Slides->listForPoll($this->poll->id));
|
||||
} catch (Throwable) {
|
||||
$token = null;
|
||||
}
|
||||
if ($token === null) {
|
||||
$this->Flash->error(__('Die Umfrage konnte nicht kopiert werden.'));
|
||||
|
||||
return $this->redirect($this->adminUrl());
|
||||
}
|
||||
|
||||
$adminUrl = $this->adminUrlFor($token);
|
||||
$sent = (new PollMailer())->sendSafely('adminLink', [$copy, $adminUrl, $this->joinUrl($copy)]);
|
||||
if ($sent) {
|
||||
$this->Flash->success(__('Kopie angelegt, ohne Antworten. Der Admin-Link der Kopie wurde per E-Mail verschickt – speichere dir diese Seite als Lesezeichen.'));
|
||||
} else {
|
||||
$this->Flash->warning(__('Kopie angelegt, ohne Antworten. Die E-Mail mit dem Admin-Link konnte nicht verschickt werden – speichere dir diese Seite unbedingt als Lesezeichen!'));
|
||||
}
|
||||
|
||||
return $this->redirect($this->adminUrlFor($token));
|
||||
}
|
||||
|
||||
/**
|
||||
* Deletes the whole poll incl. slides and responses.
|
||||
*
|
||||
|
||||
@@ -257,6 +257,37 @@ class PollsTable extends Table
|
||||
return [$poll, $token];
|
||||
}
|
||||
|
||||
/**
|
||||
* Clones a poll: new poll (same e-mail, new admin token and join code) with a copy of all slides,
|
||||
* but without any responses, and not started. Nothing is left behind if copying fails.
|
||||
*
|
||||
* @param \App\Model\Entity\Poll $poll Original
|
||||
* @param list<\App\Model\Entity\Slide> $slides Slides of the original incl. slide_options, in order
|
||||
* @return array{0: \App\Model\Entity\Poll, 1: string|null} The new poll and its plain token (null on failure)
|
||||
*/
|
||||
public function clonePoll(Poll $poll, array $slides): array
|
||||
{
|
||||
$copy = $this->newPoll([
|
||||
'title' => mb_substr(__('{0} (Kopie)', $poll->title), 0, 200),
|
||||
'email' => $poll->email,
|
||||
]);
|
||||
if ($copy->hasErrors()) {
|
||||
return [$copy, null];
|
||||
}
|
||||
$copy->set('comment_speed', $poll->comment_speed);
|
||||
|
||||
$token = null;
|
||||
$this->getConnection()->transactional(function () use ($copy, $slides, &$token): void {
|
||||
[, $token] = $this->insertPoll($copy);
|
||||
if ($token === null) {
|
||||
throw new RuntimeException('Poll could not be saved');
|
||||
}
|
||||
$this->Slides->copyAllToPoll($slides, $copy->id);
|
||||
});
|
||||
|
||||
return [$copy, $token];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \Cake\ORM\Query\SelectQuery $query Query
|
||||
* @param string $token Plain admin token
|
||||
|
||||
@@ -353,18 +353,75 @@ class SlidesTable extends Table
|
||||
*/
|
||||
public function duplicate(Slide $slide): Slide
|
||||
{
|
||||
$copy = $this->newEmptyEntity();
|
||||
$copy->poll_id = $slide->poll_id;
|
||||
$copy->type = $slide->type;
|
||||
$images = new ImageService();
|
||||
$copy = $this->prepareCopy($slide, $slide->poll_id, $slide->position + 1, $images);
|
||||
$copy->question = mb_substr(__('{0} (Kopie)', $slide->question), 0, 500);
|
||||
|
||||
try {
|
||||
return $this->persistCopy($slide, $copy);
|
||||
} catch (Throwable $e) {
|
||||
// Rolled back: the copied file belongs to no slide.
|
||||
$images->delete($slide->poll_id, $copy->image);
|
||||
|
||||
throw $e;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Copies all slides of a poll (settings, options, correct answers, images, hidden flag) into another poll,
|
||||
* in the same order. Responses are not copied. All or nothing: on failure no slide and no image file remains.
|
||||
*
|
||||
* @param list<\App\Model\Entity\Slide> $slides Original slides incl. slide_options, in order
|
||||
* @param int $targetPollId Poll that receives the copies
|
||||
* @return void
|
||||
*/
|
||||
public function copyAllToPoll(array $slides, int $targetPollId): void
|
||||
{
|
||||
$images = new ImageService();
|
||||
$files = [];
|
||||
try {
|
||||
$this->getConnection()->transactional(function () use ($slides, $targetPollId, $images, &$files): void {
|
||||
foreach ($slides as $index => $slide) {
|
||||
$copy = $this->prepareCopy($slide, $targetPollId, $index + 1, $images);
|
||||
if ($copy->image !== null) {
|
||||
$files[] = $copy->image;
|
||||
}
|
||||
$copy->is_hidden = $slide->is_hidden;
|
||||
$this->saveOrFail($copy);
|
||||
$this->copyOptions($slide, $copy);
|
||||
}
|
||||
});
|
||||
} catch (Throwable $e) {
|
||||
foreach ($files as $file) {
|
||||
$images->delete($targetPollId, $file);
|
||||
}
|
||||
|
||||
throw $e;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Builds an unsaved copy of a slide for a poll; a fresh slide (nothing started) with its own image file.
|
||||
*
|
||||
* @param \App\Model\Entity\Slide $slide Original
|
||||
* @param int $pollId Poll of the copy
|
||||
* @param int $position Position of the copy
|
||||
* @param \App\Service\ImageService $images Image storage
|
||||
* @return \App\Model\Entity\Slide
|
||||
*/
|
||||
protected function prepareCopy(Slide $slide, int $pollId, int $position, ImageService $images): Slide
|
||||
{
|
||||
$copy = $this->newEmptyEntity();
|
||||
$copy->poll_id = $pollId;
|
||||
$copy->type = $slide->type;
|
||||
$copy->question = $slide->question;
|
||||
$copy->description = $slide->description;
|
||||
$copy->settings = $slide->settings;
|
||||
$copy->position = $slide->position + 1;
|
||||
$copy->position = $position;
|
||||
$copy->startFresh();
|
||||
|
||||
// The copy gets its own file, so deleting one slide never takes the image of the other.
|
||||
$images = new ImageService();
|
||||
$imageCopy = $slide->hasImage() ? $images->copy($slide->poll_id, (string)$slide->image) : null;
|
||||
$imageCopy = $slide->hasImage() ? $images->copy($pollId, (string)$slide->image, $slide->poll_id) : null;
|
||||
if ($imageCopy !== null) {
|
||||
$copy->image = $imageCopy;
|
||||
$copy->image_alt = $slide->image_alt;
|
||||
@@ -373,13 +430,26 @@ class SlidesTable extends Table
|
||||
$copy->image_height = $slide->image_height;
|
||||
}
|
||||
|
||||
try {
|
||||
return $this->persistCopy($slide, $copy);
|
||||
} catch (Throwable $e) {
|
||||
// Rolled back: the copied file belongs to no slide.
|
||||
$images->delete($slide->poll_id, $imageCopy);
|
||||
return $copy;
|
||||
}
|
||||
|
||||
throw $e;
|
||||
/**
|
||||
* Copies the options (with the correct answers) of a slide to its saved copy.
|
||||
*
|
||||
* @param \App\Model\Entity\Slide $slide Original incl. slide_options
|
||||
* @param \App\Model\Entity\Slide $copy Saved copy
|
||||
* @return void
|
||||
*/
|
||||
protected function copyOptions(Slide $slide, Slide $copy): void
|
||||
{
|
||||
foreach ($slide->slide_options ?? [] as $option) {
|
||||
$newOption = $this->SlideOptions->newEntity([
|
||||
'label' => $option->label,
|
||||
'position' => $option->position,
|
||||
]);
|
||||
$newOption->set('slide_id', $copy->id);
|
||||
$newOption->set('is_correct', $option->is_correct);
|
||||
$this->SlideOptions->saveOrFail($newOption);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -398,15 +468,7 @@ class SlidesTable extends Table
|
||||
['poll_id' => $slide->poll_id, 'position >' => $slide->position],
|
||||
);
|
||||
$this->saveOrFail($copy);
|
||||
foreach ($slide->slide_options ?? [] as $option) {
|
||||
$newOption = $this->SlideOptions->newEntity([
|
||||
'label' => $option->label,
|
||||
'position' => $option->position,
|
||||
]);
|
||||
$newOption->set('slide_id', $copy->id);
|
||||
$newOption->set('is_correct', $option->is_correct);
|
||||
$this->SlideOptions->saveOrFail($newOption);
|
||||
}
|
||||
$this->copyOptions($slide, $copy);
|
||||
$this->normalizePositions($slide->poll_id);
|
||||
|
||||
return $this->get($copy->id, contain: ['SlideOptions']);
|
||||
|
||||
@@ -119,19 +119,24 @@ class ImageService
|
||||
/**
|
||||
* Copies a stored image (duplicated slides get their own file, so deleting one never affects the other).
|
||||
*
|
||||
* @param int $pollId Poll id
|
||||
* @param int $pollId Poll id that receives the copy
|
||||
* @param string $name Source file name
|
||||
* @return string|null Name of the copy, null if the source file is gone
|
||||
* @param int|null $sourcePollId Poll id of the source file (default: the same poll)
|
||||
* @return string|null Name of the copy, null if the source file is gone or the copy failed
|
||||
*/
|
||||
public function copy(int $pollId, string $name): ?string
|
||||
public function copy(int $pollId, string $name, ?int $sourcePollId = null): ?string
|
||||
{
|
||||
$source = $this->path($pollId, $name);
|
||||
$source = $this->path($sourcePollId ?? $pollId, $name);
|
||||
if ($source === null) {
|
||||
return null;
|
||||
}
|
||||
$directory = $this->directory($pollId);
|
||||
if (!is_dir($directory) && !@mkdir($directory, 0775, true) && !is_dir($directory)) {
|
||||
return null;
|
||||
}
|
||||
$copy = bin2hex(random_bytes(12)) . '.webp';
|
||||
|
||||
return @copy($source, $this->directory($pollId) . DS . $copy) ? $copy : null;
|
||||
return @copy($source, $directory . DS . $copy) ? $copy : null;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in new issue
Block a user