<?php
// $Id$

/**
 * Implementation of hook_install()
 */
function boincuser_install() {
  // Use schema API to create database table
  drupal_install_schema('boincuser');
  // Add aliases for account pages
  if (module_exists('path')) {
    path_set_alias('dashboard', 'account');
    path_set_alias('dashboard', 'account/dashboard');
    path_set_alias('user/me/edit', 'account/info/edit');
    path_set_alias('user/me/edit/profile', 'account/info/edit/profile');
  }
}

/**
* Implementation of hook_uninstall().
*/
function boincuser_uninstall() {
  // Use schema API to delete database table.
  drupal_uninstall_schema('boincuser');
  // Delete our module's variable from the variables table.
  variable_del('boincuser_integration');
  // Remove relevant aliases
  if (module_exists('path')) {
    path_set_alias(NULL, 'account');
    path_set_alias(NULL, 'account/dashboard');
    path_set_alias(NULL, 'account/info/edit');
    path_set_alias(NULL, 'account/info/edit/profile');
  }
}

/**
* Implementation of hook_schema().
*/
function boincuser_schema() {
  $schema['boincuser'] = array(
    'description' => t('Cross references user accounts in Drupal with BOINC user accounts.'),
    'fields' => array(
      'uid' => array(
        'description' => t('The {user}.uid of the Drupal user.'),
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ),
      'boinc_id' => array(
        'description' => t('The uid of the user in the BOINC database.'),
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ),
      'penalty_expiration' => array(
        'description' => t('A timestamp at which point any penalty period will
          expire (i.e. restrictions on posting or community participation)'),
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ),
      'uotd_time' => array(
        'description' => t('A timestamp of the last time this user was deemed
          user of the day'),
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ),
    ),
    'primary key' => array(
      'uid', 'boinc_id'
    ),
    'unique keys' => array(
      'uid' => array('uid'),
      'boinc_id' => array('boinc_id')
    )
  );
  return $schema;
}

/**
 * Add penalty_expiration field to boincuser table
 */
function boincuser_update_6100() {
  $result = array();
  $spec = array(
    'description' => 'A timestamp at which point any penalty period will
          expire (i.e. restrictions on posting or community participation)',
    'type' => 'int',
    'unsigned' => TRUE,
    'not null' => TRUE,
    'default' => 0,
  );
  db_add_field($result, 'boincuser', 'penalty_expiration', $spec);
  return $result;
}

/**
 * Add uotd_time field to boincuser table
 */
function boincuser_update_6101() {
  $result = array();
  $spec = array(
    'description' => 'A timestamp of the last time this user was deemed
          user of the day',
    'type' => 'int',
    'unsigned' => TRUE,
    'not null' => TRUE,
    'default' => 0,
  );
  db_add_field($result, 'boincuser', 'uotd_time', $spec);
  return $result;
}

/**
 * Add delete_status and grace_period to boincuser table: used by
 * boincuser_delete module.
 */
function boincuser_update_6200() {
  $result = array();

  $spec = array(
    'description' => 'A flag to determine if this account is to be deleted or disabled.',
    'type' => 'int',
    'unsigned' => TRUE,
    'not null' => TRUE,
    'default'  => 0,
  );
  db_add_field($result, 'boincuser', 'delete_status', $spec);

  $spec = array(
    'description' => 'A flag to determine if the user has been notified 24 hours before his/her account is to be deleted or disabled.',
    'type' => 'int',
    'unsigned' => TRUE,
    'not null' => TRUE,
    'default'  => 0,
  );
  db_add_field($result, 'boincuser', 'delete_notified', $spec);

  $spec = array(
    'description' => 'A timestamp which if reached the account will be disabled or deleted.',
    'type' => 'int',
    'unsigned' => TRUE,
    'not null' => TRUE,
    'default'  => 0,
  );
  db_add_field($result, 'boincuser', 'grace_period', $spec);

  return $result;
}

/**
 * Add privacy_consent_dt field to boincuser table. Used to record
 * when user consented to site's terms-of-use.
 *
 */
function boincuser_update_6300() {
  $result = array();

  $spec = array(
    'description' => 'A timestamp which records when a user has consented to the site\'s terms of use.',
    'type'     => 'int',
    'unsigned' => TRUE,
    'not null' => TRUE,
    'default'  => 0,
  );
  db_add_field($result, 'boincuser', 'privacy_consent_dt', $spec);

  return $result;
}
