2014-12-20 23:59:44 +01:00
|
|
|
<?php
|
|
|
|
namespace Framadate\Services;
|
|
|
|
|
2015-11-03 21:15:47 +01:00
|
|
|
use Framadate\Exception\MomentAlreadyExistsException;
|
2014-12-29 21:54:07 +01:00
|
|
|
use Framadate\FramaDB;
|
2015-04-02 23:32:24 +02:00
|
|
|
use Framadate\Repositories\RepositoryFactory;
|
2014-12-23 00:30:05 +01:00
|
|
|
|
2014-12-20 23:59:44 +01:00
|
|
|
/**
|
|
|
|
* Class AdminPollService
|
2014-12-23 00:30:05 +01:00
|
|
|
*
|
2014-12-20 23:59:44 +01:00
|
|
|
* @package Framadate\Services
|
|
|
|
*/
|
|
|
|
class AdminPollService {
|
|
|
|
|
|
|
|
private $connect;
|
2014-12-21 23:48:22 +01:00
|
|
|
private $pollService;
|
2014-12-24 09:40:41 +01:00
|
|
|
private $logService;
|
2015-04-03 00:11:36 +02:00
|
|
|
|
2015-04-02 23:32:24 +02:00
|
|
|
private $pollRepository;
|
|
|
|
private $slotRepository;
|
2015-04-03 00:11:36 +02:00
|
|
|
private $voteRepository;
|
2015-04-02 23:32:24 +02:00
|
|
|
private $commentRepository;
|
2014-12-20 23:59:44 +01:00
|
|
|
|
2014-12-29 21:54:07 +01:00
|
|
|
function __construct(FramaDB $connect, PollService $pollService, LogService $logService) {
|
2014-12-20 23:59:44 +01:00
|
|
|
$this->connect = $connect;
|
2014-12-21 23:48:22 +01:00
|
|
|
$this->pollService = $pollService;
|
2014-12-24 09:40:41 +01:00
|
|
|
$this->logService = $logService;
|
2015-04-02 23:32:24 +02:00
|
|
|
$this->pollRepository = RepositoryFactory::pollRepository();
|
|
|
|
$this->slotRepository = RepositoryFactory::slotRepository();
|
2015-04-03 00:11:36 +02:00
|
|
|
$this->voteRepository = RepositoryFactory::voteRepository();
|
2015-04-02 23:32:24 +02:00
|
|
|
$this->commentRepository = RepositoryFactory::commentRepository();
|
2014-12-20 23:59:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
function updatePoll($poll) {
|
2015-01-08 00:27:40 +01:00
|
|
|
global $config;
|
2015-02-28 19:18:59 +01:00
|
|
|
if ($poll->end_date > $poll->creation_date && $poll->end_date <= strtotime($poll->creation_date) + (86400 * $config['default_poll_duration'])) {
|
2015-04-02 23:32:24 +02:00
|
|
|
return $this->pollRepository->update($poll);
|
2015-01-08 00:27:40 +01:00
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
2014-12-20 23:59:44 +01:00
|
|
|
}
|
|
|
|
|
2014-12-21 00:25:00 +01:00
|
|
|
/**
|
|
|
|
* Delete a comment from a poll.
|
|
|
|
*
|
|
|
|
* @param $poll_id int The ID of the poll
|
|
|
|
* @param $comment_id int The ID of the comment
|
|
|
|
* @return mixed true is action succeeded
|
|
|
|
*/
|
2014-12-20 23:59:44 +01:00
|
|
|
function deleteComment($poll_id, $comment_id) {
|
2015-04-03 00:11:36 +02:00
|
|
|
return $this->commentRepository->deleteById($poll_id, $comment_id);
|
2014-12-20 23:59:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove all comments of a poll.
|
|
|
|
*
|
|
|
|
* @param $poll_id int The ID a the poll
|
|
|
|
* @return bool|null true is action succeeded
|
|
|
|
*/
|
|
|
|
function cleanComments($poll_id) {
|
2014-12-24 22:42:50 +01:00
|
|
|
$this->logService->log("CLEAN_COMMENTS", "id:$poll_id");
|
2015-04-02 23:32:24 +02:00
|
|
|
return $this->commentRepository->deleteByPollId($poll_id);
|
2014-12-21 00:25:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Delete a vote from a poll.
|
|
|
|
*
|
|
|
|
* @param $poll_id int The ID of the poll
|
|
|
|
* @param $vote_id int The ID of the vote
|
|
|
|
* @return mixed true is action succeeded
|
|
|
|
*/
|
|
|
|
function deleteVote($poll_id, $vote_id) {
|
2015-04-03 00:11:36 +02:00
|
|
|
return $this->voteRepository->deleteById($poll_id, $vote_id);
|
2014-12-20 23:59:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove all votes of a poll.
|
|
|
|
*
|
2014-12-21 00:25:00 +01:00
|
|
|
* @param $poll_id int The ID of the poll
|
2014-12-20 23:59:44 +01:00
|
|
|
* @return bool|null true is action succeeded
|
|
|
|
*/
|
|
|
|
function cleanVotes($poll_id) {
|
2014-12-29 21:54:07 +01:00
|
|
|
$this->logService->log('CLEAN_VOTES', 'id:' . $poll_id);
|
2015-04-03 00:11:36 +02:00
|
|
|
return $this->voteRepository->deleteByPollId($poll_id);
|
2014-12-23 09:48:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Delete the entire given poll.
|
|
|
|
*
|
|
|
|
* @param $poll_id int The ID of the poll
|
|
|
|
* @return bool true is action succeeded
|
|
|
|
*/
|
|
|
|
function deleteEntirePoll($poll_id) {
|
2015-04-02 23:32:24 +02:00
|
|
|
$poll = $this->pollRepository->findById($poll_id);
|
2014-12-30 01:41:25 +01:00
|
|
|
$this->logService->log('DELETE_POLL', "id:$poll->id, format:$poll->format, admin:$poll->admin_name, mail:$poll->admin_mail");
|
2014-12-25 01:11:06 +01:00
|
|
|
|
|
|
|
// Delete the entire poll
|
2015-04-03 00:11:36 +02:00
|
|
|
$this->voteRepository->deleteByPollId($poll_id);
|
2015-04-02 23:32:24 +02:00
|
|
|
$this->commentRepository->deleteByPollId($poll_id);
|
2015-04-03 00:11:36 +02:00
|
|
|
$this->slotRepository->deleteByPollId($poll_id);
|
|
|
|
$this->pollRepository->deleteById($poll_id);
|
2014-12-23 09:48:58 +01:00
|
|
|
|
|
|
|
return true;
|
2014-12-20 23:59:44 +01:00
|
|
|
}
|
|
|
|
|
2014-12-21 23:48:22 +01:00
|
|
|
/**
|
|
|
|
* Delete a slot from a poll.
|
|
|
|
*
|
2015-05-30 23:36:04 +02:00
|
|
|
* @param object $poll The ID of the poll
|
|
|
|
* @param object $slot The slot informations (datetime + moment)
|
2014-12-23 09:48:58 +01:00
|
|
|
* @return bool true if action succeeded
|
2014-12-21 23:48:22 +01:00
|
|
|
*/
|
2015-05-30 23:36:04 +02:00
|
|
|
public function deleteDateSlot($poll, $slot) {
|
|
|
|
$this->logService->log('DELETE_SLOT', 'id:' . $poll->id . ', slot:' . json_encode($slot));
|
2014-12-30 17:03:43 +01:00
|
|
|
|
|
|
|
$datetime = $slot->title;
|
|
|
|
$moment = $slot->moment;
|
2014-12-21 23:48:22 +01:00
|
|
|
|
2015-05-30 23:36:04 +02:00
|
|
|
$slots = $this->pollService->allSlotsByPoll($poll);
|
2014-12-21 23:48:22 +01:00
|
|
|
|
2015-10-05 23:50:00 +02:00
|
|
|
// We can't delete the last slot
|
2015-11-04 12:37:53 +01:00
|
|
|
if ($poll->format == 'D' && count($slots) === 1 && strpos($slots[0]->moments, ',') === false) {
|
2015-10-05 23:50:00 +02:00
|
|
|
return false;
|
|
|
|
} elseif ($poll->format == 'A' && count($slots) === 1) {
|
2015-05-29 18:53:09 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-12-21 23:48:22 +01:00
|
|
|
$index = 0;
|
|
|
|
$indexToDelete = -1;
|
|
|
|
$newMoments = [];
|
|
|
|
|
|
|
|
// Search the index of the slot to delete
|
|
|
|
foreach ($slots as $aSlot) {
|
2014-12-30 01:41:25 +01:00
|
|
|
$moments = explode(',', $aSlot->moments);
|
2014-12-21 23:48:22 +01:00
|
|
|
|
|
|
|
foreach ($moments as $rowMoment) {
|
2014-12-30 01:41:25 +01:00
|
|
|
if ($datetime == $aSlot->title) {
|
2014-12-21 23:48:22 +01:00
|
|
|
if ($moment == $rowMoment) {
|
|
|
|
$indexToDelete = $index;
|
|
|
|
} else {
|
|
|
|
$newMoments[] = $rowMoment;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$index++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove votes
|
|
|
|
$this->connect->beginTransaction();
|
2015-05-30 23:36:04 +02:00
|
|
|
$this->voteRepository->deleteByIndex($poll->id, $indexToDelete);
|
2014-12-21 23:48:22 +01:00
|
|
|
if (count($newMoments) > 0) {
|
2015-05-30 23:36:04 +02:00
|
|
|
$this->slotRepository->update($poll->id, $datetime, implode(',', $newMoments));
|
2014-12-21 23:48:22 +01:00
|
|
|
} else {
|
2015-05-30 23:36:04 +02:00
|
|
|
$this->slotRepository->deleteByDateTime($poll->id, $datetime);
|
2014-12-21 23:48:22 +01:00
|
|
|
}
|
|
|
|
$this->connect->commit();
|
2014-12-23 01:01:09 +01:00
|
|
|
|
|
|
|
return true;
|
2014-12-21 23:48:22 +01:00
|
|
|
}
|
|
|
|
|
2015-05-30 23:36:04 +02:00
|
|
|
public function deleteClassicSlot($poll, $slot_title) {
|
|
|
|
$this->logService->log('DELETE_SLOT', 'id:' . $poll->id . ', slot:' . $slot_title);
|
2014-12-30 17:03:43 +01:00
|
|
|
|
2015-05-30 23:36:04 +02:00
|
|
|
$slots = $this->pollService->allSlotsByPoll($poll);
|
2014-12-30 17:03:43 +01:00
|
|
|
|
2015-05-29 18:53:09 +02:00
|
|
|
if (count($slots) === 1) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-12-30 17:03:43 +01:00
|
|
|
$index = 0;
|
|
|
|
$indexToDelete = -1;
|
|
|
|
|
|
|
|
// Search the index of the slot to delete
|
|
|
|
foreach ($slots as $aSlot) {
|
|
|
|
if ($slot_title == $aSlot->title) {
|
|
|
|
$indexToDelete = $index;
|
|
|
|
}
|
|
|
|
$index++;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove votes
|
|
|
|
$this->connect->beginTransaction();
|
2015-05-30 23:36:04 +02:00
|
|
|
$this->voteRepository->deleteByIndex($poll->id, $indexToDelete);
|
|
|
|
$this->slotRepository->deleteByDateTime($poll->id, $slot_title);
|
2014-12-30 17:03:43 +01:00
|
|
|
$this->connect->commit();
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-12-23 00:30:05 +01:00
|
|
|
/**
|
2015-05-30 23:36:04 +02:00
|
|
|
* Add a new slot to a date poll. And insert default values for user's votes.
|
2014-12-23 00:30:05 +01:00
|
|
|
* <ul>
|
|
|
|
* <li>Create a new slot if no one exists for the given date</li>
|
|
|
|
* <li>Create a new moment if a slot already exists for the given date</li>
|
|
|
|
* </ul>
|
|
|
|
*
|
|
|
|
* @param $poll_id int The ID of the poll
|
2014-12-30 01:41:25 +01:00
|
|
|
* @param $datetime int The datetime
|
2014-12-23 00:30:05 +01:00
|
|
|
* @param $new_moment string The moment's name
|
2015-11-03 21:15:47 +01:00
|
|
|
* @throws MomentAlreadyExistsException When the moment to add already exists in database
|
2014-12-23 00:30:05 +01:00
|
|
|
*/
|
2015-05-30 23:36:04 +02:00
|
|
|
public function addDateSlot($poll_id, $datetime, $new_moment) {
|
2015-11-30 20:38:53 +01:00
|
|
|
$this->logService->log('ADD_COLUMN', 'id:' . $poll_id . ', datetime:' . $datetime . ', moment:' . $new_moment);
|
2015-10-05 23:54:50 +02:00
|
|
|
|
2015-04-02 23:32:24 +02:00
|
|
|
$slots = $this->slotRepository->listByPollId($poll_id);
|
2015-10-12 22:42:07 +02:00
|
|
|
$result = $this->findInsertPosition($slots, $datetime);
|
2014-12-22 14:18:33 +01:00
|
|
|
|
2014-12-23 00:30:05 +01:00
|
|
|
// Begin transaction
|
|
|
|
$this->connect->beginTransaction();
|
|
|
|
|
2015-11-03 21:15:47 +01:00
|
|
|
if ($result->slot != null) {
|
2014-12-23 00:30:05 +01:00
|
|
|
$slot = $result->slot;
|
2014-12-30 01:41:25 +01:00
|
|
|
$moments = explode(',', $slot->moments);
|
2014-12-23 00:30:05 +01:00
|
|
|
|
|
|
|
// Check if moment already exists (maybe not necessary)
|
|
|
|
if (in_array($new_moment, $moments)) {
|
2015-11-03 21:15:47 +01:00
|
|
|
throw new MomentAlreadyExistsException();
|
2014-12-22 14:18:33 +01:00
|
|
|
}
|
2014-12-23 00:30:05 +01:00
|
|
|
|
|
|
|
// Update found slot
|
|
|
|
$moments[] = $new_moment;
|
2015-04-03 00:11:36 +02:00
|
|
|
$this->slotRepository->update($poll_id, $datetime, implode(',', $moments));
|
2014-12-22 14:18:33 +01:00
|
|
|
|
|
|
|
} else {
|
2015-04-03 00:11:36 +02:00
|
|
|
$this->slotRepository->insert($poll_id, $datetime, $new_moment);
|
2014-12-22 14:18:33 +01:00
|
|
|
}
|
|
|
|
|
2015-04-03 00:11:36 +02:00
|
|
|
$this->voteRepository->insertDefault($poll_id, $result->insert);
|
2014-12-23 00:30:05 +01:00
|
|
|
|
|
|
|
// Commit transaction
|
|
|
|
$this->connect->commit();
|
|
|
|
|
2014-12-22 14:18:33 +01:00
|
|
|
}
|
|
|
|
|
2015-05-30 23:36:04 +02:00
|
|
|
/**
|
|
|
|
* Add a new slot to a classic poll. And insert default values for user's votes.
|
|
|
|
* <ul>
|
|
|
|
* <li>Create a new slot if no one exists for the given title</li>
|
|
|
|
* </ul>
|
|
|
|
*
|
|
|
|
* @param $poll_id int The ID of the poll
|
|
|
|
* @param $title int The title
|
2015-11-03 21:15:47 +01:00
|
|
|
* @throws MomentAlreadyExistsException When the moment to add already exists in database
|
2015-05-30 23:36:04 +02:00
|
|
|
*/
|
|
|
|
public function addClassicSlot($poll_id, $title) {
|
2015-11-30 20:38:53 +01:00
|
|
|
$this->logService->log('ADD_COLUMN', 'id:' . $poll_id . ', title:' . $title);
|
2015-10-05 23:54:50 +02:00
|
|
|
|
2015-05-30 23:36:04 +02:00
|
|
|
$slots = $this->slotRepository->listByPollId($poll_id);
|
|
|
|
|
|
|
|
// Check if slot already exists
|
|
|
|
$titles = array_map(function ($slot) {
|
|
|
|
return $slot->title;
|
|
|
|
}, $slots);
|
|
|
|
if (in_array($title, $titles)) {
|
|
|
|
// The moment already exists
|
2015-11-03 21:15:47 +01:00
|
|
|
throw new MomentAlreadyExistsException();
|
2015-05-30 23:36:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Begin transaction
|
|
|
|
$this->connect->beginTransaction();
|
|
|
|
|
|
|
|
// New slot
|
|
|
|
$this->slotRepository->insert($poll_id, $title, null);
|
|
|
|
// Set default votes
|
|
|
|
$this->voteRepository->insertDefault($poll_id, count($slots));
|
|
|
|
|
|
|
|
// Commit transaction
|
|
|
|
$this->connect->commit();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2014-12-23 00:30:05 +01:00
|
|
|
/**
|
|
|
|
* This method find where to insert a datatime+moment into a list of slots.<br/>
|
|
|
|
* Return the {insert:X}, where X is the index of the moment into the whole poll (ex: X=0 => Insert to the first column).
|
|
|
|
* Return {slot:Y}, where Y is not null if there is a slot existing for the given datetime.
|
|
|
|
*
|
|
|
|
* @param $slots array All the slots of the poll
|
|
|
|
* @param $datetime int The datetime of the new slot
|
2015-11-03 21:17:00 +01:00
|
|
|
* @return \stdClass An object like this one: {insert:X, slot:Y} where Y can be null.
|
2014-12-23 00:30:05 +01:00
|
|
|
*/
|
2015-10-12 22:42:07 +02:00
|
|
|
private function findInsertPosition($slots, $datetime) {
|
2014-12-23 00:30:05 +01:00
|
|
|
$result = new \stdClass();
|
|
|
|
$result->slot = null;
|
2015-11-03 21:17:00 +01:00
|
|
|
$result->insert = 0;
|
2014-12-23 00:30:05 +01:00
|
|
|
|
2016-02-22 23:51:33 +01:00
|
|
|
// Sort slots before searching where to insert
|
|
|
|
uasort($slots, function ($a, $b) {
|
|
|
|
return $a->title > $b->title;
|
|
|
|
});
|
|
|
|
|
|
|
|
// Search where to insert new column
|
|
|
|
foreach ($slots as $k=>$slot) {
|
2014-12-30 01:41:25 +01:00
|
|
|
$rowDatetime = $slot->title;
|
|
|
|
$moments = explode(',', $slot->moments);
|
2014-12-23 00:30:05 +01:00
|
|
|
|
|
|
|
if ($datetime == $rowDatetime) {
|
|
|
|
// Here we have to insert at the end of a slot
|
2015-11-03 21:17:00 +01:00
|
|
|
$result->insert += count($moments);
|
2015-10-12 22:42:07 +02:00
|
|
|
$result->slot = $slot;
|
2014-12-23 00:30:05 +01:00
|
|
|
break;
|
|
|
|
} elseif ($datetime < $rowDatetime) {
|
2015-11-03 21:17:00 +01:00
|
|
|
// We have to insert before this slot
|
2014-12-23 00:30:05 +01:00
|
|
|
break;
|
|
|
|
} else {
|
2015-11-03 21:17:00 +01:00
|
|
|
$result->insert += count($moments);
|
2014-12-23 00:30:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
2014-12-20 23:59:44 +01:00
|
|
|
}
|
|
|
|
|