From 6455f4be870870ec25b3f5c9212ae70dbe5f15a9 Mon Sep 17 00:00:00 2001 From: m Date: Fri, 13 Apr 2018 21:24:43 +0200 Subject: [PATCH] =?UTF-8?q?nettoyage=20du=20sondage=20de=20d=C3=A9monstrat?= =?UTF-8?q?ion=20https://framagit.org/framasoft/framadate/issues/316?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- admin/cleanDemo.php | 30 +++++++++++++++++++ .../Framadate/Repositories/VoteRepository.php | 6 ++++ .../Framadate/Services/PurgeService.php | 23 +++++++++++++- 3 files changed, 58 insertions(+), 1 deletion(-) create mode 100644 admin/cleanDemo.php 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. *