@@ -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 );
0 commit comments