From c8c1cf02a3ccc4dfb25e3e655f8f8c999f91c656 Mon Sep 17 00:00:00 2001 From: rootcoma Date: Sat, 30 Nov 2013 04:29:14 -0800 Subject: [PATCH] Adding paginated top.php top scores --- php/includes/storage.php | 140 +++++++++++++++++++++++++++++++++++---- php/stats.php | 12 ++-- 2 files changed, 135 insertions(+), 17 deletions(-) diff --git a/php/includes/storage.php b/php/includes/storage.php index cf6fede..c671f0b 100644 --- a/php/includes/storage.php +++ b/php/includes/storage.php @@ -147,15 +147,29 @@ return $result; } - public function getDayTopScores() { + public function getDayTopScores($page=1, $max=10) { if(!$this->isConnected()) { throw new StorageConnectionException(); } + if($page < 1) { + $page = 1; + } + if($max < 1) { + $max = 1; + } $day = date('j'); $month = date('m'); $year = date('Y'); - $q = $this->db->prepare("SELECT username, sum(points_made) as points FROM triviauserlog WHERE day=:day AND year=:year AND month=:month GROUP BY username_canonical ORDER BY points DESC LIMIT 10"); - $q->execute(array(':day'=>$day, ':year'=>$year, ':month'=>$month)); + $q = $this->db->prepare("SELECT username, + sum(points_made) as points + FROM triviauserlog + WHERE day=:day + AND year=:year + AND month=:month + GROUP BY username_canonical + ORDER BY points DESC + LIMIT :offset, :maxResults"); + $q->execute(array(':offset'=>($page-1) * $max, ':maxResults'=>$max, ':day'=>$day, ':year'=>$year, ':month'=>$month)); if ($q === false) { throw new StorageSchemaException(); } @@ -163,10 +177,27 @@ return $result; } - public function getWeekTopScores() { + public function getCountDayTopScores() { if(!$this->isConnected()) { throw new StorageConnectionException(); } + $day = date('j'); + $month = date('m'); + $year = date('Y'); + $q = $this->db->prepare('SELECT count(distinct(username_canonical)) + FROM triviauserlog + WHERE day=:day + AND year=:year + AND month=:month'); + $q->execute(array(':day'=>$day, ':year'=>$year, ':month'=>$month)); + if ($q === false) { + throw new StorageSchemaException(); + } + $result = $q->fetchColumn(); + return $result; + } + + protected function generateWeekSqlClause() { $sqlClause = ''; $day = date('N')-1; $week = new DateTime(); @@ -183,7 +214,22 @@ ')'; $week->add($interval); } - $q = $this->db->query("SELECT username, sum(points_made) as points FROM triviauserlog WHERE $sqlClause GROUP BY username_canonical ORDER BY points DESC LIMIT 10"); + return $sqlClause; + } + + public function getWeekTopScores($page=1, $max=10) { + if(!$this->isConnected()) { + throw new StorageConnectionException(); + } + if($page < 1) { + $page = 1; + } + if($max < 1) { + $max = 1; + } + $sqlClause = $this->generateWeekSqlClause(); + $q = $this->db->prepare("SELECT username, sum(points_made) as points FROM triviauserlog WHERE $sqlClause GROUP BY username_canonical ORDER BY points DESC LIMIT :offset, :maxResults"); + $q->execute(array(':offset'=>($page-1) * $max, ':maxResults'=>$max)); if ($q === false) { throw new StorageSchemaException(); } @@ -191,32 +237,93 @@ return $result; } - public function getMonthTopScores() { + public function getCountWeekTopScores() { + if(!$this->isConnected()) { + throw new StorageConnectionException(); + } + $sqlClause = $this->generateWeekSqlClause(); + $q = $this->db->query('SELECT count(distinct(username_canonical)) + FROM triviauserlog + WHERE $sqlClause'); + if ($q === false) { + throw new StorageSchemaException(); + } + $result = $q->fetchColumn(); + return $result; + } + + public function getMonthTopScores($page=1, $max=10) { + if(!$this->isConnected()) { + throw new StorageConnectionException(); + } + if($page < 1) { + $page = 1; + } + if($max < 1) { + $max = 1; + } + $month = date('m'); + $year = date('Y'); + $q = $this->db->prepare("SELECT username, sum(points_made) as points FROM triviauserlog WHERE year=:year AND month=:month GROUP BY username_canonical ORDER BY points DESC LIMIT :offset, :maxResults"); + $q->execute(array(':offset'=>($page-1) * $max, ':maxResults'=>$max, ':year'=>$year, ':month'=>$month)); + if ($q === false) { + throw new StorageSchemaException(); + } + $result = $q->fetchAll(); + return $result; + } + + public function getCountMonthTopScores() { if(!$this->isConnected()) { throw new StorageConnectionException(); } $month = date('m'); $year = date('Y'); - $q = $this->db->prepare("SELECT username, sum(points_made) as points FROM triviauserlog WHERE year=:year AND month=:month GROUP BY username_canonical ORDER BY points DESC LIMIT 10"); + $q = $this->db->prepare('SELECT count(distinct(username_canonical)) + FROM triviauserlog + WHERE year=:year + AND month=:month'); $q->execute(array(':year'=>$year, ':month'=>$month)); if ($q === false) { throw new StorageSchemaException(); } + $result = $q->fetchColumn(); + return $result; + } + + public function getYearTopScores($page=1, $max=10) { + if(!$this->isConnected()) { + throw new StorageConnectionException(); + } + if($page < 1) { + $page = 1; + } + if($max < 1) { + $max = 1; + } + $year = date('Y'); + $q = $this->db->prepare("SELECT username, sum(points_made) as points FROM triviauserlog WHERE year=:year GROUP BY username_canonical ORDER BY points DESC LIMIT :offset, :maxResults"); + $q->execute(array(':offset'=>($page-1) * $max, ':maxResults'=>$max, ':year'=>$year)); + if ($q === false) { + throw new StorageSchemaException(); + } $result = $q->fetchAll(); return $result; } - public function getYearTopScores() { + public function getCountYearTopScores() { if(!$this->isConnected()) { throw new StorageConnectionException(); } $year = date('Y'); - $q = $this->db->prepare("SELECT username, sum(points_made) as points FROM triviauserlog WHERE year=:year GROUP BY username_canonical ORDER BY points DESC LIMIT 10"); + $q = $this->db->prepare('SELECT count(distinct(username_canonical)) + FROM triviauserlog + WHERE year=:year'); $q->execute(array(':year'=>$year)); if ($q === false) { throw new StorageSchemaException(); } - $result = $q->fetchAll(); + $result = $q->fetchColumn(); return $result; } @@ -239,7 +346,8 @@ group by tl.username_canonical limit :offset, :maxResults '); - $q->execute(array(':offset'=>($page-1) * $max, ':maxResults'=>$max, ':username'=>'%'.$usernameCanonical.'%')); + $likeString = '%'.$this->escapeLikeQuery($usernameCanonical).'%'; + $q->execute(array(':offset'=>($page-1) * $max, ':maxResults'=>$max, ':username'=>$likeString)); if ($q === false) { throw new StorageSchemaException(); } @@ -256,7 +364,8 @@ from triviauserlog tl where tl.username_canonical like :username '); - $q->execute(array(':username'=>'%'.$usernameCanonical.'%')); + $likeString = '%'.$this->escapeLikeQuery($usernameCanonical).'%'; + $q->execute(array(':username'=>$likeString)); if ($q === false) { throw new StorageSchemaException(); } @@ -305,12 +414,17 @@ return $result; } - private function isConnected() { + public function isConnected() { if(is_null($this->db)) { return false; } return true; } + + protected function escapeLikeQuery($s) { + $translations = array("%"=>"\\%", "_"=>"\\_"); + return strtr($s, $translations); + } } class StorageException extends Exception { } diff --git a/php/stats.php b/php/stats.php index b02ec52..620e394 100644 --- a/php/stats.php +++ b/php/stats.php @@ -58,7 +58,7 @@ try { getDayTopScores(); + $result = $storage->getDayTopScores(1, 10); } catch(StorageSchemaException $e) { echo "
Error: Database schema is not queryable
"; } catch(StorageConnectionException $e) { @@ -85,6 +85,7 @@ try { ?> +

View all

@@ -92,7 +93,7 @@ try { getWeekTopScores(); + $result = $storage->getWeekTopScores(1, 10); } catch(StorageSchemaException $e) { echo "
Error: Database schema is not queryable
"; } catch(StorageConnectionException $e) { @@ -119,6 +120,7 @@ try { ?> +

View all

@@ -127,7 +129,7 @@ try { getMonthTopScores(); + $result = $storage->getMonthTopScores(1, 10); } catch(StorageSchemaException $e) { echo "
Error: Database schema is not queryable
"; } catch(StorageConnectionException $e) { @@ -154,13 +156,14 @@ try { ?> +

View all

Year Top Scores

getYearTopScores(); + $result = $storage->getYearTopScores(1, 10); } catch(StorageSchemaException $e) { echo "
Error: Database schema is not queryable
"; } catch(StorageConnectionException $e) { @@ -188,6 +191,7 @@ try { ?> +

View all