Implements purge methods
This commit is contained in:
parent
10c409e29e
commit
dcb711dccc
@ -33,6 +33,7 @@ class FramaDB {
|
||||
function areTablesCreated() {
|
||||
$result = $this->pdo->query('SHOW TABLES');
|
||||
$schemas = $result->fetchAll(\PDO::FETCH_COLUMN);
|
||||
|
||||
return 0 != count(array_diff($schemas, ['comments', 'sondage', 'sujet_studs', 'user_studs']));
|
||||
}
|
||||
|
||||
@ -83,23 +84,27 @@ class FramaDB {
|
||||
function allCommentsByPollId($poll_id) {
|
||||
$prepared = $this->prepare('SELECT * FROM comments WHERE id_sondage = ? ORDER BY id_comment');
|
||||
$prepared->execute(array($poll_id));
|
||||
|
||||
return $prepared->fetchAll();
|
||||
}
|
||||
|
||||
function allUserVotesByPollId($poll_id) {
|
||||
$prepared = $this->prepare('SELECT * FROM user_studs WHERE id_sondage = ? ORDER BY id_users');
|
||||
$prepared->execute(array($poll_id));
|
||||
|
||||
return $prepared->fetchAll();
|
||||
}
|
||||
|
||||
function allSlotsByPollId($poll_id) {
|
||||
$prepared = $this->prepare('SELECT * FROM sujet_studs WHERE id_sondage = ? ORDER BY sujet');
|
||||
$prepared->execute(array($poll_id));
|
||||
|
||||
return $prepared->fetchAll();
|
||||
}
|
||||
|
||||
function insertDefaultVote($poll_id, $insert_position) {
|
||||
$prepared = $this->prepare('UPDATE user_studs SET reponses = CONCAT(SUBSTRING(reponses, 1, ?), "0", SUBSTRING(reponses, ?)) WHERE id_sondage = ?');
|
||||
|
||||
return $prepared->execute([$insert_position, $insert_position + 1, $poll_id]);
|
||||
}
|
||||
|
||||
@ -118,6 +123,7 @@ class FramaDB {
|
||||
|
||||
function deleteVote($poll_id, $vote_id) {
|
||||
$prepared = $this->prepare('DELETE FROM user_studs WHERE id_sondage = ? AND id_users = ?');
|
||||
|
||||
return $prepared->execute([$poll_id, $vote_id]);
|
||||
}
|
||||
|
||||
@ -129,6 +135,7 @@ class FramaDB {
|
||||
*/
|
||||
function deleteVotesByPollId($poll_id) {
|
||||
$prepared = $this->prepare('DELETE FROM user_studs WHERE id_sondage = ?');
|
||||
|
||||
return $prepared->execute([$poll_id]);
|
||||
}
|
||||
|
||||
@ -141,6 +148,7 @@ class FramaDB {
|
||||
*/
|
||||
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]);
|
||||
}
|
||||
|
||||
@ -170,6 +178,7 @@ class FramaDB {
|
||||
*/
|
||||
function insertSlot($poll_id, $slot) {
|
||||
$prepared = $this->prepare('INSERT INTO sujet_studs (id_sondage, sujet) VALUES (?,?)');
|
||||
|
||||
return $prepared->execute([$poll_id, $slot]);
|
||||
}
|
||||
|
||||
@ -183,6 +192,7 @@ class FramaDB {
|
||||
*/
|
||||
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]);
|
||||
}
|
||||
|
||||
@ -199,7 +209,7 @@ class FramaDB {
|
||||
|
||||
function deleteSlotsByPollId($poll_id) {
|
||||
$prepared = $this->prepare('DELETE FROM sujet_studs WHERE id_sondage = ?');
|
||||
$prepared->execute([$poll_id]);
|
||||
return $prepared->execute([$poll_id]);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -210,27 +220,43 @@ class FramaDB {
|
||||
*/
|
||||
function deleteCommentsByPollId($poll_id) {
|
||||
$prepared = $this->prepare('DELETE FROM comments WHERE id_sondage = ?');
|
||||
|
||||
return $prepared->execute([$poll_id]);
|
||||
}
|
||||
|
||||
function updateVote($poll_id, $vote_id, $choices) {
|
||||
$prepared = $this->prepare('UPDATE user_studs SET reponses = ? WHERE id_sondage = ? AND id_users = ?');
|
||||
|
||||
return $prepared->execute([$choices, $poll_id, $vote_id]);
|
||||
}
|
||||
|
||||
function insertComment($poll_id, $name, $comment) {
|
||||
$prepared = $this->prepare('INSERT INTO comments (id_sondage, usercomment, comment) VALUES (?,?,?)');
|
||||
|
||||
return $prepared->execute([$poll_id, $name, $comment]);
|
||||
}
|
||||
|
||||
function deleteComment($poll_id, $comment_id) {
|
||||
$prepared = $this->prepare('DELETE FROM comments WHERE id_sondage = ? AND id_comment = ?');
|
||||
|
||||
return $prepared->execute([$poll_id, $comment_id]);
|
||||
}
|
||||
|
||||
function deleteByPollId($poll_id) {
|
||||
function deletePollById($poll_id) {
|
||||
$prepared = $this->prepare('DELETE FROM sondage WHERE poll_id = ?');
|
||||
$prepared->execute([$poll_id]);
|
||||
return $prepared->execute([$poll_id]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Find old polls. Limit: 20.
|
||||
*
|
||||
* @return array Array of old polls
|
||||
*/
|
||||
public function findOldPolls() {
|
||||
$prepared = $this->prepare('SELECT * FROM sondage WHERE end_date < NOW() LIMIT 20');
|
||||
$prepared->execute([]);
|
||||
|
||||
return $prepared->fetchAll();
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -1,6 +1,7 @@
|
||||
<?php
|
||||
namespace Framadate\Services;
|
||||
|
||||
use Framadate\FramaDB;
|
||||
use Framadate\Utils;
|
||||
|
||||
/**
|
||||
@ -14,7 +15,7 @@ class AdminPollService {
|
||||
private $pollService;
|
||||
private $logService;
|
||||
|
||||
function __construct($connect, $pollService, $logService) {
|
||||
function __construct(FramaDB $connect, PollService $pollService, LogService $logService) {
|
||||
$this->connect = $connect;
|
||||
$this->pollService = $pollService;
|
||||
$this->logService = $logService;
|
||||
@ -64,7 +65,7 @@ class AdminPollService {
|
||||
* @return bool|null true is action succeeded
|
||||
*/
|
||||
function cleanVotes($poll_id) {
|
||||
$this->logService->log("CLEAN_VOTES", "id:$poll_id");
|
||||
$this->logService->log('CLEAN_VOTES', 'id:' . $poll_id);
|
||||
return $this->connect->deleteVotesByPollId($poll_id);
|
||||
}
|
||||
|
||||
@ -76,13 +77,13 @@ class AdminPollService {
|
||||
*/
|
||||
function deleteEntirePoll($poll_id) {
|
||||
$poll = $this->connect->findPollById($poll_id);
|
||||
$this->logService->log("DELETE_POLL", "id:$poll->poll_id, format:$poll->format, admin:$poll->admin_name, mail:$poll->admin_mail");
|
||||
$this->logService->log('DELETE_POLL', "id:$poll->poll_id, format:$poll->format, admin:$poll->admin_name, mail:$poll->admin_mail");
|
||||
|
||||
// Delete the entire poll
|
||||
$this->connect->deleteVotesByPollId($poll_id);
|
||||
$this->connect->deleteCommentsByPollId($poll_id);
|
||||
$this->connect->deleteSlotsByPollId($poll_id);
|
||||
$this->connect->deleteByPollId($poll_id);
|
||||
$this->connect->deletePollById($poll_id);
|
||||
|
||||
return true;
|
||||
}
|
||||
@ -95,7 +96,7 @@ class AdminPollService {
|
||||
* @return bool true if action succeeded
|
||||
*/
|
||||
public function deleteSlot($poll_id, $slot) {
|
||||
$this->logService->log("DELETE_SLOT", "id:$poll_id, slot:" . json_encode($slot));
|
||||
$this->logService->log('DELETE_SLOT', 'id:' . $poll_id . ', slot:' . json_encode($slot));
|
||||
$ex = explode('@', $slot);
|
||||
$datetime = $ex[0];
|
||||
$moment = $ex[1];
|
||||
|
@ -133,6 +133,7 @@ class PollService {
|
||||
// Insert poll + slots
|
||||
$this->connect->beginTransaction();
|
||||
|
||||
// TODO Extract this to FramaDB (or repository layer)
|
||||
$sql = 'INSERT INTO sondage
|
||||
(poll_id, admin_poll_id, title, comment, admin_name, admin_mail, end_date, format, editable, receiveNewVotes)
|
||||
VALUES (?,?,?,?,?,?,FROM_UNIXTIME(?),?,?,?)';
|
||||
@ -166,7 +167,7 @@ class PollService {
|
||||
|
||||
$this->connect->commit();
|
||||
|
||||
$this->logService->log('CREATE_POLL', ' id:' . $poll_id . ', format:' . $form->format . ', admin:' . $form->admin_name . ', mail:' . $form->admin_mail);
|
||||
$this->logService->log('CREATE_POLL', 'id:' . $poll_id . 'title: ' . $form->title . ', format:' . $form->format . ', admin:' . $form->admin_name . ', mail:' . $form->admin_mail);
|
||||
|
||||
|
||||
return [$poll_id, $admin_poll_id];
|
||||
|
@ -3,16 +3,18 @@ namespace Framadate\Services;
|
||||
use Framadate\FramaDB;
|
||||
|
||||
/**
|
||||
* This service helps to purge old poll.
|
||||
* This service helps to purge data.
|
||||
*
|
||||
* @package Framadate\Services
|
||||
*/
|
||||
class PurgeService {
|
||||
|
||||
private $connect;
|
||||
private $logService;
|
||||
|
||||
function __construct(FramaDB $connect) {
|
||||
function __construct(FramaDB $connect, LogService $logService) {
|
||||
$this->connect = $connect;
|
||||
$this->logService = $logService;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -21,7 +23,21 @@ class PurgeService {
|
||||
* @return bool true is action succeeded
|
||||
*/
|
||||
function purgeOldPolls() {
|
||||
// TODO Implements
|
||||
$oldPolls = $this->connect->findOldPolls();
|
||||
$count = count($oldPolls);
|
||||
|
||||
if ($count > 0) {
|
||||
$this->logService->log('EXPIRATION', 'Going to purge ' . $count . ' poll(s)...');
|
||||
|
||||
foreach ($oldPolls as $poll) {
|
||||
if ($this->purgePollById($poll->poll_id)) {
|
||||
$this->logService->log('EXPIRATION_SUCCESS', 'id: ' . $poll->poll_id . ', title:' . $poll->title . ', format: '.$poll->format . ', admin: ' . $poll->admin_name);
|
||||
} else {
|
||||
$this->logService->log('EXPIRATION_FAILED', 'id: ' . $poll->poll_id . ', title:' . $poll->title . ', format: '.$poll->format . ', admin: ' . $poll->admin_name);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
@ -32,8 +48,13 @@ class PurgeService {
|
||||
* @return bool true is action succeeded
|
||||
*/
|
||||
function purgePollById($poll_id) {
|
||||
// TODO Implements
|
||||
return false;
|
||||
$done = false;
|
||||
$done |= $this->connect->deleteCommentsByPollId($poll_id);
|
||||
$done |= $this->connect->deleteVotesByPollId($poll_id);
|
||||
$done |= $this->connect->deleteSlotsByPollId($poll_id);
|
||||
$done |= $this->connect->deletePollById($poll_id);
|
||||
|
||||
return $done;
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -19,6 +19,7 @@
|
||||
use Framadate\Services\LogService;
|
||||
use Framadate\Services\PollService;
|
||||
use Framadate\Services\MailService;
|
||||
use Framadate\Services\PurgeService;
|
||||
use Framadate\Utils;
|
||||
use Framadate\Choice;
|
||||
|
||||
@ -29,6 +30,7 @@ include_once __DIR__ . '/app/inc/init.php';
|
||||
$logService = new LogService(LOG_FILE);
|
||||
$pollService = new PollService($connect, $logService);
|
||||
$mailService = new MailService($config['use_smtp']);
|
||||
$purgeService = new PurgeService($connect, $logService);
|
||||
|
||||
if (file_exists('bandeaux_local.php')) {
|
||||
include_once('bandeaux_local.php');
|
||||
@ -95,8 +97,7 @@ if (empty($_SESSION['form']->title) || empty($_SESSION['form']->admin_name) || (
|
||||
unset($_SESSION['form']);
|
||||
|
||||
// Delete old polls
|
||||
// TODO Create a PurgeService
|
||||
Utils::cleaningOldPolls($connect, 'admin/logs_studs.txt');
|
||||
$purgeService->purgeOldPolls();
|
||||
|
||||
// Redirect to poll administration
|
||||
header('Location:' . Utils::getUrlSondage($admin_poll_id, true));
|
||||
|
@ -19,6 +19,7 @@
|
||||
use Framadate\Services\LogService;
|
||||
use Framadate\Services\PollService;
|
||||
use Framadate\Services\MailService;
|
||||
use Framadate\Services\PurgeService;
|
||||
use Framadate\Utils;
|
||||
use Framadate\Choice;
|
||||
|
||||
@ -29,6 +30,7 @@ include_once __DIR__ . '/app/inc/init.php';
|
||||
$logService = new LogService(LOG_FILE);
|
||||
$pollService = new PollService($connect, $logService);
|
||||
$mailService = new MailService($config['use_smtp']);
|
||||
$purgeService = new PurgeService($connect, $logService);
|
||||
|
||||
if (is_readable('bandeaux_local.php')) {
|
||||
include_once('bandeaux_local.php');
|
||||
@ -103,8 +105,7 @@ if (!isset($_SESSION['form']->title) || !isset($_SESSION['form']->admin_name) ||
|
||||
unset($_SESSION['form']);
|
||||
|
||||
// Delete old polls
|
||||
// TODO Create a PurgeService
|
||||
Utils::cleaningOldPolls($connect, 'admin/logs_studs.txt');
|
||||
$purgeService->purgeOldPolls();
|
||||
|
||||
// Redirect to poll administration
|
||||
header('Location:' . Utils::getUrlSondage($admin_poll_id, true));
|
||||
|
Loading…
Reference in New Issue
Block a user