1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Forum access based on gender selection

Discussion in 'XenForo Development Discussions' started by Alien, Mar 4, 2011.

  1. Alien

    Alien Well-Known Member

    Hello.

    On my site, we previously had the ability (via a very simple custom vB plug-in) to allow posting in a male or female-specific forum.

    If you were male, you could post in the male forum but only read in the female forum.
    If you were female, you could post in the female forum but only read in the male forum.
    If you were undisclosed (gender not chosen), you could read in both but not post in either.

    We had added this plug-in to the old vB hook "newreply_start" and "newthread_start"

    Here was the code we had used, anyway to get it to function via XF?

    PHP:
    if ($foruminfo['forumid'] == 34 or $foruminfo['forumid'] == 35)
      {
           
    // yes, it is
           
    if ($foruminfo['forumid'] == 34 and $vbulletin->userinfo['field5'] == 'Female')
           {
                
    print_no_permission();
           }
           if (
    $foruminfo['forumid'] == 35 and $vbulletin->userinfo['field5'] == 'Male')
           {
                
    print_no_permission();
           }
           if (
    $vbulletin->userinfo['field5'] != 'Male' and $vbulletin->userinfo['field5'] != 'Female')
           {
                
    print_no_permission();
           }
      }
     
    erich37 likes this.
  2. Shadab

    Shadab Well-Known Member

    The ideal place for this customisation is the Model class for Forums and Threads.

    The Forum model (XenForo_Model_Forum) has a method canPostThreadInForum() which you can override in your own class to perform additional permission checks. Similarly, you can override canReplyToThread() in the Thread model (XenForo_Model_Thread).

    Note:
    If you don't know how to create an Event Listener for the purpose of extending a class, follow this tutorial posted by Kier. Although the tutorial describes how to dynamically extend a Controller, you can easily adapt it for a Model class.

    Anyway, here's an example class for the extended Thread model...
    PHP:
    /**
     * Dynamically extended Thread model.
     */
    class Alien_Model_Thread extends XFCP_Alien_Model_Thread
    {
        
    /**
         * Performs additional permission check based on
         * the Forum ID and the Viewing User's Gender.
         */
        
    public function canReplyToThread(array $thread, array $forum, &$errorPhraseKey '', array $nodePermissions null, array $viewingUser null)
        {
            
    $this->standardizeViewingUserReferenceForNode($thread['node_id'], $viewingUser$nodePermissions);

            
    // ForumID is available in: $thread['node_id']
            // And User's Gender is in: $viewingUser['gender']

            
    if ($thread['node_id'] == XY)
            {
                if (
    $viewingUser['gender'] != 'male')
                {
                    
    // Not a male in a Male forum
                    
    return false;
                }
            }
            else if (
    $thread['node_id'] == XX)
            {
                if (
    $viewingUser['gender'] != 'female')
                {
                    
    // Not a female in a Female forum
                    
    return false;
                }
            }

            return 
    parent::canReplyToThread($thread$forum$errorPhraseKey$nodePermissions$viewingUser);
        }
    }
     
    Onimua, BRiT and Alien like this.
  3. Alien

    Alien Well-Known Member

    Hey, I really appreciate that. I'll take a look and test out integrating this and report back!

    How would it handle no gender selected?
     
  4. Shadab

    Shadab Well-Known Member

    A "not a female" check would cover both: males and users with unspecified gender.
    Same with "not a male".
     
    Alien likes this.
  5. Alien

    Alien Well-Known Member

    Thanks again!
     
    Shadab likes this.
  6. ToDie4

    ToDie4 Well-Known Member

    Probably need to make sure gender selection is allowed during registration only (or altered by admins only there after), what would stop users changing gender in profiles to start posting in other forums other wise!
     
  7. Alien

    Alien Well-Known Member

    I know I had set up that requirement at registration (and no change afterward without an admin doing it) in vB, but unsure if this can also be done in XF yet?
     
  8. erich37

    erich37 Well-Known Member

    is there a add-on for this ?
    I am looking for the same thing......
     
  9. Alien

    Alien Well-Known Member

    Not at this time.

    While the code now exists for the forums, there is not yet code available to limit gender selection at registration ONLY.

    I don't want the ability to go back and forth with it editable in profile.
     
    erich37 likes this.
  10. erich37

    erich37 Well-Known Member

    having it for the forum should be fine for the moment... ? For the registration we could have it later.
    Where do you find the code for this? Is there an add-on?

    Many thanks!
     
  11. Alien

    Alien Well-Known Member

    If I get this resolved through custom coding, I will make sure it is released here freely.
     
    erich37 likes this.
  12. Alien

    Alien Well-Known Member

  13. erich37

    erich37 Well-Known Member

  14. Alien

    Alien Well-Known Member

    Not yet. Wasn't all too different than what was discussed here in the end, my issue is still preventing the change of gender only by admins and my members ONLY at registration. ;)

    I may just use this for the time being however, during the time I work on the next 1.0.2 update I guess.
     
  15. James

    James Well-Known Member

    http://xenforo.com/community/threads/prevent-gender-change.16525/

    Enjoy :D
     
    Alien likes this.

Share This Page