You are here

function ip_geoloc_get_recent_visitor_locations in IP Geolocation Views & Maps 7

Same name and namespace in other branches
  1. 8 ip_geoloc_blocks.inc \ip_geoloc_get_recent_visitor_locations()

Get recent visitor locations.

Parameters

int $how_many: how many locations to retrieve

Return value

object object of locations found

1 call to ip_geoloc_get_recent_visitor_locations()
ip_geoloc_block_view in ./ip_geoloc_blocks.inc
Implements hook_block_view().

File

./ip_geoloc_blocks.inc, line 433
Blocks available in IP Geolocation Views & Maps.

Code

function ip_geoloc_get_recent_visitor_locations($how_many) {
  $locations = array();
  if (db_table_exists('accesslog')) {

    // A LEFT JOIN would also pick up new IP addresses that are about to be
    // inserted into the {accesslog}.
    // However a LEFT JOIN in this query can easily make it 100 times slower
    // than the INNER JOIN used below and would only be relevant for the very
    // first click from a new IP address or in the case where the IP address was
    // removed from the {accesslog}.
    $result = db_query_range('SELECT DISTINCT ip_address, latitude, longitude, formatted_address, COUNT(a.timestamp) AS visit_count, MAX(a.timestamp) AS last_visit FROM {ip_geoloc} i INNER JOIN {accesslog} a ON i.ip_address = a.hostname GROUP BY i.ip_address ORDER BY last_visit DESC', 0, $how_many);
    $separator = '<br/>';
    foreach ($result as $location) {

      // Prevent older IP address locations overwriting the latest location.
      if (!isset($locations[$location->ip_address])) {
        $loc_rendered = new stdClass();
        $loc_rendered->latitude = $location->latitude;
        $loc_rendered->longitude = $location->longitude;
        $loc_rendered->balloon_text = t('IP address') . ' ' . $location->ip_address . '<br/>' . $location->formatted_address . $separator . t('#visits') . ' ' . (empty($location->visit_count) ? '?' : $location->visit_count) . ', ' . t('last visit') . ' ' . (empty($location->last_visit) ? '?' : format_date($location->last_visit, 'short'));
        $locations[$location->ip_address] = $loc_rendered;
      }
    }
  }
  return $locations;
}