Add polls seed command for demo and load test polls
Every run creates a new poll with one slide per interactive type and shows the first (choice) slide live with voting open. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
a9b14af8bf
commit
ebfab0ef20
3 files changed
+153
-2
No files matched your search
@@ -0,0 +1,133 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Command;
|
||||
|
||||
use App\Model\Entity\Slide;
|
||||
use Cake\Console\Arguments;
|
||||
use Cake\Console\ConsoleIo;
|
||||
use Cake\Console\ConsoleOptionParser;
|
||||
use Cake\Routing\Router;
|
||||
|
||||
/**
|
||||
* Creates a fresh demo poll with one slide of every interactive type, e.g. as target of the JMeter load test
|
||||
* (tests/load/plenario.jmx). Every run creates a NEW poll (new id and join code); the first slide, a choice
|
||||
* question, is live and open for voting. Remove them again with `bin/cake polls delete`.
|
||||
*
|
||||
* Usage: bin/cake polls seed [--title "..."] [--email "..."] [--json]
|
||||
*/
|
||||
class PollsSeedCommand extends PollsCommand
|
||||
{
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public static function defaultName(): string
|
||||
{
|
||||
return 'polls seed';
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \Cake\Console\ConsoleOptionParser $parser Parser
|
||||
* @return \Cake\Console\ConsoleOptionParser
|
||||
*/
|
||||
protected function buildOptionParser(ConsoleOptionParser $parser): ConsoleOptionParser
|
||||
{
|
||||
return $parser
|
||||
->setDescription(__('Legt eine neue Demo-Umfrage mit Beispielfolien an (z. B. für den Lasttest). Jeder Aufruf erzeugt eine neue Umfrage.'))
|
||||
->addOption('title', [
|
||||
'help' => __('Titel der Umfrage.'),
|
||||
'default' => 'Lasttest',
|
||||
])
|
||||
->addOption('email', [
|
||||
'help' => __('E-Mail-Adresse des Besitzers (es wird nichts verschickt).'),
|
||||
'default' => 'lasttest@example.org',
|
||||
])
|
||||
->addOption('json', [
|
||||
'help' => __('Ausgabe als JSON statt als Text (für Skripte), inkl. Admin-Link.'),
|
||||
'boolean' => true,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \Cake\Console\Arguments $args Arguments
|
||||
* @param \Cake\Console\ConsoleIo $io Console
|
||||
* @return int
|
||||
*/
|
||||
public function execute(Arguments $args, ConsoleIo $io): int
|
||||
{
|
||||
$polls = $this->polls();
|
||||
$slides = $this->fetchTable('Slides');
|
||||
|
||||
$result = $polls->getConnection()->transactional(function () use ($polls, $slides, $args): array {
|
||||
[$poll, $token] = $polls->createPoll([
|
||||
'title' => (string)$args->getOption('title'),
|
||||
'email' => (string)$args->getOption('email'),
|
||||
]);
|
||||
if ($token === null) {
|
||||
return [$poll, null, 0];
|
||||
}
|
||||
|
||||
$first = null;
|
||||
$count = 0;
|
||||
foreach ($this->definitions() as [$type, $question, $options, $correct]) {
|
||||
$slide = $slides->newEntity([
|
||||
'type' => $type,
|
||||
'question' => $question,
|
||||
'settings' => Slide::DEFAULT_SETTINGS[$type] ?? [],
|
||||
]);
|
||||
$slide->poll_id = $poll->id;
|
||||
$slides->saveOrFail($slide);
|
||||
if ($options !== []) {
|
||||
$slides->syncOptions($slide, $options, $correct);
|
||||
}
|
||||
$first ??= $slide;
|
||||
$count++;
|
||||
}
|
||||
|
||||
$poll->showSlide($first->id);
|
||||
$polls->saveOrFail($poll);
|
||||
|
||||
return [$poll, $token, $count];
|
||||
});
|
||||
|
||||
[$poll, $token, $count] = $result;
|
||||
if ($token === null) {
|
||||
$io->err(__('Umfrage konnte nicht angelegt werden: {0}', (string)json_encode($poll->getErrors())));
|
||||
|
||||
return static::CODE_ERROR;
|
||||
}
|
||||
|
||||
$adminUrl = Router::url(['_name' => 'admin', 'token' => $token], true);
|
||||
if ($args->getOption('json')) {
|
||||
$this->printJson($io, $this->pollData($poll) + ['slides' => $count, 'adminUrl' => $adminUrl]);
|
||||
|
||||
return static::CODE_SUCCESS;
|
||||
}
|
||||
|
||||
$io->out([
|
||||
__('Umfrage #{0} angelegt mit {1} Folien (erste Folie live, Abstimmung offen).', $poll->id, $count),
|
||||
__('Beitrittscode: {0}', $poll->join_code),
|
||||
__('Teilnahme-Link: {0}', $this->joinLink($poll)),
|
||||
__('Admin-Link: {0}', $adminUrl),
|
||||
__('Lasttest: jmeter -n -t tests/load/plenario.jmx -Jcode={0} …', $poll->join_code),
|
||||
]);
|
||||
|
||||
return static::CODE_SUCCESS;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return list<array{0: string, 1: string, 2: list<string>, 3: list<string>}> Type, question, options, correct options
|
||||
*/
|
||||
protected function definitions(): array
|
||||
{
|
||||
return [
|
||||
[Slide::TYPE_CHOICE, 'Wie gefällt dir PlenarIO?', ['Super', 'Gut', 'Geht so', 'Gar nicht'], []],
|
||||
[Slide::TYPE_WORDCLOUD, 'Was fällt dir zu Live-Umfragen ein?', [], []],
|
||||
[Slide::TYPE_OPEN, 'Was wünschst du dir für heute?', [], []],
|
||||
[Slide::TYPE_SCALE, 'Wie zufrieden bist du?', [], []],
|
||||
[Slide::TYPE_QUIZ, 'Wie viele Kontinente gibt es?', ['7', '5', '6', '8'], ['7']],
|
||||
[Slide::TYPE_QA, 'Stell deine Frage', [], []],
|
||||
[Slide::TYPE_ORDER, 'Bringe die Planeten in die richtige Reihenfolge', ['Merkur', 'Venus', 'Erde', 'Mars'], []],
|
||||
];
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user