r113686 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r113685‎ | r113686 | r113687 >
Date:23:56, 12 March 2012
Author:wikinaut
Status:deferred (Comments)
Tags:
Comment:
added method for votersto revoke their vote.
Modified paths:
  • /trunk/extensions/AJAXPoll/AJAXPoll.css (modified) (history)
  • /trunk/extensions/AJAXPoll/AJAXPoll.i18n.php (modified) (history)
  • /trunk/extensions/AJAXPoll/AJAXPoll.php (modified) (history)
  • /trunk/extensions/AJAXPoll/AJAXPoll_body.php (modified) (history)

Diff [purge]

Index: trunk/extensions/AJAXPoll/AJAXPoll.php
@@ -19,7 +19,7 @@
2020 * @author Jack Phoenix <jack@countervandalism.net>
2121 * @author Thomas Gries
2222 * @maintainer Thomas Gries
23 - * @version 1.67
 23+ * @version 1.68
2424 * @link http://www.mediawiki.org/wiki/Extension:AJAX_Poll Documentation
2525 */
2626
@@ -31,7 +31,7 @@
3232 $wgExtensionCredits['parserhook'][] = array(
3333 'path' => __FILE__,
3434 'name' => 'AJAX Poll',
35 - 'version' => '1.67 20120312',
 35+ 'version' => '1.68 20120313',
3636 'author' => array( 'Dariusz Siedlecki', 'Jack Phoenix', 'Thomas Gries' ),
3737 'descriptionmsg' => 'ajaxpoll-desc',
3838 'url' => 'https://www.mediawiki.org/wiki/Extension:AJAX_Poll',
Index: trunk/extensions/AJAXPoll/AJAXPoll.css
@@ -23,6 +23,14 @@
2424 font-size: 0.9em;
2525 }
2626
 27+.ajaxpoll .ajaxpoll-hover-vote {
 28+ background: cyan;
 29+}
 30+
 31+.ajaxpoll .ajaxpoll-hover-revoke {
 32+ background: gold;
 33+}
 34+
2735 .ajaxpoll .ajaxpoll-answer-vote {
2836 border: 1px solid #CCC;
2937 width: 100%;
Index: trunk/extensions/AJAXPoll/AJAXPoll_body.php
@@ -165,51 +165,68 @@
166166 return AJAXPoll::buildHTML( $id, $user );
167167 }
168168
169 - $answer = ++$answer;
 169+ if ( $answer != 0 ) {
170170
171 - $q = $dbw->select(
172 - 'poll_vote',
173 - 'COUNT(*) AS c',
174 - array(
175 - 'poll_id' => $id,
176 - 'poll_user' => $user
177 - ),
178 - __METHOD__
179 - );
180 - $row = $dbw->fetchRow( $q );
 171+ $answer = ++$answer;
181172
182 - if ( $row['c'] > 0 ) {
183 -
184 - $updateQuery = $dbw->update(
 173+ $q = $dbw->select(
185174 'poll_vote',
 175+ 'COUNT(*) AS count',
186176 array(
187 - 'poll_answer' => $answer,
188 - 'poll_date' => wfTimestampNow()
189 - ),
190 - array(
191177 'poll_id' => $id,
192178 'poll_user' => $user
193179 ),
194180 __METHOD__
195181 );
196 - $dbw->commit();
197 - $pollContainerText = ( $updateQuery ) ? 'ajaxpoll-vote-update' : 'ajaxpoll-vote-error';
 182+ $row = $dbw->fetchRow( $q );
198183
199 - } else {
 184+ if ( $row['count'] > 0 ) {
200185
201 - $insertQuery = $dbw->insert(
 186+ $updateQuery = $dbw->update(
 187+ 'poll_vote',
 188+ array(
 189+ 'poll_answer' => $answer,
 190+ 'poll_date' => wfTimestampNow()
 191+ ),
 192+ array(
 193+ 'poll_id' => $id,
 194+ 'poll_user' => $user,
 195+ ),
 196+ __METHOD__
 197+ );
 198+ $dbw->commit();
 199+ $pollContainerText = ( $updateQuery ) ? 'ajaxpoll-vote-update' : 'ajaxpoll-vote-error';
 200+
 201+ } else {
 202+
 203+ $insertQuery = $dbw->insert(
 204+ 'poll_vote',
 205+ array(
 206+ 'poll_id' => $id,
 207+ 'poll_user' => $user,
 208+ 'poll_ip' => wfGetIP(),
 209+ 'poll_answer' => $answer,
 210+ 'poll_date' => wfTimestampNow()
 211+ ),
 212+ __METHOD__
 213+ );
 214+ $dbw->commit();
 215+ $pollContainerText = ( $insertQuery ) ? 'ajaxpoll-vote-add' : 'ajaxpoll-vote-error';
 216+
 217+ }
 218+
 219+ } else { // revoking a vote
 220+
 221+ $deleteQuery = $dbw->delete(
202222 'poll_vote',
203223 array(
204224 'poll_id' => $id,
205225 'poll_user' => $user,
206 - 'poll_ip' => wfGetIP(),
207 - 'poll_answer' => $answer,
208 - 'poll_date' => wfTimestampNow()
209226 ),
210227 __METHOD__
211228 );
212229 $dbw->commit();
213 - $pollContainerText = ( $insertQuery ) ? 'ajaxpoll-vote-add' : 'ajaxpoll-vote-error';
 230+ $pollContainerText = ( $deleteQuery ) ? 'ajaxpoll-vote-revoked' : 'ajaxpoll-vote-error';
214231
215232 }
216233
@@ -301,7 +318,12 @@
302319 // Different message depending on if the user has already voted or not, or is entitled to vote
303320
304321 if ( $wgUser->isAllowed( 'ajaxpoll-vote' ) ) {
305 - $message = ( isset( $row[0] ) ) ? $ourLastVoteDate : wfMsg( 'ajaxpoll-no-vote' );
 322+ if ( isset( $row[0] ) ) {
 323+ $message = $ourLastVoteDate;
 324+ $lines[] = wfMsg( 'ajaxpoll-revoke-vote' );
 325+ } else {
 326+ $message = wfMsg( 'ajaxpoll-no-vote' );
 327+ }
306328 } else {
307329 $message = wfMsg( 'ajaxpoll-vote-permission' );
308330 }
@@ -313,6 +335,9 @@
314336 '" id="ajaxpoll-answer-id-' . $id . '"><input type="hidden" name="ajaxpoll-post-id" value="' . $id . '" />';
315337
316338 for ( $i = 1; $i < count( $lines ); $i++ ) {
 339+
 340+ $vote = ( $i != count( $lines ) - 1 );
 341+ $voteValue = ( $vote ) ? $i : 0;
317342 $ans_no = $i - 1;
318343
319344 if ( $amountOfVotes == 0 ) {
@@ -330,19 +355,28 @@
331356 if ( $wgUser->isAllowed( 'ajaxpoll-vote' ) ) {
332357
333358 if ( $wgUseAjax ) {
334 - $submitJS = "sajax_do_call(\"AJAXPoll::submitVote\",[\"" . $id . "\",\"" . $i . "\"], $(\"#ajaxpoll-container-" . $id . "\")[0]);";
 359+ $submitJS = "sajax_do_call(\"AJAXPoll::submitVote\",[\"" . $id . "\",\"" . $voteValue . "\"], $(\"#ajaxpoll-container-" . $id . "\")[0]);";
335360 } else {
336361 $submitJS = "$(\"#ajaxpoll-answer-id-" . $id . "\").submit();";
337362 }
338363
339364
340 - // HTML output has to be on one line thanks to a MediaWiki bug
341 - // @see https://bugzilla.wikimedia.org/show_bug.cgi?id=1319
342 - $ret .= "
343 -<div id='ajaxpoll-answer-" . $ans_no . "' class='ajaxpoll-answer'><div class='ajaxpoll-answer-name'><label for='ajaxpoll-post-answer-" . $ans_no . "' onclick='$(\"#ajaxpoll-ajax-" . $id . "\").html(\"" . wfMsg( 'ajaxpoll-submitting' ) . "\");$(\"#ajaxpoll-ajax-" . $id . "\").css(\"display\",\"block\");$(this).addClass(\"ajaxpoll-checkevent\").prop(\"checked\",true); " . $submitJS . "'><input type='radio' id='ajaxpoll-post-answer-" . $ans_no . "' name='ajaxpoll-post-answer-" . $ans_no . "' value='" . $i . "'" . ( $our ? 'checked=true ' : '' ) . "/>" . strip_tags( $lines[$i] ) .
 365+ // HTML output has to be on one line thanks to a MediaWiki bug
 366+ // @see https://bugzilla.wikimedia.org/show_bug.cgi?id=1319
 367+
 368+ if ( $vote ) {
 369+ $ret .= "
 370+<div id='ajaxpoll-answer-" . $ans_no . "' class='ajaxpoll-answer'><div class='ajaxpoll-answer-name'><label for='ajaxpoll-post-answer-" . $ans_no . "' onmouseover='$(this).addClass(\"ajaxpoll-hover-vote\");' onmouseout='$(this).removeClass(\"ajaxpoll-hover-vote\");' onclick='$(\"#ajaxpoll-ajax-" . $id . "\").html(\"" . wfMsg( 'ajaxpoll-submitting' ) . "\");$(\"#ajaxpoll-ajax-" . $id . "\").css(\"display\",\"block\");$(this).addClass(\"ajaxpoll-checkevent\").prop(\"checked\",true); " . $submitJS . "'><input type='radio' id='ajaxpoll-post-answer-" . $ans_no . "' name='ajaxpoll-post-answer-" . $ans_no . "' value='" . $voteValue . "' " . ( $our ? 'checked=true ' : '' ) . "/>" . strip_tags( $lines[$i] ) .
344371 "</label></div><div class='ajaxpoll-answer-vote" . ( $our ? ' ajaxpoll-our-vote' : '' ) ."' onmouseover='mover(this)' onmouseout='mout(this);'><span title='" . wfMsg( 'ajaxpoll-percent-votes', sprintf( $percent ) ) . "'>" . ( ( isset( $poll_result ) && !empty( $poll_result[$i + 1] ) ) ? $poll_result[$i + 1] : 0 ) . "</span><div style='width: " . $percent . "%;" . ( $percent == 0 ? ' border:0;' : '' ) . "'></div></div>
345372 </div>
346373 ";
 374+ } else {
 375+ $ret .= "
 376+<div id='ajaxpoll-answer-" . $ans_no . "' class='ajaxpoll-answer'><div class='ajaxpoll-answer-name'><label for='ajaxpoll-post-answer-" . $ans_no . "' onmouseover='$(this).addClass(\"ajaxpoll-hover-revoke\");' onmouseout='$(this).removeClass(\"ajaxpoll-hover-revoke\");' onclick='$(\"#ajaxpoll-ajax-" . $id . "\").html(\"" . wfMsg( 'ajaxpoll-submitting' ) . "\");$(\"#ajaxpoll-ajax-" . $id . "\").css(\"display\",\"block\");$(this).addClass(\"ajaxpoll-checkevent\").prop(\"checked\",true); " . $submitJS . "'><input type='radio' id='ajaxpoll-post-answer-" . $ans_no . "' name='ajaxpoll-post-answer-" . $ans_no . "' value='" . $voteValue . "' " . ( $our ? 'checked=true ' : '' ) . "/>" . strip_tags( $lines[$i] ) .
 377+"</label></div>
 378+</div>
 379+";
 380+ }
347381
348382 } else {
349383
@@ -351,7 +385,6 @@
352386 "</label></div><div class='ajaxpoll-answer-vote" . ( $our ? ' ajaxpoll-our-vote' : '' ) ."' onmouseover='mover(this)' onmouseout='mout(this);'><span title='" . wfMsg( 'ajaxpoll-percent-votes', sprintf( $percent ) ) . "'>" . ( ( isset( $poll_result ) && !empty( $poll_result[$i + 1] ) ) ? $poll_result[$i + 1] : 0 ) . "</span><div style='width: " . $percent . "%;" . ( $percent == 0 ? ' border:0;' : '' ) . "'></div></div>
353387 </div>
354388 ";
355 -
356389 }
357390
358391 }
Index: trunk/extensions/AJAXPoll/AJAXPoll.i18n.php
@@ -24,6 +24,8 @@
2525 'ajaxpoll-info' => 'There {{PLURAL:$1|was one vote|were $1 votes}} since the poll was created on $2.',
2626 'ajaxpoll-submitting' => 'Please wait, submitting your vote.',
2727 'ajaxpoll-vote-permission' => 'You are not entitled to vote.',
 28+ 'ajaxpoll-revoke-vote' => 'I want to revoke my vote',
 29+ 'ajaxpoll-vote-revoked' => 'You succesfully revoked your vote.',
2830
2931 'right-ajaxpoll-vote' => 'Can vote in AJAX-based polls',
3032 );

Follow-up revisions

RevisionCommit summaryAuthorDate
r113697follow up r113686 : added message documentation as requested.wikinaut00:37, 13 March 2012
r113700follow up r113686 . correction of algorithm for voting for the last answer, t...wikinaut01:04, 13 March 2012

Comments

#Comment by Siebrand (talk | contribs)   00:20, 13 March 2012

Please add message documentation for the newly added messages. Thanks.

#Comment by Wikinaut (talk | contribs)   00:37, 13 March 2012

added in r113697

Status & tagging log