diff --git a/admin/cleanDemo.php b/admin/cleanDemo.php new file mode 100644 index 0000000..cc6a567 --- /dev/null +++ b/admin/cleanDemo.php @@ -0,0 +1,30 @@ +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..dca8378 100644 --- a/app/classes/Framadate/Services/PurgeService.php +++ b/app/classes/Framadate/Services/PurgeService.php @@ -46,7 +46,28 @@ 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. *