Auto Assign To Group Based On Age.

That's really strange, because I couldn't find anything in the recent 2 releases that might conflict with your script. Checking the diffs for user datawriter, base datawriter class, user profile model, locale, etc. doesn't reveal any significant change.

Unless I've missed some other dependency?
 
I haven't any other mods, except for parsehtml, bbc manager, viglink, and template helper for usergroups.
 
Perhaps there should be something in the script that says remove group if age doesn't = group?

Although at the same time, it worked just fine.

If I try to add or remove secondary groups of that user and select save, the page refreshes, but no changes. If I remove this mod(remove event listener), i can add and remove groups of the user just fine. Before, it wasn't a problem...

Here is the exact script I'm using in the user.php:
Code:
class Xarcell_DataWriter_User extends XFCP_Xarcell_DataWriter_User
{
    protected function _preSaveDefaults()
    {
        if ($this->get('dob_day') && $this->get('dob_month') && $this->get('dob_year'))
        {
            // Get age
            $age = $this->getModelFromCache('XenForo_Model_UserProfile')->calculateAge(
                $this->get('dob_year'), $this->get('dob_month'), $this->get('dob_day')
            );

            // Get mapped usergroup
            $newUsergroup = $this->_getUsergroupByAge($age);

            // Get existing secondary usergroups as an array
            $secondaryUsergroups = explode(',', $this->get('secondary_group_ids'));
            if($newUsergroup != null)
            {
                // if its not in the array...
                if(!in_array($newUsergroup, $secondaryUsergroups))
                {
                    // add it.
                    $secondaryUsergroups[] = $newUsergroup;
                }
            }
            // Set the new list of secondary usergroups
            $this->setSecondaryGroups($secondaryUsergroups);
        }

        parent::_preSaveDefaults();
    }

    protected function _getUsergroupByAge($age)
    {
        if($age > 12 && $age < 100)
        {
            return $age - 2;
        }
    }
}

Auto-assign ages 13-99, group ID's 11-97

Since there is an error with the saving of group changes, is there a way to debug it? I have my forum in debug mode. I see no errors listed anywhere...
 
Sorry, I forgot. Updating this post in a minute.

The issue was that $this->get('secondary_group_ids') was fetching the existing/saved secondary groups of a user. In case the updated secondary usergroup list is already set somewhere else, it's not reflected in the "secondary_group_ids" field. So fetching this field returns stale data.

PHP:
<?php

class Xarcell_DataWriter_User extends XFCP_Xarcell_DataWriter_User
{
	protected function _preSaveDefaults()
	{
		if ($this->get('dob_day') && $this->get('dob_month') && $this->get('dob_year'))
		{
			// Get age
			$age = $this->getModelFromCache('XenForo_Model_UserProfile')->calculateAge(
				$this->get('dob_year'), $this->get('dob_month'), $this->get('dob_day')
			);

			// Get mapped usergroup
			$newUsergroup = $this->_getUsergroupByAge($age);

			if ($this->_secondaryGroups !== null)
			{
				// secondaryGroups field already populated?
				$secondaryUsergroups = $this->_secondaryGroups;
			}
			else
			{
				// Get existing secondary usergroups as an array
				$secondaryUsergroups = explode(',', $this->get('secondary_group_ids'));
			}

			if ($newUsergroup != null)
			{
				// if its not in the array...
				if (!in_array($newUsergroup, $secondaryUsergroups))
				{
					// add it.
					$secondaryUsergroups[] = $newUsergroup;
				}
				// Set the new list of secondary usergroups
				$this->setSecondaryGroups($secondaryUsergroups);
			}
		}

		parent::_preSaveDefaults();
	}

	protected function _getUsergroupByAge($age)
	{
		if ($age > 12 && $age < 100)
		{
			return $age - 2;
		}
	}
}
 
I appreciate the work you have done. I cannot run my xF installation without this. It's the single most important thing other than the forum itself.

Although, there still seems to be some problems after testing for a day

If I change the b-day of a user, it saves and places the user into the group properly.

I can add & remove groups manually and save, and it works as it should(unlike in the beginning of this problem). So that's fixed.

However if you change the user's b-day date to tomorrow, and tomorrow comes, the group is not added, or does not update.

Also, all previous age groups are not removed. So if you keep changing the user's b-day(testing), that user gets the additional group, but other ones are not removed. The problem this causes if you restrict a board to users that are under 18, and a user b-day changes to 17-18, the user still cannot view the board because even thought age group 18 as been added, he or she is still a member of age group 17, thus restricting access. Is there an way to fix this? Perhaps a conditional or that remove the secondary age groups that age doesn't equal to? (age's 13-99, group id's 11-97)

Thank you for all you do.
 
updated to RC3.

but previous problems still exist. Is there a solution for this? I'm will to donate to the cause. Otherwise I'll have to throw out my xF forum without this and revert back to SMF.
 
When does _preSaveDefaults() get triggered? Is this after registration is complete?

I have a similar problem, but would like to change primary usergroup after registration based on DOB. Then I use a nightly cron to check usergroup status and change them as necessary when people have birthdays.

Just getting started on this, though... trying to get a handle on how this would need to work on Xen.
 
Top Bottom