• Resolved Eugene

    (@euge_g)


    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/

Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Author YOP

    (@yourownprogrammer)

    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

    Thread Starter Eugene

    (@euge_g)

    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.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Allow users to change their vote’ is closed to new replies.