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
@@ -3,6 +3,7 @@ declare(strict_types=1);
|
||||
|
||||
namespace App\Model\Entity;
|
||||
|
||||
use Cake\Core\Configure;
|
||||
use Cake\I18n\DateTime;
|
||||
use Cake\ORM\Entity;
|
||||
|
||||
@@ -20,6 +21,7 @@ use Cake\ORM\Entity;
|
||||
* @property bool $is_active
|
||||
* @property int $comment_speed Speed of rotating scale comments in percent, see COMMENT_SPEEDS
|
||||
* @property \Cake\I18n\DateTime|null $last_admin_access
|
||||
* @property \Cake\I18n\DateTime|null $expiry_reminded_at When the reminder before the expiry was mailed
|
||||
* @property \Cake\I18n\DateTime $created
|
||||
* @property \Cake\I18n\DateTime $modified
|
||||
* @property array<\App\Model\Entity\Slide> $slides
|
||||
@@ -69,6 +71,53 @@ class Poll extends Entity
|
||||
return trim(chunk_split($this->join_code, 3, ' '));
|
||||
}
|
||||
|
||||
/**
|
||||
* Polls live `Plenario.retention.days` days from their creation, then `bin/cake polls cleanup`
|
||||
* deletes them (a reminder mail goes out beforehand, see reminderAt()). Computed, not stored:
|
||||
* changing the setting applies to all polls at once.
|
||||
*
|
||||
* @return \Cake\I18n\DateTime|null Null if polls never expire (setting 0)
|
||||
*/
|
||||
public function expiresAt(): ?DateTime
|
||||
{
|
||||
$days = (int)Configure::read('Plenario.retention.days', 90);
|
||||
|
||||
return $days > 0 ? $this->created->addDays($days) : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int Days before the expiry date the reminder goes out (0 = no reminder). Always shorter than the
|
||||
* lifetime, otherwise the reminder would be due the moment a poll is created.
|
||||
*/
|
||||
public static function reminderLeadDays(): int
|
||||
{
|
||||
$days = (int)Configure::read('Plenario.retention.days', 90);
|
||||
$lead = (int)Configure::read('Plenario.retention.reminderDays', 14);
|
||||
|
||||
return $days > 0 ? max(0, min($lead, $days - 1)) : 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return \Cake\I18n\DateTime|null When the reminder becomes due, null without expiry or reminder
|
||||
*/
|
||||
public function reminderAt(): ?DateTime
|
||||
{
|
||||
$expires = $this->expiresAt();
|
||||
$lead = static::reminderLeadDays();
|
||||
|
||||
return $expires === null || $lead === 0 ? null : $expires->subDays($lead);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool Past its expiry date
|
||||
*/
|
||||
public function isExpired(): bool
|
||||
{
|
||||
$expires = $this->expiresAt();
|
||||
|
||||
return $expires !== null && $expires->isPast();
|
||||
}
|
||||
|
||||
/**
|
||||
* Shows a slide to the participants (from PlenarIO itself: no embed timeout).
|
||||
*
|
||||
|
||||
@@ -9,6 +9,7 @@ use ArrayObject;
|
||||
use Cake\Core\Configure;
|
||||
use Cake\Datasource\EntityInterface;
|
||||
use Cake\Event\EventInterface;
|
||||
use Cake\I18n\DateTime;
|
||||
use Cake\ORM\Query\SelectQuery;
|
||||
use Cake\ORM\RulesChecker;
|
||||
use Cake\ORM\Table;
|
||||
@@ -276,6 +277,45 @@ class PollsTable extends Table
|
||||
return $query->where(['Polls.join_code' => static::normalizeJoinCode($code)]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Polls past their expiry date (created + retention, see Poll::expiresAt()); none if the retention is 0.
|
||||
*
|
||||
* @param \Cake\ORM\Query\SelectQuery $query Query
|
||||
* @return \Cake\ORM\Query\SelectQuery
|
||||
*/
|
||||
public function findExpired(SelectQuery $query): SelectQuery
|
||||
{
|
||||
$days = (int)Configure::read('Plenario.retention.days', 90);
|
||||
if ($days <= 0) {
|
||||
return $query->where('1 = 0');
|
||||
}
|
||||
|
||||
return $query->where(['Polls.created <' => DateTime::now()->subDays($days)]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Polls whose reminder is due and not yet sent: inside the last `reminderDays` before the expiry,
|
||||
* but not expired yet (then it is too late, they are deleted).
|
||||
*
|
||||
* @param \Cake\ORM\Query\SelectQuery $query Query
|
||||
* @return \Cake\ORM\Query\SelectQuery
|
||||
*/
|
||||
public function findReminderDue(SelectQuery $query): SelectQuery
|
||||
{
|
||||
$lead = Poll::reminderLeadDays();
|
||||
if ($lead === 0) {
|
||||
return $query->where('1 = 0');
|
||||
}
|
||||
$days = (int)Configure::read('Plenario.retention.days', 90);
|
||||
$now = DateTime::now();
|
||||
|
||||
return $query->where([
|
||||
'Polls.expiry_reminded_at IS' => null,
|
||||
'Polls.created <=' => $now->subDays($days - $lead),
|
||||
'Polls.created >=' => $now->subDays($days),
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \Cake\ORM\Query\SelectQuery $query Query
|
||||
* @param string $key Embed key
|
||||
|
||||
Reference in new issue
Block a user