We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 15566
    • 73 Posts
    I have the plugin ActivationEmail http://modx.com/extras/package/activationemail by Robray http://modx.com/extras/author/bobray installed on a site. I have users self-register and when an administrator activates them they would get an email - which worked perfectly. Now, my client would like all users to self-register and become activated straight away but if the administrator is happy they will add them to a user group. I would like an email to go out once they have been added to a group (not all groups, just one in particular), is this possible?
      • 3749
      • 24,544 Posts
      I think it's quite possible, but it would take a different (but very similar) plugin. It would be connected to the OnUserAddToGroup System Event, but I'm not sure it would be reliable. It depends on whether MODX invokes that event when adding a user to a group. It definitely would not work when users are added to a group in code without calling the add processor.

      Here's a way to check (untested):

      Create a plugin with this code, connected to OnUserAddToGroup and OnUserGroupFormRemove:

      $event = $modx->event->name;
      $userName = $user->get('username');
      $groupName = $usergroup->get('name');
      
      switch($event) {
         case 'OnUserAddToGroup':
             $modx->log(modX::LOG_LEVEL_ERROR, 'User: ' . $userName . ' added to ' . $groupName);
      
             break;
      
         case 'OnUserGroupFormRemove':
             $modx->log(modX::LOG_LEVEL_ERROR, 'User: ' . $userName . ' removed from ' . $groupName);
      
             break;
      }
      


      Add and remove a few users from groups using the various methods in the Manager and see if you get a message in the error log.


      If it works, the plugin could be modified to send the email in each case.



        Did I help you? Buy me a beer
        Get my Book: MODX:The Official Guide
        MODX info for everyone: http://bobsguides.com/modx.html
        My MODX Extras
        Bob's Guides is now hosted at A2 MODX Hosting
        • 15566
        • 73 Posts
        Hmm, doesn't work. No logs and if I get it to send an email instead I also get nothing (just double checking). It is from the main user management interface that I need to trigger it from (not from any front-end code).

        I have also tried using the ActivationEmail plugin and added this in:

        $memberbefore = $dbUser->isMember('My Group');
        $memberafter = $user->isMember('My Group');

        But it doesn't seem to work. Even when I tie it to all sorts of different events they are either both true or both false.
          • 3749
          • 24,544 Posts
          If you're not getting anything in the error log, that would suggest that MODX is not using the processors when adding and dropping user group members, but rather dealing with the modUserGroupMember object directly, which would be faster -- so that method is out (assuming that you correctly connected the plugin to the events and there are no typos in my code).

          For you second method, I think you'd need to consult the DB directly by looking for a modUserGroupMember object before and after. Also, remember that that plugin only fires when a user is saved, which doesn't necessarily happen when you add a user to a user group.



            Did I help you? Buy me a beer
            Get my Book: MODX:The Official Guide
            MODX info for everyone: http://bobsguides.com/modx.html
            My MODX Extras
            Bob's Guides is now hosted at A2 MODX Hosting
            • 15566
            • 73 Posts
            I have something working. It could be better, but does the job...

            session_start();
            
            if($modx->event->name == 'OnUserFormPrerender' && !$user->isMember('My Group')){
               $_SESSION['newuser'] = "YES"; // Set a Session
            }
            
            else if($modx->event->name == 'OnUserFormSave' && $_SESSION['newuser'] == 'YES' && $user->isMember('My Group')){
            	$_SESSION['newuser'] = ''; // Clear the session data
            	// Send the email etc. here
            }
            [ed. note: keiron77 last edited this post 12 years, 9 months ago.]
              • 3749
              • 24,544 Posts
              Looks good to me. Is session_start() necessary? MODX should have already started one.

                Did I help you? Buy me a beer
                Get my Book: MODX:The Official Guide
                MODX info for everyone: http://bobsguides.com/modx.html
                My MODX Extras
                Bob's Guides is now hosted at A2 MODX Hosting