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

[livejournal] r19667: LJSUP-9341: votepoll error processing ad...

Committer: vtroitsky
LJSUP-9341: votepoll error processing added: in case of wring pollitid specified
U   trunk/cgi-bin/LJ/Poll.pm
U   trunk/cgi-bin/ljprotocol.pl
Modified: trunk/cgi-bin/LJ/Poll.pm
===================================================================
--- trunk/cgi-bin/LJ/Poll.pm	2011-08-09 09:33:40 UTC (rev 19666)
+++ trunk/cgi-bin/LJ/Poll.pm	2011-08-09 09:40:15 UTC (rev 19667)
@@ -1783,6 +1783,7 @@
     my $answers = shift;
     my $error = shift;
     my $warnings = shift;
+    my %opts = @_;
 
     my $poll = LJ::Poll->new($pollid);
     unless ($poll) {
@@ -1862,6 +1863,38 @@
     ### load all the questions
     my @qs = $poll->questions;
 
+    if ($opts{wrong_value_as_error}) {
+        foreach my $q (@qs) {
+            my $qid = $q->pollqid;
+            my $val = $answers->{$qid};
+            next unless ($val);   # check given values only, so allow the user to change his mind
+
+            my @vals = ();
+            if ($q->type eq "check") {
+                ## multi-selected items are comma separated from htdocs/poll/index.bml
+                @vals = split(/,/, $val);
+            } elsif ($q->type eq "scale") {
+                my ($from, $to, $by) = split(m!/!, $q->opts);
+                if ($val < $from || $val > $to) {
+                    $$error = LJ::Lang::ml('poll.error.pollitid');
+                    return 0;
+                }
+            } elsif ($q->type ne 'text') {
+                push @vals, $val;
+            }
+            if ($q->type ne 'text' && $q->type ne 'scale' && $opts{wrong_value_as_error}) {
+                my %pollitids;
+                map { $pollitids{$_->{pollitid}} = 1 } $q->items;
+                foreach my $v (@vals) {
+                    unless ($pollitids{$v}) {
+                        $$error = LJ::Lang::ml('poll.error.pollitid');
+                        return 0;
+                    }
+                }
+            }
+        }
+    }
+
     unless (LJ::get_lock($dbh, 'global', "poll:$pollid:$remote->{userid}")) {
         $$error = LJ::Lang::ml('poll.error.cantlock');
         return 0;

Modified: trunk/cgi-bin/ljprotocol.pl
===================================================================
--- trunk/cgi-bin/ljprotocol.pl	2011-08-09 09:33:40 UTC (rev 19666)
+++ trunk/cgi-bin/ljprotocol.pl	2011-08-09 09:40:15 UTC (rev 19667)
@@ -402,7 +402,7 @@
     my @warnings;
     my $errors;
 
-    unless (LJ::Poll->process_vote($u, $pollid, $answers, \$errors, \@warnings)) {
+    unless (LJ::Poll->process_vote($u, $pollid, $answers, \$errors, \@warnings, wrong_value_as_error => 1)) {
         return fail($err, 103, $errors);
     }
 

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