I posted this in the post installation forum (http://drupal.org/node/90424), however this is probably a more appropriate location. I'm trying to add the functionality of automatically adding a buddy into the buddylist for every new user registration. I'm assuming that adding the following code, or something like it, to a user registration function, would do the trick.

db_query('INSERT INTO {buddylist} (received, uid, buddy, timestamp) VALUES (1, %d, %d, %d)' , $user->uid , '1', time());

Question 1: is this the correct line, syntax and all, for this functionality?

Question 2: what function, or where, can I add this to achieve this functionality?

Any help is greatly appreciated.

-Jesse

Comments

rkn-dupe’s picture

Have you seen this:

http://drupal.org/node/75884

apt94jesse’s picture

Thanks for the response, I had seen that, but that seems to be more of a "how they want it done" rather than "how it can be done."

I got my functionality to work like this. I added these lines in the page.tpl.php:

$buddycount = db_query('SELECT COUNT(*) FROM {buddylist} WHERE uid = %d', $GLOBALS['user']->uid);
  $buddyobject=db_result($buddycount, $row=0);
  if ($buddyobject=='0') {
  db_query('INSERT INTO {buddylist} (received, uid, buddy, timestamp) VALUES (1, %d, %d, %d)' , $GLOBALS['user']->uid , '1' , time());
  }

As you can see, I query the buddylist table, count the entries where the current user has buddies. If that count is 0, then it executes the insert query adding the default buddy. This way, whenever a new user logs in for the first time, their buddy count is of course 0, so it adds the buddy into the table.

vm’s picture

Thanks for the snippet.

scenario. user logs in , snippet sees no buddies, snippets adds default buddy. user deletes default buddy. logs out, logs back in, default buddy added again ?

apt94jesse’s picture

Good point. Honestly I'm not worried about it because for my site I don't really want users to be able to delete the admin. Your scenario forgot to say that "user deletes default buddy and has no other buddies." Feel free to come up with a better function to add this sql code to, mine was chosen as the first one to work, not the best one.

Thanks for the interest, though, I hope someone wiser than me comes up with a better solution.

apt94jesse’s picture

In case anyone is interested, I fixed the above scenario by adding these two lines to the user.module. This actually executes two queries, one is adding a default buddy, the other is sending an opening private message from said buddy.


db_query('INSERT INTO {buddylist} (received, uid, buddy, timestamp) VALUES (1, %d, %d, %d)' , $user->uid , '1' , time());


db_query("INSERT INTO {privatemsg} (author, recipient, subject, message, timestamp, newmsg, hostname, format) VALUES ('1', '%d', 'MESSAGE SUBJECT', 'MESSAGE CONTENT', '%d', '%d', '%s', '0')", $user->uid, time(), 1, getenv("REMOTE_ADDR"));

The first query adds the buddy. Just change the '1' near the end to change the default buddy. I used 1 because I want the admin to be the default.

The second query sends the pm. Change the MESSAGE SUBJECT and MESSAGE CONTENT sections to suit your needs.

And now for placement. This is works in 4.7.4. You need to open your user.module file and navigate to the user_save function.

Find the following lines of code:

// Build the finished user object.
    $user = user_load(array('uid' => $array['uid']));

The commented line should start at line 212.

Then just insert the two queries directly after those lines.

Mine looks as follows:

// Build the finished user object.
    $user = user_load(array('uid' => $array['uid']));
    db_query('INSERT INTO {buddylist} (received, uid, buddy, timestamp) VALUES (1, %d, %d, %d)' , $user->uid , '1' , time());
    db_query("INSERT INTO {privatemsg} (author, recipient, subject, message, timestamp, newmsg, hostname, format) VALUES ('1', '%d', 'Welcome', 'Thank you for joining WrestleSpace.com.  As this
    is currently a Beta Service, we are constantly updating our features.  Look around and enjoy, and we will send you updates on things we change.', '%d', '%d', '%s', '0')", $user->uid, time(), 1, getenv("REMOTE_ADDR"));

As I just implemented this, there may be some bugs. I have determined that the user_save function is used both to update information and to save a new user. I tested to see if the queries were executed when a user updates their account info, and they don't appear to.

Should anyone know of a better way to do this, let me know, or tell me what I'm doing wrong.

-Jesse

fago’s picture

please do it properly and provide a patch for it.
you can react on user-creation by using hook_user()

apt94jesse’s picture

I'm honestly not quite sure how to do that. If someone with more knowledge than me would like to do so, go right ahead. I'm just happy that a solution exists.

rkn-dupe’s picture

subscribe

kbahey’s picture

Version: 4.7.x-1.x-dev » 5.x-1.x-dev

Fago

I wrote a contrib module for 5.x that adds a predefined user to the buddy list for new users.

I can commit it to the contrib directory directly. Are you Ok with that?

robertdouglass’s picture

Khalid, please feel free to give buddylist any loving treatment that you feel it needs. It is not getting much attention from me at the moment, so your help is very welcome.

kbahey’s picture

Status: Active » Fixed

There is now a module called buddylistautoadd under the contrib directory that does this.

droople’s picture

Please supply the download link

thank you

kbahey’s picture

There is no download link needed.

It is included already in the download of buddylist.

Anonymous’s picture

Status: Fixed » Closed (fixed)