Refactor a bit

Also fix utf8 issue on licence file

Signed-off-by: Thomas Citharel <tcit@tcit.fr>
This commit is contained in:
Thomas Citharel 2018-04-06 09:40:53 +02:00
parent c79b32984d
commit 61ac8fcf76
No known key found for this signature in database
GPG Key ID: A061B9DDE0CA0773
2 changed files with 56 additions and 42 deletions

View File

@ -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;

View File

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