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

How can I retrieve default_watch_state for a user?

#1
I have a bit of code that I'm trying to tweak that currently says:

Code:
if (isset($permissions['view']) AND $permissions['view']){$ThreadWatchModel->setThreadWatchState($user['user_id'], $thread['thread_id'], 'watch_email');
I want to replace the constant: 'watch_email' with the user's value for 'default_watch_state'. What functions do I need to call to retrieve that user value?

Thanks!
 
#2
Hmm ... 30 views but no replies. I must not be asking my question well.

Is there additional info I should provide so that I can get some help on this? It seems like there ought to be an easy way to get the user's value for 'default_watch_state' but I'm too much of a Xenforo novice to see how.

Thanks for your help
 

Mike

XenForo developer
Staff member
#3
Normally you would have it in the user info you've retrieved already, but there are a lot of variables. Look at what's in the $user array in your example. If you don't, you would need to retrieve it, likely by fetching the necessary data via the user model. (Don't have the code in front of me so can't really be more specific.)
 
#4
Thanks Mike. Here's a larger chunk of the code:

Code:
        $node_id = $this->_input->filterSingle('node_id', XenForo_Input::UINT);
        $forum = $this->_getForumModel()->getForumById($node_id);
        if ($forum["auto_subscription"]){
            $ForumModel = $this->_getForumModel();
            $ThreadWatchModel = $this->_getThreadWatchModel();
            $fetchOptions['nodeIdPermissions'] = $node_id;
            $users = $this->_getUserModel()->getAllUsers($fetchOptions);
            foreach ($users as $user){
                if (isset($user["node_permission_cache"]) AND $user["node_permission_cache"]){
                    $permissions = XenForo_Permission::unserializePermissions($user["node_permission_cache"]);
                }else{
                    $permissions = array();
                }
                if (isset($permissions['view']) AND $permissions['view']){
                    $ThreadWatchModel->setThreadWatchState($user['user_id'], $thread['thread_id'], 'watch_email');
                }
            }
        }
where

Code:
    protected function _getUserModel()
    {
        return $this->getModelFromCache('XenForo_Model_User');
    }
Would it then be simply $user['default_watch_state']?
 

Mike

XenForo developer
Staff member
#5
You're likely not fetching the user option values when you get all users. Look at how the fetch options system works and the various joins options listed at the top of the user model class. You'll need to fetch the user options, then default_watch_state should work.
 
#6
Does this look like it should work?

Code:
        $node_id = $this->_input->filterSingle('node_id', XenForo_Input::UINT);
        $forum = $this->_getForumModel()->getForumById($node_id);
        if ($forum["auto_subscription"]){
            $ForumModel = $this->_getForumModel();
            $ThreadWatchModel = $this->_getThreadWatchModel();
            $fetchOptions['nodeIdPermissions'] = $node_id;
            $this->_getUserModel()->addFetchOptionJoin($fetchOptions, XenForo_Model_User::FETCH_USER_OPTION);
            $users = $this->_getUserModel()->getAllUsers($fetchOptions);
            foreach ($users as $user){
                if (isset($user["node_permission_cache"]) AND $user["node_permission_cache"]){
                    $permissions = XenForo_Permission::unserializePermissions($user["node_permission_cache"]);
                }else{
                    $permissions = array();
                }
                if (isset($permissions['view']) AND $permissions['view']){
                    $ThreadWatchModel->setThreadWatchState($user['user_id'], $thread['thread_id'], $user['default_watch_state']);
                }
            }
        }
where the key addition is
Code:
$this->_getUserModel()->addFetchOptionJoin($fetchOptions, XenForo_Model_User::FETCH_USER_OPTION);
 

Mike

XenForo developer
Staff member
#7
Technically there is a slightly more direct way to write that code, but I think that should work.