Merge branch 'bug/undefined-offset-on-best-choices' into 'develop'

bug/undefined-offset-on-best-choices

See merge request framasoft/framadate!277
This commit is contained in:
Thomas Citharel 2018-04-06 09:46:50 +02:00
commit f726adf2ee
4 changed files with 52 additions and 10 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

@ -4,16 +4,16 @@
* is not distributed with this file, you can obtain one at * is not distributed with this file, you can obtain one at
* http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.txt * 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) * 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 * ne se trouve pas avec ce fichier vous pouvez l'obtenir sur
* http://www.cecill.info/licences/Licence_CeCILL-B_V1-fr.txt * 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) * Auteurs de Framadate/OpenSondage : Framasoft (https://github.com/framasoft)
*/ */
namespace Framadate\Repositories; namespace Framadate\Repositories;

View File

@ -25,7 +25,6 @@ use Framadate\Form;
use Framadate\FramaDB; use Framadate\FramaDB;
use Framadate\Repositories\RepositoryFactory; use Framadate\Repositories\RepositoryFactory;
use Framadate\Security\Token; use Framadate\Security\Token;
use Framadate\Utils;
class PollService { class PollService {
private $connect; private $connect;
@ -178,8 +177,18 @@ class PollService {
return $this->pollRepository->findAllByAdminMail($mail); return $this->pollRepository->findAllByAdminMail($mail);
} }
function computeBestChoices($votes) { /**
$result = ['y' => [0], 'inb' => [0]]; * @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) { foreach ($votes as $vote) {
$choices = str_split($vote->choices); $choices = str_split($vote->choices);
foreach ($choices as $i => $choice) { foreach ($choices as $i => $choice) {
@ -262,6 +271,39 @@ class PollService {
return $slots; 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) { private function random($length) {
return Token::getToken($length); return Token::getToken($length);
} }
@ -293,7 +335,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);