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]++; } } }