You are here

public function StatsPro::get_period in Statistics Pro 6.2

Same name and namespace in other branches
  1. 6 statspro.inc \statspro::get_period()

Generate SQL fragment for period query.

2 calls to StatsPro::get_period()
StatsPro::get_aggregate_stat in ./statspro.inc
StatsPro::get_stats in ./statspro.inc
Get statistics for output.

File

./statspro.inc, line 449
statspro class for Statistics Pro module.

Class

StatsPro
Manages the data saving and retrieval according to the user defined parameters.

Code

public function get_period($field = 'day', $use_timestamp = FALSE, $period = FALSE) {
  if ($period !== FALSE) {
    $this->period = $period;
  }
  $now = time();
  if (is_numeric($this->period)) {
    $timestamp_starting_date = strtotime(sprintf('-%u day', $this->period), $now);
    if ($use_timestamp) {
      return sprintf('%s >= %u', $field, $timestamp_starting_date);
    }
    else {
      $starting_date = date('Y-m-d', $timestamp_starting_date);
      return sprintf('%s >= \'%s\'', $field, $starting_date);
    }
  }
  else {
    switch ($this->period) {
      case 'today':
        if ($use_timestamp) {
          $timestamp_begining_today = strtotime('today', $now);
          return sprintf('%s >= %u', $field, $timestamp_begining_today);
        }
        else {
          $today = date('Y-m-d', $now);
          return sprintf('%s = \'%s\'', $field, $today);
        }
        break;
      case 'yesterday':
        if ($use_timestamp) {
          $min = strtotime('-1 day', $now);
          $max = strtotime('today', $now);
          return $this
            ->get_min_max_period($field, $min, $max);
        }
        else {
          $day = date('Y-m-d', strtotime('-1 day', $now));
          return sprintf('%s = \'%s\'', $field, $day);
        }
        break;
      case 'week_current':
        $min = $this
          ->get_first_day_of_week_from_reference($now, $use_timestamp);
        if ($use_timestamp) {
          return sprintf('%s >= %u', $field, $min);
        }
        else {
          return sprintf('%s >= \'%s\'', $field, $min);
        }
        break;
      case 'week_last':
        $reference = strtotime('-1 week', $now);
        return $this
          ->get_week_from_reference($reference, $field, $use_timestamp);
        break;
      case 'week_last2':
        $reference = strtotime('-2 week', $now);
        return $this
          ->get_week_from_reference($reference, $field, $use_timestamp);
        break;
      case 'month_current':
        $min = $this
          ->get_first_day_of_month_from_reference($now, $use_timestamp);
        if ($use_timestamp) {
          return sprintf('%s >= %u', $field, $min);
        }
        else {
          return sprintf('%s >= \'%s\'', $field, $min);
        }
        break;
      case 'month_last':
        $reference = strtotime('-1 month', $now);
        return $this
          ->get_month_from_reference($reference, $field, $use_timestamp);
        break;
      case 'month_last3':
        $reference = strtotime('-3 month', $now);
        $min = $this
          ->get_first_day_of_month_from_reference($reference, $use_timestamp);
        if ($use_timestamp) {
          return sprintf('%s >= %u', $field, $min);
        }
        else {
          return sprintf('%s >= \'%s\'', $field, $min);
        }
        break;
      case 'month_last6':
        $reference = strtotime('-6 month', $now);
        $min = $this
          ->get_first_day_of_month_from_reference($reference, $use_timestamp);
        if ($use_timestamp) {
          return sprintf('%s >= %u', $field, $min);
        }
        else {
          return sprintf('%s >= \'%s\'', $field, $min);
        }
        break;
      case 'quarter_current':
        $min = $this
          ->get_first_day_of_quarter_from_reference($now, $use_timestamp);
        if ($use_timestamp) {
          return sprintf('%s >= %u', $field, $min);
        }
        else {
          return sprintf('%s >= \'%s\'', $field, $min);
        }
        break;
      case 'quarter_last':
        $reference = strtotime('-3 month', $now);
        $min = $this
          ->get_first_day_of_quarter_from_reference($reference, $use_timestamp);
        $max = $this
          ->get_last_day_of_quarter_from_reference($reference, $use_timestamp);
        if ($use_timestamp) {
          return $this
            ->get_min_max_period($field, $min, $max);
        }
        else {
          return sprintf('(%s >= \'%s\' AND %1$s <= \'%s\')', $field, $min, $max);
        }
        break;
      case 'year_current':
        $min = $this
          ->get_first_day_of_year_from_reference($now, $use_timestamp);
        if ($use_timestamp) {
          return sprintf('%s >= %u', $field, $min);
        }
        else {
          return sprintf('%s >= \'%s\'', $field, $min);
        }
        break;
      case 'year_last':
        $reference = strtotime('-1 year', $now);
        $min = $this
          ->get_first_day_of_year_from_reference($reference, $use_timestamp);
        $max = $this
          ->get_last_day_of_year_from_reference($reference, $use_timestamp);
        if ($use_timestamp) {
          return $this
            ->get_min_max_period($field, $min, $max);
        }
        else {
          return sprintf('(%s >= \'%s\' AND %1$s <= \'%s\')', $field, $min, $max);
        }
        break;
      default:
        watchdog('statspro', "Unknown period '@period'.", array(
          '@period' => $this->period,
        ), WATCHDOG_ERROR);
        return FALSE;
        break;
    }
  }
}