bug/undefined-offset-on-best-choices

This commit is contained in:
m 2018-04-05 17:34:43 +02:00
parent 5b4b8a8606
commit c79b32984d
3 changed files with 45 additions and 16 deletions

View File

@ -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', $poll->format === 'D' ? $pollService->splitSlots($slots) : $slots);
$smarty->assign('slots_hash', $pollService->hashSlots($slots)); $smarty->assign('slots_hash', $pollService->hashSlots($slots));
$smarty->assign('votes', $pollService->splitVotes($votes)); $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('comments', $comments);
$smarty->assign('editingVoteId', $editingVoteId); $smarty->assign('editingVoteId', $editingVoteId);
$smarty->assign('message', $message); $smarty->assign('message', $message);

View File

@ -178,8 +178,36 @@ class PollService {
return $this->pollRepository->findAllByAdminMail($mail); return $this->pollRepository->findAllByAdminMail($mail);
} }
function computeBestChoices($votes) { function computeBestChoices($votes, $poll) {
$result = ['y' => [0], 'inb' => [0]]; $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;
}
} else {
// poll format date
$slots = $this->splitSlots($slots);
foreach ($slots as $slot) {
foreach ($slot->moments as $_) {
$result['y'][] = 0;
$result['inb'][] = 0;
}
}
}
} else {
// if there is votes
foreach ($votes as $vote) { foreach ($votes as $vote) {
$choices = str_split($vote->choices); $choices = str_split($vote->choices);
foreach ($choices as $i => $choice) { foreach ($choices as $i => $choice) {
@ -195,6 +223,7 @@ class PollService {
} }
} }
} }
}
return $result; return $result;
} }
@ -293,7 +322,7 @@ class PollService {
if (count($votes) <= 0) { if (count($votes) <= 0) {
return; return;
} }
$best_choices = $this->computeBestChoices($votes); $best_choices = $this->computeBestChoices($votes, $poll);
foreach ($best_choices['y'] as $i => $nb_choice) { 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 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") {

View File

@ -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', $poll->format === 'D' ? $pollService->splitSlots($slots) : $slots);
$smarty->assign('slots_hash', $pollService->hashSlots($slots)); $smarty->assign('slots_hash', $pollService->hashSlots($slots));
$smarty->assign('votes', $pollService->splitVotes($votes)); $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('comments', $comments);
$smarty->assign('editingVoteId', $editingVoteId); $smarty->assign('editingVoteId', $editingVoteId);
$smarty->assign('message', $message); $smarty->assign('message', $message);