Merge pull request 'Feature/hide slides' (#55) from feature/hide-slides into main
Reviewed-on: #55
This commit was merged in pull request #55.
This commit is contained in:
commit
aefaaba086
16 files changed
+546
-39
No files matched your search
@@ -161,7 +161,7 @@ class AdminController extends AdminAppController
|
||||
{
|
||||
if ($this->poll->current_slide_id === null && !$this->poll->isPaused()) {
|
||||
$first = $this->Slides->find()
|
||||
->where(['poll_id' => $this->poll->id])
|
||||
->where(['poll_id' => $this->poll->id, 'is_hidden' => false])
|
||||
->orderBy(['position' => 'ASC', 'id' => 'ASC'])
|
||||
->first();
|
||||
if ($first !== null) {
|
||||
@@ -203,7 +203,8 @@ class AdminController extends AdminAppController
|
||||
{
|
||||
$this->request->allowMethod(['post']);
|
||||
$action = (string)$this->request->getData('action');
|
||||
$slides = $this->Slides->listForPoll($this->poll->id);
|
||||
// Hidden slides are skipped and can not go live.
|
||||
$slides = $this->Slides->listForPoll($this->poll->id, true);
|
||||
$ids = array_map(fn(Slide $s) => $s->id, $slides);
|
||||
$index = array_search($this->poll->current_slide_id, $ids, true);
|
||||
$current = $index === false ? null : $slides[$index];
|
||||
|
||||
@@ -114,14 +114,14 @@ class EmbedController extends AppController
|
||||
}
|
||||
|
||||
/**
|
||||
* The embed became visible: this slide goes live. Idempotent.
|
||||
* The embed became visible: this slide goes live (unless it is hidden). Idempotent.
|
||||
*
|
||||
* @return \Cake\Http\Response
|
||||
*/
|
||||
public function activate(): Response
|
||||
{
|
||||
$this->request->allowMethod(['post']);
|
||||
if ($this->poll->is_active) {
|
||||
if ($this->poll->is_active && !$this->slide->is_hidden) {
|
||||
$this->poll->showEmbeddedSlide($this->slide->id);
|
||||
$this->Polls->saveOrFail($this->poll);
|
||||
}
|
||||
|
||||
@@ -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 = [];
|
||||
@@ -106,15 +104,7 @@ class SlidesController extends AdminAppController
|
||||
$this->request->allowMethod(['post']);
|
||||
$slide = $this->loadSlide($id);
|
||||
// Deleting the live (or paused) slide pauses the poll; → resumes with the slide taking its place.
|
||||
$ids = $this->Slides->find()
|
||||
->select(['id'])
|
||||
->where(['poll_id' => $this->poll->id])
|
||||
->orderBy(['position' => 'ASC', 'id' => 'ASC'])
|
||||
->all()
|
||||
->extract('id')
|
||||
->toList();
|
||||
$index = (int)array_search($slide->id, $ids, true);
|
||||
$this->poll->forgetSlide($slide->id, $ids[$index + 1] ?? $ids[$index - 1] ?? null);
|
||||
$this->poll->forgetSlide($slide->id, $this->Slides->replacementFor($slide));
|
||||
|
||||
$this->Slides->getConnection()->transactional(function () use ($slide): void {
|
||||
$this->Polls->saveOrFail($this->poll);
|
||||
@@ -126,6 +116,49 @@ class SlidesController extends AdminAppController
|
||||
return $this->redirect($this->adminUrl());
|
||||
}
|
||||
|
||||
/**
|
||||
* Hides a slide from the presentation or shows it again. Field `hidden` (1|0) is explicit
|
||||
* (not a toggle), so repeating the request changes nothing. Hiding the live (or paused)
|
||||
* slide pauses the poll like deleting it.
|
||||
*
|
||||
* @param string $id Slide id
|
||||
* @return \Cake\Http\Response|null
|
||||
*/
|
||||
public function visibility(string $id): ?Response
|
||||
{
|
||||
$this->request->allowMethod(['post']);
|
||||
$slide = $this->loadSlide($id);
|
||||
$hidden = filter_var($this->request->getData('hidden'), FILTER_VALIDATE_BOOLEAN);
|
||||
|
||||
$this->Slides->getConnection()->transactional(function () use ($slide, $hidden): void {
|
||||
if ($hidden) {
|
||||
$this->poll->forgetSlide($slide->id, $this->Slides->replacementFor($slide));
|
||||
$this->Polls->saveOrFail($this->poll);
|
||||
}
|
||||
$this->Slides->updateAll(['is_hidden' => $hidden], ['id' => $slide->id]);
|
||||
});
|
||||
$this->Flash->success($hidden
|
||||
? __('Folie „{0}“ ist ausgeblendet und wird beim Präsentieren übersprungen.', $slide->question)
|
||||
: __('Folie „{0}“ ist wieder eingeblendet.', $slide->question));
|
||||
|
||||
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.
|
||||
*
|
||||
|
||||
@@ -110,10 +110,11 @@ class Poll extends Entity
|
||||
}
|
||||
|
||||
/**
|
||||
* A slide is being deleted. If it was live or is the one a pause resumes, the poll pauses
|
||||
* and resumes with $resumeSlideId instead (null: no slides left = not started). Idempotent.
|
||||
* A slide leaves the presentation (deleted or hidden). If it was live or is the one a pause
|
||||
* resumes, the poll pauses and resumes with $resumeSlideId instead (null: no slides left =
|
||||
* not started). Idempotent.
|
||||
*
|
||||
* @param int $slideId Deleted slide
|
||||
* @param int $slideId Deleted or hidden slide
|
||||
* @param int|null $resumeSlideId Slide that takes its place (must belong to this poll)
|
||||
* @return void
|
||||
*/
|
||||
|
||||
@@ -17,6 +17,7 @@ use Cake\ORM\Entity;
|
||||
* @property string $question
|
||||
* @property string|null $description
|
||||
* @property int $position
|
||||
* @property bool $is_hidden Prepared, but skipped in the presentation (never live)
|
||||
* @property array<string, mixed>|null $settings
|
||||
* @property bool $is_voting_open
|
||||
* @property \Cake\I18n\DateTime|null $voting_started_at Quiz: start of the voting window
|
||||
@@ -408,6 +409,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.
|
||||
|
||||
@@ -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).
|
||||
*
|
||||
@@ -382,18 +423,51 @@ class SlidesTable extends Table
|
||||
* Ordered slides of a poll including options.
|
||||
*
|
||||
* @param int $pollId Poll id
|
||||
* @param bool $visibleOnly Only slides that are part of the presentation (not hidden)
|
||||
* @return list<\App\Model\Entity\Slide>
|
||||
*/
|
||||
public function listForPoll(int $pollId): array
|
||||
public function listForPoll(int $pollId, bool $visibleOnly = false): array
|
||||
{
|
||||
/** @var list<\App\Model\Entity\Slide> $slides */
|
||||
$slides = $this->find()
|
||||
$query = $this->find()
|
||||
->where(['Slides.poll_id' => $pollId])
|
||||
->contain(['SlideOptions'])
|
||||
->orderBy(['Slides.position' => 'ASC', 'Slides.id' => 'ASC'])
|
||||
->all()
|
||||
->toList();
|
||||
->orderBy(['Slides.position' => 'ASC', 'Slides.id' => 'ASC']);
|
||||
if ($visibleOnly) {
|
||||
$query->where(['Slides.is_hidden' => false]);
|
||||
}
|
||||
/** @var list<\App\Model\Entity\Slide> $slides */
|
||||
$slides = $query->all()->toList();
|
||||
|
||||
return $slides;
|
||||
}
|
||||
|
||||
/**
|
||||
* The visible slide that takes the place of a slide leaving the presentation
|
||||
* (deleted or hidden): the next visible one behind it, otherwise the one before.
|
||||
*
|
||||
* @param \App\Model\Entity\Slide $slide Slide leaving the presentation
|
||||
* @return int|null Slide id, null if no other visible slide is left
|
||||
*/
|
||||
public function replacementFor(Slide $slide): ?int
|
||||
{
|
||||
$rows = $this->find()
|
||||
->select(['id', 'is_hidden'])
|
||||
->where(['poll_id' => $slide->poll_id])
|
||||
->orderBy(['position' => 'ASC', 'id' => 'ASC'])
|
||||
->disableHydration()
|
||||
->all()
|
||||
->toList();
|
||||
$index = array_search($slide->id, array_column($rows, 'id'), true);
|
||||
if ($index === false) {
|
||||
return null;
|
||||
}
|
||||
$candidates = array_merge(array_slice($rows, $index + 1), array_reverse(array_slice($rows, 0, $index)));
|
||||
foreach ($candidates as $row) {
|
||||
if (!$row['is_hidden']) {
|
||||
return (int)$row['id'];
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -83,7 +83,8 @@ class PollStateService
|
||||
public function forPresenter(Poll $poll): array
|
||||
{
|
||||
$this->Polls->pauseIfEmbedStale($poll);
|
||||
$slides = $this->Slides->listForPoll($poll->id);
|
||||
// The presentation consists of the visible slides only.
|
||||
$slides = $this->Slides->listForPoll($poll->id, true);
|
||||
$current = null;
|
||||
$index = null;
|
||||
foreach ($slides as $i => $slide) {
|
||||
@@ -102,6 +103,7 @@ class PollStateService
|
||||
], $slides),
|
||||
'index' => $index,
|
||||
'count' => count($slides),
|
||||
'hiddenCount' => $this->Slides->find()->where(['poll_id' => $poll->id, 'is_hidden' => true])->count(),
|
||||
'slide' => $current ? $this->slidePayload($current) : null,
|
||||
'results' => $current ? $this->results->forSlide($current) : null,
|
||||
];
|
||||
@@ -123,6 +125,7 @@ class PollStateService
|
||||
return [
|
||||
'poll' => $this->pollPayload($poll),
|
||||
'isLive' => $poll->current_slide_id === $slide->id,
|
||||
'isHidden' => $slide->is_hidden,
|
||||
'index' => null,
|
||||
'count' => null,
|
||||
'slide' => $this->slidePayload($slide),
|
||||
|
||||
@@ -16,7 +16,7 @@ use Cake\ORM\Locator\LocatorAwareTrait;
|
||||
* screen. With $includeInternal it also contains their answers, distribution and comments –
|
||||
* that version is meant for the poll owner only.
|
||||
*
|
||||
* Per slide: ['slide' => Slide, 'number' => int (position in the poll, 1-based),
|
||||
* Per slide: ['slide' => Slide, 'number' => int (position in the presentation, 1-based; hidden slides are left out),
|
||||
* 'internal' => bool (private slide), 'withheld' => bool (private and left out),
|
||||
* 'results' => ResultsService::forSlide() shape, where
|
||||
* open: 'answers' contains all texts (not only the newest ones), and
|
||||
@@ -49,7 +49,7 @@ class ReportService
|
||||
*/
|
||||
public function build(Poll $poll, bool $includeInternal): array
|
||||
{
|
||||
$slides = $this->Slides->listForPoll($poll->id);
|
||||
$slides = $this->Slides->listForPoll($poll->id, true);
|
||||
$includeInternal = $includeInternal
|
||||
&& array_filter($slides, fn(Slide $slide) => $slide->isInteractive() && $slide->isPrivate()) !== [];
|
||||
$sections = [];
|
||||
|
||||
Reference in new issue
Block a user