From 07d5a336fbe1e40e0b1c2539a347591a28660148 Mon Sep 17 00:00:00 2001
From: Olivier PEREZ <olivier@olivierperez.fr>
Date: Fri, 19 Dec 2014 00:36:09 +0100
Subject: [PATCH] Add availability to delete all comments of one poll.

+ Simplify call to remove all votes of one poll
---
 adminstuds.php                                |  9 +++++-
 app/classes/Framadate/FramaDB.php             | 30 ++++++++++++-------
 .../Framadate/Services/PollService.php        |  8 +++--
 3 files changed, 33 insertions(+), 14 deletions(-)

diff --git a/adminstuds.php b/adminstuds.php
index 6da08c69..689d51bd 100644
--- a/adminstuds.php
+++ b/adminstuds.php
@@ -56,7 +56,14 @@ if (!$poll) {
 // Remove all votes
 // -------------------------------
 if (isset($_POST['remove_all_votes'])) {
-    $pollService->cleanVotes($admin_poll_id, $poll_id);
+    $pollService->cleanVotes($poll_id);
+}
+
+// -------------------------------
+// Remove all comments
+// -------------------------------
+if (isset($_POST['remove_all_comments'])) {
+    $pollService->cleanComments($poll_id);
 }
 
 // -------------------------------
diff --git a/app/classes/Framadate/FramaDB.php b/app/classes/Framadate/FramaDB.php
index 2359bc17..33f97b96 100644
--- a/app/classes/Framadate/FramaDB.php
+++ b/app/classes/Framadate/FramaDB.php
@@ -102,18 +102,26 @@ class FramaDB
         return $newVote;
     }
 
-    function deleteVotesByAdminPollId($admin_poll_id, $poll_id) {
-        $prepared = $this->prepare('SELECT 1 FROM sondage WHERE admin_poll_id = ? AND poll_id = ?');
-        $prepared->execute([$admin_poll_id, $poll_id]);
-        $count = $prepared->rowCount();
-        $prepared->closeCursor();
+    /**
+     * Delete all votes of a given poll.
+     *
+     * @param $poll_id int The ID of the given poll.
+     * @return bool|null true if action succeeded.
+     */
+    function deleteVotesByAdminPollId($poll_id) {
+        $prepared = $this->prepare('DELETE FROM user_studs WHERE id_sondage = ?');
+        return $prepared->execute([$poll_id]);
+    }
 
-        if ($count === 1) {
-            $prepared = $this->prepare('DELETE FROM user_studs WHERE id_sondage = ?');
-            return $prepared->execute([$poll_id]);
-        } else {
-            return null;
-        }
+    /**
+     * Delete all comments of a given poll.
+     *
+     * @param $poll_id int The ID of the given poll.
+     * @return bool|null true if action succeeded.
+     */
+    function deleteCommentssByAdminPollId($poll_id) {
+        $prepared = $this->prepare('DELETE FROM comments WHERE id_sondage = ?');
+        return $prepared->execute([$poll_id]);
     }
 
     function updateVote($poll_id, $vote_id, $choices) {
diff --git a/app/classes/Framadate/Services/PollService.php b/app/classes/Framadate/Services/PollService.php
index 34d93e1f..ca73c9ca 100644
--- a/app/classes/Framadate/Services/PollService.php
+++ b/app/classes/Framadate/Services/PollService.php
@@ -60,8 +60,8 @@ class PollService {
         return $this->connect->insertVote($poll_id, $name, $choices);
     }
 
-    function cleanVotes($admin_poll_id, $poll_id) {
-        $this->connect->deleteVotesByAdminPollId($admin_poll_id, $poll_id);
+    function cleanVotes($poll_id) {
+        $this->connect->deleteVotesByAdminPollId($poll_id);
     }
 
     function addComment($poll_id, $name, $comment) {
@@ -72,6 +72,10 @@ class PollService {
         return $this->connect->deleteComment($poll_id, $comment_id);
     }
 
+    function cleanComments($poll_id) {
+        $this->connect->deleteCommentssByAdminPollId($poll_id);
+    }
+
     function computeBestMoments($votes) {
         $result = [];
         foreach ($votes as $vote) {
-- 
GitLab