On March 26, 2019 we launched new MODX Forums. Please join us at the new MODX Community Forums.
Subscribe: RSS
  • Hello.

    In one script I wrote this (tacken from ’Register’ snippet):
    // Group ID for user membership
    $eventGroup = 4;
    
    // Add to group
    $member = $modx->newObject('modUserGroupMember');
    $member->set('member', $modx->user->id);
    $member->set('user_group', $eventGroup);
    $member->save();
    


    Then in another code I’m using this code to check user is member of group (taken from here http://rtfm.modx.com/display/revolution20/modUser.isMember):

    // Get group object
    $result = $modx->db->query('SELECT * FROM ' . $modx->getFullTableName("membergroup_names") . ' WHERE id = ' . $eventGroup);
    $group = $modx->db->getRow($result);
      
    $check = $modx->user->isMember($group['name']);
    var_dump($check); // print "false"
    


    How can I check membership?

    • This should do it (assuming that you want it for the current user):

      if ($modx->user->isMember('GroupName') ) {
        return "user is a member";
      } else {
        return "not a member";
      }
      


      You can also send an array of group names in the argument to isMember.
        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
      • I didn’t see differences between your code and mine.

        My code:

        // Get group object
        $result = $modx->db->query('SELECT * FROM ' . $modx->getFullTableName("membergroup_names") . ' WHERE id = ' . $eventGroup);
        $group = $modx->db->getRow($result); // here I have array('name' => 'GroupName', ...)
        
        // Trying to check
        $check = $modx->user->isMember($group['name']); // $group['name'] == 'GroupName'
        var_dump($check); // print "false"


        I know, that user is in the ’GroupName’ group. I can see it in MODX Manager interface and in the database. But ’$modx->user->isMember($group[’name’])’ returns false. Why?
        • Sorry, your subject line misled me. Your code for checking if a user is in a group is correct.

          I think you might want this:

          $userGroup = $modx->GetObject('modUserGroup', array ('id'=>$event) );
          
          return isMember($userGroup->get('name'));
            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
          • I used your code. I have this now:
            
            // Group ID
            $eventGroup = 4;
            
            // Get group
            $group = $modx->GetObject('modUserGroup', array ('id' => $eventGroup));
            
            // Check
            $check = $modx->user->isMember($group->get('name'));
            var_dump($check); // print 'bool(false)'
            
            var_dump($modx->user->id); // print 'int(1)'
            


            But in database I see:


            So, result of isMember is wrong. Why?
            • Hello

              Not sure if this can help, but if I compare your code and the Bob’s one there’s a difference in the get group part

              YOUR code
              // Get group
              $group = $modx->GetObject('modUserGroup', array ('id' => $eventGroup));
              
              BOB's code
              $modx->GetObject('modUserGroup', array ('id')=>$event) ); 
              


              => parenthesis in Bob’s code are around (’id’), in your code parenthesis begins before ’id’ but is closed after $eventGroup ... However, no idea if it can explain your problem...
              Cheers
              • Bob has typo in his code
                • Quote from: noff at Aug 11, 2010, 09:28 AM

                  Bob has typo in his code

                  Fixed. Thanks.
                    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
                  • Quote from: noff at Aug 11, 2010, 09:02 AM

                    So, result of isMember is wrong. Why?

                    Let’s back up.

                    I’m having trouble figuring out why you want to know if a user is a member of a group you don’t know the name of.

                    Can you explain exactly what you are trying to do and why, without mentioning any MODx methods? In Revolution, there’s a very good chance that you can solve the problem with no code at all by using the Access Control system.
                      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
                    • Ok. English is not my native lang, but I’ll try.

                      I have site hackday.ru

                      At site I have Events (HackDay’s):
                      http://s3.amazonaws.com/floomby/8_11_2010/1vLyhgfv2Uic4unxC7lw.jpg

                      Every HackDay is a single event with separate auditory.

                      Every user can join to any event.

                      Membership of user in event I checking by:
                      1. For every event there is unique usergroup: http://s3.amazonaws.com/floomby/8_11_2010/z2oTGNAuZk2KpTdnuJLGIw.jpg
                      2. If user is in the group, he is a member of event.

                      So, if he is not a member of event/group, he can join it. Else he can leave this event/group.

                      I can check current group/event. It works. In my code I know group name - I got it from database.

                      But when I trying to use $modx->user->isMember() it telling me, that user is not in this group. But I see in database, he is member of this group.

                      PS: I am telling about webusers, registered buy "Login" snippet.
                      PPS: I am trying to use groups because I see this is single method to use native MODX features without creating additional tables in database.