From 632fd5befce8a7a97453dba27354a2177de1a332 Mon Sep 17 00:00:00 2001 From: Thomas Gries Date: Mon, 12 Mar 2012 23:56:59 +0000 Subject: [PATCH] added method for votersto revoke their vote. --- AJAXPoll.css | 8 ++++ AJAXPoll.i18n.php | 2 + AJAXPoll.php | 4 +- AJAXPoll_body.php | 99 +++++++++++++++++++++++++++++++---------------- 4 files changed, 78 insertions(+), 35 deletions(-) diff --git a/AJAXPoll.css b/AJAXPoll.css index 7c6d999..0dbc7e7 100644 --- a/AJAXPoll.css +++ b/AJAXPoll.css @@ -23,6 +23,14 @@ font-size: 0.9em; } +.ajaxpoll .ajaxpoll-hover-vote { + background: cyan; +} + +.ajaxpoll .ajaxpoll-hover-revoke { + background: gold; +} + .ajaxpoll .ajaxpoll-answer-vote { border: 1px solid #CCC; width: 100%; diff --git a/AJAXPoll.i18n.php b/AJAXPoll.i18n.php index 1146267..979fa36 100644 --- a/AJAXPoll.i18n.php +++ b/AJAXPoll.i18n.php @@ -24,6 +24,8 @@ $messages['en'] = array( 'ajaxpoll-info' => 'There {{PLURAL:$1|was one vote|were $1 votes}} since the poll was created on $2.', 'ajaxpoll-submitting' => 'Please wait, submitting your vote.', 'ajaxpoll-vote-permission' => 'You are not entitled to vote.', + 'ajaxpoll-revoke-vote' => 'I want to revoke my vote', + 'ajaxpoll-vote-revoked' => 'You succesfully revoked your vote.', 'right-ajaxpoll-vote' => 'Can vote in AJAX-based polls', ); diff --git a/AJAXPoll.php b/AJAXPoll.php index 7f69445..f1c7399 100644 --- a/AJAXPoll.php +++ b/AJAXPoll.php @@ -19,7 +19,7 @@ * @author Jack Phoenix * @author Thomas Gries * @maintainer Thomas Gries - * @version 1.67 + * @version 1.68 * @link http://www.mediawiki.org/wiki/Extension:AJAX_Poll Documentation */ @@ -31,7 +31,7 @@ if( !defined( 'MEDIAWIKI' ) ) { $wgExtensionCredits['parserhook'][] = array( 'path' => __FILE__, 'name' => 'AJAX Poll', - 'version' => '1.67 20120312', + 'version' => '1.68 20120313', 'author' => array( 'Dariusz Siedlecki', 'Jack Phoenix', 'Thomas Gries' ), 'descriptionmsg' => 'ajaxpoll-desc', 'url' => 'https://www.mediawiki.org/wiki/Extension:AJAX_Poll', diff --git a/AJAXPoll_body.php b/AJAXPoll_body.php index 0a1c215..9f00b16 100644 --- a/AJAXPoll_body.php +++ b/AJAXPoll_body.php @@ -165,51 +165,68 @@ class AJAXPoll { return AJAXPoll::buildHTML( $id, $user ); } - $answer = ++$answer; + if ( $answer != 0 ) { - $q = $dbw->select( - 'poll_vote', - 'COUNT(*) AS c', - array( - 'poll_id' => $id, - 'poll_user' => $user - ), - __METHOD__ - ); - $row = $dbw->fetchRow( $q ); + $answer = ++$answer; - if ( $row['c'] > 0 ) { - - $updateQuery = $dbw->update( + $q = $dbw->select( 'poll_vote', - array( - 'poll_answer' => $answer, - 'poll_date' => wfTimestampNow() - ), + 'COUNT(*) AS count', array( 'poll_id' => $id, 'poll_user' => $user ), __METHOD__ ); - $dbw->commit(); - $pollContainerText = ( $updateQuery ) ? 'ajaxpoll-vote-update' : 'ajaxpoll-vote-error'; + $row = $dbw->fetchRow( $q ); - } else { + if ( $row['count'] > 0 ) { - $insertQuery = $dbw->insert( + $updateQuery = $dbw->update( + 'poll_vote', + array( + 'poll_answer' => $answer, + 'poll_date' => wfTimestampNow() + ), + array( + 'poll_id' => $id, + 'poll_user' => $user, + ), + __METHOD__ + ); + $dbw->commit(); + $pollContainerText = ( $updateQuery ) ? 'ajaxpoll-vote-update' : 'ajaxpoll-vote-error'; + + } else { + + $insertQuery = $dbw->insert( + 'poll_vote', + array( + 'poll_id' => $id, + 'poll_user' => $user, + 'poll_ip' => wfGetIP(), + 'poll_answer' => $answer, + 'poll_date' => wfTimestampNow() + ), + __METHOD__ + ); + $dbw->commit(); + $pollContainerText = ( $insertQuery ) ? 'ajaxpoll-vote-add' : 'ajaxpoll-vote-error'; + + } + + } else { // revoking a vote + + $deleteQuery = $dbw->delete( 'poll_vote', array( 'poll_id' => $id, 'poll_user' => $user, - 'poll_ip' => wfGetIP(), - 'poll_answer' => $answer, - 'poll_date' => wfTimestampNow() ), __METHOD__ ); $dbw->commit(); - $pollContainerText = ( $insertQuery ) ? 'ajaxpoll-vote-add' : 'ajaxpoll-vote-error'; + $pollContainerText = ( $deleteQuery ) ? 'ajaxpoll-vote-revoked' : 'ajaxpoll-vote-error'; } @@ -301,7 +318,12 @@ function mout(x){ // Different message depending on if the user has already voted or not, or is entitled to vote if ( $wgUser->isAllowed( 'ajaxpoll-vote' ) ) { - $message = ( isset( $row[0] ) ) ? $ourLastVoteDate : wfMsg( 'ajaxpoll-no-vote' ); + if ( isset( $row[0] ) ) { + $message = $ourLastVoteDate; + $lines[] = wfMsg( 'ajaxpoll-revoke-vote' ); + } else { + $message = wfMsg( 'ajaxpoll-no-vote' ); + } } else { $message = wfMsg( 'ajaxpoll-vote-permission' ); } @@ -313,6 +335,9 @@ function mout(x){ '" id="ajaxpoll-answer-id-' . $id . '">'; for ( $i = 1; $i < count( $lines ); $i++ ) { + + $vote = ( $i != count( $lines ) - 1 ); + $voteValue = ( $vote ) ? $i : 0; $ans_no = $i - 1; if ( $amountOfVotes == 0 ) { @@ -330,19 +355,28 @@ function mout(x){ if ( $wgUser->isAllowed( 'ajaxpoll-vote' ) ) { if ( $wgUseAjax ) { - $submitJS = "sajax_do_call(\"AJAXPoll::submitVote\",[\"" . $id . "\",\"" . $i . "\"], $(\"#ajaxpoll-container-" . $id . "\")[0]);"; + $submitJS = "sajax_do_call(\"AJAXPoll::submitVote\",[\"" . $id . "\",\"" . $voteValue . "\"], $(\"#ajaxpoll-container-" . $id . "\")[0]);"; } else { $submitJS = "$(\"#ajaxpoll-answer-id-" . $id . "\").submit();"; } - // HTML output has to be on one line thanks to a MediaWiki bug - // @see https://bugzilla.wikimedia.org/show_bug.cgi?id=1319 - $ret .= " -
" . ( ( isset( $poll_result ) && !empty( $poll_result[$i + 1] ) ) ? $poll_result[$i + 1] : 0 ) . "
"; - } }