Merge pull request #193 from rootcoma/top_scores
Adding paginated top.php top scores
This commit is contained in:
commit
ab2dcbb5e9
|
|
@ -5,4 +5,5 @@
|
|||
<FilesMatch "config\.php">
|
||||
Order Allow,Deny
|
||||
Deny from all
|
||||
</FilesMatch>
|
||||
</FilesMatch>
|
||||
AcceptPathInfo Off
|
||||
|
|
|
|||
|
|
@ -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 { }
|
||||
|
|
|
|||
|
|
@ -58,7 +58,7 @@ try {
|
|||
<?php
|
||||
$result = array();
|
||||
try {
|
||||
$result = $storage->getDayTopScores();
|
||||
$result = $storage->getDayTopScores(1, 10);
|
||||
} catch(StorageSchemaException $e) {
|
||||
echo "<div class='alert alert-error'>Error: Database schema is not queryable</div>";
|
||||
} catch(StorageConnectionException $e) {
|
||||
|
|
@ -85,6 +85,7 @@ try {
|
|||
?>
|
||||
</tbody>
|
||||
</table>
|
||||
<p><a class="btn btn-info btn-block" href="top.php">View all</a></p>
|
||||
</div>
|
||||
|
||||
<div class="span6">
|
||||
|
|
@ -92,7 +93,7 @@ try {
|
|||
<?php
|
||||
$result = array();
|
||||
try {
|
||||
$result = $storage->getWeekTopScores();
|
||||
$result = $storage->getWeekTopScores(1, 10);
|
||||
} catch(StorageSchemaException $e) {
|
||||
echo "<div class='alert alert-error'>Error: Database schema is not queryable</div>";
|
||||
} catch(StorageConnectionException $e) {
|
||||
|
|
@ -119,6 +120,7 @@ try {
|
|||
?>
|
||||
</tbody>
|
||||
</table>
|
||||
<p><a class="btn btn-info btn-block" href="top.php?t=w">View all</a></p>
|
||||
</div>
|
||||
</div>
|
||||
<div class="row">
|
||||
|
|
@ -127,7 +129,7 @@ try {
|
|||
<?php
|
||||
$result = array();
|
||||
try {
|
||||
$result = $storage->getMonthTopScores();
|
||||
$result = $storage->getMonthTopScores(1, 10);
|
||||
} catch(StorageSchemaException $e) {
|
||||
echo "<div class='alert alert-error'>Error: Database schema is not queryable</div>";
|
||||
} catch(StorageConnectionException $e) {
|
||||
|
|
@ -154,13 +156,14 @@ try {
|
|||
?>
|
||||
</tbody>
|
||||
</table>
|
||||
<p><a class="btn btn-info btn-block" href="top.php?t=m">View all</a></p>
|
||||
</div>
|
||||
<div class="span6">
|
||||
<h2>Year Top Scores</h2>
|
||||
<?php
|
||||
$result = array();
|
||||
try {
|
||||
$result = $storage->getYearTopScores();
|
||||
$result = $storage->getYearTopScores(1, 10);
|
||||
} catch(StorageSchemaException $e) {
|
||||
echo "<div class='alert alert-error'>Error: Database schema is not queryable</div>";
|
||||
} catch(StorageConnectionException $e) {
|
||||
|
|
@ -188,6 +191,7 @@ try {
|
|||
?>
|
||||
</tbody>
|
||||
</table>
|
||||
<p><a class="btn btn-info btn-block" href="top.php?t=y">View all</a></p>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
|
|
|
|||
|
|
@ -0,0 +1,173 @@
|
|||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<?php
|
||||
include('config.php');
|
||||
include('includes/pagination.php');
|
||||
include('includes/storage.php');
|
||||
try {
|
||||
$storage = new Storage($config['dbLocation']);
|
||||
} catch(StorageException $e) {
|
||||
|
||||
}
|
||||
$timespans = array('d'=>'Day', 'w'=>'Week', 'm'=>'Month', 'y'=>'Year');
|
||||
$timespan = 'd';
|
||||
$timeDesc = 'Day';
|
||||
if(array_key_exists('t', $_GET)) {
|
||||
if(array_key_exists(strtolower($_GET['t']), $timespans)) {
|
||||
$timespan = strtolower($_GET['t']);
|
||||
$timeDesc = $timespans[$timespan];
|
||||
}
|
||||
}
|
||||
|
||||
if(array_key_exists('page', $_GET)) {
|
||||
$page = $_GET['page'];
|
||||
}
|
||||
if(!isset($page)) {
|
||||
$page = 1;
|
||||
}
|
||||
if($page < 1) {
|
||||
$page = 1;
|
||||
}
|
||||
|
||||
$maxResults = 20;
|
||||
|
||||
function replaceTimespanVariable($t) {
|
||||
$pathInfo = parse_url($_SERVER['REQUEST_URI']);
|
||||
if(array_key_exists('query', $pathInfo)) {
|
||||
$queryString = $pathInfo['query'];
|
||||
} else {
|
||||
$queryString = '';
|
||||
}
|
||||
parse_str($queryString, $queryArray);
|
||||
$queryArray['t'] = $t;
|
||||
if($t == 'd') {
|
||||
unset($queryArray['t']);
|
||||
}
|
||||
$queryArray['page'] = 1;
|
||||
unset($queryArray['page']);
|
||||
$queryString = http_build_query($queryArray);
|
||||
$new = $pathInfo['path'];
|
||||
if($queryString != ''){
|
||||
$new .= '?' . $queryString;
|
||||
}
|
||||
return $new;
|
||||
}
|
||||
|
||||
?>
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<title>Top Scores for <?php echo $timeDesc; ?> · TriviaTime</title>
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<meta name="description" content="">
|
||||
<meta name="author" content="">
|
||||
|
||||
<link href="css/bootstrap.css" rel="stylesheet">
|
||||
<link href="css/triviatime.css" rel="stylesheet">
|
||||
<link href="css/bootstrap-responsive.css" rel="stylesheet">
|
||||
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<div class="navbar navbar-inverse navbar-fixed-top">
|
||||
<div class="navbar-inner">
|
||||
<div class="container">
|
||||
<button class="btn btn-navbar collapsed" data-toggle="collapse" data-target=".nav-collapse" type="button">
|
||||
<span class="icon-bar"></span>
|
||||
<span class="icon-bar"></span>
|
||||
<span class="icon-bar"></span>
|
||||
</button>
|
||||
<a href="index.php" class="brand">TriviaTime</a>
|
||||
<div class="nav-collapse collapse">
|
||||
<ul class="nav">
|
||||
<li><a href="index.php">Home</a></li>
|
||||
<li><a href="stats.php">Stats</a></li>
|
||||
<li><a href="user.php">Players</a></li>
|
||||
<li><a href="reports.php">Reports</a></li>
|
||||
<li><a href="about.php">About</a></li>
|
||||
</ul>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div><!-- /.navbar -->
|
||||
<div class="container">
|
||||
<div class="hero-unit">
|
||||
<h1>Top Scores</h1>
|
||||
<p>Player rankings.</p>
|
||||
</div>
|
||||
<div class="row">
|
||||
<div class="span12">
|
||||
<ul class="nav nav-pills">
|
||||
<li<?php if($timespan=='d') { echo ' class="active"'; } ?>><a href="<?php echo replaceTimespanVariable('d'); ?>">Day</a></li>
|
||||
<li<?php if($timespan=='w') { echo ' class="active"'; } ?>><a href="<?php echo replaceTimespanVariable('w'); ?>">Week</a></li>
|
||||
<li<?php if($timespan=='m') { echo ' class="active"'; } ?>><a href="<?php echo replaceTimespanVariable('m'); ?>">Month</a></li>
|
||||
<li<?php if($timespan=='y') { echo ' class="active"'; } ?>><a href="<?php echo replaceTimespanVariable('y'); ?>">Year</a></li>
|
||||
</ul>
|
||||
</div>
|
||||
</div>
|
||||
<div class="row">
|
||||
<div class="span6">
|
||||
<h2>Top Scores for <?php echo $timeDesc; ?></h2>
|
||||
<?php
|
||||
$resultCount = 0;
|
||||
$result = array();
|
||||
try {
|
||||
if ($timespan == 'w') {
|
||||
$result = $storage->getWeekTopScores($page, $maxResults);
|
||||
$resultCount = $storage->getCountWeekTopScores();
|
||||
} else if ($timespan == 'm') {
|
||||
$result = $storage->getMonthTopScores($page, $maxResults);
|
||||
$resultCount = $storage->getCountMonthTopScores();
|
||||
} else if ($timespan == 'y') {
|
||||
$result = $storage->getYearTopScores($page, $maxResults);
|
||||
$resultCount = $storage->getCountYearTopScores();
|
||||
} else {
|
||||
$result = $storage->getDayTopScores($page, $maxResults);
|
||||
$resultCount = $storage->getCountDayTopScores();
|
||||
}
|
||||
} catch(StorageSchemaException $e) {
|
||||
echo "<div class='alert alert-error'>Error: Database schema is not queryable</div>";
|
||||
} catch(StorageConnectionException $e) {
|
||||
echo "<div class='alert alert-error'>Error: Database is not available</div>";
|
||||
}
|
||||
$storage->close();
|
||||
?>
|
||||
<table class="table">
|
||||
<thead>
|
||||
<tr>
|
||||
<th>#</th>
|
||||
<th>Username</th>
|
||||
<th>Score</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<?php
|
||||
$currentRank = ($page-1)*$maxResults + 1;
|
||||
foreach($result as $res) {
|
||||
echo '<tr>';
|
||||
echo '<td>' . $currentRank . '</td>';
|
||||
echo '<td><a href="profile.php?username=' . $res['username'] . '">' . $res['username'] . '</a></td>';
|
||||
echo '<td>' . number_format($res['points'],0) . '</td>';
|
||||
echo '</tr>';
|
||||
$currentRank++;
|
||||
}
|
||||
?>
|
||||
</tbody>
|
||||
</table>
|
||||
<?php
|
||||
$pagination = new Paginator($page, $resultCount, $maxResults);
|
||||
$pagination->paginate();
|
||||
?>
|
||||
</div>
|
||||
<div class="offset6"></div>
|
||||
</div>
|
||||
<div class="footer">
|
||||
<p>© Trivialand 2013 - <a target="_blank" href="https://github.com/tannn/TriviaTime">Github</a></p>
|
||||
</div>
|
||||
|
||||
</div> <!-- /container -->
|
||||
|
||||
<script src="http://codeorigin.jquery.com/jquery-2.0.3.min.js"></script>
|
||||
<script src="js/bootstrap.min.js"></script>
|
||||
|
||||
</body>
|
||||
</html>
|
||||
Loading…
Reference in New Issue