From c79b32984dcf1e12e74cec9b57ddfca1c4821b3a Mon Sep 17 00:00:00 2001 From: m Date: Thu, 5 Apr 2018 17:34:43 +0200 Subject: [PATCH 1/3] 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); From 61ac8fcf764935c0d4f7c5db3f0dba5edc95b3bd Mon Sep 17 00:00:00 2001 From: Thomas Citharel Date: Fri, 6 Apr 2018 09:40:53 +0200 Subject: [PATCH 2/3] Refactor a bit Also fix utf8 issue on licence file Signed-off-by: Thomas Citharel --- .../Repositories/RepositoryFactory.php | 6 +- .../Framadate/Services/PollService.php | 92 +++++++++++-------- 2 files changed, 56 insertions(+), 42 deletions(-) diff --git a/app/classes/Framadate/Repositories/RepositoryFactory.php b/app/classes/Framadate/Repositories/RepositoryFactory.php index 1ebef18..810d391 100644 --- a/app/classes/Framadate/Repositories/RepositoryFactory.php +++ b/app/classes/Framadate/Repositories/RepositoryFactory.php @@ -4,16 +4,16 @@ * is not distributed with this file, you can obtain one at * http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.txt * - * Authors of STUdS (initial project): Guilhem BORGHESI (borghesi@unistra.fr) and Raphaël DROZ + * Authors of STUdS (initial project): Guilhem BORGHESI (borghesi@unistra.fr) and Raphaël DROZ * Authors of Framadate/OpenSondage: Framasoft (https://github.com/framasoft) * * ============================= * - * Ce logiciel est régi par la licence CeCILL-B. Si une copie de cette licence + * Ce logiciel est régi par la licence CeCILL-B. Si une copie de cette licence * ne se trouve pas avec ce fichier vous pouvez l'obtenir sur * http://www.cecill.info/licences/Licence_CeCILL-B_V1-fr.txt * - * Auteurs de STUdS (projet initial) : Guilhem BORGHESI (borghesi@unistra.fr) et Raphaël DROZ + * Auteurs de STUdS (projet initial) : Guilhem BORGHESI (borghesi@unistra.fr) et Raphaël DROZ * Auteurs de Framadate/OpenSondage : Framasoft (https://github.com/framasoft) */ namespace Framadate\Repositories; diff --git a/app/classes/Framadate/Services/PollService.php b/app/classes/Framadate/Services/PollService.php index 05e326c..a01e683 100644 --- a/app/classes/Framadate/Services/PollService.php +++ b/app/classes/Framadate/Services/PollService.php @@ -25,7 +25,6 @@ use Framadate\Form; use Framadate\FramaDB; use Framadate\Repositories\RepositoryFactory; use Framadate\Security\Token; -use Framadate\Utils; class PollService { private $connect; @@ -178,49 +177,64 @@ class PollService { return $this->pollRepository->findAllByAdminMail($mail); } - function computeBestChoices($votes, $poll) { + /** + * @param \stdClass $poll + * @return array + */ + private function computeEmptyBestChoices($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) { + // if there is no votes, calculates the number of slot + + $slots = $this->allSlotsByPoll($poll); + + if ($poll->format === 'A') { + // poll format classic + + for ($i = 0; $i < count($slots); $i++) { + $result['y'][] = 0; + $result['inb'][] = 0; + } + } else { + // poll format date + + $slots = $this->splitSlots($slots); + + foreach ($slots as $slot) { + for ($i = 0; $i < count($slot->moments); $i++) { $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) { - $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]++; - } + } + return $result; + } + + /** + * @param array $votes + * @param \stdClass $poll + * @return array + */ + public function computeBestChoices($votes, $poll) { + + if (0 === count($votes)) { + return $this->computeEmptyBestChoices($poll); + } + $result = ['y' => [], 'inb' => []]; + + // if there are 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]++; } } } From 08f7ca141d3f7f054dccba1e86b9f4d94f316e82 Mon Sep 17 00:00:00 2001 From: Thomas Citharel Date: Fri, 6 Apr 2018 09:43:32 +0200 Subject: [PATCH 3/3] CS Signed-off-by: Thomas Citharel --- .../Framadate/Services/PollService.php | 67 +++++++++---------- 1 file changed, 33 insertions(+), 34 deletions(-) diff --git a/app/classes/Framadate/Services/PollService.php b/app/classes/Framadate/Services/PollService.php index a01e683..ad180b1 100644 --- a/app/classes/Framadate/Services/PollService.php +++ b/app/classes/Framadate/Services/PollService.php @@ -177,46 +177,12 @@ class PollService { return $this->pollRepository->findAllByAdminMail($mail); } - /** - * @param \stdClass $poll - * @return array - */ - private function computeEmptyBestChoices($poll) - { - $result = ['y' => [], 'inb' => []]; - // if there is no votes, calculates the number of slot - - $slots = $this->allSlotsByPoll($poll); - - if ($poll->format === 'A') { - // poll format classic - - for ($i = 0; $i < count($slots); $i++) { - $result['y'][] = 0; - $result['inb'][] = 0; - } - } else { - // poll format date - - $slots = $this->splitSlots($slots); - - foreach ($slots as $slot) { - for ($i = 0; $i < count($slot->moments); $i++) { - $result['y'][] = 0; - $result['inb'][] = 0; - } - } - } - return $result; - } - /** * @param array $votes * @param \stdClass $poll * @return array */ public function computeBestChoices($votes, $poll) { - if (0 === count($votes)) { return $this->computeEmptyBestChoices($poll); } @@ -305,6 +271,39 @@ class PollService { return $slots; } + /** + * @param \stdClass $poll + * @return array + */ + private function computeEmptyBestChoices($poll) + { + $result = ['y' => [], 'inb' => []]; + // if there is no votes, calculates the number of slot + + $slots = $this->allSlotsByPoll($poll); + + if ($poll->format === 'A') { + // poll format classic + + for ($i = 0; $i < count($slots); $i++) { + $result['y'][] = 0; + $result['inb'][] = 0; + } + } else { + // poll format date + + $slots = $this->splitSlots($slots); + + foreach ($slots as $slot) { + for ($i = 0; $i < count($slot->moments); $i++) { + $result['y'][] = 0; + $result['inb'][] = 0; + } + } + } + return $result; + } + private function random($length) { return Token::getToken($length); }