From 7f33cf6ef5f3fbe576c3d56813447eed91de75bd Mon Sep 17 00:00:00 2001 From: Frederic Guillot Date: Sat, 25 Jul 2015 17:07:07 -0400 Subject: Fix bug: avoid the creation of multiple subtask timer for the same task and user --- app/Model/SubtaskTimeTracking.php | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) (limited to 'app') diff --git a/app/Model/SubtaskTimeTracking.php b/app/Model/SubtaskTimeTracking.php index 51743198..9f17ee3f 100644 --- a/app/Model/SubtaskTimeTracking.php +++ b/app/Model/SubtaskTimeTracking.php @@ -29,7 +29,7 @@ class SubtaskTimeTracking extends Base public function getTimerQuery($user_id) { return sprintf( - "SELECT %s FROM %s WHERE %s='%d' AND %s='0' AND %s=%s", + "SELECT %s FROM %s WHERE %s='%d' AND %s='0' AND %s=%s LIMIT 1", $this->db->escapeIdentifier('start'), $this->db->escapeIdentifier(self::TABLE), $this->db->escapeIdentifier('user_id'), @@ -226,6 +226,19 @@ class SubtaskTimeTracking extends Base return $events; } + /** + * Return true if a timer is started for this use and subtask + * + * @access public + * @param integer $subtask_id + * @param integer $user_id + * @return boolean + */ + public function hasTimer($subtask_id, $user_id) + { + return $this->db->table(self::TABLE)->eq('subtask_id', $subtask_id)->eq('user_id', $user_id)->eq('end', 0)->exists(); + } + /** * Log start time * @@ -236,9 +249,11 @@ class SubtaskTimeTracking extends Base */ public function logStartTime($subtask_id, $user_id) { - return $this->db - ->table(self::TABLE) - ->insert(array('subtask_id' => $subtask_id, 'user_id' => $user_id, 'start' => time(), 'end' => 0)); + return + ! $this->hasTimer($subtask_id, $user_id) && + $this->db + ->table(self::TABLE) + ->insert(array('subtask_id' => $subtask_id, 'user_id' => $user_id, 'start' => time(), 'end' => 0)); } /** -- cgit v1.2.3