Poll unvote add-on

AndyB

Well-known member
I'd like to be able to have a link under each poll that would allow members to remove their vote.

I'm not sure if creating an add-on is the best way to do this.

I think the following needs to happen:

1) call a php script where I verify if the member has a vote and delete all rows from the xf_poll_vote that match that user_id

2) Call or duplicate the two functions from the following file -> library/Xenforo/Model/Poll.php
call -> public function rebuildPollData($pollId)
call -> public function rebuildPollResponseCache($pollId)

3) redirect back to the thread

Is it possible to create an add-on that can call the two public functions show in step #2?

Thank you.
 
You extend a ControllerPublic function. Your function could be named actionUnvote(){...}

For ControllerPublic Functions the link you will use in your templates to do an "action" like unvoting is always the string after "action". You will write the string with a beginning uppercase letter, but the link will be lowercase. I don't know the route for polls, but maybe it is /poll/. In that case, your link would be /poll/unvote/1234 where 1234 is the poll_id. Your script would basically be:

PHP:
public function actionUnvote()
{
  $pollId =  $this->_input->filterSingle('poll_id', XenForo_Input::UINT);

  if($pollId)
  {
    $pollModel = $this->_getPollModel();
    $pollModel->unvote($pollId);

    $this->rebuildPollData($pollId);
    $this->rebuildPollResponseCache($pollId);
  }
  return .....
}

Pretty easy. $poll_id is the given (third) numerical parameter in /poll/unvote/1234, so it's 1234. Why does xenForo know the number is named "poll_id"? Because there is a route set and within that route the given parameter is named "poll_id". You can also give it an alphanumeric string, I don't know how that is done for polls.

If the function is in the same class (in xenForo that means, it's in the same file), then you call the function with $this->function();. If the function is somewhere else, you would have to name the class like XenForo_Visitor::function(); If the function is a model, you should get the model first ($xxxModel = ...) like it's done here.

You might assert the visitor being a registered member. You could check if the user voted within that poll, or you could return a yes or no from the unvote function (DELETE * FROM ...) so you know if there was data changed, and only if something changed you would start the rebuild.
 
Last edited:
Back
Top Bottom