Add poll expiry, reminder mail and polls admin CLI
Polls are deleted POLL_RETENTION_DAYS (default 90) after creation by the cron job `bin/cake polls cleanup`. POLL_REMINDER_DAYS (default 14) before that the owner gets a one-time reminder mail (expiry_reminded_at). New commands: polls list, show (admin link only with --link), delete, remind, cleanup. The expiry date is shown in the creation mail and the dashboard. Docs: README, AGENTS.md, .env.example. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
4caff47a09
commit
a19cd8339e
20 files changed
+976
-1
No files matched your search
@@ -0,0 +1,79 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Command;
|
||||
|
||||
use Cake\Console\Arguments;
|
||||
use Cake\Console\ConsoleIo;
|
||||
use Cake\Console\ConsoleOptionParser;
|
||||
use Cake\Core\Configure;
|
||||
|
||||
/**
|
||||
* Sends the reminders (`polls remind`), then deletes expired polls (POLL_RETENTION_DAYS after creation,
|
||||
* see Poll::expiresAt()). Idempotent, made for cron:
|
||||
*
|
||||
* 17 3 * * * cd /path/to/plenario && php bin/cake.php polls cleanup --quiet
|
||||
*
|
||||
* Usage: bin/cake polls cleanup [--dry-run]
|
||||
*/
|
||||
class PollsCleanupCommand extends PollsCommand
|
||||
{
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public static function defaultName(): string
|
||||
{
|
||||
return 'polls cleanup';
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \Cake\Console\ConsoleOptionParser $parser Parser
|
||||
* @return \Cake\Console\ConsoleOptionParser
|
||||
*/
|
||||
protected function buildOptionParser(ConsoleOptionParser $parser): ConsoleOptionParser
|
||||
{
|
||||
return $parser
|
||||
->setDescription(__('Der Cronjob: erinnert Besitzer bald ablaufender Umfragen, löscht abgelaufene und danach verwaiste Bilddateien.'))
|
||||
->addOption('dry-run', [
|
||||
'help' => __('Nur anzeigen, nichts löschen.'),
|
||||
'boolean' => true,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \Cake\Console\Arguments $args Arguments
|
||||
* @param \Cake\Console\ConsoleIo $io Console
|
||||
* @return int
|
||||
*/
|
||||
public function execute(Arguments $args, ConsoleIo $io): int
|
||||
{
|
||||
if ((int)Configure::read('Plenario.retention.days', 90) <= 0) {
|
||||
$io->out(__('Umfragen laufen nie ab (POLL_RETENTION_DAYS=0), nichts zu tun.'));
|
||||
|
||||
return static::CODE_SUCCESS;
|
||||
}
|
||||
|
||||
// Reminder first, so owners hear about it before the deletion (idempotent: once per poll).
|
||||
$remind = $this->executeCommand(PollsRemindCommand::class, $args->getOption('dry-run') ? ['--dry-run'] : [], $io);
|
||||
|
||||
/** @var list<\App\Model\Entity\Poll> $expired */
|
||||
$expired = $this->polls()->find('expired')->orderBy(['Polls.id' => 'ASC'])->all()->toList();
|
||||
|
||||
if ($args->getOption('dry-run')) {
|
||||
foreach ($expired as $poll) {
|
||||
$io->out(__('Abgelaufen: #{0} {1} ({2}), seit {3}', $poll->id, $poll->title, $poll->email, $this->formatDate($poll->expiresAt())));
|
||||
}
|
||||
$io->success(__('{0} abgelaufene Umfrage(n) gefunden (nichts gelöscht).', count($expired)));
|
||||
|
||||
return static::CODE_SUCCESS;
|
||||
}
|
||||
|
||||
$deleted = $this->deletePolls($expired, $io);
|
||||
$io->success(__('{0} abgelaufene Umfrage(n) gelöscht.', $deleted));
|
||||
|
||||
// Sweep image files a crash may have left behind (cheap, idempotent).
|
||||
$this->executeCommand(ImagesCleanupCommand::class, [], $io);
|
||||
|
||||
return $deleted === count($expired) && $remind === static::CODE_SUCCESS ? static::CODE_SUCCESS : static::CODE_ERROR;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,118 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Command;
|
||||
|
||||
use App\Model\Entity\Poll;
|
||||
use App\Model\Table\PollsTable;
|
||||
use Cake\Command\Command;
|
||||
use Cake\Console\ConsoleIo;
|
||||
use Cake\I18n\DateTime;
|
||||
use Cake\Routing\Router;
|
||||
|
||||
/**
|
||||
* Shared helpers of the `polls …` admin commands (list, show, delete, cleanup).
|
||||
*/
|
||||
abstract class PollsCommand extends Command
|
||||
{
|
||||
/**
|
||||
* @return \App\Model\Table\PollsTable
|
||||
*/
|
||||
protected function polls(): PollsTable
|
||||
{
|
||||
/** @var \App\Model\Table\PollsTable $polls */
|
||||
$polls = $this->fetchTable('Polls');
|
||||
|
||||
return $polls;
|
||||
}
|
||||
|
||||
/**
|
||||
* Finds a poll by numeric id or join code ("K7P2QX", "k7p-2qx").
|
||||
*
|
||||
* @param string $reference Id or join code
|
||||
* @return \App\Model\Entity\Poll|null
|
||||
*/
|
||||
protected function findPoll(string $reference): ?Poll
|
||||
{
|
||||
$reference = trim($reference);
|
||||
if ($reference !== '' && ctype_digit($reference)) {
|
||||
$poll = $this->polls()->find()->where(['Polls.id' => (int)$reference])->first();
|
||||
if ($poll !== null) {
|
||||
/** @var \App\Model\Entity\Poll $poll */
|
||||
return $poll;
|
||||
}
|
||||
}
|
||||
|
||||
/** @var \App\Model\Entity\Poll|null $poll */
|
||||
$poll = $this->polls()->find('byJoinCode', code: $reference)->first();
|
||||
|
||||
return $poll;
|
||||
}
|
||||
|
||||
/**
|
||||
* Absolute admin link (contains the secret token: show it only on explicit request).
|
||||
*
|
||||
* @param \App\Model\Entity\Poll $poll Poll
|
||||
* @return string|null Null if the token cannot be decrypted (e.g. SECURITY_SALT changed)
|
||||
*/
|
||||
protected function adminLink(Poll $poll): ?string
|
||||
{
|
||||
$token = $this->polls()->decryptToken($poll);
|
||||
|
||||
return $token === null ? null : Router::url(['_name' => 'admin', 'token' => $token], true);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \App\Model\Entity\Poll $poll Poll
|
||||
* @return string Absolute participant link
|
||||
*/
|
||||
protected function joinLink(Poll $poll): string
|
||||
{
|
||||
return Router::url(['_name' => 'participate', 'code' => $poll->join_code], true);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \Cake\I18n\DateTime|null $date Date
|
||||
* @return string "2026-11-16 14:30" in the server's time zone, "-" if empty
|
||||
*/
|
||||
protected function formatDate(?DateTime $date): string
|
||||
{
|
||||
return $date === null ? '-' : $date->format('Y-m-d H:i');
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \App\Model\Entity\Poll $poll Poll
|
||||
* @return string Expiry date, with a marker once it has passed
|
||||
*/
|
||||
protected function formatExpiry(Poll $poll): string
|
||||
{
|
||||
$expires = $poll->expiresAt();
|
||||
if ($expires === null) {
|
||||
return __('nie');
|
||||
}
|
||||
|
||||
return $this->formatDate($expires) . ($expires->isPast() ? ' ' . __('(abgelaufen)') : '');
|
||||
}
|
||||
|
||||
/**
|
||||
* Deletes polls including slides, answers and images, one transaction per poll.
|
||||
*
|
||||
* @param iterable<\App\Model\Entity\Poll> $polls Polls
|
||||
* @param \Cake\Console\ConsoleIo $io Console
|
||||
* @return int Number of deleted polls
|
||||
*/
|
||||
protected function deletePolls(iterable $polls, ConsoleIo $io): int
|
||||
{
|
||||
$deleted = 0;
|
||||
foreach ($polls as $poll) {
|
||||
if ($this->polls()->delete($poll)) {
|
||||
$deleted++;
|
||||
$io->out(__('Gelöscht: #{0} {1} ({2})', $poll->id, $poll->title, $poll->email));
|
||||
} else {
|
||||
$io->err(__('Konnte #{0} nicht löschen.', $poll->id));
|
||||
}
|
||||
}
|
||||
|
||||
return $deleted;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Command;
|
||||
|
||||
use Cake\Console\Arguments;
|
||||
use Cake\Console\ConsoleIo;
|
||||
use Cake\Console\ConsoleOptionParser;
|
||||
|
||||
/**
|
||||
* Deletes polls for good (slides, answers and images included). Unknown polls are an error (exit code 1).
|
||||
*
|
||||
* Usage: bin/cake polls delete <id|code> [--yes]
|
||||
*/
|
||||
class PollsDeleteCommand extends PollsCommand
|
||||
{
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public static function defaultName(): string
|
||||
{
|
||||
return 'polls delete';
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \Cake\Console\ConsoleOptionParser $parser Parser
|
||||
* @return \Cake\Console\ConsoleOptionParser
|
||||
*/
|
||||
protected function buildOptionParser(ConsoleOptionParser $parser): ConsoleOptionParser
|
||||
{
|
||||
return $parser
|
||||
->setDescription(__('Löscht Umfragen endgültig, samt Folien, Antworten und Bildern.'))
|
||||
->addArgument('poll', [
|
||||
'help' => __('ID oder Beitrittscode der Umfrage.'),
|
||||
'required' => true,
|
||||
])
|
||||
->addOption('yes', [
|
||||
'short' => 'y',
|
||||
'help' => __('Ohne Rückfrage löschen.'),
|
||||
'boolean' => true,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \Cake\Console\Arguments $args Arguments
|
||||
* @param \Cake\Console\ConsoleIo $io Console
|
||||
* @return int
|
||||
*/
|
||||
public function execute(Arguments $args, ConsoleIo $io): int
|
||||
{
|
||||
$reference = (string)$args->getArgument('poll');
|
||||
$poll = $this->findPoll($reference);
|
||||
if ($poll === null) {
|
||||
$io->err(__('Umfrage "{0}" nicht gefunden.', $reference));
|
||||
|
||||
return static::CODE_ERROR;
|
||||
}
|
||||
|
||||
$io->out(__('#{0} {1} ({2}), angelegt {3}', $poll->id, $poll->title, $poll->email, $this->formatDate($poll->created)));
|
||||
if (!$args->getOption('yes') && $io->askChoice(__('Diese Umfrage endgültig löschen?'), ['y', 'n'], 'n') !== 'y') {
|
||||
$io->out(__('Abgebrochen.'));
|
||||
|
||||
return static::CODE_ERROR;
|
||||
}
|
||||
|
||||
return $this->deletePolls([$poll], $io) === 1 ? static::CODE_SUCCESS : static::CODE_ERROR;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,104 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Command;
|
||||
|
||||
use Cake\Console\Arguments;
|
||||
use Cake\Console\ConsoleIo;
|
||||
use Cake\Console\ConsoleOptionParser;
|
||||
|
||||
/**
|
||||
* Lists polls with owner, activity and expiry date. Never prints admin links.
|
||||
*
|
||||
* Usage: bin/cake polls list [--expired] [--email=<part>] [--sort=created|activity|title]
|
||||
*/
|
||||
class PollsListCommand extends PollsCommand
|
||||
{
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public static function defaultName(): string
|
||||
{
|
||||
return 'polls list';
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \Cake\Console\ConsoleOptionParser $parser Parser
|
||||
* @return \Cake\Console\ConsoleOptionParser
|
||||
*/
|
||||
protected function buildOptionParser(ConsoleOptionParser $parser): ConsoleOptionParser
|
||||
{
|
||||
return $parser
|
||||
->setDescription(__('Zeigt alle Umfragen mit E-Mail-Adresse, Ablaufdatum und Erinnerung.'))
|
||||
->addOption('expired', [
|
||||
'help' => __('Nur abgelaufene Umfragen.'),
|
||||
'boolean' => true,
|
||||
])
|
||||
->addOption('email', [
|
||||
'help' => __('Nur Umfragen, deren E-Mail-Adresse diesen Text enthält.'),
|
||||
])
|
||||
->addOption('sort', [
|
||||
'help' => __('Sortierung (aufsteigend).'),
|
||||
'default' => 'created',
|
||||
'choices' => ['created', 'activity', 'title'],
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \Cake\Console\Arguments $args Arguments
|
||||
* @param \Cake\Console\ConsoleIo $io Console
|
||||
* @return int
|
||||
*/
|
||||
public function execute(Arguments $args, ConsoleIo $io): int
|
||||
{
|
||||
$query = $this->polls()->find()
|
||||
->select([
|
||||
'Polls.id', 'Polls.title', 'Polls.email', 'Polls.join_code', 'Polls.current_slide_id',
|
||||
'Polls.paused_slide_id', 'Polls.last_admin_access', 'Polls.expiry_reminded_at', 'Polls.created',
|
||||
])
|
||||
->orderBy(['Polls.id' => 'ASC']);
|
||||
if ($args->getOption('expired')) {
|
||||
$query->find('expired');
|
||||
}
|
||||
$email = $args->getOption('email');
|
||||
if (is_string($email) && $email !== '') {
|
||||
$query->where(['Polls.email LIKE' => '%' . addcslashes(strtolower($email), '%_\\') . '%']);
|
||||
}
|
||||
|
||||
/** @var list<\App\Model\Entity\Poll> $polls */
|
||||
$polls = $query->all()->toList();
|
||||
$sort = (string)$args->getOption('sort');
|
||||
if ($sort !== 'created') {
|
||||
usort($polls, fn($a, $b): int => match ($sort) {
|
||||
'activity' => ($a->last_admin_access ?? $a->created) <=> ($b->last_admin_access ?? $b->created),
|
||||
default => strcasecmp($a->title, $b->title),
|
||||
});
|
||||
}
|
||||
|
||||
if ($polls === []) {
|
||||
$io->out(__('Keine Umfragen gefunden.'));
|
||||
|
||||
return static::CODE_SUCCESS;
|
||||
}
|
||||
|
||||
$rows = [[__('ID'), __('Code'), __('Titel'), __('E-Mail'), __('Angelegt'), __('Letzter Admin-Besuch'), __('Läuft ab'), __('Erinnert')]];
|
||||
foreach ($polls as $poll) {
|
||||
$rows[] = [
|
||||
(string)$poll->id,
|
||||
$poll->join_code,
|
||||
mb_strimwidth($poll->title, 0, 40, '…'),
|
||||
$poll->email,
|
||||
$this->formatDate($poll->created),
|
||||
$this->formatDate($poll->last_admin_access),
|
||||
$this->formatExpiry($poll),
|
||||
$this->formatDate($poll->expiry_reminded_at),
|
||||
];
|
||||
}
|
||||
$io->helper('Table')->output($rows);
|
||||
|
||||
$expired = count(array_filter($polls, fn($poll): bool => $poll->isExpired()));
|
||||
$io->out(__('{0} Umfrage(n), davon {1} abgelaufen.', count($polls), $expired));
|
||||
|
||||
return static::CODE_SUCCESS;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,93 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Command;
|
||||
|
||||
use App\Mailer\PollMailer;
|
||||
use App\Model\Entity\Poll;
|
||||
use Cake\Console\Arguments;
|
||||
use Cake\Console\ConsoleIo;
|
||||
use Cake\Console\ConsoleOptionParser;
|
||||
use Cake\I18n\DateTime;
|
||||
|
||||
/**
|
||||
* Mails the owners of polls that expire soon (POLL_REMINDER_DAYS before the end), once per poll.
|
||||
* Idempotent: a poll is marked after a successful send; a failed send is retried at the next run.
|
||||
* `polls cleanup` runs this first, a separate cron entry is not needed.
|
||||
*
|
||||
* Usage: bin/cake polls remind [--dry-run]
|
||||
*/
|
||||
class PollsRemindCommand extends PollsCommand
|
||||
{
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public static function defaultName(): string
|
||||
{
|
||||
return 'polls remind';
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \Cake\Console\ConsoleOptionParser $parser Parser
|
||||
* @return \Cake\Console\ConsoleOptionParser
|
||||
*/
|
||||
protected function buildOptionParser(ConsoleOptionParser $parser): ConsoleOptionParser
|
||||
{
|
||||
return $parser
|
||||
->setDescription(__('Erinnert Besitzer per E-Mail, bevor ihre Umfrage automatisch gelöscht wird.'))
|
||||
->addOption('dry-run', [
|
||||
'help' => __('Nur anzeigen, nichts verschicken.'),
|
||||
'boolean' => true,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \Cake\Console\Arguments $args Arguments
|
||||
* @param \Cake\Console\ConsoleIo $io Console
|
||||
* @return int
|
||||
*/
|
||||
public function execute(Arguments $args, ConsoleIo $io): int
|
||||
{
|
||||
if (Poll::reminderLeadDays() === 0) {
|
||||
$io->out(__('Erinnerungen sind aus (POLL_REMINDER_DAYS=0 oder Umfragen laufen nie ab).'));
|
||||
|
||||
return static::CODE_SUCCESS;
|
||||
}
|
||||
|
||||
$dryRun = (bool)$args->getOption('dry-run');
|
||||
/** @var list<\App\Model\Entity\Poll> $due */
|
||||
$due = $this->polls()->find('reminderDue')->orderBy(['Polls.id' => 'ASC'])->all()->toList();
|
||||
|
||||
$sent = 0;
|
||||
$failed = 0;
|
||||
foreach ($due as $poll) {
|
||||
$label = __('#{0} {1} ({2}), läuft ab {3}', $poll->id, $poll->title, $poll->email, $this->formatDate($poll->expiresAt()));
|
||||
if ($dryRun) {
|
||||
$io->out(__('Würde erinnern: {0}', $label));
|
||||
continue;
|
||||
}
|
||||
|
||||
$adminUrl = $this->adminLink($poll);
|
||||
if ($adminUrl === null) {
|
||||
$io->err(__('Admin-Link nicht entschlüsselbar, keine Erinnerung: {0}', $label));
|
||||
$failed++;
|
||||
continue;
|
||||
}
|
||||
if (!(new PollMailer())->sendSafely('expiryReminder', [$poll, $adminUrl])) {
|
||||
$io->err(__('E-Mail fehlgeschlagen: {0}', $label));
|
||||
$failed++;
|
||||
continue;
|
||||
}
|
||||
// Only the marker column; the poll may have been changed meanwhile.
|
||||
$this->polls()->updateAll(['expiry_reminded_at' => DateTime::now()], ['id' => $poll->id]);
|
||||
$io->out(__('Erinnert: {0}', $label));
|
||||
$sent++;
|
||||
}
|
||||
|
||||
$io->success($dryRun
|
||||
? __('{0} Erinnerung(en) wären fällig (nichts verschickt).', count($due))
|
||||
: __('{0} Erinnerung(en) verschickt, {1} fehlgeschlagen.', $sent, $failed));
|
||||
|
||||
return $failed === 0 ? static::CODE_SUCCESS : static::CODE_ERROR;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,85 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Command;
|
||||
|
||||
use Cake\Console\Arguments;
|
||||
use Cake\Console\ConsoleIo;
|
||||
use Cake\Console\ConsoleOptionParser;
|
||||
|
||||
/**
|
||||
* Details of one poll. The secret admin link is only printed with --link.
|
||||
*
|
||||
* Usage: bin/cake polls show <id|code> [--link]
|
||||
*/
|
||||
class PollsShowCommand extends PollsCommand
|
||||
{
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public static function defaultName(): string
|
||||
{
|
||||
return 'polls show';
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \Cake\Console\ConsoleOptionParser $parser Parser
|
||||
* @return \Cake\Console\ConsoleOptionParser
|
||||
*/
|
||||
protected function buildOptionParser(ConsoleOptionParser $parser): ConsoleOptionParser
|
||||
{
|
||||
return $parser
|
||||
->setDescription(__('Zeigt Details einer Umfrage, auf Wunsch mit dem geheimen Admin-Link.'))
|
||||
->addArgument('poll', [
|
||||
'help' => __('ID oder Beitrittscode der Umfrage.'),
|
||||
'required' => true,
|
||||
])
|
||||
->addOption('link', [
|
||||
'help' => __('Auch den Admin-Link ausgeben (enthält das geheime Token!).'),
|
||||
'boolean' => true,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \Cake\Console\Arguments $args Arguments
|
||||
* @param \Cake\Console\ConsoleIo $io Console
|
||||
* @return int
|
||||
*/
|
||||
public function execute(Arguments $args, ConsoleIo $io): int
|
||||
{
|
||||
$poll = $this->findPoll((string)$args->getArgument('poll'));
|
||||
if ($poll === null) {
|
||||
$io->err(__('Umfrage nicht gefunden.'));
|
||||
|
||||
return static::CODE_ERROR;
|
||||
}
|
||||
|
||||
$slides = $this->fetchTable('Slides')->find()->where(['poll_id' => $poll->id])->count();
|
||||
$io->out([
|
||||
__('ID: {0}', $poll->id),
|
||||
__('Titel: {0}', $poll->title),
|
||||
__('E-Mail: {0}', $poll->email),
|
||||
__('Beitrittscode: {0}', $poll->join_code),
|
||||
__('Teilnahme-Link: {0}', $this->joinLink($poll)),
|
||||
__('Folien: {0}', $slides),
|
||||
__('Angelegt: {0}', $this->formatDate($poll->created)),
|
||||
__('Letzter Besuch: {0}', $this->formatDate($poll->last_admin_access)),
|
||||
__('Läuft ab: {0}', $this->formatExpiry($poll)),
|
||||
__('Erinnerung: {0}', $poll->expiry_reminded_at === null ? __('nicht verschickt') : $this->formatDate($poll->expiry_reminded_at)),
|
||||
]);
|
||||
|
||||
if ($args->getOption('link')) {
|
||||
$link = $this->adminLink($poll);
|
||||
if ($link === null) {
|
||||
$io->err(__('Der Admin-Link lässt sich nicht entschlüsseln (SECURITY_SALT geändert?).'));
|
||||
|
||||
return static::CODE_ERROR;
|
||||
}
|
||||
$io->out(__('Admin-Link: {0}', $link));
|
||||
} else {
|
||||
$io->out(__('Admin-Link: (mit --link anzeigen)'));
|
||||
}
|
||||
|
||||
return static::CODE_SUCCESS;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user