OSDN Git Service

[feature]統計情報のソートカラム選択
authorHabu <habu@users.sourceforge.jp>
Sat, 17 Mar 2018 08:47:53 +0000 (17:47 +0900)
committerHabu <habu@users.sourceforge.jp>
Sat, 17 Mar 2018 08:47:53 +0000 (17:47 +0900)
db_common.inc
popularity_ranking.php

index ec3c4a8..0586ee4 100644 (file)
@@ -222,14 +222,37 @@ EOM
         return $killers;
     }
 
-    private function update_statistics_tables() {
+
+    /**
+     * 統計情報のキャッシュテーブルを更新する
+     *
+     * 種族・職業・性格について各種統計情報を取得しキャッシュテーブルに保存する
+     * 通常の統計情報の取得はこのキャッシュテーブルから行う
+     *
+     * @return boolean 生成に成功したらTRUE、失敗したらFALSE
+     */
+    private function update_statistics_cache_tables() {
         $statistics_list = ['race', 'class', 'personality'];
 
         try {
             foreach ($statistics_list as $stat) {
                 $table_name = $stat."_statistics";
                 $this->dbh->exec("DROP TABLE IF EXISTS ".$table_name);
-                $stmt = $this->dbh->query("CREATE TABLE ".$table_name." AS SELECT ".$stat."_id, count(sex=1 or NULL) AS male_count, count(sex=0 or NULL) AS female_count, count(*) AS total_count, count(winner=1 OR NULL) AS winner_count, avg(score) AS average_score, max(score) AS max_score from scores GROUP BY ".$stat."_id");
+                $stmt = $this->dbh->query(
+                    <<<EOM
+CREATE TABLE $table_name AS
+  SELECT
+    {$stat}_id,
+    count(sex=1 or NULL) AS male_count,
+    count(sex=0 or NULL) AS female_count,
+    count(*) AS total_count,
+    count(winner=1 OR NULL) AS winner_count,
+    avg(score) AS average_score,
+    max(score) AS max_score
+  FROM scores
+GROUP BY ${stat}_id
+EOM
+                );
             }
 
             return TRUE;
@@ -238,17 +261,25 @@ EOM
         }
     }
 
-    public function get_statistics_tables() {
+
+    /**
+     * 統計情報を取得する
+     *
+     * @param integer $sort_key_column 表示順序のキーとするカラムの名称
+     *
+     * @return array 統計情報
+     */
+    public function get_statistics_tables($sort_key_column) {
         if (!$this->get_cache_status('statistics_cache')) {
-            $this->update_statistics_tables();
-            $this->update_cache_status('statistics_cache', 1);
+            $result = $this->update_statistics_cache_tables();
+            $this->update_cache_status('statistics_cache', $result ? 1 : 0);
         }
 
         $stat = [];
 
         $this->dbh->beginTransaction();
         foreach ([['race', 'races'], ['class', 'classes'], ['personality', 'personalities']] as $kind) {
-            $stmt = $this->dbh->query("SELECT ${kind[0]}_id AS id, ${kind[0]}_name AS name, * FROM ${kind[0]}_statistics NATURAL JOIN ${kind[1]} ORDER BY total_count DESC");
+            $stmt = $this->dbh->query("SELECT ${kind[0]}_id AS id, ${kind[0]}_name AS name, * FROM ${kind[0]}_statistics NATURAL JOIN ${kind[1]} ORDER BY ${sort_key_column} DESC");
             $stat[$kind[0]] = $stmt->fetchAll(PDO::FETCH_ASSOC);
         }
         $this->dbh->commit();
index 2a82f89..fa25d27 100644 (file)
@@ -8,22 +8,30 @@ ini_set('zlib.output_compression', 'On');
 
 include "db_common.inc";
 
-function print_popularity_table($stat, $id_name, $name)
+function print_popularity_table($stat, $id_name, $name, $current_sort_key)
 {
     echo <<<EOM
 <table>
 <tr>
 <th>#</th>
 <th>$name</th>
-<th>計</th>
-<th>男性</th>
-<th>女性</th>
-<th>勝利</th>
-<th>平均スコア</th>
-<th>最大スコア</th>
-</tr>
-
 EOM;
+    
+    foreach ([
+        '計' => 'total_count',
+        '男性' => 'male_count',
+        '女性' => 'female_count',
+        '勝利' => 'winner_count',
+        '平均スコア' => 'average_score',
+        '最大スコア' => 'max_score',
+    ] as $name => $sort_key) {
+        if ($sort_key !== $current_sort_key)
+            echo "<th><a href='popularity_ranking.php?sort_key=${sort_key}'>${name}</a></th>";
+        else {
+            echo "<th><strong>${name}</strong></th>";
+        }
+    }
+    echo "</tr>\n";
 
     $rank = 0;
     foreach ($stat as $k => $s) {
@@ -52,7 +60,8 @@ $db = new ScoreDB();
 
 $time_start = microtime(true);
 
-$statistics = $db->get_statistics_tables();
+$sort_key_column = filter_input(INPUT_GET, 'sort_key') ?: 'total_count';
+$statistics = $db->get_statistics_tables($sort_key_column);
 
 $query_time = microtime(true) - $time_start;
 ?>
@@ -74,19 +83,19 @@ echo sprintf("(%.2f 秒)", $query_time);
 <h1>人気のある種族</h1>
 
 <?php
-print_popularity_table($statistics['race'], 'race_id', "種族");
+print_popularity_table($statistics['race'], 'race_id', "種族", $sort_key_column);
 ?>
 
 <hr>
 <h1>人気のある職業</h1>
 
 <?php
-print_popularity_table($statistics['class'], 'class_id', "職業");
+print_popularity_table($statistics['class'], 'class_id', "職業", $sort_key_column);
 ?>
 
 <hr>
 <h1>人気のある性格</h1>
 
 <?php
-print_popularity_table($statistics['personality'], 'personality_id', "性格");
+print_popularity_table($statistics['personality'], 'personality_id', "性格", $sort_key_column);
 ?>