Skip to content

Commit 517e227

Browse files
committed
Optimization of polling code
1 parent 59db775 commit 517e227

4 files changed

Lines changed: 63 additions & 59 deletions

File tree

src/controllers/poll-controller.php

Lines changed: 54 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -86,75 +86,93 @@ private function placeVote($sessionId, $memberId, $voteValue)
8686
}
8787

8888
// Wrap up current poll in reponse object
89-
private function currentPoll($sessionId)
89+
private function current($sessionId)
9090
{
9191
// Load the user-vote.php required for this
9292
include __DIR__ . "/user-vote.php";
9393

9494
$session = $this->getSession($sessionId);
95-
$cardSet = $this->getCardSet($session);
9695

97-
// Create response array
98-
$votes = array();
99-
$currentPoll = $session->getCurrentPoll();
100-
foreach($session->getMembers() as $index=>$member)
101-
{
102-
$votes[$index] = UserVote::create($member, $currentPoll, $cardSet);
103-
}
104-
10596
// Create reponse object
10697
$response = new stdClass();
10798
$response->name = $session->getName();
108-
$response->topic = $currentPoll != null ? $currentPoll->getTopic() : "";
109-
// Time taken for estimation
110-
if($currentPoll != null)
99+
$response->votes = array();
100+
101+
// Include votes in response
102+
$currentPoll = $session->getCurrentPoll();
103+
if ($currentPoll == null)
111104
{
112-
$diff = $currentPoll->getEndTime()->diff($currentPoll->getStartTime());
113-
$response->duration = new stdClass();
114-
$response->duration->min = $diff->i;
115-
$response->duration->sec = $diff->s;
105+
$response->topic = "";
106+
$response->flipped = false;
107+
$response->consensus = false;
116108
}
117-
// Vote estimation
118-
$response->votes = $votes;
119-
$response->flipped = is_null($currentPoll) ? false : $currentPoll->getResult() >= 0;
120-
$response->consensus = is_null($currentPoll) ? false : $currentPoll->getConsensus();
109+
else
110+
{
111+
$response->topic = $currentPoll->getTopic();
112+
$response->flipped = $currentPoll->getResult() >= 0;
113+
$response->consensus = $currentPoll->getConsensus();
114+
115+
$diff = $currentPoll->getEndTime()->diff($currentPoll->getStartTime());
116+
$response->duration = $diff;
117+
}
118+
119+
// Members votes
120+
$cardSet = $this->getCardSet($session);
121+
$query = $this->entityManager
122+
->createQuery('SELECT m.id, m.name, v.value, v.highlighted FROM member m LEFT JOIN m.votes v WITH (v.member = m AND v.poll = ?1) WHERE m.session = ?2')
123+
->setParameter(1, $currentPoll)
124+
->setParameter(2, $session);
125+
$result = $query->getArrayResult();
126+
foreach($result as $vote)
127+
$response->votes[] = UserVote::fromQuery($cardSet, $vote);
121128

122129
return $response;
123130
}
131+
132+
private function topic($sessionId)
133+
{
134+
$session = $this->getSession($sessionId);
135+
$currentPoll = $session->getCurrentPoll();
136+
137+
// Result object. Only votable until all votes received
138+
$result = new stdClass();
139+
if ($currentPoll == null)
140+
{
141+
$result->topic = "No topic";
142+
$result->votable = false;
143+
}
144+
else
145+
{
146+
$result->topic = $currentPoll->getTopic();
147+
$result->votable = $currentPoll->getResult() < 0;
148+
}
149+
150+
return $result;
151+
}
124152

125153
public function execute()
126154
{
127155
switch($this->requestedMethod())
128156
{
129157
case "current":
130158
$sessionId = $_GET["id"];
131-
return $this->currentPoll($sessionId);
159+
return $this->current($sessionId);
132160

133161
case "start":
134-
$data = $this->jsonInput();
135-
162+
$data = $this->jsonInput();
136163
$this->startPoll($data["sessionId"], $data["topic"]);
137164
return null;
138165

139166
case "place":
140167
$data = $this->jsonInput();
141-
142168
$this->placeVote($data["sessionId"], $data["memberId"], $data["vote"]);
143169
return null;
144170

145171
case "topic":
146-
$session = $this->getSession($_GET["sid"]);
147-
$currentPoll = $session->getCurrentPoll();
148-
149-
// Result object. Only votable until all votes received
150-
$result = new stdClass();
151-
$result->topic = is_null($currentPoll) ? "No topic" : $currentPoll->getTopic();
152-
$result->votable = is_null($currentPoll) ? false : $currentPoll->getResult() < 0;
153-
154-
return $result;
172+
$sessionId = $_GET["sid"];
173+
return $this->topic($sessionId);
155174
}
156175
}
157176
}
158177

159-
return new PollController($entityManager, $cardSets);
160-
?>
178+
return new PollController($entityManager, $cardSets);

src/controllers/user-vote.php

Lines changed: 7 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -8,31 +8,17 @@ private function __construct()
88
$this->active = false;
99
}
1010

11-
// Create instance from member entity
12-
public static function create($member, $currentPoll, $cardSet)
11+
// Create vote object from query object
12+
public static function fromQuery($cardSet, $entity)
1313
{
1414
$vote = new UserVote();
15-
$vote->id = $member->getId();
16-
$vote->name = $member->getName();
17-
18-
// Poll related values
19-
if(is_null($currentPoll))
20-
return $vote;
21-
22-
// Find matching member in poll
23-
foreach($currentPoll->getVotes() as $candidate)
24-
{
25-
if($candidate->getMember() === $member)
26-
{
27-
$match = $candidate;
28-
}
29-
}
30-
31-
if(isset($match))
15+
$vote->id = $entity['id'];
16+
$vote->name = $entity['name'];
17+
if($entity['value'] !== null)
3218
{
3319
$vote->placed = true;
34-
$vote->value = $cardSet[$match->getValue()];
35-
$vote->active = $match->getHighlighted();
20+
$vote->value = $cardSet[$entity['value']];
21+
$vote->active = $entity['highlighted'];
3622
}
3723

3824
return $vote;

src/model/poll.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ function __construct()
3232
/** @ManyToOne(targetEntity="Session", inversedBy="polls") **/
3333
protected $session;
3434

35-
/** @OneToMany(targetEntity="Vote", mappedBy="poll", fetch="EAGER") **/
35+
/** @OneToMany(targetEntity="Vote", mappedBy="poll") **/
3636
protected $votes;
3737

3838
public function getId()

src/model/session.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class Session
2323
/** @Column(type="datetime") **/
2424
protected $lastAction;
2525

26-
/** @OneToMany(targetEntity="Member", mappedBy="session", fetch="EAGER", orphanRemoval=true) **/
26+
/** @OneToMany(targetEntity="Member", mappedBy="session", orphanRemoval=true) **/
2727
protected $members;
2828

2929
/** @OneToMany(targetEntity="Poll", mappedBy="session", orphanRemoval=true) **/

0 commit comments

Comments
 (0)