Index: regcode.install =================================================================== RCS file: /cvs/drupal/contributions/modules/regcode/regcode.install,v retrieving revision 1.2 diff -u -r1.2 regcode.install --- regcode.install 27 Jan 2008 07:08:26 -0000 1.2 +++ regcode.install 20 Jan 2009 14:40:38 -0000 @@ -1,11 +1,55 @@ atid); + } + } + else { + variable_del('regcode_codes'); + } + + $query1 = db_query('DROP TABLE {accounttypes}'); + + if ($query1) { + drupal_set_message('The Registraton Code module was uninstalled successfully.'); + } + else { + drupal_set_message('There was an error removing the Registraton Code database table.', 'error'); + } } ?> Index: regcode.module =================================================================== RCS file: /cvs/drupal/contributions/modules/regcode/regcode.module,v retrieving revision 1.2 diff -u -r1.2 regcode.module --- regcode.module 27 Jan 2008 07:08:26 -0000 1.2 +++ regcode.module 20 Jan 2009 14:40:38 -0000 @@ -1,5 +1,5 @@ '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.'), - ); + +//Check if account types module is installed + if(module_exists('accounttypes')){ + $accounttypes = array(); + $sql = "SELECT atid, name from {accounttypes}"; + $result = db_query($sql); + + while ($at = db_fetch_object($result)) { + $form['regcode_codes_account_type_' . $at->atid ] = array( + '#type' => 'textarea', + '#title' => t($at->name . ' registration code(s)'), + '#default_value' => variable_get('regcode_codes_account_type_' . $at->atid, ''), + '#description' => t('If set, new users must correctly enter one of these + codes in order to register. Enter one code per line.'), + ); + } + } else { + $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; } + function regcode_form_alter($form_id, &$form) { + if ($form_id == 'user_register'){ + // Add custom validate/submit handlers. + $form['#submit']['regcode_user_register_submit'] = array(); + } + } + + function regcode_user_register_submit($form_id, $form_values) { + //Update user with the right account type based on regcode type + if(module_exists('accounttypes')){ + $user = db_fetch_object(db_query("SELECT u.uid,r.account_type FROM {users} u,{regcodes} r WHERE u.uid=r.uid and u.name = '%s'", $form_values['name'])); + db_query("UPDATE {accounttypes_users} SET atid = '%s' WHERE uid = %d",$user->account_type, $user->uid); + //Set expiration date based in right account type + if(module_exists('account_expiry')){ + account_expiry_edit($user->uid,$user->account_type); + } + } + } + /** * Act on user account actions. * @@ -106,11 +143,27 @@ */ function regcode_user($op, &$edit, &$account, $category = NULL) { - // Get the array of registration codes. - $regcodes = variable_get('regcode_codes', ''); + $validate = FALSE; + if(module_exists('accounttypes')){ + // Get the array of registration codes by account type. + $account_types_regcodes = array(); + $sql = "SELECT atid, name from {accounttypes}"; + $result = db_query($sql); + + while ($at = db_fetch_object($result)) { + $account_types_regcodes[$at->atid] = variable_get('regcode_codes_account_type_' . $at->atid, ''); + if(strlen(trim($account_types_regcodes[$at->atid]))) + $validate = TRUE; + } + } else { + // Get the array of registration codes. + $regcodes = variable_get('regcode_codes', ''); + if(strlen(trim($regcodes))) + $validate = TRUE; + } // Only do the checking if the codes variable is set. - if (strlen(trim($regcodes))) { + if ($validate) { switch ($op) { case 'register': @@ -125,16 +178,69 @@ 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); - } + if(module_exists('accounttypes')){ + $regcode_valid = FALSE; + foreach($account_types_regcodes as $atid => $account_type_regcodes){ + $account_type_regcodes = explode("\n", $account_type_regcodes); + array_walk($account_type_regcodes, create_function('&$a', '$a = trim($a);')); + if (in_array(trim($edit['regcode_code']), $account_type_regcodes)) { + //Check previous codes used before + $nofusers=db_result(db_query("select count(*) from {regcodes} where code='%s' and account_type='%s'",trim($edit['regcode_code']),$atid)); + if (!$nofusers) { + $regcode_valid = TRUE; + } + break; + } + } + + if(!$regcode_valid){ + form_set_error('regcode_code', t('The registration code you provided has been used before or is invalid')); + watchdog('regcode', t('User entered a registration code used before or is invalid: ') . trim($edit['regcode_code']) , WATCHDOG_WARNING); + } + } + else { + // 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); + }else { + $nofusers=db_result(db_query("select count(*) from {regcodes} where code='%s'",trim($edit['regcode_code']))); + if ($nofusers) { + form_set_error('regcode_code', t('The registration code you provided has been used before')); + watchdog('regcode', t('User entered a registration code used before: ') . trim($edit['regcode_code']) , WATCHDOG_WARNING); + } + } + } } break; + case 'insert': + if(module_exists('accounttypes')){ + foreach($account_types_regcodes as $atid => $account_type_regcodes){ + $account_type_regcodes = explode("\n", $account_type_regcodes); + array_walk($account_type_regcodes, create_function('&$a', '$a = trim($a);')); + if (in_array(trim($edit['regcode_code']), $account_type_regcodes)) { + //Insert regcode by account in database + db_query("insert into regcodes(uid,code,account_type) values(%d,'%s','%s')",$account->uid,trim($edit['regcode_code']),$atid); + //Remove the code from the availability list + $removekey= array_search(trim($edit['regcode_code']), $account_type_regcodes); + unset ($account_type_regcodes[$removekey]); + $regcodestring= join("\n",$account_type_regcodes); + variable_set('regcode_codes_account_type_' . $atid,$regcodestring); + break; + } + } + } else { + //Insert regcode by account in database + db_query("insert into regcodes(uid,code) values(%d,'%s')",$account->uid,trim($edit['regcode_code'])); + //Remove the code from the availability list + $removekey= array_search(trim($edit['regcode_code']), $regcodes); + unset ($regcodes[$removekey]); + $regcodestring= join("\n",$regcodes); + variable_set('regcode_codes',$regcodestring); + } + break; } // switch - } + } }