You are here

autoassignrole.module in Auto Assign Role 5

File

autoassignrole.module
View source
<?php

define("AUTOASSIGNROLE_ROLE", "");
define("AUTOASSIGNROLE_ROLE_ACTIVE", 0);
define("AUTOASSIGNROLE_ROLE_USER_ACTIVE", 0);
define("AUTOASSIGNROLE_ROLE_USER", "");
define("AUTOASSIGNROLE_ROLE_USER_MULTIPLE", 0);
define("AUTOASSIGNROLE_ROLE_USER_DESCRIPTION", "");
define("AUTOASSIGNROLE_ROLE_USER_TITLE", t("Role"));
define("AUTOASSIGNROLE_ROLE_SORT", "SORT_ASC");

/**
 * Implementation of hook_menu().
 *
 * @return array
 */
function autoassignrole_menu($may_cache) {
  $items = array();
  if ($may_cache) {
    $items[] = array(
      'path' => 'admin/user/autoassignrole',
      'title' => t('Auto assign role'),
      'description' => t('Auto Assign Role Settings.'),
      'callback' => 'drupal_get_form',
      'callback arguments' => 'autoassignrole_settings',
      'access' => user_access('administer autoassignrole'),
    );
  }
  return $items;
}
function autoassignrole_settings() {
  $roles = user_roles();

  // Unset Anonymous & Authenticated RID.
  unset($roles[1]);
  unset($roles[2]);
  $form['autoassignrole_settings_auto'] = array(
    '#type' => 'fieldset',
    '#title' => t('Automatic Role Assignment'),
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
  );
  $desc = "Automatic role assignment occurs when the user first logins to the ";
  $desc .= "account.  This happens without the users knowledge.";
  $form['autoassignrole_settings_auto']['AUTOASSIGNROLE_ROLE_ACTIVE'] = array(
    '#type' => 'radios',
    '#title' => t('Automatic role assignment'),
    '#description' => t($desc),
    '#default_value' => variable_get('AUTOASSIGNROLE_ROLE_ACTIVE', 0),
    '#options' => array(
      1 => t('Enabled'),
      0 => t('Disabled'),
    ),
  );
  $form['autoassignrole_settings_auto']['AUTOASSIGNROLE_ROLE'] = array(
    '#type' => 'checkboxes',
    '#title' => t('Role'),
    '#default_value' => variable_get('AUTOASSIGNROLE_ROLE', ''),
    '#options' => $roles,
    '#description' => t('Select the roles to assign new users.'),
  );
  $form['autoassignrole_settings_user'] = array(
    '#type' => 'fieldset',
    '#title' => t('User Role Assignment'),
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
  );
  $desc = "The end user will be allowed to select the following roles when ";
  $desc .= "they log in.";
  $form['autoassignrole_settings_user']['AUTOASSIGNROLE_ROLE_USER_ACTIVE'] = array(
    '#type' => 'radios',
    '#title' => t('User Role Assignment'),
    '#description' => t($desc),
    '#default_value' => variable_get('AUTOASSIGNROLE_ROLE_USER_ACTIVE', 0),
    '#options' => array(
      1 => t('Enabled'),
      0 => t('Disabled'),
    ),
  );
  $form['autoassignrole_settings_user']['AUTOASSIGNROLE_ROLE_USER'] = array(
    '#type' => 'checkboxes',
    '#title' => t('Role'),
    '#default_value' => variable_get('AUTOASSIGNROLE_ROLE_USER', ''),
    '#options' => $roles,
    '#description' => t('Select the roles that are offered to new users.'),
  );
  $desc = "Should the end user be allowed to choose a single role or can they ";
  $desc .= "choose multiple roles?";
  $form['autoassignrole_settings_user']['AUTOASSIGNROLE_ROLE_USER_MULTIPLE'] = array(
    '#type' => 'radios',
    '#title' => t('User Role Selection'),
    '#description' => t($desc),
    '#default_value' => variable_get('AUTOASSIGNROLE_ROLE_USER_MULTIPLE', 0),
    '#options' => array(
      0 => t('Single Role'),
      1 => t('Multiple Roles'),
    ),
  );
  $form['autoassignrole_settings_user']['AUTOASSIGNROLE_ROLE_USER_REQUIRED'] = array(
    '#type' => 'checkbox',
    '#title' => t('Required'),
    '#description' => t('Should the end user be required to choose a role?'),
    '#default_value' => variable_get('AUTOASSIGNROLE_ROLE_USER_REQUIRED', 0),
  );
  $form['autoassignrole_settings_user']['AUTOASSIGNROLE_ROLE_SORT'] = array(
    '#type' => 'select',
    '#title' => t('Sorting'),
    '#description' => t('Default sort order of roles the user will see.'),
    '#default_value' => variable_get('AUTOASSIGNROLE_ROLE_SORT', 'SORT_ASC'),
    '#options' => array(
      'SORT_ASC' => t('Ascending'),
      'SORT_DESC' => t('Descending'),
    ),
  );
  $desc = "The title of the fieldset that contains role options.";
  $form['autoassignrole_settings_user']['AUTOASSIGNROLE_ROLE_USER_FIELDSET'] = array(
    '#type' => 'textfield',
    '#title' => t('User Role Fieldset Title'),
    '#description' => t($desc),
    '#default_value' => variable_get('AUTOASSIGNROLE_ROLE_USER_FIELDSET', t('User Role Selection')),
    '#size' => 60,
    '#maxlength' => 128,
    '#required' => FALSE,
  );
  $desc = "The title of the field that contains the role options the end ";
  $desc .= "user sees during registration.";
  $form['autoassignrole_settings_user']['AUTOASSIGNROLE_ROLE_USER_TITLE'] = array(
    '#type' => 'textfield',
    '#title' => t('User Role Title'),
    '#description' => t($desc),
    '#default_value' => variable_get('AUTOASSIGNROLE_ROLE_USER_TITLE', ""),
    '#size' => 60,
    '#maxlength' => 128,
    '#required' => FALSE,
  );
  $desc = "The description displayed to the end user when they are ";
  $desc .= "selecting thier role during registration.";
  $form['autoassignrole_settings_user']['AUTOASSIGNROLE_ROLE_USER_DESCRIPTION'] = array(
    '#type' => 'textarea',
    '#title' => t('User Role Description'),
    '#description' => t($desc),
    '#default_value' => variable_get('AUTOASSIGNROLE_ROLE_USER_DESCRIPTION', ""),
    '#required' => FALSE,
  );
  return system_settings_form($form);
}

/**
 * Implementation of hook_perm().
 * @return array
 */
function autoassignrole_perm() {
  return array(
    'administer autoassignrole',
  );
}

/**
 * Implementation of hook_user().
 */
function autoassignrole_user($type, &$edit, &$user, $category = NULL) {
  switch ($type) {
    case 'insert':
      if (variable_get('AUTOASSIGNROLE_ROLE_ACTIVE', '0') == 1) {
        $roles = variable_get('AUTOASSIGNROLE_ROLE', '0');
        if (is_array($roles)) {
          $sql = 'INSERT INTO {users_roles} (uid, rid) values (%d, %d)';
          foreach ($roles as $key => $value) {
            if ($value > 0) {
              db_query($sql, $user->uid, $value);
            }
          }
        }
      }
      if (variable_get('AUTOASSIGNROLE_ROLE_USER_ACTIVE', '0') == 1) {
        $sql = 'INSERT INTO {users_roles} (uid, rid) values (%d, %d)';
        if (is_array($edit['AUTOASSIGNROLE_ROLE_USER'])) {
          foreach ($edit['AUTOASSIGNROLE_ROLE_USER'] as $key => $value) {
            if ($value > 0) {
              db_query($sql, $user->uid, $value);
            }
          }
        }
        else {
          db_query($sql, $user->uid, $edit['AUTOASSIGNROLE_ROLE_USER']);
        }
      }
      $user = user_load(array(
        "uid" => $user->uid,
      ));
      break;
  }
}
function autoassignrole_form_alter($form_id, &$form) {
  if ($form_id == "user_register" && variable_get('AUTOASSIGNROLE_ROLE_USER_ACTIVE', '0') == 1 && !user_access('administer users')) {
    $form['autoassignrole_user'] = array(
      '#type' => 'fieldset',
      '#title' => variable_get('AUTOASSIGNROLE_ROLE_USER_FIELDSET', t('User Role Selection')),
      '#collapsible' => FALSE,
      '#collapsed' => FALSE,
    );
    $form['autoassignrole_user']['AUTOASSIGNROLE_ROLE_USER'] = array(
      '#type' => _autoassignrole_user_input('type'),
      '#title' => variable_get('AUTOASSIGNROLE_ROLE_USER_TITLE', ""),
      '#options' => _autoassignrole_intersect(),
      '#required' => _autoassignrole_user_input('required'),
      '#description' => variable_get('AUTOASSIGNROLE_ROLE_USER_DESCRIPTION', ""),
    );
  }
}
function _autoassignrole_user_input($args) {
  switch ($args) {
    case 'type':
      if (variable_get('AUTOASSIGNROLE_ROLE_USER_MULTIPLE', 0) == 0) {
        $type = 'radios';
      }
      else {
        $type = 'checkboxes';
      }
      return $type;
      break;
    case 'required':
      if (variable_get('AUTOASSIGNROLE_ROLE_USER_REQUIRED', 0) == 0) {
        $required = FALSE;
      }
      else {
        $required = TRUE;
      }
      return $required;
      break;
  }
}
function _autoassignrole_intersect() {
  $autoassignrole_roles = variable_get("AUTOASSIGNROLE_ROLE_USER", "");
  foreach ($autoassignrole_roles as $key => $value) {
    if ($value == 0) {
      unset($autoassignrole_roles[$key]);
    }
  }
  $result = _autoassignrole_array_intersect_key(user_roles(), $autoassignrole_roles);
  if (variable_get("AUTOASSIGNROLE_ROLE_SORT", "SORT_ASC") == "SORT_ASC") {
    uasort($result, _autoassignrole_array_asc);
  }
  else {
    uasort($result, _autoassignrole_array_desc);
  }
  return $result;
}

/**
 * method to sort array in a descending fashion while preserving keys
 * @param string $a a string to compare
 * @param string $b a string to compare
 * @return int 
 */
function _autoassignrole_array_desc($a, $b) {
  if ($a == $b) {
    return 0;
  }
  return $a < $b ? -1 : 1;
}

/**
 * method to sort array in an ascending fashion while preserving keys
 * @param string $a a string to compare
 * @param string $b a string to compare
 * @return int 
 */
function _autoassignrole_array_asc($a, $b) {
  if ($a == $b) {
    return 0;
  }
  return $a > $b ? -1 : 1;
}
function _autoassignrole_array_intersect_key($isec, $keys) {
  $argc = func_num_args();
  if ($argc > 2) {
    for ($i = 1; !empty($isec) && $i < $argc; $i++) {
      $arr = func_get_arg($i);
      foreach (array_keys($isec) as $key) {
        if (!isset($arr[$key])) {
          unset($isec[$key]);
        }
      }
    }
    return $isec;
  }
  else {
    $res = array();
    foreach (array_keys($isec) as $key) {
      if (isset($keys[$key])) {
        $res[$key] = t($isec[$key]);
      }
    }
    return $res;
  }
}