You are here

regcode.module in Registration codes 5

File

regcode.module
View source
<?php

/**
 * @file
 *   regcode.module
 */

// Initial written by nevets (http://drupal.org/user/12856).
// See http://drupal.org/node/85861 for details.
// Edited and hacked on by colan (http://drupal.org/user/58704).
// Updated to Drupal 5.x by kaushik_sarkar (http://drupal.org/user/95377).

/**
 * Display help and module information.
 *
 * @param section
 *  The section of the site where we're displaying help.
 * @return
 *  The help text for the section.
 */
function regcode_help($section) {
  switch ($section) {
    case 'admin/help#regcode':
      return t('
<p>Allows administrators to set a registration code or codes that new users
must enter before they can complete the registration process. If a new user
enters a code that is not in the list of valid codes, he or she is denied
registration.</p>

<p>To set the codes, go to Administer->Site configuration->Registration code
and enter them there. To disable the registration code requirement for new
users, simply disable the module (in Administer->Site building->Modules).</p>
      ');
  }
}

/**
 * Set valid permissions for this module.
 *
 * @return
 *   An array of valid permissions for the module.
 */
function regcode_perm() {
  return array(
    'administer registration codes',
  );
}

/**
 * Define menu items and page callbacks.
 *
 * @param $may_cache
 *   A boolean indicating whether cacheable menu items should be returned.
 *
 * @return
 *   An array of menu items. Each menu item is an associative array.
 */
function regcode_menu($may_cache) {
  $items = array();
  $items[] = array(
    'path' => 'admin/settings/regcode',
    'title' => t('Registration code'),
    'callback' => 'drupal_get_form',
    'callback arguments' => array(
      'regcode_admin_settings',
    ),
    'access' => user_access('administer registration codes'),
    'description' => t('Require a registration code on the registration page.'),
  );
  return $items;
}

/**
 * Return the form associated with the module settings.
 *
 * @return
 *   The settings form.
 */
function regcode_admin_settings() {
  $form['regcode_codes'] = array(
    '#type' => 'textarea',
    '#title' => t('Registration code(s)'),
    '#required' => TRUE,
    '#default_value' => variable_get('regcode_codes', ''),
    '#description' => t('If set, new users must correctly enter one of these
      codes in order to register. Enter one code per line.'),
  );
  $form = system_settings_form($form);
  return $form;
}

/**
 * Act on user account actions.
 *
 * @param $op
 *   The type of action is being performed.
 * @param $edit
 *   The array of form values submitted by the user.
 * @param $account
 *   The user object on which the operation is being performed.
 * @param $category
 *   The active category of user information being edited.
 * @return
 *   This varies depending on the operation.
 */
function regcode_user($op, &$edit, &$account, $category = NULL) {

  // Get the array of registration codes.
  $regcodes = variable_get('regcode_codes', '');

  // Only do the checking if the codes variable is set.
  if (strlen(trim($regcodes))) {
    switch ($op) {
      case 'register':

        // Inject the registration code field into the registration form.
        $form['regcode_code'] = array(
          '#type' => 'textfield',
          '#title' => t('Registration Code'),
          '#required' => TRUE,
          '#description' => t('Please enter your registration code.'),
        );
        return $form;
      case 'validate':
        if ($category == 'account' && !$account->uid) {

          // Make sure that the entered code is in the list.
          $regcodes = explode("\n", $regcodes);
          array_walk($regcodes, create_function('&$a', '$a = trim($a);'));
          if (!in_array(trim($edit['regcode_code']), $regcodes)) {
            form_set_error('regcode_code', t('Invalid registration code'));
            watchdog('regcode', t('User entered invalid registration code: ') . $edit['regcode_code'], WATCHDOG_WARNING);
          }
        }
        break;
    }

    // switch
  }
}

Functions

Namesort descending Description
regcode_admin_settings Return the form associated with the module settings.
regcode_help Display help and module information.
regcode_menu Define menu items and page callbacks.
regcode_perm Set valid permissions for this module.
regcode_user Act on user account actions.