}
require_once BACKDROP_ROOT . '/core/includes/bootstrap.inc';
backdrop_bootstrap(BACKDROP_BOOTSTRAP_FULL);
if (!module_exists('project_usage')) {
    print "ERROR: Project usage module does not exist, aborting.\n";
    exit(1);
}
// Load the API functions we need for manipulating dates and timestamps.
module_load_include('inc', 'project_usage', 'project_usage.date');
// ------------------------------------------------------------
// Call the daily and weekly processing tasks as needed.
// ------------------------------------------------------------
$now = time();
// Figure out if it's been 24 hours since our last daily processing.
if (state_get('project_usage_last_daily', 0) <= $now - PROJECT_USAGE_DAY) {
    project_usage_process_daily();
    state_set('project_usage_last_daily', $now);
}
// We can't process the weekly data until the week has completed. To see if
// there's data available: determine the last time we completed the weekly
// processing and compare that to the start of this week. If the last
// weekly processing occurred before the current week began then there should
// be one (or more) week's worth of data ready to process.
$default = $now - config_get('project_usage.settings', 'life_daily');
$last_weekly = state_get('project_usage_last_weekly', $default);
$current_week_start = project_usage_weekly_timestamp(NULL, 0);
if ($last_weekly <= $current_week_start) {
    project_usage_process_weekly($last_weekly);
    state_set('project_usage_last_weekly', $now);
    // Reset the list of active weeks.
    project_usage_get_active_weeks(TRUE);
// since the oldest data has been processed, start with from that day and
// calculate that day's totals. Continue processing each day until we reach the
// current day. Data for the current (partial) day is not calculated.
$last_processed_daily_timestamp = state_get('project_usage_last_daily', REQUEST_TIME - PROJECT_USAGE_YEAR);
$start_of_current_day = project_usage_daily_timestamp();
// Run if we haven't processed yesterday's numbers.
if ($last_processed_daily_timestamp < $start_of_current_day) {
    // Assign project NIDs to the raw data since the last run.
    project_usage_process_raw_data();
    // Timestamp for beginning of the oldest available data.
    $oldest_time = (int) db_query("SELECT MIN(timestamp) FROM {project_usage_raw}")->fetchField();
    $oldest_day_end = project_usage_daily_timestamp($oldest_time, 1);
    $daily_timestamp = project_usage_daily_timestamp($oldest_time);
    // Process all days up until the current one.
    while ($daily_timestamp < $start_of_current_day) {
        project_usage_process_daily($daily_timestamp);
        // Increment the timestamp to the next day.
        $daily_timestamp = project_usage_daily_timestamp($daily_timestamp, 1);
    }
    state_set('project_usage_last_daily', $daily_timestamp);
    $tables_updated = TRUE;
}
// Process the weekly data up until the current week. Data for the current
// (partial) week is not calculated.
$last_processed_weekly_timestamp = state_get('project_usage_last_weekly', REQUEST_TIME - PROJECT_USAGE_YEAR);
$start_of_current_week = project_usage_weekly_timestamp();
if ($last_processed_weekly_timestamp < $start_of_current_week) {
    // Start with the week following last processed one.
    $next_weekly_timestamp = project_usage_weekly_timestamp($last_processed_weekly_timestamp, 1);
    // Process all weeks up until the current one.
    while ($next_weekly_timestamp < $start_of_current_week) {