General developer forum

Automatically add user to a group based on another group

 
Picture of J C
Automatically add user to a group based on another group
 

Hi there!

Does anyone know how to setup an event-driven assignment of a user to an existing group based on their membership in a different group?  Events could include a page load, after enrollment, when a user is assigned to a group, etc.

For example, say I have groups 1, 2, 3, 4, A, and B already existing in Moodle.  Anytime someone is added to either group 1 or 2, I would like them to also be added to group A.  Anytime someone is added to either group 3 or 4, I would like them to be also added to group B.

Any advice is appreciated!  Thanks!

 
Average of ratings: -
Tim at Lone Pine Koala Sanctuary
Re: Automatically add user to a group based on another group
Group Core developersGroup Documentation writersGroup Particularly helpful MoodlersGroup Plugin developers

You need to add an even observer, which listens to the \core\event\group_member_added event. When you get one, check the course id, and which group the user is being added to, and the do what you want if necessary. https://docs.moodle.org/dev/Event_2#Event_dispatching_and_observers

 
Average of ratings: -
Picture of Paul Holden
Re: Automatically add user to a group based on another group
Group Core developersGroup Particularly helpful MoodlersGroup Plugin developers

The Meta-course group synchronization plugin does something similar; it listens for group related events to create/update other groups - you should be able to modify this to suit your needs.

 
Average of ratings: -
Picture of J C
Re: Automatically add user to a group based on another group
 

Thanks for the replies!  I'll look into the code for that plugin and see if I can modify it to my needs.  In the mean time, if anyone else sees this post and happens to know of an existing plugin that does this, then please let me know!

 
Average of ratings: -