fu: Close-up of Fu, bringing a scoop of water to her mouth (Default)
fu ([personal profile] fu) wrote in [site community profile] changelog2011-08-24 11:07 am

[dw-free] Mark your answer in answered polls

[commit: http://hg.dwscoalition.org/dw-free/rev/c3bbfdeafd16]

http://bugs.dwscoalition.org/show_bug.cgi?id=2600

Mark your own poll answers even in the results view.

Patch by [personal profile] yvi.

Files modified:
  • bin/upgrading/en.dat
  • cgi-bin/LJ/Poll.pm
--------------------------------------------------------------------------------
diff -r 6808519e7eec -r c3bbfdeafd16 bin/upgrading/en.dat
--- a/bin/upgrading/en.dat	Wed Aug 24 18:52:56 2011 +0800
+++ b/bin/upgrading/en.dat	Wed Aug 24 19:07:13 2011 +0800
@@ -2141,6 +2141,8 @@
 
 poll.submit=Answer
 
+poll.useranswer=Your answer was: [[answer]]
+
 poll.viewanswers=View Answers
 
 portal.bdays.count.des=By default, the 5 friends with the soonest birthdays are shown.
diff -r 6808519e7eec -r c3bbfdeafd16 cgi-bin/LJ/Poll.pm
--- a/cgi-bin/LJ/Poll.pm	Wed Aug 24 18:52:56 2011 +0800
+++ b/cgi-bin/LJ/Poll.pm	Wed Aug 24 19:07:13 2011 +0800
@@ -617,6 +617,28 @@
     $self->{status} = 'X';
 }
 
+# get the answer a user gave in a poll
+sub get_pollanswers {
+    my ( $pollid, $u ) = @_;
+
+    # try getting the has from memcache
+    my $memkey = [$u->userid, "pollresult2:$u->userid:$pollid"];
+    my $result = LJ::MemCache::get( $memkey );
+    return %$result if $result;
+
+    my $sth;
+    my %answers;
+    $sth = $u->prepare( "SELECT pollqid, value FROM pollresult2 WHERE pollid=? AND userid=?" );
+    $sth->execute( $pollid, $u->userid );
+
+    while ( my ( $qid, $value ) = $sth->fetchrow_array ) {
+        $answers{$qid} = $value;
+    }
+
+    LJ::MemCache::set( $memkey, \%answers );
+    return %answers;
+}
+
 # Mark poll as open
 sub open_poll {
     my $self = $_[0];
@@ -889,14 +911,11 @@
     my %preval;
 
     $ret .= qq{<div id='poll-$pollid-container' class='poll-container'>};
+    if ( $remote ) {
+        %preval = get_pollanswers( $pollid, $remote );
+    }
+
     if ( $do_form ) {
-        $sth = $self->journal->prepare( "SELECT pollqid, value FROM pollresult2 WHERE pollid=? AND userid=? AND journalid=?" );
-        $sth->execute( $pollid, $remote->userid, $self->journalid );
-
-        while ( my ( $qid, $value ) = $sth->fetchrow_array ) {
-            $preval{$qid} = $value;
-        }
-
         my $url = LJ::create_url( "/poll/", host => $LJ::DOMAIN_WEB, viewing_style => 1, args => { id => $pollid } );
         $ret .= "<form class='LJ_PollForm' action='$url' method='post'>";
         $ret .= LJ::form_auth();
@@ -1016,8 +1035,12 @@
                      id="LJ_PollAnswerLink_${pollid}_$qid">
                 } . LJ::Lang::ml('poll.viewanswers') . "</a><br />" if $self->can_view;
 
-            ### but, if this is a non-text item, and we're showing results, need to load the answers:
-            if ($q->type ne "text") {
+            ### if this is a text question and the viewing user answered it, show that answer
+            if ( $q->type eq "text" && $preval{$qid} ) {
+                LJ::Poll->clean_poll( \$preval{$qid} );
+                $results_table .= "<br />" . BML::ml('poll.useranswer', { "answer" => $preval{$qid} } );
+            } elsif ( $q->type ne "text" ) {
+                ### but, if this is a non-text item, and we're showing results, need to load the answers:
                 $sth = $self->journal->prepare( "SELECT value FROM pollresult2 WHERE pollid=? AND pollqid=? AND journalid=?" );
                 $sth->execute( $pollid, $qid, $self->journalid );
                 while (my ($val) = $sth->fetchrow_array) {
@@ -1127,8 +1150,8 @@
 
                 # displaying a radio or checkbox
                 if ($do_form) {
-                    my $preval_qid = $preval{$qid} || '';
-                    $prevanswer = $clearanswers ? 0 : $preval_qid =~ /\b$itid\b/;
+                    my $qvalue = $preval{$qid} || '';
+                    $prevanswer = $clearanswers ? 0 : $qvalue =~ /\b$itid\b/;
                     $results_table .= LJ::html_check({ 'type' => $q->type, 'name' => "pollq-$qid", 'class'=>"poll-$pollid",
                                               'value' => $itid, 'id' => "pollq-$pollid-$qid-$itid",
                                               'selected' => $prevanswer });
@@ -1141,18 +1164,22 @@
                 my $percent = sprintf("%.1f", (100 * $count / ($usersvoted||1)));
                 my $width = 20+int(($count/$maxitvotes)*380);
 
+                # did the user viewing this poll choose this option? If so, mark it
+                my $qvalue = $preval{$qid} || '';
+                my $answered = ( $qvalue =~ /\b$itid\b/ ) ? "*" : "";
+
                 if ($do_table) {
                     $results_table .= "<tr valign='middle'><td align='right'>$item</td><td>";
                     $results_table .= LJ::img( 'poll_left', '', { style => 'vertical-align:middle' } );
                     $results_table .= "<img src='$LJ::IMGPREFIX/poll/mainbar.gif' style='vertical-align:middle' height='14' width='$width' alt='' />";
                     $results_table .= LJ::img( 'poll_right', '', { style => 'vertical-align:middle' } );
-                    $results_table .= "<b>$count</b> ($percent%)</td></tr>";
+                    $results_table .= "<b>$count</b> ($percent%) $answered</td></tr>";
                 } else {
                     $results_table .= "<p>$item<br /><span style='white-space: nowrap'>";
                     $results_table .= LJ::img( 'poll_left', '', { style => 'vertical-align:middle' } );
                     $results_table .= "<img src='$LJ::IMGPREFIX/poll/mainbar.gif' style='vertical-align:middle' height='14' width='$width' alt='' />";
                     $results_table .= LJ::img( 'poll_right', '', { style => 'vertical-align:middle' } );
-                    $results_table .= "<b>$count</b> ($percent%)</span></p>";
+                    $results_table .= "<b>$count</b> ($percent%) $answered</span></p>";
                 }
             }
 
@@ -1376,6 +1403,10 @@
         return 0;
     }
 
+    # delete user answer MemCache entry
+    my $memkey = [$remote->userid, "pollresult2:$remote->userid:$pollid"];
+    LJ::MemCache::delete( $memkey );
+
     # if unique prop is on, make sure that a particular email address can only vote once
     if ($poll->is_unique) {
         # make sure their email address is validated
--------------------------------------------------------------------------------