diff --git a/AGENTS.md b/AGENTS.md index 88cd67d..09eb082 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -58,7 +58,7 @@ src/ HomeController Startseite, Anlegen (/create), Bestätigung, Join, Recover AdminAppController Basis für /admin/{token}: löst Poll über Token-Hash auf (404 sonst), setzt Privacy-Header (no-referrer, no-store) AdminController Dashboard, Settings, Present, State-JSON, Control, QR, PDF-Export, Token rotieren, Zurücksetzen, Löschen - SlidesController Folien-CRUD, Antworten offener Fragen bzw. Skala-Auswertung (`answers`), Sortieren (Drag & Drop/Pfeiltasten via `reorder`, JSON; erfordert JS), Antworten zurücksetzen, + SlidesController Folien-CRUD, Duplizieren (`duplicate` → `SlidesTable::duplicate()`, Kopie direkt dahinter, öffnet das Formular der Kopie), Antworten offener Fragen bzw. Skala-Auswertung (`answers`), Sortieren (Drag & Drop/Pfeiltasten via `reorder`, JSON; erfordert JS), Antworten zurücksetzen, YouTube-Link prüfen (`youtubeCheck`, JSON) ParticipateController /p/{code}: Ansicht, State-JSON, Abstimmen, eigene Antwort/Kommentar bearbeiten; anonymes Cookie plenario_pid EmbedController /e/{key}/{id}: eine Folie zum Einbetten (z. B. Canva), activate/heartbeat/pause/control @@ -195,6 +195,10 @@ tests/ PHPUnit; PlenarioTestTrait erzeugt Testdaten, Fixtures (`admin:reset`, Dashboard → „Sicherheit“). Nur Quiz (`ready`) und Q&A (`collect`) springen zurück, weil ihre Phasen ohne Antworten keinen Sinn ergeben; offen/geschlossen anderer Folien, aktuelle Folie/Pause, Einstellungen bleiben. Idempotent, in einer Transaktion. Tests: `PollResetTest.php`. +- **Duplizieren** (`slides:duplicate`, POST): kopiert Fragetext (+ „(Kopie)“), Beschreibung, Einstellungen und Optionen + inkl. `is_correct`, **nie Antworten**. Die Kopie landet direkt hinter dem Original und startet wie eine neue Folie + (`Slide::startFresh()`: Quiz `ready`, Q&A `collect`, sonst offen) – auch `add()` nutzt `startFresh()`. + Tests: `DuplicateSlideTest.php`. - **Pause:** `current_slide_id = null` + `paused_slide_id` = Pause (Teilnehmende sehen „Kurze Pause“), ohne `paused_slide_id` = noch nicht gestartet. Immer über `Poll::showSlide()` / `showEmbeddedSlide()` / `pause()` / `forgetSlide()` setzen, nie `current_slide_id` direkt. Presenter: P bzw. der Pause-Button schaltet um (Aktionen `stop`/`resume`, beide idempotent, kein Server-Toggle), diff --git a/config/routes.php b/config/routes.php index 54b189e..2fd3a47 100644 --- a/config/routes.php +++ b/config/routes.php @@ -83,6 +83,8 @@ return function (RouteBuilder $routes): void { ->setPatterns($token)->setPass(['id']); $admin->connect('/slides/{id}/delete', 'Slides::delete', ['_name' => 'slides:delete']) ->setPatterns($token)->setPass(['id'])->setMethods(['POST']); + $admin->connect('/slides/{id}/duplicate', 'Slides::duplicate', ['_name' => 'slides:duplicate']) + ->setPatterns($token)->setPass(['id'])->setMethods(['POST']); $admin->connect('/slides/reorder', 'Slides::reorder', ['_name' => 'slides:reorder']) ->setPatterns($token)->setMethods(['POST']); $admin->connect('/slides/youtube-check', 'Slides::youtubeCheck', ['_name' => 'slides:youtube-check']) diff --git a/src/Controller/SlidesController.php b/src/Controller/SlidesController.php index 0908100..4cf998a 100644 --- a/src/Controller/SlidesController.php +++ b/src/Controller/SlidesController.php @@ -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. * diff --git a/src/Model/Entity/Slide.php b/src/Model/Entity/Slide.php index cd88f98..4391d9d 100644 --- a/src/Model/Entity/Slide.php +++ b/src/Model/Entity/Slide.php @@ -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. diff --git a/src/Model/Table/SlidesTable.php b/src/Model/Table/SlidesTable.php index afb3340..12eb2ad 100644 --- a/src/Model/Table/SlidesTable.php +++ b/src/Model/Table/SlidesTable.php @@ -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). * diff --git a/templates/Admin/index.php b/templates/Admin/index.php index e3bb1b9..18d38f6 100644 --- a/templates/Admin/index.php +++ b/templates/Admin/index.php @@ -144,6 +144,11 @@ $hasInternal = array_filter($slides, fn($slide) => $slide->isInteractive() && $s type === 'scale' ? __('Auswertung') : __('Antworten') ?> + Form->postLink(__('Duplizieren'), $url('slides:duplicate', ['id' => $slide->id]), [ + 'class' => 'button button--small', + 'title' => __('Kopie dieser Folie direkt dahinter anlegen (ohne Antworten)'), + 'aria-label' => __('Folie „{0}“ duplizieren', $slide->question), + ]) ?> 0) : ?> Form->postLink(__('Zurücksetzen'), $url('slides:reset', ['id' => $slide->id]), [ 'class' => 'button button--small', diff --git a/tests/TestCase/Controller/DuplicateSlideTest.php b/tests/TestCase/Controller/DuplicateSlideTest.php new file mode 100644 index 0000000..b7671e1 --- /dev/null +++ b/tests/TestCase/Controller/DuplicateSlideTest.php @@ -0,0 +1,129 @@ + + */ + protected array $fixtures = ['app.Polls', 'app.Slides', 'app.SlideOptions', 'app.Responses']; + + protected function setUp(): void + { + parent::setUp(); + $this->enableCsrfToken(); + } + + public function testCopyIsPlacedBehindOriginalWithoutResponses(): void + { + [$poll, $token] = $this->createPoll(); + $first = $this->createSlide($poll, Slide::TYPE_CHOICE, [ + 'description' => '*Wichtig*', + 'settings' => ['multiple' => true] + Slide::DEFAULT_SETTINGS[Slide::TYPE_CHOICE], + ], ['Rot', 'Grün', 'Blau']); + $last = $this->createSlide($poll, Slide::TYPE_OPEN); + $poll = $this->goLive($poll, $first); + (new VoteService())->submit($poll, $first, VoteService::participantHash(str_repeat('a', 32), $poll->id), [ + 'option_id' => $first->slide_options[0]->id, + ]); + + $this->post("/admin/$token/slides/{$first->id}/duplicate"); + + $slides = $this->slidesTable()->listForPoll($poll->id); + $this->assertSame([$first->id, $last->id], [$slides[0]->id, $slides[2]->id]); + $copy = $slides[1]; + $this->assertRedirect(['_name' => 'slides:edit', 'token' => $token, 'id' => $copy->id]); + $this->assertSame([1, 2, 3], array_map(fn($s) => $s->position, $slides)); + $this->assertSame('Frage choice (Kopie)', $copy->question); + $this->assertSame('*Wichtig*', $copy->description); + $this->assertSame($first->getSettings(), $copy->getSettings()); + $this->assertSame(['Rot', 'Grün', 'Blau'], array_map(fn($o) => $o->label, $copy->slide_options)); + $this->assertNotContains($copy->slide_options[0]->id, array_map(fn($o) => $o->id, $first->slide_options)); + + $responses = TableRegistry::getTableLocator()->get('Responses'); + $this->assertSame(0, $responses->find()->where(['slide_id' => $copy->id])->count()); + $this->assertSame(1, $responses->find()->where(['slide_id' => $first->id])->count(), 'original keeps its votes'); + $this->assertSame($first->id, $this->pollsTable()->get($poll->id)->current_slide_id, 'live slide stays'); + } + + public function testCopyStartsFresh(): void + { + [$poll, $token] = $this->createPoll(); + $quiz = $this->createSlide($poll, Slide::TYPE_QUIZ, [], ['Richtig', 'Falsch', 'Auch falsch']); + $quiz->setVotingOpen(true); + $quiz->is_voting_open = false; + $qa = $this->createSlide($poll, Slide::TYPE_QA); + $qa->stage = Slide::QA_DISCUSS; + $qa->is_voting_open = false; + $closed = $this->createSlide($poll, Slide::TYPE_SCALE, ['is_voting_open' => false]); + $this->slidesTable()->saveManyOrFail([$quiz, $qa]); + + foreach ([$quiz, $qa, $closed] as $slide) { + $this->post("/admin/$token/slides/{$slide->id}/duplicate"); + $this->assertResponseCode(302); + } + + $slides = $this->slidesTable()->listForPoll($poll->id); + $this->assertCount(6, $slides); + [, $quizCopy, , $qaCopy, , $scaleCopy] = $slides; + + $this->assertSame(Slide::QUIZ_READY, $quizCopy->quizPhase()); + $this->assertNull($quizCopy->voting_started_at); + $this->assertSame([true, false, false], array_map(fn($o) => $o->is_correct, $quizCopy->slide_options)); + $this->assertSame(Slide::QA_COLLECT, $qaCopy->qaStage()); + $this->assertTrue($qaCopy->is_voting_open); + $this->assertSame(Slide::TYPE_SCALE, $scaleCopy->type); + $this->assertTrue($scaleCopy->is_voting_open); + } + + public function testOnlySlidesOfOwnPollAndOnlyPost(): void + { + [$poll, $token] = $this->createPoll(); + [$other] = $this->createPoll('Andere'); + $own = $this->createSlide($poll); + $foreign = $this->createSlide($other); + + $this->post("/admin/$token/slides/{$foreign->id}/duplicate"); + $this->assertResponseCode(404); + $this->assertSame(1, $this->slidesTable()->find()->where(['poll_id' => $other->id])->count()); + + $this->get("/admin/$token/slides/{$own->id}/duplicate"); + $this->assertResponseCode(404); + $this->assertSame(1, $this->slidesTable()->find()->where(['poll_id' => $poll->id])->count()); + } + + public function testLongQuestionIsShortened(): void + { + [$poll, $token] = $this->createPoll(); + $slide = $this->createSlide($poll, Slide::TYPE_CONTENT, ['question' => str_repeat('x', 500)], []); + + $this->post("/admin/$token/slides/{$slide->id}/duplicate"); + + $copy = $this->slidesTable()->listForPoll($poll->id)[1]; + $this->assertSame(500, mb_strlen($copy->question)); + } + + public function testDashboardOffersDuplicate(): void + { + [$poll, $token] = $this->createPoll(); + $slide = $this->createSlide($poll); + $this->get("/admin/$token"); + $this->assertResponseContains("/admin/$token/slides/{$slide->id}/duplicate"); + $this->assertResponseContains('Duplizieren'); + } +}