From c95a2a558c1d91d2d142d91ffc1f6d4beacb5b2c Mon Sep 17 00:00:00 2001
From: Demian Katz <demian.katz@villanova.edu>
Date: Wed, 30 Jan 2013 14:12:12 -0500
Subject: [PATCH] Added new admin module action to display statistics on usage
 of social features.

---
 module/VuFind/config/module.config.php        |  4 +--
 .../src/VuFind/Controller/AdminController.php | 14 +++++++++
 .../VuFind/src/VuFind/Db/Table/Comments.php   | 27 ++++++++++++++++
 .../src/VuFind/Db/Table/ResourceTags.php      | 26 ++++++++++++++++
 .../src/VuFind/Db/Table/UserResource.php      | 30 ++++++++++++++++++
 themes/blueprint/templates/admin/menu.phtml   |  9 +++---
 .../templates/admin/socialstats.phtml         | 31 +++++++++++++++++++
 7 files changed, 135 insertions(+), 6 deletions(-)
 create mode 100644 themes/blueprint/templates/admin/socialstats.phtml

diff --git a/module/VuFind/config/module.config.php b/module/VuFind/config/module.config.php
index e1d4563a06a..ef918e24e89 100644
--- a/module/VuFind/config/module.config.php
+++ b/module/VuFind/config/module.config.php
@@ -576,8 +576,8 @@ $listRoutes = array('userList' => 'MyList', 'editList' => 'EditList');
 // Define static routes -- Controller/Action strings
 $staticRoutes = array(
     'Admin/Config', 'Admin/DeleteExpiredSearches', 'Admin/EnableAutoConfig',
-    'Admin/Home', 'Admin/Maintenance', 'Admin/Statistics', 'Alphabrowse/Home',
-    'Author/Home', 'Author/Search',
+    'Admin/Home', 'Admin/Maintenance', 'Admin/SocialStats', 'Admin/Statistics',
+    'Alphabrowse/Home', 'Author/Home', 'Author/Search',
     'Authority/Home', 'Authority/Record', 'Authority/Search',
     'Browse/Author', 'Browse/Dewey', 'Browse/Era', 'Browse/Genre', 'Browse/Home',
     'Browse/LCC', 'Browse/Region', 'Browse/Tag', 'Browse/Topic',
diff --git a/module/VuFind/src/VuFind/Controller/AdminController.php b/module/VuFind/src/VuFind/Controller/AdminController.php
index 93b78d5ae9c..86d1b651abb 100644
--- a/module/VuFind/src/VuFind/Controller/AdminController.php
+++ b/module/VuFind/src/VuFind/Controller/AdminController.php
@@ -139,6 +139,20 @@ class AdminController extends AbstractBase
         return $view;
     }
 
+    /**
+     * Social statistics reporting
+     *
+     * @return \Zend\View\Model\ViewModel
+     */
+    public function socialstatsAction()
+    {
+        $view = $this->createViewModel();
+        $view->comments = $this->getTable('comments')->getStatistics();
+        $view->favorites = $this->getTable('userresource')->getStatistics();
+        $view->tags = $this->getTable('resourcetags')->getStatistics();
+        return $view;
+    }
+
     /**
      * Statistics reporting
      *
diff --git a/module/VuFind/src/VuFind/Db/Table/Comments.php b/module/VuFind/src/VuFind/Db/Table/Comments.php
index 41fc9071842..5c412c9fbc3 100644
--- a/module/VuFind/src/VuFind/Db/Table/Comments.php
+++ b/module/VuFind/src/VuFind/Db/Table/Comments.php
@@ -26,6 +26,7 @@
  * @link     http://vufind.org   Main Site
  */
 namespace VuFind\Db\Table;
+use Zend\Db\Sql\Expression;
 
 /**
  * Table Definition for comments
@@ -105,4 +106,30 @@ class Comments extends Gateway
         $row->delete();
         return true;
     }
+
+    /**
+     * Get statistics on use of comments.
+     *
+     * @return array
+     */
+    public function getStatistics()
+    {
+        $select = $this->sql->select();
+        $select->columns(
+            array(
+                'users' => new Expression(
+                    'COUNT(DISTINCT(?))', array('user_id'),
+                    array(Expression::TYPE_IDENTIFIER)
+                ),
+                'resources' => new Expression(
+                    'COUNT(DISTINCT(?))', array('resource_id'),
+                    array(Expression::TYPE_IDENTIFIER)
+                ),
+                'total' => new Expression('COUNT(*)')
+            )
+        );
+        $statement = $this->sql->prepareStatementForSqlObject($select);
+        $result = $statement->execute();
+        return (array)$result->current();
+    }
 }
diff --git a/module/VuFind/src/VuFind/Db/Table/ResourceTags.php b/module/VuFind/src/VuFind/Db/Table/ResourceTags.php
index b1085ed22fa..fb74c103bd7 100644
--- a/module/VuFind/src/VuFind/Db/Table/ResourceTags.php
+++ b/module/VuFind/src/VuFind/Db/Table/ResourceTags.php
@@ -158,6 +158,32 @@ class ResourceTags extends Gateway
         return $this->select($callback);
     }
 
+    /**
+     * Get statistics on use of tags.
+     *
+     * @return array
+     */
+    public function getStatistics()
+    {
+        $select = $this->sql->select();
+        $select->columns(
+            array(
+                'users' => new Expression(
+                    'COUNT(DISTINCT(?))', array('user_id'),
+                    array(Expression::TYPE_IDENTIFIER)
+                ),
+                'resources' => new Expression(
+                    'COUNT(DISTINCT(?))', array('resource_id'),
+                    array(Expression::TYPE_IDENTIFIER)
+                ),
+                'total' => new Expression('COUNT(*)')
+            )
+        );
+        $statement = $this->sql->prepareStatementForSqlObject($select);
+        $result = $statement->execute();
+        return (array)$result->current();
+    }
+
     /**
      * Unlink rows for the specified resource.
      *
diff --git a/module/VuFind/src/VuFind/Db/Table/UserResource.php b/module/VuFind/src/VuFind/Db/Table/UserResource.php
index 4fef8686756..a619cbfd180 100644
--- a/module/VuFind/src/VuFind/Db/Table/UserResource.php
+++ b/module/VuFind/src/VuFind/Db/Table/UserResource.php
@@ -163,4 +163,34 @@ class UserResource extends Gateway
         // Delete the rows:
         $this->delete($callback);
     }
+
+    /**
+     * Get statistics on use of lists.
+     *
+     * @return array
+     */
+    public function getStatistics()
+    {
+        $select = $this->sql->select();
+        $select->columns(
+            array(
+                'users' => new Expression(
+                    'COUNT(DISTINCT(?))', array('user_id'),
+                    array(Expression::TYPE_IDENTIFIER)
+                ),
+                'lists' => new Expression(
+                    'COUNT(DISTINCT(?))', array('list_id'),
+                    array(Expression::TYPE_IDENTIFIER)
+                ),
+                'resources' => new Expression(
+                    'COUNT(DISTINCT(?))', array('resource_id'),
+                    array(Expression::TYPE_IDENTIFIER)
+                ),
+                'total' => new Expression('COUNT(*)')
+            )
+        );
+        $statement = $this->sql->prepareStatementForSqlObject($select);
+        $result = $statement->execute();
+        return (array)$result->current();
+    }
 }
diff --git a/themes/blueprint/templates/admin/menu.phtml b/themes/blueprint/templates/admin/menu.phtml
index 85338e43d69..b3c51308725 100644
--- a/themes/blueprint/templates/admin/menu.phtml
+++ b/themes/blueprint/templates/admin/menu.phtml
@@ -1,6 +1,7 @@
 <ul id="list1">
-  <li<?=ucwords($this->layout()->templateName) == "Home" ? ' class="active"' : ''?>><a href="<?=$this->url('admin-home')?>"><?=$this->transEsc('Home')?></a></li>
-  <li<?=ucwords($this->layout()->templateName) == "Statistics" ? ' class="active"' : ''?>><a href="<?=$this->url('admin-statistics')?>"><?=$this->transEsc('Statistics')?></a></li>
-  <li<?=ucwords($this->layout()->templateName) == "Config" ? ' class="active"' : ''?>><a href="<?=$this->url('admin-config')?>"><?=$this->transEsc('Configuration')?></a>
-  <li<?=ucwords($this->layout()->templateName) == "Maintenance" ? ' class="active"' : ''?>><a href="<?=$this->url('admin-maintenance')?>"><?=$this->transEsc('System Maintenance')?></a></li>
+  <li<?=strtolower($this->layout()->templateName) == "home" ? ' class="active"' : ''?>><a href="<?=$this->url('admin-home')?>"><?=$this->transEsc('Home')?></a></li>
+  <li<?=strtolower($this->layout()->templateName) == "socialstats" ? ' class="active"' : ''?>><a href="<?=$this->url('admin-socialstats')?>"><?=$this->transEsc('Social Statistics')?></a></li>
+  <li<?=strtolower($this->layout()->templateName) == "statistics" ? ' class="active"' : ''?>><a href="<?=$this->url('admin-statistics')?>"><?=$this->transEsc('Statistics')?></a></li>
+  <li<?=strtolower($this->layout()->templateName) == "config" ? ' class="active"' : ''?>><a href="<?=$this->url('admin-config')?>"><?=$this->transEsc('Configuration')?></a>
+  <li<?=strtolower($this->layout()->templateName) == "maintenance" ? ' class="active"' : ''?>><a href="<?=$this->url('admin-maintenance')?>"><?=$this->transEsc('System Maintenance')?></a></li>
 </ul>
diff --git a/themes/blueprint/templates/admin/socialstats.phtml b/themes/blueprint/templates/admin/socialstats.phtml
new file mode 100644
index 00000000000..be43c79a91c
--- /dev/null
+++ b/themes/blueprint/templates/admin/socialstats.phtml
@@ -0,0 +1,31 @@
+<?
+    // Set page title.
+    $this->headTitle($this->translate('VuFind Administration - Social Statistics'));
+?>
+<div class="span-5">
+  <?=$this->render("admin/menu.phtml")?>
+</div>
+
+<div class="span-18 last">
+  <h1><?=$this->transEsc('Social Statistics')?></h1>
+
+  <h2>Comments</h2>
+  <table>
+    <tr><th>Total Users</th><th>Total Resources</th><th>Total Comments</th></tr>
+    <tr><td><?=$comments['users']?></td><td><?=$comments['resources']?></td><td><?=$comments['total']?></td></tr>
+  </table>
+
+  <h2>Favorites</h2>
+  <table>
+    <tr><th>Total Users</th><th>Total Resources</th><th>Total Lists</th><th>Total Saved Items</th></tr>
+    <tr><td><?=$favorites['users']?></td><td><?=$favorites['resources']?></td><td><?=$favorites['lists']?></td><td><?=$favorites['total']?></td></tr>
+  </table>
+
+  <h2>Tags</h2>
+  <table>
+    <tr><th>Total Users</th><th>Total Resources</th><th>Total Tags</th></tr>
+    <tr><td><?=$tags['users']?></td><td><?=$tags['resources']?></td><td><?=$tags['total']?></td></tr>
+  </table>
+</div>
+
+<div class="clear"></div>
-- 
GitLab