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

+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).
*