add earlyaccess code generation

This commit is contained in:
2017-06-29 12:40:47 +10:00
parent e90d02f46e
commit b5d9b5ea7f
4 changed files with 73 additions and 9 deletions

View File

@@ -628,6 +628,14 @@ function booking_admin() {
),
'#default_value' => variable_get('booking_enable_earlyaccess_codes', 0)
);
$form['regn_options']['booking_earlyaccess_codes_count'] = array(
'#type' => 'textfield',
'#title' => t('Number of early access codes'),
'#description' => t("Set to the number of early access codes to generate. You will need to visit the configuration page to actually generate these codes."),
'#size' => 3,
'#maxlength' => 3,
'#default_value' => variable_get('booking_earlyaccess_codes_count', '0'),
);
$form['management'] = array(
'#type' => 'fieldset',

View File

@@ -39,3 +39,58 @@ function booking_earlyaccess_admin() {
return $output;
}
/**
* Function to generate early access codes for the current event
*/
function booking_earlyaccess_generate_codes() {
global $event;
$num_codes_to_add = 0;
$num_codes_available = 0;
$code_length = 6;
//determine number of currently available access codes for this event
$codecount_query = db_query("SELECT count(*) as num_codes FROM {booking_regn_earlyaccess_codes} where booking_eventid = :eventid and booking_regn_earlyaccess_code_avail = 'Y'",
array(':eventid' => $event->eid))
->fetchObject();
$num_codes_available = $codecount_query->num_codes;
//get the number of codes we need to have available from variable_get('booking_earlyaccess_codes_count')
$num_codes_to_add = variable_get('booking_earlyaccess_codes_count', 0) - $num_codes_available;
//add required number of codes to database table
//use the multi-insert query type at https://drupal.org/node/310079
$insert_query = db_insert('booking_regn_earlyaccess_codes')
->fields(array(
'booking_eventid',
'booking_regn_earlyaccess_code',
'booking_regn_earlyaccess_code_avail',
));
for ($i = 0; $i < $num_codes_to_add; $i++) {
$insert_query->values(array(
'booking_eventid' => $event->eid,
'booking_regn_earlyaccess_code' => booking_getToken(code_length),
'booking_regn_earlyaccess_code_avail' => 'Y',
));
}
$insert_query->execute();
}
/**
* Function to calculate unique code
* @see https://stackoverflow.com/questions/1846202/php-how-to-generate-a-random-unique-alphanumeric-string
* Requires PHP7
*/
function booking_getToken($length){
$token = "";
$codeAlphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
//$codeAlphabet.= "abcdefghijklmnopqrstuvwxyz";
$codeAlphabet.= "0123456789";
$max = strlen($codeAlphabet); // edited
for ($i=0; $i < $length; $i++) {
$token .= $codeAlphabet[random_int(0, $max-1)];
}
return $token;
}

View File

@@ -41,10 +41,3 @@ function booking_generate_luckynumbers() {
return t("<h3>Generate Lucky Numbers</h3>");
}
/**
* Function to populate the booking_regn_earlyaccess_codes table used for storing and checking early registration access codes
*/
function booking_generate_earlyregn_codes() {
global $event;
}

View File

@@ -714,10 +714,18 @@ function booking_menu() {
'title' => 'Booking module Early Access Codes',
'description' => 'Define and configure Early Access Codes for the Booking module',
'page callback' => 'booking_earlyaccess_admin',
'access arguments' => array('administer site configuration'),
'access arguments' => array('access administration pages'),
'file' => 'booking.earlyaccess_admin.inc',
'type' => MENU_NORMAL_ITEM,
);
$items['admin/booking/earlyaccess/generate'] = array(
'title' => 'Generate Early Access Codes',
'description' => 'Generate Early Access Codes',
'page callback' => 'booking_earlyaccess_generate_codes',
'access arguments' => array('access administration pages'),
'file' => 'booking.earlyaccess_admin.inc',
'type' => MENU_LOCAL_ACTION,
);
//the paypal IPN
$items[BOOKING_PAYPAL_IPN_PATH] = array(