Skip to content

Commit

Permalink
Evaluate after removing member. Closes #36.
Browse files Browse the repository at this point in the history
  • Loading branch information
Toxantron committed Mar 11, 2017
1 parent aa5b6d8 commit 5f64218
Show file tree
Hide file tree
Showing 4 changed files with 123 additions and 98 deletions.
12 changes: 11 additions & 1 deletion src/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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()
Expand Down
103 changes: 8 additions & 95 deletions src/controllers/poll-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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)
{
Expand Down Expand Up @@ -241,7 +156,5 @@ public function execute()
}
}

$ctrl = new PollController($entityManager);
$ctrl->cardSets = $cardSets;
return $ctrl;
return new PollController($entityManager, $cardSets);
?>
18 changes: 16 additions & 2 deletions src/controllers/session-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -142,5 +156,5 @@ public function execute()
}
}

return new SessionController($entityManager);
return new SessionController($entityManager, $cardSets);
?>
88 changes: 88 additions & 0 deletions src/controllers/session-evaluation.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
<?php
/*
* Shared function to evaluate sessions
*/
class SessionEvaluation
{
// Evaluate the polls average
public static function evaluatePoll($session, $currentPoll)
{
$sum = 0;
$count = $currentPoll->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;
}
}

0 comments on commit 5f64218

Please sign in to comment.