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
models/application_user.php
The Log entry never gets called, and the user never gets added. Can anyone see what I'm doing wrong?
Thanks in advance!
-Lucas
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
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
![ScottC](/files/avatars/171.jpg)
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..
Yes I am calling it, even though the docs says it's unnecessary. I'll try the other events...
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/...
http://www.concrete5.org/marketplace/addons/registrant-group/...
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:
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:
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
One thing that was pretty important was to include the following line in the model:
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
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
Maybe that's your problem?
Also you might want to try
$ui = UserInfo::getByID($u->getUserID());
I tried -- 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...
$ui = UserInfo::getByID($u->getUserID());
$nuType isn't an issue, as I'm not even getting a value for $uType.
Still grinding away at this...
oups sorry :/
It might not make a difference but maybe in your model you should modify your class to read
It might not make a difference but maybe in your model you should modify your class to read
class ApplicationUser extends Model{
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.
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.
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!
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!
Hello,
I answered you in your original posthttp://www.concrete5.org/index.php?cID=20169&editmode=...
I answered you in your original posthttp://www.concrete5.org/index.php?cID=20169&editmode=...