@@ -19,6 +19,13 @@ private function getValue($session, $vote)
1919 return intval ($ value );
2020 }
2121
22+ // Check if the session as changed since the last polling call
23+ private function sessionUnchanged ($ session )
24+ {
25+ // Check if anything changed since the last polling call
26+ return isset ($ _GET ['last ' ]) && $ _GET ['last ' ] >= $ session ->getLastAction ()->getTimestamp ();
27+ }
28+
2229 // Start a new poll in the session
2330 private function startPoll ($ sessionId , $ topic )
2431 {
@@ -47,6 +54,7 @@ private function placeVote($sessionId, $memberId, $voteValue)
4754
4855 // Fetch entities
4956 $ session = $ this ->getSession ($ sessionId );
57+ $ session ->setLastAction (new DateTime ());
5058 $ currentPoll = $ session ->getCurrentPoll ();
5159 $ member = $ this ->getMember ($ memberId );
5260
@@ -57,8 +65,11 @@ private function placeVote($sessionId, $memberId, $voteValue)
5765 // Find or create vote
5866 foreach ($ currentPoll ->getVotes () as $ vote )
5967 {
60- if ($ vote ->getMember () == $ member )
61- $ match = $ vote ;
68+ if ($ vote ->getMember () != $ member )
69+ continue ;
70+
71+ $ match = $ vote ;
72+ break ;
6273 }
6374
6475 // Create vote if not found
@@ -81,7 +92,7 @@ private function placeVote($sessionId, $memberId, $voteValue)
8192 }
8293
8394 // Save all to db
84- $ this ->saveAll ([$ match , $ currentPoll ]);
95+ $ this ->saveAll ([$ session , $ match , $ currentPoll ]);
8596 $ this ->saveAll ($ currentPoll ->getVotes ()->toArray ());
8697 }
8798
@@ -90,14 +101,23 @@ private function current($sessionId)
90101 {
91102 // Load the user-vote.php required for this
92103 include __DIR__ . "/user-vote.php " ;
104+
105+ // Create reponse object
106+ $ response = new stdClass ();
93107
94108 $ session = $ this ->getSession ($ sessionId );
109+
110+ // Check if anything changed since the last polling call
111+ if ($ this ->sessionUnchanged ($ session ))
112+ {
113+ $ response ->unchanged = true ;
114+ return $ response ;
115+ }
95116
96- // Create reponse object
97- $ response = new stdClass ();
117+ // Fill response object
98118 $ response ->name = $ session ->getName ();
119+ $ response ->timestamp = $ session ->getLastAction ()->getTimestamp ();
99120 $ response ->votes = array ();
100-
101121 // Include votes in response
102122 $ currentPoll = $ session ->getCurrentPoll ();
103123 if ($ currentPoll == null )
@@ -131,23 +151,32 @@ private function current($sessionId)
131151
132152 private function topic ($ sessionId )
133153 {
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- }
154+ $ session = $ this ->getSession ($ sessionId );
149155
156+ // Check if anything changed since the last polling call
157+ if ($ this ->sessionUnchanged ($ session ))
158+ {
159+ $ result ->unchanged = true ;
150160 return $ result ;
161+ }
162+
163+ $ currentPoll = $ session ->getCurrentPoll ();
164+
165+ // Result object. Only votable until all votes received
166+ $ result = new stdClass ();
167+ $ result ->timestamp = $ session ->getLastAction ()->getTimestamp ();
168+ if ($ currentPoll == null )
169+ {
170+ $ result ->topic = "No topic " ;
171+ $ result ->votable = false ;
172+ }
173+ else
174+ {
175+ $ result ->topic = $ currentPoll ->getTopic ();
176+ $ result ->votable = $ currentPoll ->getResult () < 0 ;
177+ }
178+
179+ return $ result ;
151180 }
152181
153182 public function execute ()
0 commit comments