From 08090f2cbab6af3b9b99ba08a1bdfd831288c0d5 Mon Sep 17 00:00:00 2001
From: Thomas Steur <tsteur@users.noreply.github.com>
Date: Tue, 19 Apr 2016 14:45:48 +1200
Subject: [PATCH] minor UserId tweaks, enable row evolution for userId etc
 (#10072)

---
 plugins/UserId/API.php                  | 13 ++++------
 plugins/UserId/Archiver.php             |  7 ++---
 plugins/UserId/Columns/UserId.php       | 29 +++++++++++++++++++++
 plugins/UserId/Reports/GetUsers.php     | 34 +++++++++++++++----------
 plugins/UserId/javascripts/rowaction.js |  1 +
 plugins/UserId/lang/en.json             |  6 +++++
 6 files changed, 65 insertions(+), 25 deletions(-)
 create mode 100644 plugins/UserId/Columns/UserId.php
 create mode 100644 plugins/UserId/lang/en.json

diff --git a/plugins/UserId/API.php b/plugins/UserId/API.php
index 6e29c3afc9..932e19b113 100644
--- a/plugins/UserId/API.php
+++ b/plugins/UserId/API.php
@@ -22,20 +22,17 @@ use Piwik\DataTable\Row;
 class API extends \Piwik\Plugin\API
 {
     /**
-     * Get DataTable with User Ids and some aggregated data. Supports pagination, sorting
-     * and filtering by user_id
+     * Get a report of all User Ids.
      *
      * @param int $idSite
      *
-     * @param     $period
-     * @param     $date
-     * @param     $segment
-     * @param     $expanded
-     * @param     $flat
+     * @param string  $period
+     * @param int  $date
+     * @param string|bool  $segment
      *
      * @return DataTable
      */
-    public function getUsers($idSite, $period, $date, $segment = false, $expanded = false, $flat = false)
+    public function getUsers($idSite, $period, $date, $segment = false)
     {
         Piwik::checkUserHasViewAccess($idSite);
         $archive = Archive::build($idSite, $period, $date, $segment);
diff --git a/plugins/UserId/Archiver.php b/plugins/UserId/Archiver.php
index 3e20dcf141..28c32bf3c6 100644
--- a/plugins/UserId/Archiver.php
+++ b/plugins/UserId/Archiver.php
@@ -77,12 +77,13 @@ class Archiver extends \Piwik\Plugin\Archiver
         $userIdFieldName = self::USER_ID_FIELD;
         $visitorIdFieldName = self::VISITOR_ID_FIELD;
 
-        /** @var Zend_Db_Statement $query */
+        /** @var \Zend_Db_Statement $query */
         $query = $this->getLogAggregator()->queryVisitsByDimension(
             array(self::USER_ID_FIELD),
             "$userIdFieldName IS NOT NULL AND $userIdFieldName != ''",
             array("LOWER(HEX($visitorIdFieldName)) as $visitorIdFieldName")
         );
+
         if ($query === false) {
             return;
         }
@@ -114,7 +115,7 @@ class Archiver extends \Piwik\Plugin\Archiver
      *
      * @param array $row
      */
-    protected function rememberVisitorId($row)
+    private function rememberVisitorId($row)
     {
         if (!empty($row[self::USER_ID_FIELD]) && !empty($row[self::VISITOR_ID_FIELD])) {
             $this->visitorIdsUserIdsMap[$row[self::USER_ID_FIELD]] = $row[self::VISITOR_ID_FIELD];
@@ -126,7 +127,7 @@ class Archiver extends \Piwik\Plugin\Archiver
      *
      * @param DataTable $dataTable
      */
-    protected function setVisitorIds(DataTable $dataTable)
+    private function setVisitorIds(DataTable $dataTable)
     {
         foreach ($dataTable->getRows() as $row) {
             $userId = $row->getColumn('label');
diff --git a/plugins/UserId/Columns/UserId.php b/plugins/UserId/Columns/UserId.php
new file mode 100644
index 0000000000..7ac474cf73
--- /dev/null
+++ b/plugins/UserId/Columns/UserId.php
@@ -0,0 +1,29 @@
+<?php
+/**
+ * Piwik - free/libre analytics platform
+ *
+ * @link http://piwik.org
+ * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
+ *
+ */
+namespace Piwik\Plugins\UserId\Columns;
+
+use Piwik\Piwik;
+use Piwik\Plugin\Dimension\VisitDimension;
+
+/**
+ * UserId dimension
+ */
+class UserId extends VisitDimension
+{
+
+    /**
+     * The name of the dimension which will be visible for instance in the UI of a related report and in the mobile app.
+     * @return string
+     */
+    public function getName()
+    {
+        return Piwik::translate('UserId_UserId');
+    }
+
+}
\ No newline at end of file
diff --git a/plugins/UserId/Reports/GetUsers.php b/plugins/UserId/Reports/GetUsers.php
index c5914776c5..aff063541c 100644
--- a/plugins/UserId/Reports/GetUsers.php
+++ b/plugins/UserId/Reports/GetUsers.php
@@ -11,6 +11,8 @@ namespace Piwik\Plugins\UserId\Reports;
 use Piwik\Piwik;
 use Piwik\Plugin\Report;
 use Piwik\Plugin\ViewDataTable;
+use Piwik\Plugins\CoreVisualizations\Visualizations\HtmlTable;
+use Piwik\Plugins\UserId\Columns\UserId;
 use Piwik\View;
 
 /**
@@ -19,26 +21,27 @@ use Piwik\View;
  */
 class GetUsers extends Base
 {
-    /**
-     * @return array
-     */
-    public static function getColumnsToDisplay()
-    {
-        return array(
-            'label', 'nb_visits', 'nb_actions', 'nb_visits_converted'
-        );
-    }
-
     protected function init()
     {
         parent::init();
 
-        $this->name          = Piwik::translate('UsersManager_MenuUsers');
+        $this->name          = Piwik::translate('UserId_UserReportTitle');
         $this->menuTitle     = $this->name;
+        $this->widgetTitle   = $this->menuTitle;
         $this->documentation = '';
+        $this->dimension = new UserId();
+        $this->metrics = array('label', 'nb_visits', 'nb_actions', 'nb_visits_converted');
 
         // This defines in which order your report appears in the mobile app, in the menu and in the list of widgets
-        $this->order = 1;
+        $this->order = 9;
+    }
+
+    /**
+     * @return array
+     */
+    public static function getColumnsToDisplay()
+    {
+        return ;
     }
 
     /**
@@ -52,14 +55,17 @@ class GetUsers extends Base
         /*
          * Hide most of the table footer actions, leaving only export icons and pagination
          */
-        $view->config->columns_to_display = $this->getColumnsToDisplay();
+        $view->config->columns_to_display = $this->metrics;
         $view->config->show_all_views_icons = false;
         $view->config->show_active_view_icon = false;
         $view->config->show_related_reports = false;
         $view->config->show_insights = false;
         $view->config->show_pivot_by_subtable = false;
         $view->config->show_flatten_table = false;
-        $view->config->disable_row_evolution = true;
+
+        if ($view->isViewDataTableId(HtmlTable::ID)) {
+            $view->config->disable_row_evolution = false;
+        }
 
         // exclude users with less then 2 visits, when low population filter is active
         $view->requestConfig->filter_excludelowpop_value = 2;
diff --git a/plugins/UserId/javascripts/rowaction.js b/plugins/UserId/javascripts/rowaction.js
index 5fba72dbe4..d100c1f3f5 100644
--- a/plugins/UserId/javascripts/rowaction.js
+++ b/plugins/UserId/javascripts/rowaction.js
@@ -22,6 +22,7 @@
 
     DataTable_RowActions_VisitorDetails.prototype.performAction = function (label, tr, e) {
         var visitorId = this.getRowMetadata($(tr)).idvisitor || '';
+        visitorId = encodeURIComponent(visitorId);
         if (visitorId.length > 0) {
             DataTable_RowAction.prototype.openPopover.apply(this, ['module=Live&action=getVisitorProfilePopup&visitorId=' + visitorId]);
         }
diff --git a/plugins/UserId/lang/en.json b/plugins/UserId/lang/en.json
new file mode 100644
index 0000000000..ec320c4079
--- /dev/null
+++ b/plugins/UserId/lang/en.json
@@ -0,0 +1,6 @@
+{
+    "UserId": {
+        "UserId": "UserId",
+        "UserReportTitle": "Users"
+    }
+}
\ No newline at end of file
-- 
GitLab