You are here

function _piwik_stats_summarize_field in Piwik Statistic Integration 7.2

Summarizes all statistical data from XML with matching URLs.

Parameters

array $urls: An array of url aliases.

object $xml: A XML object containing piwik URL statistics.

Return value

array An array representing the filled piwik_stats field.

1 call to _piwik_stats_summarize_field()
piwik_stats_process_queue_item in ./piwik_stats.module
Processes a dataset for filling a field.

File

./piwik_stats.module, line 791
Integrates piwik statistics as entity fields.

Code

function _piwik_stats_summarize_field($urls, $xml) {

  // Initialize field values as needed.
  $definitions = piwik_stats_definitions();
  $stats = array();
  foreach ($definitions as $stat_key => $definition) {
    $stats[$stat_key] = 0;
  }

  // Iterate thorugh all url aliases and sum up statistics.
  foreach ($urls as $url) {

    // Grab statistical data from XML per URL.
    $url_stats = $xml
      ->xpath('//url[text()="' . $url . '"]/..');
    if (!empty($url_stats)) {

      // Sum up each statistical value.
      foreach ($definitions as $stat_key => $definition) {
        if (isset($url_stats[0]->{$stat_key})) {

          // These two are percent values (##% formatted).
          // We need to transform them to integers before we can sum them.
          if ($definition['format'] == 'percent') {
            $stats[$stat_key] += (int) drupal_substr($url_stats[0]->{$stat_key}, 0, -1);
          }
          else {
            $stats[$stat_key] += $url_stats[0]->{$stat_key};
          }
        }
      }
    }
  }
  return array(
    LANGUAGE_NONE => array(
      $stats,
    ),
  );
}