Dont sort classic polls

This commit is contained in:
Olivier PEREZ 2015-05-30 23:36:04 +02:00
parent d365f18c01
commit 44928b0572
6 changed files with 71 additions and 25 deletions

View File

@ -367,9 +367,9 @@ if (!empty($_GET['delete_column'])) {
$slot->title = $ex[0]; $slot->title = $ex[0];
$slot->moment = $ex[1]; $slot->moment = $ex[1];
$result = $adminPollService->deleteDateSlot($poll_id, $slot); $result = $adminPollService->deleteDateSlot($poll, $slot);
} else { } else {
$result = $adminPollService->deleteClassicSlot($poll_id, $column); $result = $adminPollService->deleteClassicSlot($poll, $column);
} }
if ($result) { if ($result) {
@ -397,10 +397,10 @@ if (isset($_POST['confirm_add_slot'])) {
$newmoment = strip_tags($_POST['newmoment']); $newmoment = strip_tags($_POST['newmoment']);
$ex = explode('/', $newdate); $ex = explode('/', $newdate);
$result = $adminPollService->addSlot($poll_id, mktime(0, 0, 0, $ex[1], $ex[0], $ex[2]), $newmoment); $result = $adminPollService->addDateSlot($poll_id, mktime(0, 0, 0, $ex[1], $ex[0], $ex[2]), $newmoment);
} else { } else {
$newslot = strip_tags($_POST['choice']); $newslot = strip_tags($_POST['choice']);
$result = $adminPollService->addSlot($poll_id, $newslot, null); $result = $adminPollService->addClassicSlot($poll_id, $newslot);
} }
if ($result) { if ($result) {
@ -411,7 +411,7 @@ if (isset($_POST['confirm_add_slot'])) {
} }
// Retrieve data // Retrieve data
$slots = $pollService->allSlotsByPollId($poll_id); $slots = $pollService->allSlotsByPoll($poll);
$votes = $pollService->allVotesByPollId($poll_id); $votes = $pollService->allVotesByPollId($poll_id);
$comments = $pollService->allCommentsByPollId($poll_id); $comments = $pollService->allCommentsByPollId($poll_id);

View File

@ -61,7 +61,7 @@ class SlotRepository extends AbstractRepository {
} }
function listByPollId($poll_id) { function listByPollId($poll_id) {
$prepared = $this->prepare('SELECT * FROM `' . Utils::table('slot') . '` WHERE poll_id = ? ORDER BY title'); $prepared = $this->prepare('SELECT * FROM `' . Utils::table('slot') . '` WHERE poll_id = ? ORDER BY id');
$prepared->execute(array($poll_id)); $prepared->execute(array($poll_id));
return $prepared->fetchAll(); return $prepared->fetchAll();

View File

@ -105,17 +105,17 @@ class AdminPollService {
/** /**
* Delete a slot from a poll. * Delete a slot from a poll.
* *
* @param $poll_id int The ID of the poll * @param object $poll The ID of the poll
* @param $slot object The slot informations (datetime + moment) * @param object $slot The slot informations (datetime + moment)
* @return bool true if action succeeded * @return bool true if action succeeded
*/ */
public function deleteDateSlot($poll_id, $slot) { public function deleteDateSlot($poll, $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));
$datetime = $slot->title; $datetime = $slot->title;
$moment = $slot->moment; $moment = $slot->moment;
$slots = $this->pollService->allSlotsByPollId($poll_id); $slots = $this->pollService->allSlotsByPoll($poll);
if (count($slots) === 1) { if (count($slots) === 1) {
return false; return false;
@ -143,21 +143,21 @@ class AdminPollService {
// Remove votes // Remove votes
$this->connect->beginTransaction(); $this->connect->beginTransaction();
$this->voteRepository->deleteByIndex($poll_id, $indexToDelete); $this->voteRepository->deleteByIndex($poll->id, $indexToDelete);
if (count($newMoments) > 0) { if (count($newMoments) > 0) {
$this->slotRepository->update($poll_id, $datetime, implode(',', $newMoments)); $this->slotRepository->update($poll->id, $datetime, implode(',', $newMoments));
} else { } else {
$this->slotRepository->deleteByDateTime($poll_id, $datetime); $this->slotRepository->deleteByDateTime($poll->id, $datetime);
} }
$this->connect->commit(); $this->connect->commit();
return true; return true;
} }
public function deleteClassicSlot($poll_id, $slot_title) { public function deleteClassicSlot($poll, $slot_title) {
$this->logService->log('DELETE_SLOT', 'id:' . $poll_id . ', slot:' . $slot_title); $this->logService->log('DELETE_SLOT', 'id:' . $poll->id . ', slot:' . $slot_title);
$slots = $this->pollService->allSlotsByPollId($poll_id); $slots = $this->pollService->allSlotsByPoll($poll);
if (count($slots) === 1) { if (count($slots) === 1) {
return false; return false;
@ -176,15 +176,15 @@ class AdminPollService {
// Remove votes // Remove votes
$this->connect->beginTransaction(); $this->connect->beginTransaction();
$this->voteRepository->deleteByIndex($poll_id, $indexToDelete); $this->voteRepository->deleteByIndex($poll->id, $indexToDelete);
$this->slotRepository->deleteByDateTime($poll_id, $slot_title); $this->slotRepository->deleteByDateTime($poll->id, $slot_title);
$this->connect->commit(); $this->connect->commit();
return true; return true;
} }
/** /**
* Add a new slot to the poll. And insert default values for user's votes. * Add a new slot to a date poll. And insert default values for user's votes.
* <ul> * <ul>
* <li>Create a new slot if no one exists for the given date</li> * <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> * <li>Create a new moment if a slot already exists for the given date</li>
@ -195,7 +195,7 @@ class AdminPollService {
* @param $new_moment string The moment's name * @param $new_moment string The moment's name
* @return bool true if added * @return bool true if added
*/ */
public function addSlot($poll_id, $datetime, $new_moment) { public function addDateSlot($poll_id, $datetime, $new_moment) {
$slots = $this->slotRepository->listByPollId($poll_id); $slots = $this->slotRepository->listByPollId($poll_id);
$result = $this->findInsertPosition($slots, $datetime, $new_moment); $result = $this->findInsertPosition($slots, $datetime, $new_moment);
@ -232,6 +232,44 @@ class AdminPollService {
} }
/**
* 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
* @return bool true if added
*/
public function addClassicSlot($poll_id, $title) {
$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
return false;
}
// 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();
return true;
}
/** /**
* This method find where to insert a datatime+moment into a list of slots.<br/> * 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 the {insert:X}, where X is the index of the moment into the whole poll (ex: X=0 => Insert to the first column).

View File

@ -64,8 +64,16 @@ class PollService {
return $this->voteRepository->allUserVotesByPollId($poll_id); return $this->voteRepository->allUserVotesByPollId($poll_id);
} }
function allSlotsByPollId($poll_id) { function allSlotsByPoll($poll) {
return $this->slotRepository->listByPollId($poll_id); $slots = $this->slotRepository->listByPollId($poll->id);
if ($poll->format == 'D') {
uasort($slots, function ($a, $b) {
return $a->title > $b->title;
});
return $slots;
} else {
return $slots;
}
} }
public function updateVote($poll_id, $vote_id, $name, $choices) { public function updateVote($poll_id, $vote_id, $name, $choices) {

View File

@ -51,7 +51,7 @@ if (!$poll) {
} }
$slots = $pollService->allSlotsByPollId($poll_id); $slots = $pollService->allSlotsByPoll($poll);
$votes = $pollService->allVotesByPollId($poll_id); $votes = $pollService->allVotesByPollId($poll_id);
// CSV header // CSV header

View File

@ -197,7 +197,7 @@ if (isset($_POST['add_comment'])) {
} }
// Retrieve data // Retrieve data
$slots = $pollService->allSlotsByPollId($poll_id); $slots = $pollService->allSlotsByPoll($poll);
$votes = $pollService->allVotesByPollId($poll_id); $votes = $pollService->allVotesByPollId($poll_id);
$comments = $pollService->allCommentsByPollId($poll_id); $comments = $pollService->allCommentsByPollId($poll_id);