diff --git a/src/controllers/poll-controller.php b/src/controllers/poll-controller.php index 35d3b46..84664a2 100644 --- a/src/controllers/poll-controller.php +++ b/src/controllers/poll-controller.php @@ -19,6 +19,13 @@ private function getValue($session, $vote) return intval($value); } + // Check if the session as changed since the last polling call + private function sessionUnchanged($session) + { + // Check if anything changed since the last polling call + return isset($_GET['last']) && $_GET['last'] >= $session->getLastAction()->getTimestamp(); + } + // Start a new poll in the session private function startPoll($sessionId, $topic) { @@ -47,6 +54,7 @@ private function placeVote($sessionId, $memberId, $voteValue) // Fetch entities $session = $this->getSession($sessionId); + $session->setLastAction(new DateTime()); $currentPoll = $session->getCurrentPoll(); $member = $this->getMember($memberId); @@ -57,8 +65,11 @@ private function placeVote($sessionId, $memberId, $voteValue) // Find or create vote foreach($currentPoll->getVotes() as $vote) { - if($vote->getMember() == $member) - $match = $vote; + if($vote->getMember() != $member) + continue; + + $match = $vote; + break; } // Create vote if not found @@ -81,7 +92,7 @@ private function placeVote($sessionId, $memberId, $voteValue) } // Save all to db - $this->saveAll([$match, $currentPoll]); + $this->saveAll([$session, $match, $currentPoll]); $this->saveAll($currentPoll->getVotes()->toArray()); } @@ -90,14 +101,23 @@ private function current($sessionId) { // Load the user-vote.php required for this include __DIR__ . "/user-vote.php"; + + // Create reponse object + $response = new stdClass(); $session = $this->getSession($sessionId); + + // Check if anything changed since the last polling call + if($this->sessionUnchanged($session)) + { + $response->unchanged = true; + return $response; + } - // Create reponse object - $response = new stdClass(); + // Fill response object $response->name = $session->getName(); + $response->timestamp = $session->getLastAction()->getTimestamp(); $response->votes = array(); - // Include votes in response $currentPoll = $session->getCurrentPoll(); if ($currentPoll == null) @@ -131,23 +151,32 @@ private function current($sessionId) private function topic($sessionId) { - $session = $this->getSession($sessionId); - $currentPoll = $session->getCurrentPoll(); - - // Result object. Only votable until all votes received - $result = new stdClass(); - if ($currentPoll == null) - { - $result->topic = "No topic"; - $result->votable = false; - } - else - { - $result->topic = $currentPoll->getTopic(); - $result->votable = $currentPoll->getResult() < 0; - } + $session = $this->getSession($sessionId); + // Check if anything changed since the last polling call + if($this->sessionUnchanged($session)) + { + $result->unchanged = true; return $result; + } + + $currentPoll = $session->getCurrentPoll(); + + // Result object. Only votable until all votes received + $result = new stdClass(); + $result->timestamp = $session->getLastAction()->getTimestamp(); + if ($currentPoll == null) + { + $result->topic = "No topic"; + $result->votable = false; + } + else + { + $result->topic = $currentPoll->getTopic(); + $result->votable = $currentPoll->getResult() < 0; + } + + return $result; } public function execute() diff --git a/src/controllers/session-controller.php b/src/controllers/session-controller.php index 49b77cd..60bff2c 100644 --- a/src/controllers/session-controller.php +++ b/src/controllers/session-controller.php @@ -9,7 +9,7 @@ private function getAllSessions() { // Create query finding all active sessions $query = $this->entityManager->createQuery('SELECT s.id, s.name, s.isPrivate, count(m.id) memberCount FROM Session s LEFT JOIN s.members m WHERE s.lastAction > ?1 GROUP BY s.id'); - $query->setParameter(1, new DateTime('-2 hour')); + $query->setParameter(1, new DateTime('-1 hour')); $sessions = $query->getArrayResult(); return $sessions; } diff --git a/src/controllers/statistics-controller.php b/src/controllers/statistics-controller.php index 3133a56..7d7da2f 100644 --- a/src/controllers/statistics-controller.php +++ b/src/controllers/statistics-controller.php @@ -11,7 +11,7 @@ private function loadPlugins($filter) foreach(glob(__DIR__ . '/statistics/*.php') as $file) { // Check if the plugin was selected in the filter $key = basename($file, ".php"); - if($filter == null || in_array($key, $filter)) { + if($filter == null || sizeof($filter) == 0 || in_array($key, $filter)) { $plugin = include $file; $plugins[$key] = $plugin; } diff --git a/src/js/main.js b/src/js/main.js index 3ba8e4e..e4d7ef3 100644 --- a/src/js/main.js +++ b/src/js/main.js @@ -378,12 +378,19 @@ scrum.app.controller('MasterController', function ($http, $routeParams, $locatio if (scrum.current !== self) return; - $http.get("/api/poll/current?id=" + self.id).then(function(response){ + $http.get("/api/poll/current?id=" + self.id + "&last=" + self.timestamp).then(function(response){ var data = response.data; var result = data.result; - if(!data.success) { - // Error handling - return; + + // Call succeeded, but execution failed + if (!data.success) { + setTimeout(pollVotes, scrum.pollingScale.scale(300)); + return; + } + // Session was not modified + if (result.unchanged) { + setTimeout(pollVotes, scrum.pollingScale.scale(300)); + return; } // Query statistics @@ -393,6 +400,7 @@ scrum.app.controller('MasterController', function ($http, $routeParams, $locatio // Copy poll values self.name = result.name; + self.timestamp = result.timestamp; self.votes = result.votes; self.flipped = result.flipped; self.consensus = result.consensus; @@ -403,10 +411,10 @@ scrum.app.controller('MasterController', function ($http, $routeParams, $locatio } scrum.pollingScale.success(); - setTimeout(pollVotes, scrum.pollingScale.scale(300)); + setTimeout(pollVotes, scrum.pollingScale.scale(400)); }, function(){ scrum.pollingScale.failed(); - setTimeout(pollVotes, scrum.pollingScale.scale(300)); + setTimeout(pollVotes, scrum.pollingScale.scale(400)); }); } @@ -491,16 +499,24 @@ scrum.app.controller('MemberController', function MemberController ($http, $loca if (scrum.current !== self) return; // Update topic - $http.get("/api/poll/topic?sid=" + self.id).then(function(response){ + $http.get("/api/poll/topic?sid=" + self.id + "&last=" + self.timestamp).then(function(response){ var data = response.data; if(!data.success) { self.reset(); + setTimeout(update, scrum.pollingScale.scale(500)); return; } var result = data.result; + if (result.unchanged) { + setTimeout(update, scrum.pollingScale.scale(500)); + return + } + + self.timestamp = result.timestamp; + // Voting was closed, get our peers votes if(self.votable && !result.votable) { diff --git a/src/model/session.php b/src/model/session.php index 893f557..a83c0e1 100644 --- a/src/model/session.php +++ b/src/model/session.php @@ -29,7 +29,7 @@ class Session /** @OneToMany(targetEntity="Poll", mappedBy="session", orphanRemoval=true) **/ protected $polls; - /** @ManyToOne(targetEntity="Poll", fetch="EAGER", cascade={"remove"}) **/ + /** @ManyToOne(targetEntity="Poll", cascade={"remove"}) **/ protected $currentPoll; public function getId()