Wikinaut has submitted this change and it was merged.

Change subject: (bug 35119) show poll results only for users who voted (options 
to override)
......................................................................


(bug 35119) show poll results only for users who voted (options to override)

0. bump version 1.81 20130526

1. new: poll results are by default _not_ shown when the user has not voted

2. a new optional (per-poll) tag parameter "show-results-before-voting" is 
introduced
   <poll show-results-before-voting>
   <poll show-results-before-voting=1>

3. introduction of new group permissions:

   * ajaxpoll-viewresults
   * ajaxpoll-viewresults-before-voting

4. schema change
   this version requires a database update ( php maintenance/update.php )
   updater included

Change-Id: Iae1f3f2806afecbca3e3c41460dbbf20fc557525
---
M AJAXPoll.i18n.php
M AJAXPoll.php
M AJAXPoll_body.php
A patches/add-field--ajaxpoll_info-poll_show_results_before_voting.sql
4 files changed, 142 insertions(+), 36 deletions(-)

Approvals:
  Wikinaut: Verified; Looks good to me, approved



diff --git a/AJAXPoll.i18n.php b/AJAXPoll.i18n.php
index d05d2e0..7b44fca 100644
--- a/AJAXPoll.i18n.php
+++ b/AJAXPoll.i18n.php
@@ -19,11 +19,14 @@
        'ajaxpoll-vote-add' => 'Your vote has been added.',
        'ajaxpoll-vote-error' => 'There was a problem with processing your 
vote, please try again.',
        'ajaxpoll-percent-votes' => '$1% of all votes',
-       'ajaxpoll-your-vote' => 'You already voted for "$1" on $2, you can 
change your vote by clicking an answer below.',
-       'ajaxpoll-no-vote' => 'Please vote below.', // 
http://trac.wikia-code.com/changeset/867
+       'ajaxpoll-your-vote' => 'You voted for "$1" on $2. You can change your 
vote by clicking a different answer below.',
+       'ajaxpoll-no-vote' => 'Please vote below.',
+       'ajaxpoll-no-vote-results-after-voting' => 'Please vote below. Results 
will be shown when you have voted.',
        '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-view-results-permission' => 'You are not entitled to view 
results of this poll.',
+       'ajaxpoll-view-results-before-vote-permission' => 'You are not entitled 
to view results of this poll before you have voted.',
        'ajaxpoll-revoke-vote' => 'I want to revoke my vote',
        'ajaxpoll-vote-revoked' => 'You succesfully revoked your vote.',
        'ajaxpoll-error-csrf-wrong-token' => 'A wrong token was received, and 
your vote is ignored.',
@@ -37,6 +40,7 @@
  */
 $messages['qqq'] = array(
        'ajaxpoll-desc' => '{{desc|name=AJAX 
Poll|url=http://www.mediawiki.org/wiki/Extension:AJAX_Poll}}',
+       'ajaxpoll-no-vote-results-after-voting' => 'Invitation to vote and 
indication that results will be shown after voting.',
        'ajaxpoll-tracking-category' => 'The name of a category for all pages 
which use the &lt;poll&gt; parser extension tag. The category is automatically 
added unless the feature is disabled.',
        'ajaxpoll-vote-update' => 'System response that the vote was 
successfully updated.
 
diff --git a/AJAXPoll.php b/AJAXPoll.php
index e6d039b..ac9918a 100644
--- a/AJAXPoll.php
+++ b/AJAXPoll.php
@@ -31,7 +31,7 @@
 $wgExtensionCredits['parserhook'][] = array(
        'path' => __FILE__,
        'name' => 'AJAX Poll',
-       'version' => '1.80 20130526',
+       'version' => '1.81 20130526',
        'author' => array( 'Dariusz Siedlecki', 'Jack Phoenix', 'Thomas Gries' 
),
        'descriptionmsg' => 'ajaxpoll-desc',
        'url' => 'https://www.mediawiki.org/wiki/Extension:AJAX_Poll',
@@ -63,10 +63,30 @@
 
 # new user rights
 $wgAvailableRights[] = 'ajaxpoll-vote';
+$wgAvailableRights[] = 'ajaxpoll-viewresults';
+$wgAvailableRights[] = 'ajaxpoll-viewresults-before-vote';
 
-# default: everyone can vote
-$wgGroupPermissions['*']['ajaxpoll-vote'] = true;
-# if you want to allow only users to vote, use the following code lines
-# in your LocalSettings.php after calling the AJAXPoll extension:
-# $wgGroupPermissions['*']['ajaxpoll-vote'] = false;
-# $wgGroupPermissions['user']['ajaxpoll-vote'] = true;
+# The 'ajaxpoll-view-results-before-vote' group permission allows the specified
+# group members to view poll results even without having voted
+# but only if the high-level group permission 'ajaxpoll-vote' allows to view
+# results in general.
+#
+# This 'ajaxpoll-view-results-before-vote' can be overwritten with the specific
+# per-poll setting "show-results-before-voting" which takes precedence over the
+# group permission.
+#
+# permission 'ajaxpoll-view-results' >>
+# >> per-poll setting "show-results-before-voting" (if present)
+# >> permission 'ajaxpoll-view-results-before-vote'
+#
+
+# anons
+# default: anons cannot vote and will never see results
+$wgGroupPermissions['*']['ajaxpoll-vote'] = false;
+$wgGroupPermissions['*']['ajaxpoll-view-results'] = false;
+$wgGroupPermissions['*']['ajaxpoll-view-results-before-vote'] = false;
+
+# users
+# default: users can vote and can see poll results - when they have voted
+$wgGroupPermissions['user']['ajaxpoll-vote'] = true;
+$wgGroupPermissions['user']['ajaxpoll-view-results'] = true;
diff --git a/AJAXPoll_body.php b/AJAXPoll_body.php
index 8e32f7b..d5ce3d7 100644
--- a/AJAXPoll_body.php
+++ b/AJAXPoll_body.php
@@ -29,7 +29,7 @@
        }
 
        # The callback function for converting the input text to HTML output
-       static function AJAXPollRender( $input, $params = array(), Parser 
$parser ) {
+       static function AJAXPollRender( $input, $args = array(), Parser $parser 
) {
                global $wgUser, $wgOut, $wgTitle, $wgScriptPath, $wgUseAjax;
 
                $parser->disableCache();
@@ -59,7 +59,7 @@
                                && isset( $_POST[$responseAnswer] ) 
                                && ( $_POST[$responseId] == $id )
                                && isset( $_POST[$responseToken] ) ) {
-                               AJAXPoll::submitVote( $id, intval( 
$_POST[$responseAnswer] ), $_POST[$responseToken] );
+                               self::submitVote( $id, intval( 
$_POST[$responseAnswer] ), $_POST[$responseToken] );
                        }
                }
                
@@ -77,26 +77,50 @@
                        __METHOD__
                );
 
+               $showResultsBeforeVoting = null;
+               if ( array_key_exists( 'show-results-before-voting', $args ) ) {
+                       if ( strval( $args['show-results-before-voting'] ) !== 
'0' ) {
+                               $showResultsBeforeVoting = '1';
+                       } else {
+                               $showResultsBeforeVoting = '0';
+                       }
+               }
+
                if( empty( $row->count ) ) {
                        $dbw->insert(
                                'ajaxpoll_info',
                                array(
                                        'poll_id' => $id,
+                                       'poll_show_results_before_voting' => 
$showResultsBeforeVoting,
                                        'poll_txt' => $input,
                                        'poll_date' => wfTimestampNow(),
                                ),
                                __METHOD__
                        );
+               } else {
+                       $dbw->update(
+                               'ajaxpoll_info',
+                               array(
+                                       'poll_show_results_before_voting' => 
$showResultsBeforeVoting,
+                               ),
+                               array(
+                                       'poll_id' => $id,
+                               ),
+                               __METHOD__
+                       );
                }
+
                $dbw->commit( __METHOD__ );
 
                switch( $lines[0] ) {
                        case 'STATS':
-                               $retVal = AJAXPoll::buildStats( $id, $userName 
);
+                               $retVal = self::buildStats( $id, $userName );
                                break;
                        default:
                                $retVal = '
-<div id="ajaxpoll-container-' . $id . '">' . AJAXPoll::buildHTML( $id, 
$userName, $lines ) . '</div>';
+<div id="ajaxpoll-container-' . $id . '">' .
+self::buildHTML( $id, $userName, $lines ) .
+'</div>';
                                break;
                }
                return $retVal;
@@ -105,6 +129,7 @@
        private static function buildStats( $id, $userName ) {
 
                $dbr = wfGetDB( DB_SLAVE );
+               $dbr->begin( __METHOD__ );
 
                $res = $dbr->select(
                        'ajaxpoll_vote',
@@ -147,14 +172,15 @@
                );
                $tab2 = $dbr->fetchRow( $res );
 
+               $dbr->commit( __METHOD__ );
+
                return "There are $tab[1] polls and $tab[0] votes given by 
$tab[2] different people.<br />
-                       The last vote has been given $clockago ago.<br/>
-                       During the last 48 hours, $tab2[0] votes have been 
given.";
+The last vote has been given $clockago ago.<br/>
+During the last 48 hours, $tab2[0] votes have been given.";
        }
 
        public static function submitVote( $id, $answer, $token ) {
                global $wgUser,$wgOut,$wgRequest;
-               // echo "id: $id ans $answer<br/>";
 
                $dbw = wfGetDB( DB_MASTER );
                $dbw->begin( __METHOD__ );
@@ -167,13 +193,11 @@
 
                if ( !$wgUser->matchEditToken( $token, $id ) ) {
                        $pollContainerText = 'ajaxpoll-error-csrf-wrong-token';
-                       return AJAXPoll::buildHTML( $id, $userName, '', 
$pollContainerText );
+                       return self::buildHTML( $id, $userName, '', 
$pollContainerText );
                }
 
-               $dbw = wfGetDB( DB_MASTER );
-
                if ( !$wgUser->isAllowed( 'ajaxpoll-vote' ) || 
$wgUser->isAllowed( 'bot' ) ) {
-                       return AJAXPoll::buildHTML( $id, $userName );
+                       return self::buildHTML( $id, $userName );
                }
 
                if ( $answer != 0 ) {
@@ -241,7 +265,7 @@
 
                }
 
-               return AJAXPoll::buildHTML( $id, $userName, '', 
$pollContainerText );
+               return self::buildHTML( $id, $userName, '', $pollContainerText 
);
 
        }
 
@@ -252,7 +276,7 @@
 
                $q = $dbr->select(
                        'ajaxpoll_info',
-                       array( 'poll_txt', 'poll_date' ),
+                       array( 'poll_txt', 'poll_date', 
'poll_show_results_before_voting' ),
                        array( 'poll_id' => $id ),
                        __METHOD__
                );
@@ -260,6 +284,12 @@
 
                if ( empty( $lines ) ) {
                        $lines = explode( "\n", trim( $row['poll_txt'] ) );
+               }
+
+               if ( $row['poll_show_results_before_voting'] !== null ) {
+                       $showResultsBeforeVoting = ( 
$row['poll_show_results_before_voting'] === '1' );
+               } else {
+                       $showResultsBeforeVoting = $wgUser->isAllowed( 
'ajaxpoll-view-results-before-vote' );
                }
 
                $start_date = $row['poll_date'];
@@ -281,6 +311,8 @@
                $amountOfVotes = array_sum( $poll_result );
 
                // Did we vote?
+               $userVoted = false;
+
                $q = $dbr->select(
                        'ajaxpoll_vote',
                        array( 'poll_answer', 'poll_date' ),
@@ -297,6 +329,7 @@
                                $lines[$row[0] - 1],
                                $wgLang->timeanddate( wfTimestamp( TS_MW, 
$row[1] ), true /* adjust? */ )
                        )->escaped();
+                       $userVoted = true;
                }
 
                if ( is_object( $wgTitle ) ) {
@@ -324,14 +357,33 @@
                                        $canRevoke = true;
                                        $lines[] = wfMessage( 
'ajaxpoll-revoke-vote' )->text();
                                } else {
-                                       $message = wfMessage( 
'ajaxpoll-no-vote' )->text();
+                                       if ( $showResultsBeforeVoting ) {
+                                               $message = wfMessage( 
'ajaxpoll-no-vote' )->text();
+                                       } else {
+                                               $message = wfMessage( 
'ajaxpoll-no-vote-results-after-voting' )->text();
+                                       }
                                }
                        } else {
                                $message = wfMessage( 
'ajaxpoll-vote-permission' )->text();
                        }
-                       
-                       $ret .= '<div class="ajaxpoll-misc">' . $message . '
-</div>';
+
+                       if ( !$wgUser->isAllowed( 'ajaxpoll-view-results' ) ) {
+
+                               $message .= "<br/>" . wfMessage( 
'ajaxpoll-view-results-permission' )->text();
+
+                       } elseif ( !$userVoted
+                               && !$wgUser->isAllowed( 
'ajaxpoll-view-results-before-vote' )
+                               && !$showResultsBeforeVoting ) {
+
+                                       if ( $wgUser->isAllowed( 
'ajaxpoll-vote' ) ) {
+                                               $message .= "<br/>" . 
wfMessage( 'ajaxpoll-view-results-before-vote-permission' )->text();
+                                       } else {
+                                               $message .= "<br/>" . 
wfMessage( 'ajaxpoll-view-results-permission' )->text();
+                                       }
+
+                       }
+
+                       $ret .= '<div class="ajaxpoll-misc">' . $message . 
'</div>';
 
                        $ret .= '<form method="post" action="' . 
$wgTitle->getLocalURL() .
                                '" id="ajaxpoll-answer-id-' . $id . '"><input 
type="hidden" name="ajaxpoll-post-id" value="' . $id . '" />';
@@ -359,6 +411,16 @@
                                // just use sajax library function here for 
that AJAX-y feel.
                                // If not, we'll have to submit the form 
old-school way...
 
+                               if ( $wgUser->isAllowed( 
'ajaxpoll-view-results' )
+                                       && ( $showResultsBeforeVoting || ( 
!$showResultsBeforeVoting && $userVoted ) ) ) {
+                                       $resultBar = "<div 
class='ajaxpoll-answer-vote" . ( $our ? ' ajaxpoll-our-vote' : '' ) ."'>
+<span title='" . wfMessage( 'ajaxpoll-percent-votes', sprintf( $percent ) 
)->escaped() . "'>" . ( ( isset( $poll_result ) && !empty( $poll_result[$i + 1] 
) ) ? $poll_result[$i + 1] : 0 ) . "</span>
+<div style='width: " . $percent . "%;" . ( $percent == 0 ? ' border:0;' : '' ) 
. "'></div>
+</div>";
+                               } else {
+                                       $resultBar = '';
+                               }
+
                                if ( $wgUser->isAllowed( 'ajaxpoll-vote' ) ) {
 
                                        // HTML output has to be on one line 
thanks to a MediaWiki bug
@@ -367,8 +429,7 @@
                                        if ( $vote ) {
                                                $ret .= "
 <div id='ajaxpoll-answer-$xid' class='ajaxpoll-answer' poll='$id' 
answer='$answer'><div class='ajaxpoll-answer-name'><label 
for='ajaxpoll-post-answer-$xid'><input type='radio' 
id='ajaxpoll-post-answer-$xid' name='ajaxpoll-post-answer-$id' value='" . 
$answer . "' " . ( $our ? 'checked=true ' : '' ) . "/>" . strip_tags( 
$lines[$i] ) .
-"</label></div><div class='ajaxpoll-answer-vote" . ( $our ? ' 
ajaxpoll-our-vote' : '' ) ."'><span title='" . wfMessage( 
'ajaxpoll-percent-votes', sprintf( $percent ) )->escaped() . "'>" . ( ( isset( 
$poll_result ) && !empty( $poll_result[$i + 1] ) ) ? $poll_result[$i + 1] : 0 ) 
. "</span><div style='width: " . $percent . "%;" . ( $percent == 0 ? ' 
border:0;' : '' ) . "'></div></div>
-</div>
+"</label></div>{$resultBar}</div>
 ";
                                        } else {
                                                $ret .= "
@@ -382,8 +443,7 @@
 
                                        $ret .= "
 <div id='ajaxpoll-answer-" . $xid . "' class='ajaxpoll-answer' poll='$id' 
answer='$answer'><div class='ajaxpoll-answer-name'><label 
for='ajaxpoll-post-answer-" . $xid . "' onclick='$(\"#ajaxpoll-ajax-" . $xid . 
"\").html(\"" . wfMessage( 'ajaxpoll-vote-permission' )->text() . 
"\").css(\"display\",\"block\");'><input disabled='disabled' type='radio' 
id='ajaxpoll-post-answer-" . $xid . "' name='ajaxpoll-post-answer-" . $id . "' 
value='" . $answer . "'/>" . strip_tags( $lines[$i] ) .
-"</label></div><div class='ajaxpoll-answer-vote" . ( $our ? ' 
ajaxpoll-our-vote' : '' ) ."'><span title='" . wfMessage( 
'ajaxpoll-percent-votes', sprintf( $percent ) )->text() . "'>" . ( ( isset( 
$poll_result ) && !empty( $poll_result[$i + 1] ) ) ? $poll_result[$i + 1] : 0 ) 
. "</span><div style='width: " . $percent . "%;" . ( $percent == 0 ? ' 
border:0;' : '' ) . "'></div></div>
-</div>
+"</label></div>{$resultBar}</div>
 ";
                                }
 
@@ -414,34 +474,52 @@
                        // >= 1.17 support
                        $db = $updater->getDB();
 
+                       $patchPath = dirname( __FILE__ ) . '/patches/';
+
                        if ( $db->tableExists( 'poll_info' ) ) {
+
                                # poll_info.poll_title field was dropped in 
AJAXPoll version 1.72
                                $updater->dropExtensionField( 
                                        'poll_info',
                                        'poll_title',
-                                       dirname( __FILE__ ) . 
'/patches/drop-field--poll_info-poll_title.sql' 
+                                       $patchPath . 
'drop-field--poll_info-poll_title.sql'
                                );
                                $updater->addExtensionTable(
                                        'ajaxpoll_info',
-                                       dirname( __FILE__ ) . 
'/patches/rename-table--poll_info.sql' 
+                                       $patchPath . 
'rename-table--poll_info.sql'
                                );
+
                        } else {
+
                                $updater->addExtensionTable(
                                        'ajaxpoll_info',
-                                       dirname( __FILE__ ) . 
'/patches/create-table--ajaxpoll_info.sql' 
+                                       $patchPath . 
'create-table--ajaxpoll_info.sql'
+                               );
+
+                       }
+
+                       if ( $db->tableExists( 'ajaxpoll_info' ) ) {
+                               $updater->addExtensionField(
+                                       'ajaxpoll_info',
+                                       'poll_show_results_before_voting',
+                                       $patchPath . 
'add-field--ajaxpoll_info-poll_show_results_before_voting.sql'
                                );
                        }
 
                        if ( $db->tableExists( 'poll_vote' ) ) {
+
                                $updater->addExtensionTable(
-                                       'ajaxpoll_vote',
-                                       dirname( __FILE__ ) . 
'/patches/rename-table--poll_vote.sql' 
+                                       'poll_vote',
+                                       $patchPath . 
'rename-table--poll_vote.sql'
                                );
+
                        } else {
+
                                $updater->addExtensionTable(
                                        'ajaxpoll_vote',
-                                       dirname( __FILE__ ) . 
'/patches/create-table--ajaxpoll_vote.sql' 
+                                       $patchPath . 
'create-table--ajaxpoll_vote.sql'
                                );
+
                        }
 
                }
diff --git 
a/patches/add-field--ajaxpoll_info-poll_show_results_before_voting.sql 
b/patches/add-field--ajaxpoll_info-poll_show_results_before_voting.sql
new file mode 100644
index 0000000..a913e2b
--- /dev/null
+++ b/patches/add-field--ajaxpoll_info-poll_show_results_before_voting.sql
@@ -0,0 +1,4 @@
+--
+-- SQL schema update for AJAXPoll extension to add the 
poll_show_results_before_voting field
+--
+ALTER TABLE /*_*/ajaxpoll_info ADD poll_show_results_before_voting TINYINT(1);

-- 
To view, visit https://gerrit.wikimedia.org/r/65562
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: merged
Gerrit-Change-Id: Iae1f3f2806afecbca3e3c41460dbbf20fc557525
Gerrit-PatchSet: 14
Gerrit-Project: mediawiki/extensions/AJAXPoll
Gerrit-Branch: master
Gerrit-Owner: Wikinaut <[email protected]>
Gerrit-Reviewer: Wikinaut <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to