Skip to content

Commit

Permalink
fixes #4992 added support for filter_sort_order in Live.getLastVisits…
Browse files Browse the repository at this point in the history
…Details API
  • Loading branch information
tsteur committed May 6, 2014
1 parent c828a73 commit 95d93ac
Show file tree
Hide file tree
Showing 5 changed files with 1,924 additions and 4 deletions.
1 change: 1 addition & 0 deletions core/API/DocumentationGenerator.php
Expand Up @@ -185,6 +185,7 @@ public function getExampleUrl($class, $methodName, $parametersToSet = array())
$aParameters['include_aggregate_rows'] = false;
$aParameters['filter_limit'] = false; //@review without adding this, I can not set filter_limit in $otherRequestParameters integration tests
$aParameters['filter_sort_column'] = false; //@review without adding this, I can not set filter_sort_column in $otherRequestParameters integration tests
$aParameters['filter_sort_order'] = false; //@review without adding this, I can not set filter_sort_order in $otherRequestParameters integration tests
$aParameters['filter_truncate'] = false;
$aParameters['hideColumns'] = false;
$aParameters['showColumns'] = false;
Expand Down
15 changes: 11 additions & 4 deletions plugins/Live/API.php
Expand Up @@ -145,8 +145,10 @@ public function getLastVisitsDetails($idSite, $period = false, $date = false, $s
$countVisitorsToFetch = $filter_limit + $filter_offset;
}

$filterSortOrder = Common::getRequestVar('filter_sort_order', false, 'string');

Piwik::checkUserHasViewAccess($idSite);
$dataTable = $this->loadLastVisitorDetailsFromDatabase($idSite, $period, $date, $segment, $countVisitorsToFetch, $visitorId = false, $minTimestamp);
$dataTable = $this->loadLastVisitorDetailsFromDatabase($idSite, $period, $date, $segment, $countVisitorsToFetch, $visitorId = false, $minTimestamp, $filterSortOrder);
$this->addFilterToCleanVisitors($dataTable, $idSite, $flat, $doNotFetchActions);

return $dataTable;
Expand Down Expand Up @@ -576,7 +578,7 @@ private function addFilterToCleanVisitors(DataTable $dataTable, $idSite, $flat =
});
}

private function loadLastVisitorDetailsFromDatabase($idSite, $period, $date, $segment = false, $countVisitorsToFetch = 100, $visitorId = false, $minTimestamp = false)
private function loadLastVisitorDetailsFromDatabase($idSite, $period, $date, $segment = false, $countVisitorsToFetch = 100, $visitorId = false, $minTimestamp = false, $filterSortOrder = false)
{
$where = $whereBind = array();

Expand All @@ -585,8 +587,13 @@ private function loadLastVisitorDetailsFromDatabase($idSite, $period, $date, $se
$where[] = $whereClause;
$whereBind = $idSites;

$orderBy = "idsite, visit_last_action_time DESC";
$orderByParent = "sub.visit_last_action_time DESC";
if (strtolower($filterSortOrder) !== 'asc') {
$filterSortOrder = 'DESC';
}

$orderBy = "idsite, visit_last_action_time " . $filterSortOrder;
$orderByParent = "sub.visit_last_action_time " . $filterSortOrder;

if (!empty($visitorId)) {
$where[] = "log_visit.idvisitor = ? ";
$whereBind[] = @Common::hex2bin($visitorId);
Expand Down
19 changes: 19 additions & 0 deletions tests/PHPUnit/Integration/ManyVisitorsOneWebsiteTest.php
Expand Up @@ -8,6 +8,7 @@

require_once PIWIK_INCLUDE_PATH . '/tests/PHPUnit/MockLocationProvider.php';

use Piwik\Date;
/**
* Tests w/ 14 visitors w/ 2 visits each.
* Uses geoip location provider to test city/region reports.
Expand All @@ -33,6 +34,8 @@ public function getApiForTesting()
$idSite = self::$fixture->idSite;
$dateTime = self::$fixture->dateTime;

$dateString = Date::factory($dateTime)->toString();

// Note: we must set 'UserCountry.getLocationFromIP' since it's "excluded" by default in setApiNotToCall
$apiToCall = array('UserCountry');

Expand Down Expand Up @@ -95,6 +98,22 @@ public function getApiForTesting()
'periods' => array('month'),
'otherRequestParameters' => array('ip' => '194.57.91.215')
)),

array('Live.getLastVisitsDetails', array(
'idSite' => $idSite,
'date' => $dateString,
'periods' => 'month',
'testSuffix' => '_Live.getLastVisitsDetails_sortAsc',
'otherRequestParameters' => array('filter_sort_order' => 'asc', 'filter_limit' => 7)
)),

array('Live.getLastVisitsDetails', array(
'idSite' => $idSite,
'date' => $dateString,
'periods' => 'month',
'testSuffix' => '_Live.getLastVisitsDetails_sortDesc',
'otherRequestParameters' => array('filter_sort_order' => 'desc', 'filter_limit' => 7)
)),
);
}
}
Expand Down

0 comments on commit 95d93ac

Please sign in to comment.