diff --git a/adminstuds.php b/adminstuds.php index f3f123d..66af6db 100644 --- a/adminstuds.php +++ b/adminstuds.php @@ -36,7 +36,7 @@ $editingVoteId = 0; /*----------*/ $pollService = new PollService($connect); -$adminPollService = new AdminPollService($connect); +$adminPollService = new AdminPollService($connect, $pollService); $inputService = new InputService(); /* PAGE */ @@ -115,7 +115,6 @@ if (isset($_POST['update_poll_info'])) { // ------------------------------- if (!empty($_POST['edit_vote'])) { - // TODO Try what does filter_input with a wrong value $editingVoteId = filter_input(INPUT_POST, 'edit_vote', FILTER_VALIDATE_INT); } @@ -170,6 +169,7 @@ if (!empty($_POST['save'])) { // Save edition of an old vote // ------------------------------- // Delete a votes // ------------------------------- + if (!empty($_POST['delete_vote'])) { $vote_id = filter_input(INPUT_POST, 'delete_vote', FILTER_VALIDATE_INT); if ($adminPollService->deleteVote($poll_id, $vote_id)) { @@ -182,6 +182,7 @@ if (!empty($_POST['delete_vote'])) { // ------------------------------- // Remove all votes // ------------------------------- + if (isset($_POST['remove_all_votes'])) { $smarty->assign('poll_id', $poll_id); $smarty->assign('admin_poll_id', $admin_poll_id); @@ -234,6 +235,7 @@ if (!empty($_POST['delete_comment'])) { // ------------------------------- // Remove all comments // ------------------------------- + if (isset($_POST['remove_all_comments'])) { $smarty->assign('poll_id', $poll_id); $smarty->assign('admin_poll_id', $admin_poll_id); @@ -264,6 +266,20 @@ if (isset($_POST['confirm_delete_poll'])) { // TODO } +// ------------------------------- +// Delete a slot +// ------------------------------- + +if (!empty($_POST['delete_column'])) { + $column = filter_input(INPUT_POST, 'delete_column', FILTER_DEFAULT); + + if ($adminPollService->deleteSlot($poll_id, $column)) { + $message = new Message('success', _('Column deleted.')); + } else { + $message = new Message('danger', _('Failed to delete the column.')); + } +} + // Retrieve data $slots = $pollService->allSlotsByPollId($poll_id); $votes = $pollService->allUserVotesByPollId($poll_id); diff --git a/app/classes/Framadate/FramaDB.php b/app/classes/Framadate/FramaDB.php index 102ccfb..0bbb036 100644 --- a/app/classes/Framadate/FramaDB.php +++ b/app/classes/Framadate/FramaDB.php @@ -18,23 +18,20 @@ */ namespace Framadate; -class FramaDB -{ +class FramaDB { /** * PDO Object, connection to database. */ private $pdo = null; - function __construct($connection_string, $user, $password) - { + function __construct($connection_string, $user, $password) { $this->pdo = new \PDO($connection_string, $user, $password); $this->pdo->setAttribute(\PDO::ATTR_DEFAULT_FETCH_MODE, \PDO::FETCH_OBJ); $this->pdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION); } - function areTablesCreated() - { - $result= $this->pdo->query('SHOW TABLES'); + function areTablesCreated() { + $result = $this->pdo->query('SHOW TABLES'); $schemas = $result->fetchAll(\PDO::FETCH_COLUMN); return !empty(array_diff($schemas, ['comments', 'sondage', 'sujet_studs', 'user_studs'])); } @@ -118,6 +115,42 @@ class FramaDB return $prepared->execute([$poll_id]); } + /** + * Delete all votes made on given moment index. + * + * @param $poll_id int The ID of the poll + * @param $index int The index of the vote into the poll + * @return bool|null true if action succeeded. + */ + function deleteVotesByIndex($poll_id, $index) { + $prepared = $this->prepare('UPDATE user_studs SET reponses = CONCAT(SUBSTR(reponses, 1, ?), SUBSTR(reponses, ?)) WHERE id_sondage = ?'); + return $prepared->execute([$index, $index + 2, $poll_id]); + } + + /** + * Update a slot into a poll. + * + * @param $poll_id int The ID of the poll + * @param $datetime int The datetime of the slot to update + * @param $newValue mixed The new value of the entire slot + * @return bool|null true if action succeeded. + */ + function updateSlot($poll_id, $datetime, $newValue) { + $prepared = $this->prepare('UPDATE sujet_studs SET sujet = ? WHERE id_sondage = ? AND SUBSTRING_INDEX(sujet, \'@\', 1) = ?'); + return $prepared->execute([$newValue, $poll_id, $datetime]); + } + + /** + * Delete a entire slot from a poll. + * + * @param $poll_id int The ID of the poll + * @param $datetime mixed The datetime of the slot + */ + function deleteSlot($poll_id, $datetime) { + $prepared = $this->prepare('DELETE FROM sujet_studs WHERE id_sondage = ? AND SUBSTRING_INDEX(sujet, \'@\', 1) = ?'); + $prepared->execute([$poll_id, $datetime]); + } + /** * Delete all comments of a given poll. * diff --git a/app/classes/Framadate/Services/AdminPollService.php b/app/classes/Framadate/Services/AdminPollService.php index eee80a9..450ed78 100644 --- a/app/classes/Framadate/Services/AdminPollService.php +++ b/app/classes/Framadate/Services/AdminPollService.php @@ -8,9 +8,11 @@ namespace Framadate\Services; class AdminPollService { private $connect; + private $pollService; - function __construct($connect) { + function __construct($connect, $pollService) { $this->connect = $connect; + $this->pollService = $pollService; } function updatePoll($poll) { @@ -59,5 +61,50 @@ class AdminPollService { return $this->connect->deleteVotesByAdminPollId($poll_id); } + /** + * Delete a slot from a poll. + * + * @param $poll_id int The ID of the poll + * @param $slot string The name of the slot + */ + public function deleteSlot($poll_id, $slot) { + $ex = explode('@', $slot); + $datetime = $ex[0]; + $moment = $ex[1]; + + $slots = $this->pollService->allSlotsByPollId($poll_id); + + $index = 0; + $indexToDelete = -1; + $newMoments = []; + + // Search the index of the slot to delete + foreach ($slots as $aSlot) { + $ex = explode('@', $aSlot->sujet); + $moments = explode(',', $ex[1]); + + foreach ($moments as $rowMoment) { + if ($datetime == $ex[0]) { + if ($moment == $rowMoment) { + $indexToDelete = $index; + } else { + $newMoments[] = $rowMoment; + } + } + $index++; + } + } + + // Remove votes + $this->connect->beginTransaction(); + $this->connect->deleteVotesByIndex($poll_id, $indexToDelete); + if (count($newMoments) > 0) { + $this->connect->updateSlot($poll_id, $datetime, $datetime . '@' . implode(',', $newMoments)); + } else { + $this->connect->deleteSlot($poll_id, $datetime); + } + $this->connect->commit(); + } + } \ No newline at end of file diff --git a/studs.php b/studs.php index bf6db02..08a0206 100644 --- a/studs.php +++ b/studs.php @@ -55,7 +55,6 @@ if (!$poll) { // ------------------------------- if (!empty($_POST['edit_vote'])) { - // TODO Try what does filter_input with a wrong value $editingVoteId = filter_input(INPUT_POST, 'edit_vote', FILTER_VALIDATE_INT); }