<?php


define('TOTAL_COUNTS_BLOCK', 'total_counts');

/**
 * Implements hook_block_info().
 */
function custom_maps_block_info() {
  $blocks[TOTAL_COUNTS_BLOCK] = array(
    'info' => t('Display totals info'),
    'cache' => DRUPAL_NO_CACHE,
  );

  return $blocks;
}





  /**
 * Implements hook_block_view().
 */
function custom_maps_block_view($delta = '') {
  $block = array();

  switch ($delta) {
    case TOTAL_COUNTS_BLOCK:
      $block = _block_total_counts_content();
      // $block['content'] = drupal_render($content);
      break;
  }

  return $block;
}



/**
 * Implements hook_theme().
 */
function custom_maps_theme($existing, $type, $theme, $path) {
    $themes[TOTAL_COUNTS_BLOCK] = array( // replace module--delta with your block template suggestions
      'variables' => array(),   
      'template' => TOTAL_COUNTS_BLOCK, // the name fo your .tpl.php file.
      'path' => $path . '/templates',   // path to the template in your module
    );
 
  return $themes; 
}


/** 
 * Helper function to call theme
 */
 function _my_module_block1() {
    $block = array(
        'subject' => '',
        'content' => theme(MY_MODULE_BLOCK1, array()),
    );
    return $block; 
 }





function _block_total_counts_content() {

  $total_activities = 0;
  $latest_distance = 0;
  $latest_time = 0;
  $latest_date = 'mm/dd/yyyy hh.mm';
  $month_distance = 0;
  $month_time = 0;


  $content = array(
                   'total' => __count_global_totals(),
                   'latest' => __count_global_totals(FALSE, TRUE),
                   'month' => __count_global_totals(TRUE),
                  );

  $block = array(
      'subject' => '',
      'content' => theme(TOTAL_COUNTS_BLOCK, $content),
  );
  return $block; 
}

function __count_global_totals($month = FALSE, $latest = FALSE) {
  global $user;
  $total_time = 0;
  $total_distance = 0;
  $count = 0;
  $date = 0;

  $query = new EntityFieldQuery();
  $query->entityCondition('entity_type', 'node')
    ->entityCondition('bundle', 'activity')
    ->propertyCondition('status', NODE_PUBLISHED);

  if ( $user->uid == 1) {
    // Run the query as user 1.
    $query->addMetaData('account', user_load(1));
  } else {
    $query->propertyCondition('uid', $user->uid);
  }

  if ( $month ) {
    $query->fieldCondition('field_date', 'value', strtotime(date('01-m-Y')), '>');
  }

  $query->fieldOrderBy('field_date', 'value', 'DESC');

  $result = $query->execute();
  if (isset($result['node'])) {
    foreach ($result['node'] as $index => $node) {
      $count++;
      $node = node_load($node->nid);
      $total_time += $node->field_total_time[LANGUAGE_NONE][0]['value'];
      $total_distance += $node->field_distance[LANGUAGE_NONE][0]['value'];

      $date = $node->field_date[LANGUAGE_NONE][0]['value'];

      if ( $latest ) break;
    }
  } 

  return array('total_activities' => $count,
                'distance' => __number_format($total_distance / 1000, 2),
                'time' => __format_seconds($total_time),
                'date' => date('d/m/Y H:i:s', $date));
}


