diff --git a/src/bootstrap.php b/src/bootstrap.php index 8accd7b..44b9cd4 100644 --- a/src/bootstrap.php +++ b/src/bootstrap.php @@ -27,10 +27,14 @@ class ControllerBase { protected $entityManager; + + // Configured cards sets + public $cardSets; - function __construct($entityManager) + function __construct($entityManager, $cardSets = []) { $this->entityManager = $entityManager; + $this->cardSets = $cardSets; } // Get session by id @@ -48,6 +52,12 @@ protected function getMember($id) $member = $this->entityManager->find("Member", $id); return $member; } + + // Get card set of the session + protected function getCardSet($session) + { + return $this->cardSets[$session->getCardSet()]; + } // Find out witch method was requested protected function requestedMethod() diff --git a/src/controllers/poll-controller.php b/src/controllers/poll-controller.php index 7e9b200..6c77a44 100644 --- a/src/controllers/poll-controller.php +++ b/src/controllers/poll-controller.php @@ -4,15 +4,6 @@ */ class PollController extends ControllerBase implements IController { - // Configured cards sets - public $cardSets; - - // Get card set of the session - private function getCardSet($session) - { - return $this->cardSets[$session->getCardSet()]; - } - // Get card set array of the session private function getIndex($session, $voteValue) { @@ -52,6 +43,8 @@ private function startPoll($sessionId, $topic) // Place a vote for the current poll private function placeVote($sessionId, $memberId, $voteValue) { + include __DIR__ . "/session-evaluation.php"; + // Fetch entities $session = $this->getSession($sessionId); $currentPoll = $session->getCurrentPoll(); @@ -81,95 +74,17 @@ private function placeVote($sessionId, $memberId, $voteValue) $match->setValue($voteIndex); // Evaluate the poll - $this->evaluatePoll($session, $currentPoll); - if($currentPoll->getResult() >= 0) - $this->highlightVotes($session, $currentPoll); + if(SessionEvaluation::evaluatePoll($session, $currentPoll)) + { + $cardSet = $this->getCardSet($session); + SessionEvaluation::highlightVotes($session, $currentPoll, $cardSet); + } // Save all to db $this->saveAll([$match, $currentPoll]); $this->saveAll($currentPoll->getVotes()->toArray()); } - // Evaluate the polls average - private function evaluatePoll($session, $currentPoll) - { - $sum = 0; - $count = $currentPoll->getVotes()->count(); - - if($count != $session->getMembers()->count()) - return; - - foreach($currentPoll->getVotes() as $vote) - { - $sum += $vote->getValue(); - } - $currentPoll->setResult($sum / $count); - $currentPoll->setEndTime(new DateTime()); - } - - // Highlight highest and lowest estimate - private function highlightVotes($session, $currentPoll) - { - include "user-vote.php"; - - $votes = $currentPoll->getVotes(); - // Frequency for each card - $frequencies = []; - foreach($this->getCardSet($session) as $key=>$card) - { - $frequencies[$key] = new CardFrequency($key); - } - - // Count absolute frequence - foreach($votes as $vote) - { - $frequencies[$vote->getValue()]->count++; - } - - // Determine most common vote - foreach($frequencies as $frequency) - { - if(!isset($mostCommon) || $mostCommon->count < $frequency->count) - $mostCommon = $frequency; - } - - $min = 0; $max = 0; - // Iterate over frequencies and find lowest - foreach($frequencies as $frequency) - { - $min = $this->selectLimits($votes, $frequency, $mostCommon); - if($min != 0) - break; - } - // Iterate over frequencies and find highest - foreach(array_reverse($frequencies) as $frequency) - { - $max = $this->selectLimits($votes, $frequency, $mostCommon); - if($max != 0) - break; - } - - $currentPoll->setConsensus($min == -1 && $max == -1); - } - - // Select highest or lowest estimates - depends on direction of loop - private function selectLimits($votes, $frequency, $mostCommon) - { - // No card at all - if($frequency->count == 0) - return 0; - // This is the most common, no lowest found - if($frequency == $mostCommon) - return -1; - - foreach($votes as $vote) - { - if($vote->getValue() == $frequency->value) - $vote->setHighlighted(true); - } - return 1; - } - // Wrap up current poll in reponse object private function currentPoll($sessionId) { @@ -241,7 +156,5 @@ public function execute() } } -$ctrl = new PollController($entityManager); -$ctrl->cardSets = $cardSets; -return $ctrl; +return new PollController($entityManager, $cardSets); ?> \ No newline at end of file diff --git a/src/controllers/session-controller.php b/src/controllers/session-controller.php index 9127abb..49b77cd 100644 --- a/src/controllers/session-controller.php +++ b/src/controllers/session-controller.php @@ -70,9 +70,23 @@ private function addMember($id, $name) // Remove member from session private function removeMember($id) { - $member = $this->getMember($id); + include __DIR__ . "/session-evaluation.php"; + + // Get and remove member + $member = $this->getMember($id); $this->entityManager->remove($member); $this->entityManager->flush(); + + // Get the members session and its current poll + $session = $member->getSession(); + $poll = $session->getCurrentPoll(); + if($poll !== null && SessionEvaluation::evaluatePoll($session, $poll)) + { + $cardSet = $this->getCardSet($session); + SessionEvaluation::highlightVotes($session, $poll, $cardSet); + } + + $this->entityManager->flush(); } private function hasPassword($id) @@ -142,5 +156,5 @@ public function execute() } } -return new SessionController($entityManager); +return new SessionController($entityManager, $cardSets); ?> diff --git a/src/controllers/session-evaluation.php b/src/controllers/session-evaluation.php new file mode 100644 index 0000000..6b0b786 --- /dev/null +++ b/src/controllers/session-evaluation.php @@ -0,0 +1,88 @@ +getVotes()->count(); + + if($count != $session->getMembers()->count()) + return false; + + foreach($currentPoll->getVotes() as $vote) + { + $sum += $vote->getValue(); + } + $currentPoll->setResult($sum / $count); + $currentPoll->setEndTime(new DateTime()); + + return true; + } + + // Highlight highest and lowest estimate + public static function highlightVotes($session, $currentPoll, $cardSet) + { + include __DIR__ . "/card-frequency.php"; + + $votes = $currentPoll->getVotes(); + // Frequency for each card + $frequencies = []; + foreach($cardSet as $key=>$card) + { + $frequencies[$key] = new CardFrequency($key); + } + + // Count absolute frequence + foreach($votes as $vote) + { + $frequencies[$vote->getValue()]->count++; + } + + // Determine most common vote + foreach($frequencies as $frequency) + { + if(!isset($mostCommon) || $mostCommon->count < $frequency->count) + $mostCommon = $frequency; + } + + $min = 0; $max = 0; + // Iterate over frequencies and find lowest + foreach($frequencies as $frequency) + { + $min = self::selectLimits($votes, $frequency, $mostCommon); + if($min != 0) + break; + } + // Iterate over frequencies and find highest + foreach(array_reverse($frequencies) as $frequency) + { + $max = self::selectLimits($votes, $frequency, $mostCommon); + if($max != 0) + break; + } + + $currentPoll->setConsensus($min == -1 && $max == -1); + } + + // Select highest or lowest estimates - depends on direction of loop + private static function selectLimits($votes, $frequency, $mostCommon) + { + // No card at all + if($frequency->count == 0) + return 0; + // This is the most common, no lowest found + if($frequency == $mostCommon) + return -1; + + foreach($votes as $vote) + { + if($vote->getValue() == $frequency->value) + $vote->setHighlighted(true); + } + return 1; + } +} \ No newline at end of file