Add duplicating slides

Copies question, description, settings and options (incl. correct quiz
answers) directly behind the original, without responses. The copy starts
like a new slide (Slide::startFresh()) and opens in the edit form.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
gnilebeinandClaude Opus 5.5 committed 2026-11-11 15:18:10 +01:00
1 parent 5b3fccde67
commit e2f24877dd
7 files changed
+214 -5

No files matched your search

+17 -4
View File
@@ -43,10 +43,8 @@ class SlidesController extends AdminAppController
'validate' => false,
]);
$slide->poll_id = $this->poll->id;
// A quiz is started by the presenter (that also starts its timer).
$slide->is_voting_open = $type !== Slide::TYPE_QUIZ;
// Q&A starts by collecting questions.
$slide->stage = $type === Slide::TYPE_QA ? Slide::QA_COLLECT : null;
// A quiz is started by the presenter (that also starts its timer), Q&A starts by collecting questions.
$slide->startFresh();
$optionsText = $type === Slide::TYPE_CHOICE ? "\n" : '';
$quizOptions = [];
$orderOptions = [];
@@ -126,6 +124,21 @@ class SlidesController extends AdminAppController
return $this->redirect($this->adminUrl());
}
/**
* Copies a slide (without responses) directly behind the original and opens the copy for editing.
*
* @param string $id Slide id
* @return \Cake\Http\Response|null
*/
public function duplicate(string $id): ?Response
{
$this->request->allowMethod(['post']);
$copy = $this->Slides->duplicate($this->loadSlide($id));
$this->Flash->success(__('Folie dupliziert. Du bearbeitest jetzt die Kopie.'));
return $this->redirect($this->adminUrl('slides:edit', ['id' => $copy->id]));
}
/**
* Saves a complete new slide order (drag & drop), expects `ids[]` and answers with JSON.
*
+15
View File
@@ -408,6 +408,21 @@ class Slide extends Entity
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;
}
/**
* Opens or closes voting. Opening a quiz that is not running (re)starts its
* voting window and timer; opening a running quiz changes nothing.
+41
View File
@@ -6,6 +6,7 @@ namespace App\Model\Table;
use App\Model\Entity\Slide;
use App\Service\YouTubeService;
use ArrayObject;
use Cake\Database\Expression\QueryExpression;
use Cake\Datasource\EntityInterface;
use Cake\Event\EventInterface;
use Cake\ORM\RulesChecker;
@@ -323,6 +324,46 @@ class SlidesTable extends Table
});
}
/**
* Copies a slide incl. its settings and options (with the correct answers) and
* puts the copy directly behind the original. Responses are not copied: the copy
* starts like a new slide (quiz not started, Q&A collecting, others open).
*
* @param \App\Model\Entity\Slide $slide Original incl. slide_options
* @return \App\Model\Entity\Slide The saved copy
*/
public function duplicate(Slide $slide): Slide
{
$copy = $this->newEmptyEntity();
$copy->poll_id = $slide->poll_id;
$copy->type = $slide->type;
$copy->question = mb_substr(__('{0} (Kopie)', $slide->question), 0, 500);
$copy->description = $slide->description;
$copy->settings = $slide->settings;
$copy->position = $slide->position + 1;
$copy->startFresh();
return $this->getConnection()->transactional(function () use ($slide, $copy): Slide {
$this->updateAll(
['position' => new QueryExpression('position + 1')],
['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->normalizePositions($slide->poll_id);
return $this->get($copy->id, contain: ['SlideOptions']);
});
}
/**
* Applies a complete new order (e.g. from drag & drop).
*