XF 1.5 Secondary Groups Not Saving

Gossamer

Active member
Hello! I have an add-on I'm working on that is supposed to function as an activity checker. Every time someone posts, it's supposed to check their activity status and update it as necessary. However, the secondary group I'm trying to add isn't actually saving.

Here is what I have so far:
Extended DiscussionMessage\Post DataWriter
PHP:
protected function _messagePostSave()
    {
        parent::_messagePostSave();
        
        $userid = $this->get('user_id');
        
        if($this->_getUserModel()->userActivityCheckEnabled($userid))
        {
            $this->_updateUserPostsForActivity($userid);
        }
        
    }

    protected function _updateUserPostsForActivity($userId, $isDelete = false)
    {       
        if ($this->_forumCountsRPMessages() && $this->_forumCountsMessages() && $this->_getUserModel()->userActivityCheckEnabled($userId))
        {
            if ($this->_hasParentDiscussion)
            {
                $discussionDw = $this->getDiscussionDataWriter();
                if ($discussionDw && $discussionDw->get('discussion_state') != 'visible')
                {
                    return;
                }
            }
            
            if ($this->getExisting('message_state') == 'visible'
            && ($this->get('message_state') != 'visible' || $isDelete))
            {
                $postId = $this->get('post_id');
                
                /* Remove Post from list  */
                $userDw = XenForo_DataWriter::create('XenForo_DataWriter_User');
                $userDw->deleteTrackedActivityPost($userId, $postId);
            }
            else if ($this->get('message_state') == 'visible' && $this->getExisting('message_state') != 'visible')
            {
                $postId = $this->get('post_id');
                $postDate = $this->get('post_date');

                $userDw = XenForo_DataWriter::create('XenForo_DataWriter_User');
                $userDw->updateTrackedActivityPosts($userId, $postId, $postDate);

                return;
            }
        }
        
        return;
    }

Extended User DataWriter
PHP:
public function updateTrackedActivityPosts($userId, $postid, $postdate)
    {
        
        /* Get current activity posts from database */
        $activityPosts = $this->_getUserModel()->getActivityPosts($userId);
        
        /* Are there posts already? Is this account flagged for low activity? */
        if($activityPosts && !$this->_getUserModel()->isLowActivity($userId))
        {
            /* Posts exist and this account is NOT flagged for low activity.  Check for and remove expired entries from array */
            $activityPosts = $this->_getUserModel()->checkExpiredPosts($activityPosts);
        }
        
        /* Add new entry to activityPosts */
        $activityPosts[$postid] = $postdate;

        /* EXPIRED DATE TESTING */
        $activityPosts[$postid] = "1497143118"; 
        
        /* Serialize data */
        $activityPosts = serialize($activityPosts);
        
        $this->setExistingData($userId);
        $this->set('rp_messages_for_activity', $activityPosts);
        $this->save();
        
        /* Update activity status for user */
        $this->updateActivityStatus($userId);
        
        return true;
    }
    
    public function updateActivityStatus($userId)
    {
        $userModel = $this->_getUserModel();
        
        $posts = $userModel->getActivityPosts($userId);
        
        $options = XenForo_Application::get('options');
        
        /* check the activity posts and update usergroup appropriately */
        if(count($posts) < 3)
        {
            
            /* move to low activity group OR deletion group */
            
            /* set secondary usergroup based on current usergroups */
            $lowActivityUserGroup = $options->rps_ActivityCheck_LowActivity_Usergroup;
            $deletionUserGroup = $options->rps_ActivityCheck_Deletion_Usergroup;
            $secondaryUsergroups = explode(',', $this->get('secondary_group_ids'));
            
            $userModel = $this->_getUserModel();
            
            if($lowActivityUserGroup != null && $deletionUserGroup != null)
            {
                /* user is not in low activity group */
                if(!$userModel->isLowActivity($userId))
                {
                    
                    
                    /* add user to low activity group */
                    $secondaryUsergroups[] = $lowActivityUserGroup;
                    
                    $this->setExistingData($userId);
                    $this->setSecondaryGroups($secondaryUsergroups);
                    $this->save();
                }
                
                /* IF user is in low activity group AND enough time has passed Add user to deletion usergroup */
            }
            
        }
        else
        {
            /* remove from low activity or deletion group */
        }
        
        return true;
        
    }

Everything looks good until I get to this part in the Extended User Datawriter:
PHP:
$this->setSecondaryGroups($secondaryUsergroups);
$this->save();
I have the correct Secondary Usergroup id stored. The array for $secondaryUsergroups looks good. But nothing actually saves.

Any ideas?
 
Underneath $this->save(); add this:

PHP:
echo "<pre>";
print_r($this->_errors);
die();

I'm reasonably sure the problem is that there's an error.

If that returns an empty array, let me know and I'll have another look, if that's the case then the problem is the odd way the DW is being saved - usually save(); is being called externally, not internally.


Fillip
 
Underneath $this->save(); add this:

PHP:
echo "<pre>";
print_r($this->_errors);
die();

I'm reasonably sure the problem is that there's an error.

If that returns an empty array, let me know and I'll have another look, if that's the case then the problem is the odd way the DW is being saved - usually save(); is being called externally, not internally.


Fillip
Sorry for the late reply! I was busy over the holiday.

But it just returned an empty array. If you could take another look, I'd appreciate it.
 
In case anybody else struggles with something similar, I was able to fix this by moving my updateActivityStatus function out of the user DataWriter and into the User Model.
 
Top Bottom