You are here

regcode.module in Registration codes 5.3

The main module file of the registration code module

regcode.module is kept on a lean footprint and thus contains only the main logic necessary for the basic work:

  • registers menu-items/pages for administration of registration codes
  • handles registration code input and validation upon user registration in Drupal
  • delegates to include-files where necessary

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). rolebased.patch by ??? Completely reworked and refactored for db-storage and new feature set by danielnolde

File

regcode.module
View source
<?php

/**
 * @file
 * The main module file of the registration code module
 *
 * regcode.module is kept on a lean footprint and thus
 * contains only the main logic necessary for the basic work:
 * - registers menu-items/pages for administration of registration codes
 * - handles registration code input and validation upon user registration in Drupal
 * - delegates to include-files where necessary
 *
 *
 *  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).
 *  rolebased.patch by ???
 *  Completely reworked and refactored for db-storage and new feature set by danielnolde
 *
 */
define('REGCODE_VALIDITY_NOTEXISTING', 0);
define('REGCODE_VALIDITY_NOTAVAILABLE', -1);
define('REGCODE_VALIDITY_TAKEN', -3);
define('REGCODE_VALIDITY_EXPIRED', -2);

/**
 * 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',
    'bypass registration code entry',
  );

  // 'add registration codes', 'change 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/regcode',
    'title' => t('Registration code'),
    'callback' => 'regcode_admin_page',
    'callback arguments' => array(
      'regcode_admin_settings',
    ),
    'access' => user_access('administer registration codes'),
    'description' => t('Settings for registration code functionality.'),
  );
  $items[] = array(
    'path' => 'admin/regcode/settings',
    'title' => t('Settings'),
    'description' => t('Settings for registration code functionality.'),
    'type' => MENU_DEFAULT_LOCAL_TASK,
    'weight' => 10,
  );
  $items[] = array(
    'path' => 'admin/regcode/list',
    'title' => t('List'),
    'description' => t('List of registration codes'),
    'callback' => 'regcode_admin_page',
    'callback arguments' => array(
      'regcode_admin_list',
    ),
    'type' => MENU_LOCAL_TASK,
    'weight' => 20,
  );
  $items[] = array(
    'path' => 'admin/regcode/import',
    'title' => t('Import'),
    'description' => t('Import registration codes'),
    'callback' => 'regcode_admin_page',
    'callback arguments' => array(
      'regcode_admin_import',
    ),
    'type' => MENU_LOCAL_TASK,
    'weight' => 30,
  );
  return $items;
}

/**
 * Generates requested admin page
 * by including regcode_admin.inc.php containing the resp. admin page form functions
 * and delegating to drupal_get_form
 *
 * @param page
 *  The identifier if the requested admin page (=form_id of the admin form)
 * @return
 *  The rendered output for the requested admin page
 */
function regcode_admin_page($page) {
  require_once 'regcode_admin.inc.php';
  return drupal_get_form($page);
}

/**
 * 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) {
  $code = trim($edit['regcode_code']);
  $code_required = TRUE;
  if (variable_get('regcode_optional', 0)) {
    $code_required = FALSE;
  }
  if (user_access('bypass registration code entry')) {
    $code_required = FALSE;
  }
  switch ($op) {
    case 'register':

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

        // Make sure that the entered code is valid (if option is set)
        if ($code_required) {
          require_once 'regcode_admin.inc.php';
          $code = regcode_get_code($code);
          if (!is_array($code)) {
            form_set_error('regcode_code', t('Invalid !title', array(
              '!title' => variable_get('regcode_fieldtitle', 'registration code'),
            )) . ' - ' . t(regcode_message('VALIDITY_ERROR', $code)));
            watchdog('regcode', t('User entered invalid registration code: ') . ' ' . $edit['regcode_code'] . " ({$code} - " . regcode_message('VALIDITY_ERROR', $code) . ")", WATCHDOG_WARNING);
          }
        }
      }
      break;
    case 'insert':
      require_once 'regcode_admin.inc.php';
      $code = regcode_get_code($code, $account->uid);
      if (is_array($code)) {
        $edit['roles'][$code['rid']] = array();
      }
      break;
  }

  // switch
}

Functions

Namesort descending Description
regcode_admin_page Generates requested admin page by including regcode_admin.inc.php containing the resp. admin page form functions and delegating to drupal_get_form
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.

Constants

Namesort descending Description
REGCODE_VALIDITY_EXPIRED
REGCODE_VALIDITY_NOTAVAILABLE
REGCODE_VALIDITY_NOTEXISTING @file The main module file of the registration code module
REGCODE_VALIDITY_TAKEN