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

How to edit this code so members can edit polls

Discussion in 'XenForo Development Discussions' started by AndyB, Jul 26, 2013.

  1. AndyB

    AndyB Well-Known Member

    I would like to modify this code so that any registered member can edit poll questions. Once I can do that I will be able to make a check to verify it's the thread starter and no votes have been cast yet.

    Thank you.

    /library/Xenforo/Model/Thread.php

    Line 1274

    PHP:
        public function canEditPoll(array $thread, array $forum, &$errorPhraseKey '', array $nodePermissions null, array $viewingUser null)
        {
            
    // TODO: allow limited editing by starter (no votes, can add new responses, etc)
            
    $this->standardizeViewingUserReferenceForNode($thread['node_id'], $viewingUser$nodePermissions);
            return (
    $viewingUser['user_id'] && XenForo_Permission::hasContentPermission($nodePermissions'manageAnyThread'));
        }
     
  2. Chris D

    Chris D XenForo Developer Staff Member

    Below $this and above return...

    if ($viewingUser['user_id'] == $thread['user_id])
    {
    return true;
    }

    That's the check if the user is the thread starter.

    You'll have to do a bit of debugging to work out if the $thread variable contains enough information on poll responses. If not you might have to run a query to fetch that.
     
    AndyB and Marcus like this.
  3. tyteen4a03

    tyteen4a03 Well-Known Member

    Or:

    PHP:
    return ($viewingUser['user_id'] && ($viewingUser['user_id'] == $thread['user_id'] || XenForo_Permission::hasContentPermission($nodePermissions'manageAnyThread')));
     
    AndyB likes this.
  4. AndyB

    AndyB Well-Known Member

    Hi Chris and tyteen4a03,

    Thank you both for your help with this, both your codes work perfectly.

    Here is an example which allows the thread starter and the admin to edit the poll choices. Also note that once there is a second post the thread starter will not be able to edit the poll choices.

    PHP:
    public function canEditPoll(array $thread, array $forum, &$errorPhraseKey '', array $nodePermissions null, array $viewingUser null)
    {
    // TODO: allow limited editing by starter (no votes, can add new responses, etc)
    $this->standardizeViewingUserReferenceForNode($thread['node_id'], $viewingUser$nodePermissions);

    // start hack

    //return ($viewingUser['user_id'] && XenForo_Permission::hasContentPermission($nodePermissions, 'manageAnyThread'));

    if ($viewingUser['user_id'] == $thread['user_id'] AND $thread['reply_count'] == 0)
    {
    return 
    true;
    }

    if (
    $viewingUser['user_id'] == 1)
    {
    return 
    true;
    }

    // end hack
    }
     
    Last edited: Jul 26, 2013
  5. tyteen4a03

    tyteen4a03 Well-Known Member

    A problem with your code: It completely ignores the manageAnyThread permission (you commented out that bit).

    Why not just give admins the Manage Any Threads permission? If so you won't need the second if clause.

    Also, my code above was just Chris's if clause stuffed together in a single expression.
     
    xf_phantom likes this.
  6. AndyB

    AndyB Well-Known Member

    Hi tyteen4a03,

    Thank you for the improvement in the code. This is what I currently have and works great.

    PHP:
        public function canEditPoll(array $thread, array $forum, &$errorPhraseKey '', array $nodePermissions null, array $viewingUser null)
        {        
            
    // TODO: allow limited editing by starter (no votes, can add new responses, etc)
            
    $this->standardizeViewingUserReferenceForNode($thread['node_id'], $viewingUser$nodePermissions);

            
    // start hack
            
            
    if ($viewingUser['user_id'] == $thread['user_id'] AND $thread['reply_count'] == 0)
            {
                return 
    true;
            }
            
            
    // end hack
            
            
    return ($viewingUser['user_id'] && XenForo_Permission::hasContentPermission($nodePermissions'manageAnyThread'));
        }
     
  7. a.tushkanov

    a.tushkanov Active Member

    @AndyB , @Chris Deeming , @tyteen4a03 thank you all for this code. It works great!
    Could you be so kind to show how to add two things here:
    1. Disable to edit poll by author if there is at least one vote (staff still can edit it)
    2. Nobody can edit polls by admin (user == 1).
    Thank you! :)
     
  8. AndyB

    AndyB Well-Known Member

    The easiest way to do this "staff still can edit it" is to put in another 'if' condition with the staff user_id.
     

Share This Page