Commit 44928b05 authored by Olivier PEREZ's avatar Olivier PEREZ
Browse files

Dont sort classic polls

parent d365f18c
......@@ -367,9 +367,9 @@ if (!empty($_GET['delete_column'])) {
$slot->title = $ex[0];
$slot->moment = $ex[1];
$result = $adminPollService->deleteDateSlot($poll_id, $slot);
$result = $adminPollService->deleteDateSlot($poll, $slot);
} else {
$result = $adminPollService->deleteClassicSlot($poll_id, $column);
$result = $adminPollService->deleteClassicSlot($poll, $column);
}
if ($result) {
......@@ -397,10 +397,10 @@ if (isset($_POST['confirm_add_slot'])) {
$newmoment = strip_tags($_POST['newmoment']);
$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 {
$newslot = strip_tags($_POST['choice']);
$result = $adminPollService->addSlot($poll_id, $newslot, null);
$result = $adminPollService->addClassicSlot($poll_id, $newslot);
}
if ($result) {
......@@ -411,7 +411,7 @@ if (isset($_POST['confirm_add_slot'])) {
}
// Retrieve data
$slots = $pollService->allSlotsByPollId($poll_id);
$slots = $pollService->allSlotsByPoll($poll);
$votes = $pollService->allVotesByPollId($poll_id);
$comments = $pollService->allCommentsByPollId($poll_id);
......
......@@ -61,7 +61,7 @@ class SlotRepository extends AbstractRepository {
}
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));
return $prepared->fetchAll();
......
......@@ -105,17 +105,17 @@ class AdminPollService {
/**
* Delete a slot from a poll.
*
* @param $poll_id int The ID of the poll
* @param $slot object The slot informations (datetime + moment)
* @param object $poll The ID of the poll
* @param object $slot The slot informations (datetime + moment)
* @return bool true if action succeeded
*/
public function deleteDateSlot($poll_id, $slot) {
$this->logService->log('DELETE_SLOT', 'id:' . $poll_id . ', slot:' . json_encode($slot));
public function deleteDateSlot($poll, $slot) {
$this->logService->log('DELETE_SLOT', 'id:' . $poll->id . ', slot:' . json_encode($slot));
$datetime = $slot->title;
$moment = $slot->moment;
$slots = $this->pollService->allSlotsByPollId($poll_id);
$slots = $this->pollService->allSlotsByPoll($poll);
if (count($slots) === 1) {
return false;
......@@ -143,21 +143,21 @@ class AdminPollService {
// Remove votes
$this->connect->beginTransaction();
$this->voteRepository->deleteByIndex($poll_id, $indexToDelete);
$this->voteRepository->deleteByIndex($poll->id, $indexToDelete);
if (count($newMoments) > 0) {
$this->slotRepository->update($poll_id, $datetime, implode(',', $newMoments));
$this->slotRepository->update($poll->id, $datetime, implode(',', $newMoments));
} else {
$this->slotRepository->deleteByDateTime($poll_id, $datetime);
$this->slotRepository->deleteByDateTime($poll->id, $datetime);
}
$this->connect->commit();
return true;
}
public function deleteClassicSlot($poll_id, $slot_title) {
$this->logService->log('DELETE_SLOT', 'id:' . $poll_id . ', slot:' . $slot_title);
public function deleteClassicSlot($poll, $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) {
return false;
......@@ -176,15 +176,15 @@ class AdminPollService {
// Remove votes
$this->connect->beginTransaction();
$this->voteRepository->deleteByIndex($poll_id, $indexToDelete);
$this->slotRepository->deleteByDateTime($poll_id, $slot_title);
$this->voteRepository->deleteByIndex($poll->id, $indexToDelete);
$this->slotRepository->deleteByDateTime($poll->id, $slot_title);
$this->connect->commit();
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>
* <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>
......@@ -195,7 +195,7 @@ class AdminPollService {
* @param $new_moment string The moment's name
* @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);
$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/>
* Return the {insert:X}, where X is the index of the moment into the whole poll (ex: X=0 => Insert to the first column).
......
......@@ -64,8 +64,16 @@ class PollService {
return $this->voteRepository->allUserVotesByPollId($poll_id);
}
function allSlotsByPollId($poll_id) {
return $this->slotRepository->listByPollId($poll_id);
function allSlotsByPoll($poll) {
$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) {
......
......@@ -51,7 +51,7 @@ if (!$poll) {
}
$slots = $pollService->allSlotsByPollId($poll_id);
$slots = $pollService->allSlotsByPoll($poll);
$votes = $pollService->allVotesByPollId($poll_id);
// CSV header
......
......@@ -197,7 +197,7 @@ if (isset($_POST['add_comment'])) {
}
// Retrieve data
$slots = $pollService->allSlotsByPollId($poll_id);
$slots = $pollService->allSlotsByPoll($poll);
$votes = $pollService->allVotesByPollId($poll_id);
$comments = $pollService->allCommentsByPollId($poll_id);
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment