How to edit this code so members can edit polls

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'));
	}
 
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.
 
Or:

PHP:
return ($viewingUser['user_id'] && ($viewingUser['user_id'] == $thread['user_id'] || XenForo_Permission::hasContentPermission($nodePermissions, 'manageAnyThread')));
 
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:
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.
 
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'));
	}
 
@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! :)
 
The easiest way to do this "staff still can edit it" is to put in another 'if' condition with the staff user_id.
 
Top Bottom