You are here

userpoints.install in User Points 7

Install time hook userpoints module.

File

userpoints.install
View source
<?php

/**
 * @file
 * Install time hook userpoints module.
 */

/**
 * Implements hook_schema().
 */
function userpoints_schema() {
  $schema = array();
  $schema['userpoints'] = array(
    'description' => 'Holds the user points',
    'fields' => array(
      'pid' => array(
        'description' => 'Points ID',
        'type' => 'serial',
        'not null' => TRUE,
      ),
      'uid' => array(
        'description' => 'User ID',
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
      ),
      'points' => array(
        'description' => 'Current Points',
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
      ),
      'max_points' => array(
        'description' => 'Out of a maximum points',
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
      ),
      'last_update' => array(
        'description' => 'Timestamp',
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
      ),
      'tid' => array(
        'description' => 'Category ID',
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
      ),
    ),
    'primary key' => array(
      'pid',
    ),
    'indexes' => array(
      'last_update' => array(
        'last_update',
      ),
      'points' => array(
        'points',
      ),
    ),
    'unique keys' => array(
      'uid_tid' => array(
        'uid',
        'tid',
      ),
    ),
  );
  $schema['userpoints_total'] = array(
    'description' => 'Holds the total user points',
    'fields' => array(
      'uid' => array(
        'description' => 'User ID',
        'type' => 'int',
        'not null' => TRUE,
      ),
      'points' => array(
        'description' => 'Current Points',
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
      ),
      'max_points' => array(
        'description' => 'Out of a maximum points',
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
      ),
      'last_update' => array(
        'description' => 'Timestamp',
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
      ),
    ),
    'primary key' => array(
      'uid',
    ),
    'indexes' => array(
      'last_update' => array(
        'last_update',
      ),
      'points' => array(
        'points',
      ),
    ),
  );
  $schema['userpoints_txn'] = array(
    'description' => 'Userpoints Transactions',
    'fields' => array(
      'txn_id' => array(
        'description' => 'Transaction ID',
        'type' => 'serial',
        'not null' => TRUE,
      ),
      'uid' => array(
        'description' => 'User ID',
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
      ),
      'approver_uid' => array(
        'description' => 'Moderator User ID',
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
      ),
      'points' => array(
        'description' => 'Points',
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
      ),
      'time_stamp' => array(
        'description' => 'Timestamp',
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
      ),
      'changed' => array(
        'description' => 'Effective timestamp of last action on this transaction, for tracking purposes.',
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
      ),
      'status' => array(
        'description' => 'Status',
        'type' => 'int',
        'size' => 'tiny',
        'not null' => TRUE,
        'default' => 0,
      ),
      'description' => array(
        'description' => 'Description',
        'type' => 'text',
      ),
      'reference' => array(
        'description' => 'Reserved for module specific use',
        'type' => 'varchar',
        'length' => 128,
      ),
      'expirydate' => array(
        'description' => 'Expirydate',
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
      ),
      'expired' => array(
        'description' => 'Expiration status',
        'type' => 'int',
        'size' => 'tiny',
        'not null' => TRUE,
        'default' => 0,
      ),
      'parent_txn_id' => array(
        'description' => 'Parent Transaction ID',
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
      ),
      'tid' => array(
        'description' => 'Category ID',
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
      ),
      'entity_id' => array(
        'description' => 'ID of an entity in the Database',
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
      ),
      'entity_type' => array(
        'description' => 'Type of entity',
        'type' => 'varchar',
        'length' => 128,
      ),
      'operation' => array(
        'description' => 'Operation being carried out',
        'type' => 'varchar',
        'length' => 48,
      ),
    ),
    'primary key' => array(
      'txn_id',
    ),
    'indexes' => array(
      'operation' => array(
        'operation',
      ),
      'reference' => array(
        'reference',
      ),
      'status_expired_expiry' => array(
        'status',
        'expired',
        'expirydate',
      ),
      //Optional as in update_6011
      'changed' => array(
        'changed',
      ),
      'uid' => array(
        'uid',
      ),
      'approver_uid' => array(
        'approver_uid',
      ),
      'points' => array(
        'points',
      ),
    ),
  );
  return $schema;
}

/**
 * Implements hook_uninstall().
 */
function userpoints_uninstall() {
  db_delete('variable')
    ->condition('name', 'userpoints_%', 'LIKE')
    ->execute();
  $vid = db_query("SELECT vid FROM {taxonomy_vocabulary} WHERE module='userpoints'")
    ->fetchField();
  if ($vid && function_exists('taxonomy_vocabulary_delete')) {
    taxonomy_vocabulary_delete($vid);
  }
}

/**
 * Update profile category setting, allow multiple categories to be displayed.
 */
function userpoints_update_7000() {
  variable_set(USERPOINTS_CATEGORY_PROFILE_DISPLAY_TID, array(
    variable_get(USERPOINTS_CATEGORY_PROFILE_DISPLAY_TID, 0),
  ));
}

/**
 * Add a few indexes to speed up contrib modules that query the `points` columns.
 *
 * Add index on points columns in userpoints and userpoints_txn, as well as on the two
 * uid columns in userpoints_txn. No need to add a uid index in userpoints, as the
 * compound uid_tid index is used instead when needed.
 *
 * Since these indexes are added in a 6.x update as well, they need to be conditional
 * in this update. Otherwise users get errors, which is bad.
 */
function userpoints_update_7001() {
  if (!db_index_exists('userpoints', 'points')) {
    db_add_index('userpoints', 'points', array(
      'points',
    ));
  }
  if (!db_index_exists('userpoints_txn', 'uid')) {
    db_add_index('userpoints_txn', 'uid', array(
      'uid',
    ));
  }
  if (!db_index_exists('userpoints_txn', 'approver_uid')) {
    db_add_index('userpoints_txn', 'approver_uid', array(
      'approver_uid',
    ));
  }
  if (!db_index_exists('userpoints_txn', 'points')) {
    db_add_index('userpoints_txn', 'points', array(
      'points',
    ));
  }
}

/**
 * Increase length of operation and entity_type columns.
 */
function userpoints_update_7002() {
  db_change_field('userpoints_txn', 'operation', 'operation', array(
    'description' => 'Operation being carried out',
    'type' => 'varchar',
    'length' => 48,
  ));
  db_change_field('userpoints_txn', 'entity_type', 'entity_type', array(
    'description' => 'Type of entity',
    'type' => 'varchar',
    'length' => 128,
  ));
}

/**
 * Create the {userpoints_total} table.
 */
function userpoints_update_7003() {
  db_create_table('userpoints_total', array(
    'description' => 'Holds the total user points',
    'fields' => array(
      'uid' => array(
        'description' => 'User ID',
        'type' => 'int',
        'not null' => TRUE,
      ),
      'points' => array(
        'description' => 'Current Points',
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
      ),
      'max_points' => array(
        'description' => 'Out of a maximum points',
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
      ),
      'last_update' => array(
        'description' => 'Timestamp',
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
      ),
    ),
    'primary key' => array(
      'uid',
    ),
    'indexes' => array(
      'last_update' => array(
        'last_update',
      ),
      'points' => array(
        'points',
      ),
    ),
  ));
}

/**
 * Filling the {userpoints_total} table.
 */
function userpoints_update_7004(&$sandbox) {

  // First run, initialize sandbox and check if we are ready to run.
  if (!isset($sandbox['current_uid'])) {
    $sandbox['current_uid'] = 0;

    // Assume that the uid's are distributed more or less equally over the
    // whole data set. This allows us to calculate the approximate progress.
    $sandbox['max'] = db_query('SELECT MAX(uid) FROM {userpoints}')
      ->fetchField();
  }

  // Fetch the next 10 thread_ids.
  $result = db_query_range('SELECT uid, SUM(points) AS points, SUM(max_points) AS max_points, MAX(last_update) AS last_update FROM {userpoints} WHERE uid > :uid GROUP BY uid ORDER BY uid ASC', 0, 10, array(
    ':uid' => $sandbox['current_uid'],
  ), array(
    'fetch' => PDO::FETCH_ASSOC,
  ));
  $insert = db_insert('userpoints_total')
    ->fields(array(
    'uid',
    'points',
    'max_points',
    'last_update',
  ));
  $last_uid = 0;
  foreach ($result as $row) {
    $insert
      ->values($row);
    $last_uid = $row['uid'];
  }
  $insert
    ->execute();
  $sandbox['current_uid'] = $last_uid;

  // Set #finished based on sandbox.
  $sandbox['#finished'] = empty($sandbox['max']) || $last_uid == 0 ? 1 : $sandbox['current_uid'] / $sandbox['max'];
}

Functions

Namesort descending Description
userpoints_schema Implements hook_schema().
userpoints_uninstall Implements hook_uninstall().
userpoints_update_7000 Update profile category setting, allow multiple categories to be displayed.
userpoints_update_7001 Add a few indexes to speed up contrib modules that query the `points` columns.
userpoints_update_7002 Increase length of operation and entity_type columns.
userpoints_update_7003 Create the {userpoints_total} table.
userpoints_update_7004 Filling the {userpoints_total} table.