Events not hooked up?

Permalink
Heya everyone --

In my site, during registration a new user needs to choose a group to join (student, parent, alumni). I then need to assign that new user to the group. I'm using 5.6.

So I'm trying to set up an event listener, and have the following:

config/site_events.php
<?php  defined('C5_EXECUTE') or die("Access Denied.");
   Events::extendPageType('user_blog_post', 'on_page_add');
   Events::extendPageType('user_blog_list', 'on_page_add');
   // Added by Lucas
   Events::extend('on_user_add', 'ApplicationUser', 'placeNewUserInGroup', 'models/application_user.php');
?>


models/application_user.php
class ApplicationUser {
    public function placeNewUserInGroup($u) {
   $ui = UserInfo::getByID($u->getUserID());
   $uType = $ui->getAttribute('generic_type');
   $nuType = str_replace("<br/>", "", $uType);
   Log::addEntry($nuType);
        $g = Group::getByName($nuType);
        $u->enterGroup($g);
    }
}


The Log entry never gets called, and the user never gets added. Can anyone see what I'm doing wrong?

Thanks in advance!

-Lucas

lucashaley
View Replies:
ScottC replied on at Permalink Reply
ScottC
i'd try calling the event from another standard event to make sure that the event manager is finding your class correctly. I'm currently working with on_page_view and on_page_output and those are both working correctly. Are oyu calling Events::enableEvents(); ? Just a shot in the dark there..
lucashaley replied on at Permalink Reply
lucashaley
Yes I am calling it, even though the docs says it's unnecessary. I'll try the other events...
mnakalay replied on at Permalink Reply
mnakalay
You should have a look at this free add-on by jordanlev, it allows selecting a default group for new registrations and does almost exactly what you want. I am sure the code will help you.

http://www.concrete5.org/marketplace/addons/registrant-group/...
lucashaley replied on at Permalink Reply
lucashaley
Thanks for the help! I've made some progress, but not quite there yet.

One thing that was pretty important was to include the following line in the model:

<?php  defined('C5_EXECUTE') or die("Access Denied.");


Now I've got the model responding to the new user event.

The issue that remains is that I cannot get any of the user attributes. In my registration, I have a select user attribute with the three group names (aulmni, parent, and student). I'd like to read the new user's selection, and add them to that particular group. It seems like this would be pretty straightforward. My code now looks like this:

<?php  defined('C5_EXECUTE') or die("Access Denied.");
class ApplicationUser {
  public function placeNewUserInGroup($u) {
    Loader::model('groups');
    $ui = UserInfo::getByID($u);
    $uType = $u->getAttribute('generic_type');
    // $nuType = str_replace("<br/>", "", $uType);
    Log::addEntry("User: ".$u);
    Log::addEntry("UserInfo: ".$ui);
    Log::addEntry("uType: ".$uType);
    // Log::addEntry("nuType: ".$nuType);
    $g = Group::getByName($nuType);
    $u->enterGroup($g);
  }
}


You might be able to tell from my logging that I am unsure of the following:

1) if the $u input parameter is the UserInfo itself, or just a reference number that I need to use to get the UserInfo
2) if the getAttribute function actually works.

In my log, I get the first log spitting out $u, but neither of the following log entries have any information.

Once I get that, it seems like adding the user to the group is trivial.

I'd appreciate any insight anyone might have!

-Lucas
mnakalay replied on at Permalink Reply
mnakalay
it seems you are trying to get the group by name by using $nuType while it was not defined. The line where it should be defined is commented out.

Maybe that's your problem?

Also you might want to try
$ui = UserInfo::getByID($u->getUserID());
lucashaley replied on at Permalink Reply
lucashaley
I tried
$ui = UserInfo::getByID($u->getUserID());
-- see my original post. It just doesn't work for me.

$nuType isn't an issue, as I'm not even getting a value for $uType.

Still grinding away at this...
mnakalay replied on at Permalink Reply
mnakalay
oups sorry :/

It might not make a difference but maybe in your model you should modify your class to read
class ApplicationUser extends Model{
lucashaley replied on at Permalink Reply
lucashaley
Alright, I figured this out. The issue is that C5 was firing the on_user_add event before the UserInfo was attached to the user, so the $u variable didn't have any information.

So I needed to override the register user function to add a on_user_info event, and it worked.

I'm away from the code right now, I'll post later.
brabin replied on at Permalink Reply
Hi there,

I read this thread with interest as I'm trying to do just what you describe. I do not know how to write code but would really like to be able to implement your solution, if you would be so kind as to make it available.

What would be even better is if this code could be incorporated as an option within jb1's great add-on:
http://www.concrete5.org/marketplace/addons/register-user-pro/...

which currently allows defining a single, non-user-selectable group to join upon user registration.

Thanks!
mnakalay replied on at Permalink Reply
mnakalay
Hello,
I answered you in your original posthttp://www.concrete5.org/index.php?cID=20169&editmode=...