place new member into groups during registration

Permalink 3 users found helpful
Hi,

I really need to know how to do this.

I had a read of this pagehttp://www.concrete5.org/community/forums/customizing_c5/place-regi...

and the linked pagehttp://www.concrete5.org/documentation/developers/system/events/...

But I would really appreciate seeing a concrete example somewhere.
When I tried to follow the example I just ran into some weird problems and ended up adding a new nav on the top of the site???

So I deleted that page from config/ but I'm kinda lost.

It seems like it should be quite easy.

Could some kind hearted C5 guru point me in the right direction please?

Nige

nige
 
jshannon replied on at Permalink Reply
jshannon
Where do you want this to work "from"? The following will work if the user is added through registration but not through the dashboard "add user" page (in which case you should be manually selecting the user groups):

Create a new file called /config/site_events.php and paste the following in:

<?php
Events::extend('on_user_add', 'SiteEvents', 'addUser', false);
class SiteEvents {
   public static function addUser($ui) {
      $g = Group::getByName('GROUP NAME');
      $ui->getUserObject()->enterGroup($g);
   }
}
?>
nige replied on at Permalink Reply
nige
Great thanks for this. Good Karma to you

Just to clarify:

<?php
Events::extend('on_user_add', 'SiteEvents', 'addUser', false);
class SiteEvents {
   public static function addUser($ui) {
      $g = Group::getByName('Vendors');
      $ui->getUserObject()->enterGroup($g);
   }
}
?>


Once this is done do I add a checkbox or something to the registration info required that says "Vendors"?

Then when they register they get an option to check this box to join the Group "Vendors".

If so is it possible to allow them to choose from several different groups?

Thanks for the help.

Nigel
jshannon replied on at Permalink Reply
jshannon
No. 'Vendors' is the name of the group that I was testing this with.

I thought you wanted to add them to an arbitrary group, like "Users who registered themselves in July". That's what my code would do. (Replace "Vendors" with "Your arbitrary user group".)

If you want to allow them to choose their own groups:

1. I don't know how important it is that you use "groups". You might want to look at user attributes, which can be set up so that the user has to enter them at login. You shouldn't have to fiddle with the login page.

2. Otherwise, you'll have to use some combination of what I've done and a custom login page template (by copying /concrete/single_pages/login to /single_pages/login and modifying). You'd manually create the checkboxes, like:

Choose your group(s):
<input type="checkbox" name="groups[]" value="Group Name 1" /> Group 1
<input type="checkbox" name="groups[]" value="Group Name 2" /> Group 2


Then you'd need to modify the event that I provided to do something like:

foreach ($_REQUEST['groups'] as $groupName) {
  $g = Group::getByName[$groupName];
  $ui->getUserObject()->enterGroup($g);
}
nige replied on at Permalink Reply
nige
OK I'll take your advice. I do want to add them to a pre-created group, just more than one.

I'll start in the simplest way and see how it works.

Thank you.

Nige
crossbones replied on at Permalink Reply
Hey jshannon,
Could you give me a hand? I used that exact code (replacing the foreach for the $g and $ui stuff from the previous post) into /config/site_events.php as well as /concrete/config/site_events.php. I also added two checkboxes to my register page. I edited it a bit to say:
<input type="checkbox" name="groups[]" value="Teachers" /> Teachers
<input type="checkbox" name="groups[]" value="Students" /> Students

The name of the groups are Teachers and Students. I thought case might matter so I've matched it. Thanks in advance for your help!
ThomasJ replied on at Permalink Reply
ThomasJ
This is a very old thread and I hope that it is still being monitored.

I am using C5.6.3.2. I tried this foreach loop and with it in the addUser event, pages will not load. I believe that it is because the array "$_REQUEST['groups']" does not yet exist and cannot be used in the foreach statement before it is declared or $_REQUEST just doesn't exist in this newer version. Anyone have any thoughts or suggestions?

Thanks.
dilio replied on at Permalink Reply
dilio
Thank you for this. It works well!
jordanlev replied on at Permalink Reply 1 Attachment
jordanlev
Try this -- download the attachment, unzip it, place it into your site's "packages" directory (be careful that when you unzip it you only get one folder, not two folders with the same name inside each other). Install from Dashboard -> Add Functionality, then go to Dashboard -> Users and Groups -> Registrant Group.

Let me know how it works out -- I'll throw it up on the marketplace if it works okay (I tested it on a dev site and seemed to work fine, so I don't expect there to be issues, it's really simple).

-Jordan


EDIT: I now have this packaged up more nicely and available for free in the marketplace:
http://www.concrete5.org/marketplace/addons/registrant-group/...
jordanlev replied on at Permalink Reply
jordanlev
Huh, just saw a more recent reply from you that you want them to be able to select their own group in the reg process -- this is a bit more complex than my package will handle -- probably best to go along with what jshannon suggested in that case.
nige replied on at Permalink Reply
nige
No Jordan thank you. Thats brilliant too. It might just do what I want. Appreciate your help. Also I'll be able to have a good look at the block and I might learn something.

I'd love to be able to crack things like this out.

Nige
jshannon replied on at Permalink Reply
jshannon
Nice, ummm, package.

If you add to the marketplace, you should add the caveat somewhere that it won't work for dashboard-based registrations.
jordanlev replied on at Permalink Reply
jordanlev
Is that true? I believe you because you seem to have more, ummm, experience with this than I do. But in the documentation page under the "Example" section it says "This could happen through the dashboard, or through front-end registration".
The docs has plenty of errors in it so I wouldn't be surprised though (just want to make sure).
jshannon replied on at Permalink Reply
jshannon
Yeah. It was driving me nuts when I was testing my event yesterday, especially because printing out debug info and then die'ing worked fine.

The dashboard page creates the user (which triggers the event) then calls $user->putUsersInGroups($arrayOfGroups) (can't remember the name). The function adds and removes as appropriate, thus removing the user from whatever group you just added them to.
jordanlev replied on at Permalink Reply
jordanlev
Ahh... I see. So the event is called when adding from the dashboard, but for this specific use case of putting users in groups there's a weird side-effect with negates it. Very interesting...
That's a bummer that you had to waste so much time discovering that, but for my own intended usage I actually think this will work out well (I want self-registered users to go into one group, and users created by the admins to go into other groups).

Thanks for the info!

-Jordan
jshannon replied on at Permalink Reply
jshannon
Yeah. That was a better way of phrasing it.

And I agree that your use (case) works out fine, and most people who want the auto-group join are thinking in terms of registration not dashboard (where they can choose the groups manually)... I just wanted you to be aware of the bug and mention it somewhere, so that users know what to expect and don't have the same confusion i did.
Scafidi454 replied on at Permalink Reply
Scafidi454
It looks like there's a feature built-in to set the users in a specific group during registration called "This group is automatically entered."

However, for your case (allowing someone to choose their own group), a custom code might be the answer.

However, if you're dealing with vendors, would it be better practice to require confirmation of vendor license before allowing those types of accounts?