Referred by in the registration form.
xamox - November 21, 2006 - 15:22
| Project: | User Referral |
| Version: | 5.x-1.x-dev |
| Component: | Code |
| Category: | support request |
| Priority: | normal |
| Assigned: | Unassigned |
| Status: | active |
Jump to:
Description
Not all people will know to send people their link, someone may just say something like, "Hey check out this site" or follow it via a link on their myspace page or whatever. It would be nice if they could fill in the username or user's e-mail whom referred them if they didn't follow it via the referral link.

#1
well until there is an option in the module for this - i added the following code to do what i think you are looking for.
I did this for 2 reasons - my client requested this sort of interface; plus i use rolesignup and registerprofile modules which don't work with the referral link approach
in case it isn't obvious you need to add this to a module such as example.module
/**
* Implement hook_form_alter
*
**/
function example_form_alter($form_id, &$form) {
switch ($form_id) {
case 'user_register':
$form['referral'] = array(
'#type' => 'textfield',
'#title' => t('Referral'),
//'#default_value' => $node->body,
'#description' => t('If you were referred to our site by an existing member, you may enter either
their username on this site or the email address which they have on file with us.'),
'#required' => false);
break;
}
}
/**
* Implementation of hook_user
*/
function example_user($op, &$edit, &$account, $category = NULL) {
switch ($op) {
case 'validate':
global $form_values;
if (isset($form_values['referral'])) {
// did they enter an email address or username?
if (valid_email_address($form_values['referral'])) $check = array('mail'=>$form_values['referral']);
else $check = array('name'=>$form_values['referral']);
// and is it in the db
if (!user_load($check)) form_set_error('referral',"the email address or username that you have entered does not belong to a member of
our site");
}
break;
case 'insert':
global $form_values;
// need to do same checks now as we did to validate - except that we will know the member is valid
if (isset($form_values['referral'])) {
// did they enter an email address or username?
if (valid_email_address($form_values['referral'])) $check = array('mail'=>$form_values['referral']);
else $check = array('name'=>$form_values['referral']);
// and is it in the db
$member = user_load($check);
create_referral($member->uid, $account->uid); // insert the referral into referals table
}
break;
}
}
// insert a new entry in referrals table if someone enters a referrer when they register
// - $old_member is uid of referring member (existing member)
// - $new_member is uid of regsitering member
function create_referral ($old_member, $new_member) {
// create an entry in the referral table
$ts = time();
$host = $_SERVER['REMOTE_ADDR']; // doesn't really mean anything
db_query("insert into referral (uid, referral_uid, created, flag, host, http_referer)
values ($new_member, $old_member, $ts, 0, '$host', '')");
//plus lets add the points for being referred
if (module_exist('userpoints')) {
$points = variable_get(REFERRAL_USERPOINTS, 1);
userpoints_userpointsapi('points', $points, $old_member, 'referral');
}
}
hope this helps.
Peter Lindstrom
LiquidCMS - Content Management Solution Experts
#2
You could do this with the drupal module "Profile". It allows to add several user/admin-defined forms to the registration process. also it makes it possible to add things like gender, title or whatever to profiles, public or private.
regards
http://drupal.org/handbook/modules/profile
#3
ok, but i don't think you understand what this is doing.. or what the original posted asked for.
The field i added does not store any data to the database - it is simply a field to ask for a username (or email) so that it can add entries into the referral table used by the referral module.
Just adding a field with profile module doesn't do anything for me other than add a piece of info to db which i don't need for anything.
#4
does it work on 5.1?
#5
Hi enxox,
I got your email and NO i havent done this for D5 hey. But have you tried it - it might work as is?
You nede to make a module - my code assumes it is called example.module.. basically just add the code into a file called modules/example/example.module
For Drupal 5 you iwll also need an .info file - but they are pretty small simple files - loko at any of the ones in std Drupal install copy it over to modules/example/example.info and edit a couple lines it.
Can't say it will work for sure as i havent done much Dr 5 work yet... but my code is not too complex so it may work as is.
Peter Lindstrom
LiquidCMS - Content Management Solution Experts
#6
Hello Peter,
I just gave it a shot, but ran into this error when registering (with a referred userid entered on the user register page):
Fatal error: Call to undefined function module_exist() in /var/www/modules/referralid/referralid.module on line 64
Any idea why the userpoints part of this code isn't working with Drupal 5?? UserPoints latest version is installed...
'if (module_exist('userpoints')) {'
Regards
#7
If you are using 5.x, then it is module_exists() with an S.
#8
Thanks again kbahey, that fixed my error, and the referee is properly informed that the referrer will receive x number of points.
The above code is working on my Drupal 5.1 instance. Please consider including this functionality in the User Referral module, but of course it needs a lot more testing...
#9
If you (or someone) merges it into the latest 5.x version of referral, and create a patch, I will include it.