You are here

apply_for_role.install in Apply for role 7

Installs the Apply for Role (AFR) module.

File

apply_for_role.install
View source
<?php

/**
 * @file
 * Installs the Apply for Role (AFR) module.
 */

/**
 * Implements hook_install().
 */
function apply_for_role_install() {
}

/**
 * Implements hook_schema().
 */
function apply_for_role_schema() {
  $schema['users_roles_apply'] = array(
    'fields' => array(
      'aid' => array(
        'type' => 'serial',
        'description' => 'Primary Key: Unique role application ID.',
        'unsigned' => TRUE,
        'not null' => TRUE,
      ),
      'uid' => array(
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
        'description' => 'Foreign Key: {users}.uid.',
      ),
      'rid' => array(
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
        'description' => 'Foreign Key: {role}.rid.',
      ),
      'approved' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
        'size' => 'tiny',
        'description' => 'The status of the role application.',
      ),
      'message' => array(
        'type' => 'text',
        'size' => 'big',
        'description' => 'The message written by the role applicant.',
      ),
      'apply_date' => array(
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
        'description' => 'Timestamp for when the role application was created.',
      ),
      'approve_date' => array(
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
        'description' => 'Timestamp for when the role application was processed.',
      ),
    ),
    'unique keys' => array(
      'uid_rid' => array(
        'uid',
        'rid',
      ),
    ),
    'primary key' => array(
      'aid',
    ),
  );
  return $schema;
}

/**
 * Implements hook_uninstall().
 */
function apply_for_role_uninstall() {

  //Remove variables
  variable_del('users_apply_roles');
  variable_del('apply_for_role_multiple');
  variable_del('apply_for_role_register');
}

/**
 * Add a non-composite primary key named aid.
 */
function apply_for_role_update_7000(&$sandbox) {
  $sandbox['#finished'] = 0;
  if (!db_table_exists('users_roles_apply_temp')) {
    $schema['users_roles_apply_temp'] = array(
      'fields' => array(
        'aid' => array(
          'type' => 'serial',
          'description' => 'Primary Key: Unique role application ID.',
          'unsigned' => TRUE,
          'not null' => TRUE,
        ),
        'uid' => array(
          'type' => 'int',
          'not null' => TRUE,
          'default' => 0,
          'description' => 'Foreign Key: {users}.uid.',
        ),
        'rid' => array(
          'type' => 'int',
          'not null' => TRUE,
          'default' => 0,
          'description' => 'Foreign Key: {role}.rid.',
        ),
        'approved' => array(
          'type' => 'int',
          'unsigned' => TRUE,
          'not null' => TRUE,
          'default' => 0,
          'size' => 'tiny',
          'description' => 'The status of the role application.',
        ),
        'apply_date' => array(
          'type' => 'int',
          'not null' => TRUE,
          'default' => 0,
          'description' => 'Timestamp for when the role application was created.',
        ),
        'approve_date' => array(
          'type' => 'int',
          'not null' => TRUE,
          'default' => 0,
          'description' => 'Timestamp for when the role application was processed.',
        ),
      ),
      'unique keys' => array(
        'uid_rid' => array(
          'uid',
          'rid',
        ),
      ),
      'primary key' => array(
        'aid',
      ),
    );
    db_create_table('users_roles_apply_temp', $schema['users_roles_apply_temp']);
  }

  // Multi-part update.
  if (!isset($sandbox['apply_from'])) {
    $sandbox['apply_from'] = 0;
    $sandbox['apply_count'] = db_query("SELECT COUNT(*) FROM {users_roles_apply}")
      ->fetchField();
  }
  else {
    $has_rows = FALSE;

    // Update this many per page load.
    $count = 1000;
    $results = db_query_range("SELECT * FROM {users_roles_apply}", $sandbox['apply_from'], $count);
    foreach ($results as $apply) {
      $has_rows = TRUE;
      db_insert('users_roles_apply_temp')
        ->fields(array(
        'uid' => $apply->uid,
        'rid' => $apply->rid,
        'approved' => $apply->approved,
        'apply_date' => $apply->apply_date,
        'approve_date' => $apply->approve_date,
      ))
        ->execute();
    }
    $sandbox['#finished'] = $sandbox['apply_from'] / $sandbox['apply_count'];
    $sandbox['apply_from'] += $count;
    if (!$has_rows) {
      db_drop_table('users_roles_apply');
      db_rename_table('users_roles_apply_temp', 'users_roles_apply');
      $sandbox['#finished'] = 1;
      return t('The <em>Apply for Role</em> table has been updated.');
    }
  }
}

/**
 * Set the 'deny role applications' and 'delete role applications' permissions
 * for users who have the 'approve role applications' permission.
 */
function apply_for_role_update_7001(&$sandbox) {
  $sandbox['#finished'] = 0;

  // Multi-part update.
  if (!isset($sandbox['permission_from'])) {
    $sandbox['permission_from'] = 0;
    $sandbox['permission_count'] = db_query("SELECT COUNT(rid) FROM {role_permission} WHERE permission = :permission", array(
      ':permission' => 'approve role applications',
    ))
      ->fetchField();
  }
  else {
    $has_rows = FALSE;

    // Update this many per page load.
    $count = 1000;
    $results = db_query_range("SELECT rid FROM {role_permission} WHERE permission = :permission", $sandbox['permission_from'], $count, array(
      ':permission' => 'approve role applications',
    ));
    foreach ($results as $result) {
      $has_rows = TRUE;
      db_insert('role_permission')
        ->fields(array(
        'rid' => $result->rid,
        'permission' => 'view role applications',
      ))
        ->execute();
      db_insert('role_permission')
        ->fields(array(
        'rid' => $result->rid,
        'permission' => 'deny role applications',
      ))
        ->execute();
      db_insert('role_permission')
        ->fields(array(
        'rid' => $result->rid,
        'permission' => 'delete role applications',
      ))
        ->execute();
    }
    $sandbox['#finished'] = $sandbox['permission_from'] / $sandbox['permission_count'];
    $sandbox['permission_from'] += $count;
    if (!$has_rows) {
      $sandbox['#finished'] = 1;
      return t('The role permissions for <em>Apply for Role</em> have been updated.');
    }
  }
}

/**
 * Add a 'message' field for applicants to justify the role request.
 */
function apply_for_role_update_7002(&$sandbox) {
  $message_field = array(
    'type' => 'text',
    'size' => 'big',
    'description' => 'The message written by the role applicant.',
  );

  // Check that the field hasn't been created in an aborted run of this
  // update.
  if (!db_field_exists('users_roles_apply', 'message')) {

    // Add a new field for the fid.
    db_add_field('users_roles_apply', 'message', $message_field);
  }
}

Functions

Namesort descending Description
apply_for_role_install Implements hook_install().
apply_for_role_schema Implements hook_schema().
apply_for_role_uninstall Implements hook_uninstall().
apply_for_role_update_7000 Add a non-composite primary key named aid.
apply_for_role_update_7001 Set the 'deny role applications' and 'delete role applications' permissions for users who have the 'approve role applications' permission.
apply_for_role_update_7002 Add a 'message' field for applicants to justify the role request.