2014-12-20 23:59:44 +01:00
|
|
|
<?php
|
|
|
|
namespace Framadate\Services;
|
|
|
|
|
2014-12-29 21:54:07 +01:00
|
|
|
use Framadate\FramaDB;
|
2014-12-23 00:30:05 +01:00
|
|
|
use Framadate\Utils;
|
|
|
|
|
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;
|
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;
|
2014-12-20 23:59:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
function updatePoll($poll) {
|
|
|
|
return $this->connect->updatePoll($poll);
|
|
|
|
}
|
|
|
|
|
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) {
|
|
|
|
return $this->connect->deleteComment($poll_id, $comment_id);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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");
|
2014-12-23 09:48:58 +01:00
|
|
|
return $this->connect->deleteCommentsByPollId($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) {
|
|
|
|
return $this->connect->deleteVote($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);
|
2014-12-23 09:48:58 +01:00
|
|
|
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) {
|
2014-12-24 09:40:41 +01:00
|
|
|
$poll = $this->connect->findPollById($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
|
|
|
|
$this->connect->deleteVotesByPollId($poll_id);
|
2014-12-23 09:48:58 +01:00
|
|
|
$this->connect->deleteCommentsByPollId($poll_id);
|
|
|
|
$this->connect->deleteSlotsByPollId($poll_id);
|
2014-12-29 21:54:07 +01:00
|
|
|
$this->connect->deletePollById($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.
|
|
|
|
*
|
|
|
|
* @param $poll_id int The ID of the poll
|
2014-12-30 17:03:43 +01:00
|
|
|
* @param $slot object 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
|
|
|
*/
|
2014-12-30 17:03:43 +01:00
|
|
|
public function deleteDateSlot($poll_id, $slot) {
|
2014-12-29 21:54:07 +01:00
|
|
|
$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
|
|
|
|
|
|
|
$slots = $this->pollService->allSlotsByPollId($poll_id);
|
|
|
|
|
|
|
|
$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();
|
|
|
|
$this->connect->deleteVotesByIndex($poll_id, $indexToDelete);
|
|
|
|
if (count($newMoments) > 0) {
|
2014-12-30 01:41:25 +01:00
|
|
|
$this->connect->updateSlot($poll_id, $datetime, implode(',', $newMoments));
|
2014-12-21 23:48:22 +01:00
|
|
|
} else {
|
|
|
|
$this->connect->deleteSlot($poll_id, $datetime);
|
|
|
|
}
|
|
|
|
$this->connect->commit();
|
2014-12-23 01:01:09 +01:00
|
|
|
|
|
|
|
return true;
|
2014-12-21 23:48:22 +01:00
|
|
|
}
|
|
|
|
|
2014-12-30 17:03:43 +01:00
|
|
|
public function deleteClassicSlot($poll_id, $slot_title) {
|
|
|
|
$this->logService->log('DELETE_SLOT', 'id:' . $poll_id . ', slot:' . $slot_title);
|
|
|
|
|
|
|
|
$slots = $this->pollService->allSlotsByPollId($poll_id);
|
|
|
|
|
|
|
|
$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();
|
|
|
|
$this->connect->deleteVotesByIndex($poll_id, $indexToDelete);
|
|
|
|
$this->connect->deleteSlot($poll_id, $slot_title);
|
|
|
|
$this->connect->commit();
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-12-23 00:30:05 +01:00
|
|
|
/**
|
|
|
|
* Add a new slot to the poll. And insert default values for user's votes.
|
|
|
|
* <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
|
|
|
|
* @return bool true if added
|
|
|
|
*/
|
2014-12-30 01:41:25 +01:00
|
|
|
public function addSlot($poll_id, $datetime, $new_moment) {
|
2014-12-23 00:30:05 +01:00
|
|
|
$slots = $this->connect->allSlotsByPollId($poll_id);
|
|
|
|
$result = $this->findInsertPosition($slots, $datetime, $new_moment);
|
2014-12-22 14:18:33 +01:00
|
|
|
|
2014-12-23 00:30:05 +01:00
|
|
|
// Begin transaction
|
|
|
|
$this->connect->beginTransaction();
|
|
|
|
|
|
|
|
if ($result == null) {
|
|
|
|
// The moment already exists
|
|
|
|
return false;
|
|
|
|
} elseif ($result->slot != null) {
|
|
|
|
$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)) {
|
|
|
|
return false;
|
2014-12-22 14:18:33 +01:00
|
|
|
}
|
2014-12-23 00:30:05 +01:00
|
|
|
|
|
|
|
// Update found slot
|
|
|
|
$moments[] = $new_moment;
|
|
|
|
sort($moments);
|
2014-12-30 01:41:25 +01:00
|
|
|
$this->connect->updateSlot($poll_id, $datetime, implode(',', $moments));
|
2014-12-22 14:18:33 +01:00
|
|
|
|
|
|
|
} else {
|
2014-12-30 01:41:25 +01:00
|
|
|
$this->connect->insertSlot($poll_id, $datetime, $new_moment);
|
2014-12-22 14:18:33 +01:00
|
|
|
}
|
|
|
|
|
2014-12-23 00:30:05 +01:00
|
|
|
$this->connect->insertDefaultVote($poll_id, $result->insert);
|
|
|
|
|
|
|
|
// Commit transaction
|
|
|
|
$this->connect->commit();
|
|
|
|
|
2014-12-22 14:18:33 +01:00
|
|
|
return true;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
* @param $moment string The moment's name
|
|
|
|
* @return null|\stdClass An object like this one: {insert:X, slot:Y} where Y can be null.
|
|
|
|
*/
|
|
|
|
private function findInsertPosition($slots, $datetime, $moment) {
|
|
|
|
$result = new \stdClass();
|
|
|
|
$result->slot = null;
|
|
|
|
$result->insert = -1;
|
|
|
|
|
|
|
|
$i = 0;
|
|
|
|
|
|
|
|
foreach ($slots as $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) {
|
|
|
|
$result->slot = $slot;
|
|
|
|
|
|
|
|
foreach ($moments as $rowMoment) {
|
|
|
|
$strcmp = strcmp($moment, $rowMoment);
|
|
|
|
if ($strcmp < 0) {
|
|
|
|
// Here we have to insert at First place or middle of the slot
|
|
|
|
break(2);
|
|
|
|
} elseif ($strcmp == 0) {
|
|
|
|
// Here we dont have to insert at all
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
$i++;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Here we have to insert at the end of a slot
|
|
|
|
$result->insert = $i;
|
|
|
|
break;
|
|
|
|
} elseif ($datetime < $rowDatetime) {
|
|
|
|
// Here we have to insert a new slot
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
$i += count($moments);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$result->insert = $i;
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
2014-12-20 23:59:44 +01:00
|
|
|
}
|
|
|
|
|