We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 26503
    • 620 Posts
    Any ideas how I can I find out what group a user was just added to in a plugin? [i.e. the plugin is attached to the OnUserAddToGroup event & needs to check which group the user was added before further processing]
      *** Not just websites, we also create signage, banners, print, trade show displays and more! ***

      Sean Kimball CLP, CLS.
      Technical Director / Sr. Developer | BigBlock Studios
      ._______________________________________________.
      Bigblock Studios http://www.bigblockstudios.ca Web site design & development.
      27-1300 King Street East. Box 167 Oshawa, Ontario L1H8J4 Canada.
      phone/fax: 905-426-5525
      • 3749
      • 24,544 Posts
      The group should be available there as:

      $usergroup


      The $user object ($user) and the modUserGroupMember ($membership) object are also available .



      ------------------------------------------------------------------------------------------
      PLEASE, PLEASE specify the version of MODX you are using.
      MODX info for everyone: http://bobsguides.com/modx.html
        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
        • 9207 ☆ A M B ☆
        • 2,475 Posts
        There's gotta be a last-insert-id you can pull up for that somewhere.
          • 26503
          • 620 Posts
          Looking at the tables, it appears modx edits the group membership records rather than deleting & creating new ones when someone changes groups... so the last insert id thing does not look like it's going to work.

          Is there a way to pass information from one plugin to another?

          i.e. query the user's group membership during the OnUserBeforeAddToGroup then pass that to the plugin running during the OnUserAddToGroup event?
            *** Not just websites, we also create signage, banners, print, trade show displays and more! ***

            Sean Kimball CLP, CLS.
            Technical Director / Sr. Developer | BigBlock Studios
            ._______________________________________________.
            Bigblock Studios http://www.bigblockstudios.ca Web site design & development.
            27-1300 King Street East. Box 167 Oshawa, Ontario L1H8J4 Canada.
            phone/fax: 905-426-5525
            • 9207 ☆ A M B ☆
            • 2,475 Posts
            You could stash data inside the $modx object, since that persists throughout a single request. E.g. stash it as a placeholder.
              • 26503
              • 620 Posts
              Thanks guys, I got it working [or at least the test is working] It also gives me another avenue to work on for the onAfterGroupAdd action...

              in the OnBeforeUserSave event I;

              <?php
              
              $ext = '';
              $ext .= '<pre>';
              $ext .= print_r(json_decode($user->get('groups'),true));
              $ext .= '</pre>';
              
              $modx->setPlaceholder('myPlaceholder', 'this is my placeholder data'.$ext);
              
              $eventname = $modx->event->name;
              
              $modx->log(modX::LOG_LEVEL_ERROR,'System event running = ' . $eventname);
              
              


              then on the On AfterUserSave event:

              
              $mydata = $modx->getPlaceholder('myPlaceholder');
              
              $eventname = $modx->event->name;
              
              $modx->log(modX::LOG_LEVEL_ERROR,'placeholder data = ' . $mydata);
              
              $modx->log(modX::LOG_LEVEL_ERROR,'System event running = ' . $eventname);
              
              


              And am rewarded with this in the logs:

              
              [2012-07-22 17:23:28] (ERROR @ /connectors/security/user.php) System event running = OnUserBeforeSave
              [2012-07-22 17:23:28] (ERROR @ /connectors/security/user.php) [OnUserBeforeSave]Array
              (
                  [0] => Array
                      (
                          [usergroup] => 2
                          [name] => RegisteredUser
                          [member] => 69
                          [role] => 1
                          [rolename] => Member
                          [primary_group] =>
                          [rank] => 0
                          [menu] =>
                      )
              
                  [1] => Array
                      (
                          [usergroup] => 3
                          [name] => Artists
                          [member] => 69
                          [role] => 1
                          [rolename] => Member
                          [primary_group] => 1
                          [rank] => 0
                          [menu] =>
                      )
              
              )
              
              [2012-07-22 17:23:28] (ERROR @ /connectors/security/user.php) placeholder data = this is my placeholder data<pre>1</pre>
              [2012-07-22 17:23:28] (ERROR @ /connectors/security/user.php) System event running = OnUserSave
              





              Looks like the array itself isn't being passed, will have to tinker with it a bit [suggestions?]

                *** Not just websites, we also create signage, banners, print, trade show displays and more! ***

                Sean Kimball CLP, CLS.
                Technical Director / Sr. Developer | BigBlock Studios
                ._______________________________________________.
                Bigblock Studios http://www.bigblockstudios.ca Web site design & development.
                27-1300 King Street East. Box 167 Oshawa, Ontario L1H8J4 Canada.
                phone/fax: 905-426-5525
                • 9207 ☆ A M B ☆
                • 2,475 Posts
                Your print_r statement woud need a "true" in the 2nd parameter, otherwise it just prints to stdout and returns a 1 (i.e. a success message).

                Keep the array as a JSON string, e.g.

                $ext .= $user->get('groups');


                Or use the 2nd parameter in your print_r:

                $ext .= print_r(json_decode($user->get('groups'),true), true);


                For debugging, the latter might be helpful, but for functionality, you'll want to stash the data in a serialized or JSON format.
                  • 26503
                  • 620 Posts
                  What a giant pain in the a** ... addin/removing a user to a group in the manage user section is done before the beforeUserFormSave event, i.e. the group memberships are the same before and after the form is saved... whenever loading a users page in the manager you have. OnUserFormPrerender & OnUserFormRender - the $user object [and thus the groups] do not appear to be available during either event.

                  So now I'm back to square one, I don't see a way to compare the user's groups before and after they were modified!!

                  Is there a way to add a custom event?
                    *** Not just websites, we also create signage, banners, print, trade show displays and more! ***

                    Sean Kimball CLP, CLS.
                    Technical Director / Sr. Developer | BigBlock Studios
                    ._______________________________________________.
                    Bigblock Studios http://www.bigblockstudios.ca Web site design & development.
                    27-1300 King Street East. Box 167 Oshawa, Ontario L1H8J4 Canada.
                    phone/fax: 905-426-5525
                    • 9207 ☆ A M B ☆
                    • 2,475 Posts
                    Is there a way to add a custom event?

                    Yes, but the trick would be firing it. I think you've uncovered something that should be filed as a refactor request. WordPress uses ONLY plugins (triggered by events) for EVERYTHING, but MODX rarely relies on them, and as you're seeing, I think sometimes the events are not thoroughly tested (gulp, hope that doesn't land me in the doghouse).

                    Maybe you can do a cron script that can query the database to see who's in the groups and periodically email the new additions? (I know that's a lousy work-around -- I feel your frustration)
                      • 26503
                      • 620 Posts
                      Thanks, I'll pester some of the modx guys tomorrow to take a look at the threads & bug report and see what can/should be done from there... [ability to add a custom event sure would be nice!]

                      Unfortunately, I'm trying to make thing easier/less manual for the client, currently they have to review registered users who have applied for the next user/group level [allows them to fill in a more advanced profile] so it would have been handy to be able to promote the user to the new group, activate their advanced profile & send an activation notice with instructions etc. all in one go....

                      -sean
                        *** Not just websites, we also create signage, banners, print, trade show displays and more! ***

                        Sean Kimball CLP, CLS.
                        Technical Director / Sr. Developer | BigBlock Studios
                        ._______________________________________________.
                        Bigblock Studios http://www.bigblockstudios.ca Web site design & development.
                        27-1300 King Street East. Box 167 Oshawa, Ontario L1H8J4 Canada.
                        phone/fax: 905-426-5525