sunnyman's (sunnyman) wrote in changelog,
sunnyman's
sunnyman
changelog

[livejournal] r19487: LJSUP-9265: get_polls parameter added in...

Committer: vtroitsky
LJSUP-9265: get_polls parameter added into getevents and getfriendspage functions
U   trunk/cgi-bin/LJ/Poll/Question.pm
U   trunk/cgi-bin/LJ/Poll.pm
U   trunk/cgi-bin/ljprotocol.pl
Modified: trunk/cgi-bin/LJ/Poll/Question.pm
===================================================================
--- trunk/cgi-bin/LJ/Poll/Question.pm	2011-07-18 07:56:39 UTC (rev 19486)
+++ trunk/cgi-bin/LJ/Poll/Question.pm	2011-07-18 08:14:33 UTC (rev 19487)
@@ -237,6 +237,32 @@
     return $res;
 }
 
+sub get_question_xml {
+    my $self = shift;
+    my $ret;
+    my $attrs = ' id="'.$self->pollqid.'" type="'.$self->type.'"';
+
+    my $opts = $self->opts;
+    if ($self->type eq 'text') {
+        my ($size,$max) = split(m!/!, $opts);
+        $attrs .= ' size="'.$size.'" max="'.$max.'"';
+    } elsif ($self->type eq 'scale') {
+        my ($from, $to, $by) = split(m!/!, $opts);
+        $by ||= 1;
+        $attrs .= ' from="'.$from.'" to="'.$to.'" by="'.$by.'"';
+    }
+    $ret = "<lj-pq$attrs>";
+    my $text = $self->qtext;
+    if($text) {
+        LJ::Poll->clean_poll(\$text);
+        $ret .= $text;
+    }
+    my @items = $self->items;
+    map { $ret .= '<lj-pi id="'.$_->{pollitid}.'" sortorder="'.$_->{sortorder}.'">'.$_->{item}.'</lj-pi>' } @items;
+    $ret .= '</lj-pq>';
+    return $ret;
+}
+
 # Count answers pages
 sub answers_pages {
     my $self = shift;

Modified: trunk/cgi-bin/LJ/Poll.pm
===================================================================
--- trunk/cgi-bin/LJ/Poll.pm	2011-07-18 07:56:39 UTC (rev 19486)
+++ trunk/cgi-bin/LJ/Poll.pm	2011-07-18 08:14:33 UTC (rev 19487)
@@ -1010,6 +1010,26 @@
     return $ret;
 }
 
+# get poll description as a xml
+sub get_poll_xml {
+    my $self = shift;
+    my $ret = '';
+
+    my $attrs = ' id="'.$self->pollid.'"';
+    my $name = $self->name;
+    if ($name) {
+        LJ::Poll->clean_poll(\$name);
+        $attrs .=" name=\"$name\"";
+    }
+
+    $attrs .= ' whovote="'.$self->whovote.'" whoview="'.$self->whoview.'"';
+    $ret .= "<lj-poll$attrs>";
+    my @questions = $self->questions;
+    map { $ret .= $_->get_question_xml() } @questions;
+    $ret .= '</lj-poll>';
+    return $ret;
+}
+
 sub render_results {
     my $self = shift;
     my %opts = @_;
@@ -1716,20 +1736,22 @@
 
 # takes a scalarref to entry text and expands lj-poll tags into the polls
 sub expand_entry {
-    my ($class, $entryref) = @_;
+    my ($class, $entryref, %opts) = @_;
+    my $getpolls = $opts{getpolls};
 
     my $expand = sub {
         my $pollid = (shift) + 0;
+        my $viewpoll = shift;
 
         return "[Error: no poll ID]" unless $pollid;
 
         my $poll = LJ::Poll->new($pollid);
         return "[Error: Invalid poll ID $pollid]" unless $poll && $poll->valid;
 
-        return $poll->render;
+        return ($viewpoll ? $poll->get_poll_xml : $poll->render);
     };
 
-    $$entryref =~ s/<lj-poll-(\d+)>/$expand->($1)/eg;
+    $$entryref =~ s/<lj-poll-(\d+)>/$expand->($1, $getpolls)/eg;
 }
 
 sub process_submission {

Modified: trunk/cgi-bin/ljprotocol.pl
===================================================================
--- trunk/cgi-bin/ljprotocol.pl	2011-07-18 07:56:39 UTC (rev 19486)
+++ trunk/cgi-bin/ljprotocol.pl	2011-07-18 08:14:33 UTC (rev 19487)
@@ -279,7 +279,7 @@
 
     # check arguments
     my $mode = $req->{mode} || 'all';
-    return fail($err, 203, 'wrong mode') unless($mode =~ /enter|results|answers|all/);
+    return fail($err, 203, 'mode') unless($mode =~ /enter|results|answers|all/);
 
     my $pollid = $req->{pollid} + 0;
     return fail($err, 200, 'pollid') unless($pollid);
@@ -313,10 +313,14 @@
 
     my $time = $poll->get_time_user_submitted($u);
     $res->{submitted_time} = $time if ($time);
+    $res->{pollqid} = $pollqid if($pollqid);
 
     # Get all questions
     my @questions = $poll->questions;
 
+    @questions = grep { $_->pollqid eq $pollqid } @questions if ($pollqid);
+    return fail($err, 203, 'pollqid') unless(@questions);
+
     # mode to show poll questions
     if($mode =~ /enter|all/) {
         # render_enter
@@ -986,6 +990,7 @@
         ) if $req->{trim_widgets};
 
         LJ::EmbedModule->expand_entry($entry->poster, \$h{event_raw}, get_video_id => 1) if $req->{get_video_ids};
+        LJ::Poll->expand_entry(\$h{event_raw}, getpolls => 1) if $req->{get_polls};
 
         if ($req->{view}) {
             LJ::EmbedModule->expand_entry($entry->poster, \$h{event_raw}, edit => 1) if $req->{view} eq 'stored';
@@ -3287,6 +3292,7 @@
         ) if $req->{trim_widgets};
 
         LJ::EmbedModule->expand_entry($uowner, \$t->[1], get_video_id => 1) if($req->{get_video_ids});
+        LJ::Poll->expand_entry(\$t->[1], getpolls => 1) if $req->{get_polls};
 
         if ($req->{view}) {
             LJ::EmbedModule->expand_entry($uowner, \$t->[1], edit => 1) if $req->{view} eq 'stored';

Tags: livejournal, pl, pm, sunnyman
Subscribe
  • Post a new comment

    Error

    Anonymous comments are disabled in this journal

    default userpic

    Your reply will be screened

    Your IP address will be recorded 

  • 0 comments