From c79b32984dcf1e12e74cec9b57ddfca1c4821b3a Mon Sep 17 00:00:00 2001 From: m Date: Thu, 5 Apr 2018 17:34:43 +0200 Subject: [PATCH] bug/undefined-offset-on-best-choices --- adminstuds.php | 2 +- .../Framadate/Services/PollService.php | 57 ++++++++++++++----- studs.php | 2 +- 3 files changed, 45 insertions(+), 16 deletions(-) diff --git a/adminstuds.php b/adminstuds.php index 356f815..ab2cacd 100644 --- a/adminstuds.php +++ b/adminstuds.php @@ -435,7 +435,7 @@ $smarty->assign('deletion_date', strtotime($poll->end_date) + PURGE_DELAY * 8640 $smarty->assign('slots', $poll->format === 'D' ? $pollService->splitSlots($slots) : $slots); $smarty->assign('slots_hash', $pollService->hashSlots($slots)); $smarty->assign('votes', $pollService->splitVotes($votes)); -$smarty->assign('best_choices', $pollService->computeBestChoices($votes)); +$smarty->assign('best_choices', $pollService->computeBestChoices($votes, $poll)); $smarty->assign('comments', $comments); $smarty->assign('editingVoteId', $editingVoteId); $smarty->assign('message', $message); diff --git a/app/classes/Framadate/Services/PollService.php b/app/classes/Framadate/Services/PollService.php index 104c6f6..05e326c 100644 --- a/app/classes/Framadate/Services/PollService.php +++ b/app/classes/Framadate/Services/PollService.php @@ -178,20 +178,49 @@ class PollService { return $this->pollRepository->findAllByAdminMail($mail); } - function computeBestChoices($votes) { - $result = ['y' => [0], 'inb' => [0]]; - foreach ($votes as $vote) { - $choices = str_split($vote->choices); - foreach ($choices as $i => $choice) { - if (!isset($result['y'][$i])) { - $result['inb'][$i] = 0; - $result['y'][$i] = 0; + function computeBestChoices($votes, $poll) { + $result = ['y' => [], 'inb' => []]; + + if (0 === count($votes)) { + // if there is no votes, calculates the number of slot + + $slots = $this->allSlotsByPoll($poll); + + if ($poll->format === 'A') { + // poll format classic + + foreach ($slots as $slot) { + $result['y'][] = 0; + $result['inb'][] = 0; } - if ($choice === "1") { - $result['inb'][$i]++; + } else { + // poll format date + + $slots = $this->splitSlots($slots); + + foreach ($slots as $slot) { + foreach ($slot->moments as $_) { + $result['y'][] = 0; + $result['inb'][] = 0; + } } - if ($choice === "2") { - $result['y'][$i]++; + } + } else { + // if there is votes + + foreach ($votes as $vote) { + $choices = str_split($vote->choices); + foreach ($choices as $i => $choice) { + if (!isset($result['y'][$i])) { + $result['inb'][$i] = 0; + $result['y'][$i] = 0; + } + if ($choice === "1") { + $result['inb'][$i]++; + } + if ($choice === "2") { + $result['y'][$i]++; + } } } } @@ -293,10 +322,10 @@ class PollService { if (count($votes) <= 0) { return; } - $best_choices = $this->computeBestChoices($votes); + $best_choices = $this->computeBestChoices($votes, $poll); foreach ($best_choices['y'] as $i => $nb_choice) { // if for this option we have reached maximum value and user wants to add itself too - if ($poll->ValueMax !== null && $nb_choice >= $poll->ValueMax && $user_choice[$i] === "2") { + if ($poll->ValueMax !== null && $nb_choice >= $poll->ValueMax && $user_choice[$i] === "2") { throw new ConcurrentVoteException(); } } diff --git a/studs.php b/studs.php index f202c4a..6cef3ee 100644 --- a/studs.php +++ b/studs.php @@ -227,7 +227,7 @@ $smarty->assign('deletion_date', strtotime($poll->end_date) + PURGE_DELAY * 8640 $smarty->assign('slots', $poll->format === 'D' ? $pollService->splitSlots($slots) : $slots); $smarty->assign('slots_hash', $pollService->hashSlots($slots)); $smarty->assign('votes', $pollService->splitVotes($votes)); -$smarty->assign('best_choices', $pollService->computeBestChoices($votes)); +$smarty->assign('best_choices', $pollService->computeBestChoices($votes, $poll)); $smarty->assign('comments', $comments); $smarty->assign('editingVoteId', $editingVoteId); $smarty->assign('message', $message);