From b5fc415edccfa2797344d6737bba95709a467dc2 Mon Sep 17 00:00:00 2001
From: Olivier PEREZ <olivier@olivierperez.fr>
Date: Tue, 23 Dec 2014 09:48:58 +0100
Subject: [PATCH] admin: Add availability to delete a poll

---
 adminstuds.php                                |  22 +-
 app/classes/Framadate/FramaDB.php             |  14 +-
 .../Framadate/Services/AdminPollService.php   |  20 +-
 old_adminstuds.php                            | 318 +-----------------
 tpl/poll_deleted.tpl                          |   8 +
 5 files changed, 59 insertions(+), 323 deletions(-)
 create mode 100644 tpl/poll_deleted.tpl

diff --git a/adminstuds.php b/adminstuds.php
index f8b0d43f..a26a08c9 100644
--- a/adminstuds.php
+++ b/adminstuds.php
@@ -189,7 +189,12 @@ if (isset($_POST['remove_all_votes'])) {
     exit;
 }
 if (isset($_POST['confirm_remove_all_votes'])) {
-    $adminPollService->cleanVotes($poll_id);
+    // TODO Add log
+    if ($adminPollService->cleanVotes($poll_id)) {
+        $message = new Message('success', _('All votes deleted.'));
+    } else {
+        $message = new Message('danger', _('Failed to delete all votes.'));
+    }
 }
 
 // -------------------------------
@@ -242,6 +247,7 @@ if (isset($_POST['remove_all_comments'])) {
     exit;
 }
 if (isset($_POST['confirm_remove_all_comments'])) {
+    // TODO Add log
     if ($adminPollService->cleanComments($poll_id)) {
         $message = new Message('success', _('All comments deleted.'));
     } else {
@@ -261,7 +267,18 @@ if (isset($_POST['delete_poll'])) {
     exit;
 }
 if (isset($_POST['confirm_delete_poll'])) {
-    // TODO
+    // TODO Add log
+    if ($adminPollService->deleteEntirePoll($poll_id)) {
+        $message = new Message('success', _('Poll fully deleted.'));
+    } else {
+        $message = new Message('danger', _('Failed to delete the poll.'));
+    }
+    $smarty->assign('poll_id', $poll_id);
+    $smarty->assign('admin_poll_id', $admin_poll_id);
+    $smarty->assign('title', _('Poll') . ' - ' . $poll->title);
+    $smarty->assign('message', $message);
+    $smarty->display('poll_deleted.tpl');
+    exit;
 }
 
 // -------------------------------
@@ -269,6 +286,7 @@ if (isset($_POST['confirm_delete_poll'])) {
 // -------------------------------
 
 if (!empty($_POST['delete_column'])) {
+    // TODO Add log
     $column = filter_input(INPUT_POST, 'delete_column', FILTER_DEFAULT);
 
     if ($adminPollService->deleteSlot($poll_id, $column)) {
diff --git a/app/classes/Framadate/FramaDB.php b/app/classes/Framadate/FramaDB.php
index aab3fc1a..b6864ab2 100644
--- a/app/classes/Framadate/FramaDB.php
+++ b/app/classes/Framadate/FramaDB.php
@@ -127,7 +127,7 @@ class FramaDB {
      * @param $poll_id int The ID of the given poll.
      * @return bool|null true if action succeeded.
      */
-    function deleteVotesByAdminPollId($poll_id) {
+    function deleteVotesByPollId($poll_id) {
         $prepared = $this->prepare('DELETE FROM user_studs WHERE id_sondage = ?');
         return $prepared->execute([$poll_id]);
     }
@@ -197,13 +197,18 @@ class FramaDB {
         $prepared->execute([$poll_id, $datetime]);
     }
 
+    function deleteSlotsByPollId($poll_id) {
+        $prepared = $this->prepare('DELETE FROM sujet_studs WHERE id_sondage = ?');
+        $prepared->execute([$poll_id]);
+    }
+
     /**
      * 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 deleteCommentsByAdminPollId($poll_id) {
+    function deleteCommentsByPollId($poll_id) {
         $prepared = $this->prepare('DELETE FROM comments WHERE id_sondage = ?');
         return $prepared->execute([$poll_id]);
     }
@@ -223,4 +228,9 @@ class FramaDB {
         return $prepared->execute([$poll_id, $comment_id]);
     }
 
+    function deleteByPollId($poll_id) {
+        $prepared = $this->prepare('DELETE FROM sondage WHERE poll_id = ?');
+        $prepared->execute([$poll_id]);
+    }
+
 }
diff --git a/app/classes/Framadate/Services/AdminPollService.php b/app/classes/Framadate/Services/AdminPollService.php
index 9d9c1063..4559ce61 100644
--- a/app/classes/Framadate/Services/AdminPollService.php
+++ b/app/classes/Framadate/Services/AdminPollService.php
@@ -40,7 +40,7 @@ class AdminPollService {
      * @return bool|null true is action succeeded
      */
     function cleanComments($poll_id) {
-        return $this->connect->deleteCommentsByAdminPollId($poll_id);
+        return $this->connect->deleteCommentsByPollId($poll_id);
     }
 
     /**
@@ -61,7 +61,22 @@ class AdminPollService {
      * @return bool|null true is action succeeded
      */
     function cleanVotes($poll_id) {
-        return $this->connect->deleteVotesByAdminPollId($poll_id);
+        return $this->connect->deleteVotesByPollId($poll_id);
+    }
+
+    /**
+     * Delete the entire given poll.
+     *
+     * @param $poll_id int The ID of the poll
+     * @return bool true is action succeeded
+     */
+    function deleteEntirePoll($poll_id) {
+        /*$this->connect->deleteVotesByPollId($poll_id);
+        $this->connect->deleteCommentsByPollId($poll_id);
+        $this->connect->deleteSlotsByPollId($poll_id);
+        $this->connect->deleteByPollId($poll_id);*/
+
+        return true;
     }
 
     /**
@@ -69,6 +84,7 @@ class AdminPollService {
      *
      * @param $poll_id int The ID of the poll
      * @param $slot string The name of the slot
+     * @return bool true if action succeeded
      */
     public function deleteSlot($poll_id, $slot) {
         $ex = explode('@', $slot);
diff --git a/old_adminstuds.php b/old_adminstuds.php
index 945be49c..46a8b8ee 100644
--- a/old_adminstuds.php
+++ b/old_adminstuds.php
@@ -42,97 +42,6 @@ function send_mail_admin() {
 }
 
 
-// reload
-// TODO OPZ Pourquoi recharger
-// $dsujet= $sujets->FetchObject(false);
-// $dsondage= $sondage->FetchObject(false);
-
-if (isset($_POST['ajoutsujet'])) {
-    Utils::print_header( _('Add a column') .' - ' . stripslashes($poll->title));
-
-    bandeau_titre(_('Make your polls'));
-
-    //on recupere les données et les sujets du sondage
-
-    echo '
-        <div class="row">
-            <div class="col-md-6 col-md-offset-3">
-            <form name="formulaire" class="form-horizontal" action="' . Utils::getUrlSondage($admin_poll_id, true) . '" method="POST">
-                <h2>' . _("Column's adding") . '</h2>'."\n";
-
-    if ($poll->format == "A"){
-        echo '
-                <div class="form-group">
-                    <label for="nouvellecolonne" class="col-md-6">' . _("Add a column") .' :</label>
-                    <div class="col-md-6">
-                        <input type="text" id="nouvellecolonne" name="nouvellecolonne" class="form-control" />
-                    </div>
-                </div>'."\n";
-    } else {
-        // ajout d'une date avec creneau horaire
-        echo '
-                <p>'. _("You can add a new scheduling date to your poll.").'<br />'._("If you just want to add a new hour to an existant date, put the same date and choose a new hour.") .'</p>
-
-                <div class="form-group">
-                    <label for="newdate" class="col-md-4">'. _("Day") .'</label>
-                    <div class="col-md-8">
-                        <div class="input-group date">
-                            <span class="input-group-addon"><i class="glyphicon glyphicon-calendar"></i></span>
-                            <input type="text" id="newdate" data-date-format="'. _("dd/mm/yyyy") .'" aria-describedby="dateformat" name="newdate" class="form-control" placeholder="'. _("dd/mm/yyyy") .'" />
-                        </div>
-                        <span id="dateformat" class="sr-only">'. _("(dd/mm/yyyy)") .'</span>
-                    </div>
-                </div>
-                <div class="form-group">
-                    <label for="newhour" class="col-md-4">'. _("Time") .'</label>
-                    <div class="col-md-8">
-                        <input type="text" id="newhour" name="newhour" class="form-control" />
-                    </div>
-                </div>';
-    }
-        echo '
-                <p class="text-center">
-                    <button class="btn btn-default" type="submit" value="retoursondage" name="retoursondage">'. _('Back to the poll'). '</button>
-                    <button type="submit" name="ajoutercolonne" class="btn btn-success">'. _('Add a column'). '</button>
-                </p>
-            </form>
-            </div>
-        </div>';
-
-    bandeau_pied();
-
-    die();
-}
-
-//action si bouton confirmation de suppression est activé
-if (isset($_POST["confirmesuppression"])) {
-    $nbuser=$user_studs->RecordCount();
-    $date=date('H:i:s d/m/Y:');
-
-    if (Utils::remove_sondage($connect, $poll_id)) {
-        // on ecrit dans le fichier de logs la suppression du sondage
-        error_log($date . " SUPPRESSION: $dsondage->id_sondage\t$dsondage->format\t$dsondage->nom_admin\t$dsondage->mail_admin\n", 3, 'admin/logs_studs.txt');
-
-        // Email sent
-        send_mail_admin();
-        //affichage de l'ecran de confirmation de suppression de sondage
-        Utils::print_header(_("Your poll has been removed!"));
-
-        bandeau_titre(_("Make your polls"));
-
-        echo '
-        <div class="alert alert-success text-center">
-            <h2>' . _("Your poll has been removed!") . '</h2>
-            <p>' . _('Back to the homepage of ') . ' <a href="' . Utils::get_server_name() . '"> ' . NOMAPPLICATION . '</a></p>
-        </div>
-    </form>'."\n";
-
-        bandeau_pied();
-
-        die();
-    }
-}
-
 $nbcolonnes = count($sujets);
 $nblignes = count($users);
 
@@ -158,165 +67,11 @@ if (isset($_POST["ajoutercolonne"]) && !empty($_POST['nouvellecolonne']) && $pol
 }
 
 
-// [begin] action quand on ajoute une colonne au format DATE
-if (isset($_POST['ajoutercolonne']) && $dsondage->format == 'D') {
-
-    if (!empty($_POST["newdate"])) {
-        $new_choice = mktime(0, 0, 0, substr($_POST["newdate"],3,2), substr($_POST["newdate"],0,2), substr($_POST["newdate"],6,4));
-
-        if (!empty($_POST["newhour"])){
-            $new_choice .= '@' . $_POST["newhour"];
-        }
-
-
-
-
-
-        // TODO OPZ Delete the code below
-        // TODO OPZ Insert new choice
-        // TODO OPZ Update users votes (add "0" in the right column^^)
-
-
-
-        //on rajoute la valeur dans les valeurs
-        $datesbase = explode(",",$dsujet->sujet);
-        $taillebase = sizeof($datesbase);
-
-        //recherche de l'endroit de l'insertion de la nouvelle date dans les dates deja entrées dans le tableau
-        if ($nouvelledate < $datesbase[0]) {
-            $cleinsertion = 0;
-        } elseif ($nouvelledate > $datesbase[$taillebase-1]) {
-            $cleinsertion = count($datesbase);
-        } else {
-            for ($i = 0; $i < count($datesbase); $i++) {
-                $j = $i + 1;
-                if ($nouvelledate > $datesbase[$i] && $nouvelledate < $datesbase[$j]) {
-                    $cleinsertion = $j;
-                }
-            }
-        }
-
-        array_splice($datesbase, $cleinsertion, 0, $nouvelledate);
-        $cle = array_search($nouvelledate, $datesbase);
-        $dateinsertion = '';
-        for ($i = 0; $i < count($datesbase); $i++) {
-            $dateinsertion.=",";
-            $dateinsertion.=$datesbase[$i];
-        }
-
-        $dateinsertion = substr("$dateinsertion", 1);
-
-        //mise a jour avec les nouveaux sujets dans la base
-        //if (isset($erreur_ajout_date) && !$erreur_ajout_date){
-            $sql = 'UPDATE sujet_studs SET sujet = '.$connect->Param('dateinsertion').' WHERE id_sondage = '.$connect->Param('numsondage');
-            $sql = $connect->Prepare($sql);
-            $connect->Execute($sql, array($dateinsertion, $poll_id));
-
-            if ($nouvelledate > strtotime($dsondage->date_fin)) {
-                $date_fin=$nouvelledate+200000;
-                $sql = 'UPDATE sondage SET date_fin = '.$connect->Param('date_fin').' WHERE id_sondage = '.$connect->Param('numsondage');
-                $sql = $connect->Prepare($sql);
-                $connect->Execute($sql, array($date_fin, $poll_id));
-            }
-        //}
-
-        //mise a jour des reponses actuelles correspondant au sujet ajouté
-        $sql = 'UPDATE user_studs SET reponses = '.$connect->Param('reponses').' WHERE nom = '.$connect->Param('nom').' AND id_users='.$connect->Param('id_users');
-        $sql = $connect->Prepare($sql);
-        while ($data = $user_studs->FetchNextObject(false)) {
-            $ensemblereponses=$data->reponses;
-            $newcar = '';
-
-            //parcours de toutes les réponses actuelles
-            for ($j = 0; $j < $nbcolonnes; $j++) {
-                $car=substr($ensemblereponses,$j,1);
-
-                //si les reponses ne concerne pas la colonne ajoutée, on concatene
-                if ($j==$cle) {
-                    $newcar.="0";
-                }
-
-                $newcar.=$car;
-            }
-
-            //mise a jour des reponses utilisateurs dans la base
-            if (isset($erreur_ajout_date) && !$erreur_ajout_date){
-                $connect->Execute($sql, array($newcar, $data->nom, $data->id_users));
-            }
-        }
-
-        //Email sent to the admin
-        send_mail_admin();
-
-    } else {
-        $erreur_ajout_date="yes";
-    }
-}
-// [end] action quand on ajoute une colonne au format DATE
-
-
 //on teste pour voir si une ligne doit etre modifiée
 $testmodifier = false;
 $testligneamodifier = false;
 
 
-//suppression de colonnes dans la base
-for ($i = 0; $i < $nbcolonnes; $i++) {
-    if ((isset($_POST["effacecolonne$i"])) && $nbcolonnes > 1){
-        $sujets = explode(",",$dsujet->sujet);
-        //sort($toutsujet, SORT_NUMERIC);
-        $j = 0;
-        $nouveauxsujets = '';
-
-        //parcours de tous les sujets actuels
-        while (isset($sujets[$j])) {
-            //si le sujet n'est pas celui qui a été effacé alors on concatene
-            if ($i != $j) {
-                $nouveauxsujets .= ',';
-                $nouveauxsujets .= $sujets[$j];
-            }
-
-            $j++;
-        }
-
-        //on enleve la virgule au début
-        $nouveauxsujets = substr("$nouveauxsujets", 1);
-
-        //nettoyage des reponses actuelles correspondant au sujet effacé
-        $compteur = 0;
-        $sql = 'UPDATE user_studs SET reponses = '.$connect->Param('reponses').' WHERE nom = '.$connect->Param('nom').' AND id_users = '.$connect->Param('id_users');
-        $sql = $connect->Prepare($sql);
-
-        while ($data = $user_studs->FetchNextObject(false)) {
-            $newcar = '';
-            $ensemblereponses = $data->reponses;
-
-            //parcours de toutes les réponses actuelles
-            for ($j = 0; $j < $nbcolonnes; $j++) {
-                $car=substr($ensemblereponses, $j, 1);
-                //si les reponses ne concerne pas la colonne effacée, on concatene
-                if ($i != $j) {
-                    $newcar .= $car;
-                }
-            }
-
-            $compteur++;
-
-            //mise a jour des reponses utilisateurs dans la base
-            $connect->Execute($sql, array($newcar, $data->nom, $data->id_users));
-        }
-
-        //mise a jour des sujets dans la base
-        $sql = 'UPDATE sujet_studs SET sujet = '.$connect->Param('nouveauxsujets').' WHERE id_sondage = '.$connect->Param('numsondage');
-        $sql = $connect->Prepare($sql);
-        $connect->Execute($sql, array($nouveauxsujets, $poll_id));
-    }
-}
-
-
-
-// Table headers
-$thead = '<thead>';
 
 // Button in the first td to avoid remove col on "Return" keypress)
 $tr_add_remove_col = '<tr><td role="presentation"><button type="submit" class="invisible" name="boutonp" ></button></td>';
@@ -325,78 +80,7 @@ $border = array(); // bordure pour distinguer les mois
 $td_headers = array(); // for a11y, headers="M1 D4 H5" on each td
 $radio_title = array(); // date for
 
-// Display dates poll
-if ($poll->format == "D") {
-
-    $tr_months = '<tr><th role="presentation"></th>';
-    $tr_days = '<tr><th role="presentation"></th>';
-    $tr_hours = '<tr><th role="presentation"></th>';
-
-    // Headers
-    $colspan_month = 1;
-    $colspan_day = 1;
-
-    foreach ($sujets as $i=>$sujet) {
-
-        // Current date
-        $horoCur = explode('@', $sujet->sujet); //horoCur[0] = date, horoCur[1] = hour
-        if (isset($sujets[$i+1])){
-            $next = $sujets[$i+1]->sujet;
-            $horoNext = explode('@', $next);
-        }
-        $border[$i] = false;
-        $radio_title[$i] = strftime($date_format['txt_short'], $horoCur[0]);
-
-        // Months
-        $td_headers[$i] = 'M'.($i+1-$colspan_month);
-
-        if (isset($sujets[$i+1]) && strftime("%B", $horoCur[0]) == strftime("%B", $horoNext[0]) && strftime("%Y", $horoCur[0]) == strftime("%Y", $horoNext[0])){
-            $colspan_month++;
-        } else {
-            $border[$i] = true;
-            $tr_months .= '<th colspan="'.$colspan_month.'" class="bg-primary month" id="M'.($i+1-$colspan_month).'">'.strftime("%B",$horoCur[0]).' '.strftime("%Y", $horoCur[0]).'</th>';
-            $colspan_month=1;
-        }
-
-        // Days
-        $td_headers[$i] .= ' D'.($i+1-$colspan_day);
-
-        if (isset($sujets[$i+1]) && strftime($date_format['txt_day'],$horoCur[0])==strftime($date_format['txt_day'],$horoNext[0]) && strftime("%B",$horoCur[0])==strftime("%B",$horoNext[0])){
-            $colspan_day++;
-        } else {
-            $rbd = ($border[$i]) ? ' rbd' : '';
-            $tr_days .= '<th colspan="'.$colspan_day.'" class="bg-primary day'.$rbd.'" id="D'.($i+1-$colspan_day).'">'.strftime($date_format['txt_day'],$horoCur[0]).'</th>';
-            $colspan_day=1;
-        }
-
-        // Hours
-        $rbd = ($border[$i]) ? ' rbd' : '';
-        if ($horoCur[1] !== "") {
-                $tr_hours .= '<th class="bg-info'.$rbd.'" id="H'.$i.'" title="'.$horoCur[1].'">'.$horoCur[1].'</th>';
-                $radio_title[$i] .= ' - '.$horoCur[1];
-                $td_headers[$i] .= ' H'.$i;
-        } else {
-                $tr_hours .= '<th class="bg-info'.$rbd.'"></th>';
-        }
-
-        // Remove col
-        $tr_add_remove_col .= (count($sujets) > 2 ) ? '<td headers="'.$td_headers[$i].'"><button type="submit" name="effacecolonne'.$i.'" class="btn btn-link btn-sm" title="' . _('Remove the column') . ' ' .$radio_title[$i]. '"><span class="glyphicon glyphicon-remove text-danger"></span><span class="sr-only">'. _("Remove") .'</span></button></td>' : '<td role="presentation"></td>';
-
-    }
-
-    $border[count($border)-1] = false; // suppression de la bordure droite du dernier mois
-
-    $tr_months .= '<th></th></tr>';
-    $tr_days .= '<th></th></tr>';
-    $tr_hours .= '<th></th></tr>';
-
-    // Add col
-    $tr_add_remove_col .= '<td><button type="submit" name="ajoutsujet" class="btn btn-link btn-sm" title="'. _('Add a column') . '"><span class="glyphicon glyphicon-plus text-success"></span><span class="sr-only">'. _("Add a column") .'</span></button></td></tr>';
-
-    $thead = "\n".$tr_add_remove_col."\n".$tr_months."\n".$tr_days."\n".$tr_hours."\n";
-
-// Subjects poll
-} else {
+if ($poll->format == "A") {
     $tr_subjects = '<tr><th role="presentation"></th>';
 
     foreach ($sujets as $i=>$sujet) {
diff --git a/tpl/poll_deleted.tpl b/tpl/poll_deleted.tpl
new file mode 100644
index 00000000..b7b0ce32
--- /dev/null
+++ b/tpl/poll_deleted.tpl
@@ -0,0 +1,8 @@
+{extends file='page.tpl'}
+
+{block name=main}
+    <div class="alert alert-success text-center">
+        <h2>{_("Your poll has been removed!")}</h2>
+        <p>{_('Back to the homepage of')} <a href="{$SERVER_URL}">{$APPLICATION_NAME}</a></p>
+    </div>
+{/block}
\ No newline at end of file
-- 
GitLab