<?php

/**
 * @file
 * Install and update hooks for the User Stats module.
 */

/**
 * Implements hook_schema().
 */
function user_stats_schema() {
  $schema['user_stats_values'] = array(
    'description' => 'User Stats data.',
    'fields' => array(
      'name' => array(
        'type' => 'varchar',
        'length' => 128,
        'not null' => TRUE,
        'default' => '',
        'description' => 'The name of the statistic.',
      ),
      'uid' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
        'description' => 'The {users}.uid of the statistic user.',
      ),
      'value' => array(
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
        'description' => 'The value of the statistic.',
      ),
    ),
    'primary key' => array('name', 'uid'),
  );
  $schema['user_stats_ips'] = array(
    'description' => 'IP address storage, links timestamps and uids to IP',
    'fields' => array(
      'iid' => array(
        'type' => 'serial',
        'not null' => TRUE,
        'description' => 'Primary key: IP address unique ID.',
      ),
      'uid' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
        'description' => 'The {users}.uid of the user.',
      ),
      'ip_address' => array(
        'type' => 'varchar',
        'length' => 40,
        'not null' => TRUE,
        'default' => '',
        'description' => "The user's IP address.",
      ),
      'first_seen_timestamp' => array(
        'description' => 'The Unix timestamp when the IP address was first used by this user.',
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
      ),
    ),
    'primary key' => array('iid'),
    'indexes' => array(
      'uid' => array('uid'),
      'first_seen_timestamp' => array('first_seen_timestamp'),
    ),
  );

  return $schema;
}

/**
 * Implements hook_uninstall().
 */
function user_stats_uninstall() {
  variable_del('user_stats_rebuild_stats');
  variable_del('user_stats_last_cron_check');
  variable_del('user_stats_included_content_types');
  variable_del('user_stats_reset_login_count');
  variable_del('user_stats_reset_post_count');
  variable_del('user_stats_user_per_cron');
  variable_del('user_stats_count_posts');
  variable_del('user_stats_count_comments');
  variable_del('user_stats_count_logins');
}

/**
 * Expands the width of the field table to 40.
 */
function user_stats_update_7102(&$sandbox) {
  $ip_address = array(
    'type' => 'varchar',
    'length' => 40,
    'not null' => TRUE,
    'default' => '',
    'description' => "The user's IP address.",
  );
  db_change_field('user_stats_ips', 'ip_address', 'ip_address', $ip_address);
}
