Forum access based on gender selection

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();
       }
  }
 
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);
	}
}
 
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?
 
A "not a female" check would cover both: males and users with unspecified gender.
Same with "not a male".
 
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!
 
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?
 
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.
 
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!
 
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.
 
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!
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?
is there a add-on for this ?
I am looking for the same thing......
http://xenforo.com/community/threads/prevent-gender-change.16525/

This modification will prevent users from changing their gender once it has been set.

If a user's gender is unspecified, the user will be allowed to change their gender. Once a gender is specified (either male/female) the user cannot change their gender at all.

Enjoy :D
 
Top Bottom