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_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);

View File

@ -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();
}
}

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_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);