Atlast I upgrade the regcode module for Drupal 5.1

The code is here

1. For regcode.info
==================
; $Id: regcode.info,v 1.1 2007/01/03 01:29:15 drewish Exp $
name = Regcode Example
description = An example demonstrating how a registration code appear in user registration form.
package = Regcode

2. For regcode.module
=================
<?php
// $Id: regcode.module,v 1.1 2007/02/22 20:10:18 colan Exp $

/**
* @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).

/**
* Display help and module information.
*
* @param section
* which section of the site we're displaying help
* @return
* help text for section
*/
function regcode_help($section) {
switch ($section) {
case 'admin/help#regcode':
return t('

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.

To set the codes, go to Administer->Site Configuration->Regcode and enter
them there. To disable the registration code requirement for new
users, simply disable the module (in administer->modules).

');
case 'admin/modules#description':
return t('If enabled, users must enter a valid registration code to
register');
}
}

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

/**
* Declare administrative settings for a module.
*
* @return
* An array containing form items to place on the module settings page
*/
/* Actually hook_setting does not work in Drupal 5.1 environment So, the equivalent is hook_menu($may_cache) api, I implement this api
*/
function regcode_menu($may_cache)
{
$items = array();

$items[] = array('path' => 'admin/settings/regcode',
'title' => t('Regcode'),
'callback' => 'drupal_get_form',
'callback arguments' => array('regcode_admin_settings'),
'access' => user_access('administer registration codes
'),
'description' => t('Display registration code on the registration page.')

);

return $items;

}//end of function regcode_menu

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 kind 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'),
//'#default_value' => variable_get('regcode_codes', ''),
'#required' => TRUE,
'#description' => t('Please enter your registration code.')
);
return $form;

case 'validate':
if ($category == 'account') {
// 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;
}
}
}

Comments

schmitty’s picture

big thx for the great work

Add the bold text and it should work as in 4.7.
I think it can be added to the Registration Code module. (http://drupal.org/project/regcode)

case 'validate':
if ( ! $account->uid && $category == 'account') {
// 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('Ungültiges Lehrstuhlpasswort.'));
watchdog('regcode', t('User entered invalid registration code: ') .
$edit['regcode_code'], WATCHDOG_WARNING);
}
}
break;
}
}
}