From 0d95d2b9f2d6b0440ab2803930e7f68c4816f37d Mon Sep 17 00:00:00 2001
From: Thomas Steur <thomas.steur@gmail.com>
Date: Mon, 26 Jan 2015 04:05:11 +0000
Subject: [PATCH] refs #6705 one way to detect whether user feature is enabled
 for a given site. looking for other solutions as well

---
 plugins/CoreHome/Columns/UserId.php   | 24 ++++++++++++++++++++++++
 plugins/VisitsSummary/Reports/Get.php | 23 +++++++++++++++++++++++
 2 files changed, 47 insertions(+)

diff --git a/plugins/CoreHome/Columns/UserId.php b/plugins/CoreHome/Columns/UserId.php
index 55fea6b517..ee7512444c 100644
--- a/plugins/CoreHome/Columns/UserId.php
+++ b/plugins/CoreHome/Columns/UserId.php
@@ -9,6 +9,7 @@
 namespace Piwik\Plugins\CoreHome\Columns;
 
 use Piwik\Plugin\Dimension\VisitDimension;
+use Piwik\Plugins\VisitsSummary\API as VisitsSummaryApi;
 use Piwik\Tracker\Request;
 use Piwik\Tracker\Visitor;
 use Piwik\Tracker\Action;
@@ -51,4 +52,27 @@ class UserId extends VisitDimension
         return $request->getForcedUserId();
     }
 
+    public function isUsedInAtLeastOneSite($idSites, $period, $date)
+    {
+        if ($period === 'day' || $period === 'week') {
+            $period = 'month';
+        }
+
+        foreach ($idSites as $idSite) {
+            $result = VisitsSummaryApi::getInstance()->get($idSite, $period, $date, false, 'nb_users');
+
+            if (!$result->getRowsCount()) {
+                continue;
+            }
+
+            $numUsers = $result->getFirstRow()->getColumn('nb_users');
+
+            if (!empty($numUsers)) {
+                return true;
+            }
+        }
+
+        return false;
+    }
+
 }
\ No newline at end of file
diff --git a/plugins/VisitsSummary/Reports/Get.php b/plugins/VisitsSummary/Reports/Get.php
index f2a087a517..9df37d9bf5 100644
--- a/plugins/VisitsSummary/Reports/Get.php
+++ b/plugins/VisitsSummary/Reports/Get.php
@@ -12,6 +12,7 @@ use Piwik\Piwik;
 use Piwik\Plugins\CoreHome\Columns\Metrics\ActionsPerVisit;
 use Piwik\Plugins\CoreHome\Columns\Metrics\AverageTimeOnSite;
 use Piwik\Plugins\CoreHome\Columns\Metrics\BounceRate;
+use Piwik\Plugins\CoreHome\Columns\UserId;
 
 class Get extends \Piwik\Plugin\Report
 {
@@ -39,6 +40,28 @@ class Get extends \Piwik\Plugin\Report
         $this->order = 1;
     }
 
+    public function configureReportMetadata(&$availableReports, $infos)
+    {
+        if (!$this->isEnabled()) {
+            return;
+        }
+
+        if (!empty($infos['idSites']) && !empty($infos['period']) && !empty($infos['date'])) {
+            $userId = new UserId();
+            $isUserIdUsed = $userId->isUsedInAtLeastOneSite($infos['idSites'], $infos['period'], $infos['date']);
+
+            if (!$isUserIdUsed) {
+                $key = array_search('nb_users', $this->metrics);
+                if (false !== $key) {
+                    unset($this->metrics[$key]);
+                    $this->metrics = array_values($this->metrics);
+                }
+            };
+        }
+
+        parent::configureReportMetadata($availableReports, $infos);
+    }
+
     public function getMetrics()
     {
         $metrics = parent::getMetrics();
-- 
GitLab