WordPress.org

Forums

YOP Poll
[resolved] Allow users to change their vote (3 posts)

  1. Eugene
    Member
    Posted 1 year ago #

    This isn't really a support post but I though I would relay the info in case you either want to develop this further, or if you have suggestions on a better way to implement this.

    I have a small site that is members only. Members need to be able to change their vote easily.

    In order to do this, I added the below code right before the following line in yop_poll_model.php:

    if ( $this->user_have_votes_to_vote( $voter ) ){

    Custom code:

    if ( 'wordpress' == $voter['user_type'] ) {
    	global $wpdb;
    	$wpdb->query( $wpdb->prepare( "
    			update " . $wpdb->yop_polls . " p
    			set total_votes = total_votes - (
    				select count(pv.poll_id)
    				from " . $wpdb->yop_poll_voters . " pv
    				where p.id = pv.poll_id and pv.user_id = %d and pv.user_type = 'wordpress'
    				),
    			total_answers = total_answers - (
    				select count(pl.answer_id)
    				from wp_2_yop_poll_logs pl
    				where p.id = pl.poll_id and pl.user_id = %d and pl.user_type = 'wordpress'
    				)
    			where p.id = %d;
    			", $current_user->ID, $current_user->ID, $poll_id ) );
    	$wpdb->query( $wpdb->prepare( "
    			update " . $wpdb->yop_poll_answers . " pa
    			set votes = votes - (
    				select count(pl.answer_id)
    				from " . $wpdb->yop_poll_logs . " pl
    				where pa.id = pl.answer_id and pa.poll_id = pl.poll_id and pl.user_id = %d and pl.user_type = 'wordpress'
    				)
    			where pa.poll_id = %d;
    			", $current_user->ID, $poll_id ) );
    	$wpdb->query( $wpdb->prepare( "
    			delete from " . $wpdb->yop_poll_voters . "
    			where poll_id = %d and
    			user_id = %d and
    			user_type = 'wordpress';
    			", $poll_id, $current_user->ID ) );
    	$wpdb->query( $wpdb->prepare( "
    			delete from " . $wpdb->yop_poll_logs . "
    			where poll_id = %d and
    			user_id = %d and
    			user_type = 'wordpress';
    			", $poll_id, $current_user->ID ) );
    }

    https://wordpress.org/plugins/yop-poll/

  2. YOP
    Member
    Plugin Author

    Posted 1 year ago #

    Hey Eugene,

    We have reviewed the code and it looks good.
    We would recommend, however, not to delete data from the logs. Instead you could flag that the vote has been modified.

    Best wishes,

    YOP Team

  3. Eugene
    Member
    Posted 1 year ago #

    I made some changes to do as you suggested and not delete the logs. The only problem is the poll_id and answer_id need to be set to 0, otherwise the vote stats aren't updated. In that case the logs will hang around forever. Not necessarily a bad thing but I may elect to just delete the log entries as I had written in my first post. It's a small community and for logged in users only so we don't really need the historical data.

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic

Tags

No tags yet.