diff --git a/admin/cleanDemo.php b/admin/cleanDemo.php new file mode 100644 index 0000000..e6e4a32 --- /dev/null +++ b/admin/cleanDemo.php @@ -0,0 +1,29 @@ +cleanDemoPoll(); + diff --git a/app/classes/Framadate/Repositories/VoteRepository.php b/app/classes/Framadate/Repositories/VoteRepository.php index cbbbd77..f015162 100644 --- a/app/classes/Framadate/Repositories/VoteRepository.php +++ b/app/classes/Framadate/Repositories/VoteRepository.php @@ -41,7 +41,13 @@ class VoteRepository extends AbstractRepository { return $prepared->execute([$poll_id, $vote_id]); } + + public function deleteOldVotesByPollId($poll_id, $votesToDelete) { + $prepared = $this->prepare('DELETE FROM `' . Utils::table('vote') . '` WHERE poll_id = ? ORDER BY `poll_id` ASC LIMIT ' . $votesToDelete); + return $prepared->execute([$poll_id]); + } + /** * Delete all votes of a given poll. * diff --git a/app/classes/Framadate/Services/PurgeService.php b/app/classes/Framadate/Services/PurgeService.php index 7b59b7a..50c15df 100644 --- a/app/classes/Framadate/Services/PurgeService.php +++ b/app/classes/Framadate/Services/PurgeService.php @@ -23,6 +23,14 @@ class PurgeService { $this->commentRepository = RepositoryFactory::commentRepository(); } + public function repeatedCleanings() { + $this->purgeOldPolls(); + + if (0 === time() % 10) { + $this->cleanDemoPoll(); + } + } + /** * This methode purges all old polls (the ones with end_date in past). * @@ -46,7 +54,24 @@ class PurgeService { return $count; } - + + public function cleanDemoPoll() { + if (!defined("DEMO_POLL_ID") || !defined("DEMO_POLL_NUMBER_VOTES")) { + return; + } + + $this->voteRepository->beginTransaction(); + + $demoVotes = $this->voteRepository->allUserVotesByPollId(DEMO_POLL_ID); + $votesToDelete = count($demoVotes) - DEMO_POLL_NUMBER_VOTES; + + if ($votesToDelete > 0) { + $this->voteRepository->deleteOldVotesByPollId(DEMO_POLL_ID, $votesToDelete); + } + + $this->voteRepository->commit(); + } + /** * This methode delete all data about a poll. * diff --git a/create_classic_poll.php b/create_classic_poll.php index 920b0ae..193b7c8 100644 --- a/create_classic_poll.php +++ b/create_classic_poll.php @@ -107,8 +107,7 @@ if (empty($_SESSION['form']->title) || empty($_SESSION['form']->admin_name) || ( // Clean Form data in $_SESSION unset($_SESSION['form']); - // Delete old polls - $purgeService->purgeOldPolls(); + $purgeService->repeatedCleanings(); // creation message $sessionService->set("Framadate", "messagePollCreated", TRUE); diff --git a/create_date_poll.php b/create_date_poll.php index a66330d..69a5b93 100644 --- a/create_date_poll.php +++ b/create_date_poll.php @@ -231,8 +231,7 @@ switch ($step) { // Clean Form data in $_SESSION unset($_SESSION['form']); - // Delete old polls - $purgeService->purgeOldPolls(); + $purgeService->repeatedCleanings(); // creation message $sessionService->set("Framadate", "messagePollCreated", TRUE); diff --git a/index.php b/index.php index 0052b98..4782288 100644 --- a/index.php +++ b/index.php @@ -35,14 +35,19 @@ $pollService = new PollService($connect, new $logService()); /* PAGE */ /* ---- */ -$demoPoll = $pollService->findById('aqg259dth55iuhwm'); +$demoPollURL = ""; + +if (defined("DEMO_POLL_ID")) { + $demoPollURL = Utils::getUrlSondage(DEMO_POLL_ID); +} + $nbcol = max( $config['show_what_is_that'] + $config['show_the_software'] + $config['show_cultivate_your_garden'], 1 ); $smarty->assign('show_what_is_that', $config['show_what_is_that']); $smarty->assign('show_the_software', $config['show_the_software']); $smarty->assign('show_cultivate_your_garden', $config['show_cultivate_your_garden']); $smarty->assign('col_size', 12 / $nbcol); -$smarty->assign('demo_poll', $demoPoll); +$smarty->assign('demo_poll_url', $demoPollURL); $smarty->assign('title', __('Generic', 'Make your polls')); diff --git a/tpl/admin/config.tpl b/tpl/admin/config.tpl index b0ae98f..e3b22c0 100644 --- a/tpl/admin/config.tpl +++ b/tpl/admin/config.tpl @@ -82,6 +82,13 @@ const MAX_SLOTS_PER_POLL = 366; // Number of seconds before we allow to resend an "Remember Edit Link" email. const TIME_EDIT_LINK_EMAIL = 60; +// uncomment to display a link to the demo poll at the home page +//const DEMO_POLL_ID = "aqg259dth55iuhwm"; + +// number of recent votes that are not deleted +const DEMO_POLL_NUMBER_VOTES = 10; + + // Config $config = [ /* general config */ diff --git a/tpl/index.tpl b/tpl/index.tpl index 2524f1f..1d3bafd 100644 --- a/tpl/index.tpl +++ b/tpl/index.tpl @@ -58,10 +58,10 @@
  • {__('1st section', 'Discuss and make a decision')}
  • - {if $demo_poll != null} + {if $demo_poll_url}

    {__('1st section', 'Do you want to')} - {__('1st section', 'view an example?')} + {__('1st section', 'view an example?')}

    {/if}