]> git.ipfire.org Git - thirdparty/bugzilla.git/commitdiff
Bug 290511: Move RemoveVotes() out of globals.pl - Patch by Frédéric Buclin <LpSolit...
authorlpsolit%gmail.com <>
Sun, 17 Apr 2005 00:17:04 +0000 (00:17 +0000)
committerlpsolit%gmail.com <>
Sun, 17 Apr 2005 00:17:04 +0000 (00:17 +0000)
Bugzilla/Bug.pm
editproducts.cgi
globals.pl

index 8accc452c964cb5f94adc4eb137f1143b64b1cc7..962b1de429537d97099aaf65ca6fa3d4ab87c60a 100755 (executable)
@@ -38,6 +38,7 @@ use CGI::Carp qw(fatalsToBrowser);
 
 use Bugzilla;
 use Bugzilla::Attachment;
+use Bugzilla::BugMail;
 use Bugzilla::Config;
 use Bugzilla::Constants;
 use Bugzilla::Flag;
@@ -48,9 +49,9 @@ use Bugzilla::Error;
 
 use base qw(Exporter);
 @Bugzilla::Bug::EXPORT = qw(
-    AppendComment
+    AppendComment ValidateComment
     bug_alias_to_id
-    ValidateComment
+    RemoveVotes
 );
 
 use constant MAX_COMMENT_LENGTH => 65535;
@@ -835,6 +836,97 @@ sub ValidateComment ($) {
     }
 }
 
+# If a bug is moved to a product which allows less votes per bug
+# compared to the previous product, extra votes need to be removed.
+sub RemoveVotes {
+    my ($id, $who, $reason) = (@_);
+    my $dbh = Bugzilla->dbh;
+
+    my $whopart = ($who) ? " AND votes.who = $who" : "";
+
+    my $sth = $dbh->prepare("SELECT profiles.login_name, " .
+                            "profiles.userid, votes.vote_count, " .
+                            "products.votesperuser, products.maxvotesperbug " .
+                            "FROM profiles " . 
+                            "LEFT JOIN votes ON profiles.userid = votes.who " .
+                            "LEFT JOIN bugs USING(bug_id) " .
+                            "LEFT JOIN products ON products.id = bugs.product_id " .
+                            "WHERE votes.bug_id = ? " . $whopart);
+    $sth->execute($id);
+    my @list;
+    while (my ($name, $userid, $oldvotes, $votesperuser, $maxvotesperbug) = $sth->fetchrow_array()) {
+        push(@list, [$name, $userid, $oldvotes, $votesperuser, $maxvotesperbug]);
+    }
+    if (scalar(@list)) {
+        foreach my $ref (@list) {
+            my ($name, $userid, $oldvotes, $votesperuser, $maxvotesperbug) = (@$ref);
+            my $s;
+
+            $maxvotesperbug = min($votesperuser, $maxvotesperbug);
+
+            # If this product allows voting and the user's votes are in
+            # the acceptable range, then don't do anything.
+            next if $votesperuser && $oldvotes <= $maxvotesperbug;
+
+            # If the user has more votes on this bug than this product
+            # allows, then reduce the number of votes so it fits
+            my $newvotes = $maxvotesperbug;
+
+            my $removedvotes = $oldvotes - $newvotes;
+
+            $s = ($oldvotes == 1) ? "" : "s";
+            my $oldvotestext = "You had $oldvotes vote$s on this bug.";
+
+            $s = ($removedvotes == 1) ? "" : "s";
+            my $removedvotestext = "You had $removedvotes vote$s removed from this bug.";
+
+            my $newvotestext;
+            if ($newvotes) {
+                $dbh->do("UPDATE votes SET vote_count = ? " .
+                         "WHERE bug_id = ? AND who = ?",
+                         undef, ($newvotes, $id, $userid));
+                $s = $newvotes == 1 ? "" : "s";
+                $newvotestext = "You still have $newvotes vote$s on this bug."
+            } else {
+                $dbh->do("DELETE FROM votes WHERE bug_id = ? AND who = ?",
+                         undef, ($id, $userid));
+                $newvotestext = "You have no more votes remaining on this bug.";
+            }
+
+            # Notice that we did not make sure that the user fit within the $votesperuser
+            # range.  This is considered to be an acceptable alternative to losing votes
+            # during product moves.  Then next time the user attempts to change their votes,
+            # they will be forced to fit within the $votesperuser limit.
+
+            # Now lets send the e-mail to alert the user to the fact that their votes have
+            # been reduced or removed.
+            my %substs;
+
+            $substs{"to"} = $name . Param('emailsuffix');
+            $substs{"bugid"} = $id;
+            $substs{"reason"} = $reason;
+
+            $substs{"votesremoved"} = $removedvotes;
+            $substs{"votesold"} = $oldvotes;
+            $substs{"votesnew"} = $newvotes;
+
+            $substs{"votesremovedtext"} = $removedvotestext;
+            $substs{"votesoldtext"} = $oldvotestext;
+            $substs{"votesnewtext"} = $newvotestext;
+
+            $substs{"count"} = $removedvotes . "\n    " . $newvotestext;
+
+            my $msg = PerformSubsts(Param("voteremovedmail"), \%substs);
+            Bugzilla::BugMail::MessageToMTA($msg);
+        }
+        my $votes = $dbh->selectrow_array("SELECT SUM(vote_count) " .
+                                          "FROM votes WHERE bug_id = ?",
+                                          undef, $id) || 0;
+        $dbh->do("UPDATE bugs SET votes = ? WHERE bug_id = ?",
+                 undef, ($votes, $id));
+    }
+}
+
 sub AUTOLOAD {
   use vars qw($AUTOLOAD);
   my $attr = $AUTOLOAD;
index fdc70aefb485aa3f78f38f9150c3c69d41aeb5e3..ac54ab51724653618561e674e381044b7f222d7f 100755 (executable)
@@ -35,6 +35,7 @@ use vars qw ($template $vars);
 use Bugzilla::Constants;
 require "CGI.pl";
 require "globals.pl";
+use Bugzilla::Bug;
 use Bugzilla::Series;
 use Bugzilla::User;
 use Bugzilla::Config qw(:DEFAULT $datadir);
index a9c3a36ce4976db05683754179d69a0d294b6d02..d33319e980fa3cc0d58ec71257fd6e576380019d 100644 (file)
@@ -1040,94 +1040,6 @@ sub OpenStates {
 }
 
 
-sub RemoveVotes {
-    my ($id, $who, $reason) = (@_);
-    my $whopart = "";
-    if ($who) {
-        $whopart = " AND votes.who = $who";
-    }
-    SendSQL("SELECT profiles.login_name, profiles.userid, votes.vote_count, " .
-            "products.votesperuser, products.maxvotesperbug " .
-            "FROM profiles " . 
-            "LEFT JOIN votes ON profiles.userid = votes.who " .
-            "LEFT JOIN bugs USING(bug_id) " .
-            "LEFT JOIN products ON products.id = bugs.product_id " .
-            "WHERE votes.bug_id = $id " .
-            $whopart);
-    my @list;
-    while (MoreSQLData()) {
-        my ($name, $userid, $oldvotes, $votesperuser, $maxvotesperbug) = (FetchSQLData());
-        push(@list, [$name, $userid, $oldvotes, $votesperuser, $maxvotesperbug]);
-    }
-    if (0 < @list) {
-        foreach my $ref (@list) {
-            my ($name, $userid, $oldvotes, $votesperuser, $maxvotesperbug) = (@$ref);
-            my $s;
-
-            $maxvotesperbug = $votesperuser if ($votesperuser < $maxvotesperbug);
-
-            # If this product allows voting and the user's votes are in
-            # the acceptable range, then don't do anything.
-            next if $votesperuser && $oldvotes <= $maxvotesperbug;
-
-            # If the user has more votes on this bug than this product
-            # allows, then reduce the number of votes so it fits
-            my $newvotes = $votesperuser ? $maxvotesperbug : 0;
-
-            my $removedvotes = $oldvotes - $newvotes;
-
-            $s = $oldvotes == 1 ? "" : "s";
-            my $oldvotestext = "You had $oldvotes vote$s on this bug.";
-
-            $s = $removedvotes == 1 ? "" : "s";
-            my $removedvotestext = "You had $removedvotes vote$s removed from this bug.";
-
-            my $newvotestext;
-            if ($newvotes) {
-                SendSQL("UPDATE votes SET vote_count = $newvotes " .
-                        "WHERE bug_id = $id AND who = $userid");
-                $s = $newvotes == 1 ? "" : "s";
-                $newvotestext = "You still have $newvotes vote$s on this bug."
-            } else {
-                SendSQL("DELETE FROM votes WHERE bug_id = $id AND who = $userid");
-                $newvotestext = "You have no more votes remaining on this bug.";
-            }
-
-            # Notice that we did not make sure that the user fit within the $votesperuser
-            # range.  This is considered to be an acceptable alternative to losing votes
-            # during product moves.  Then next time the user attempts to change their votes,
-            # they will be forced to fit within the $votesperuser limit.
-
-            # Now lets send the e-mail to alert the user to the fact that their votes have
-            # been reduced or removed.
-            my %substs;
-
-            $substs{"to"} = $name . Param('emailsuffix');
-            $substs{"bugid"} = $id;
-            $substs{"reason"} = $reason;
-
-            $substs{"votesremoved"} = $removedvotes;
-            $substs{"votesold"} = $oldvotes;
-            $substs{"votesnew"} = $newvotes;
-
-            $substs{"votesremovedtext"} = $removedvotestext;
-            $substs{"votesoldtext"} = $oldvotestext;
-            $substs{"votesnewtext"} = $newvotestext;
-
-            $substs{"count"} = $removedvotes . "\n    " . $newvotestext;
-
-            my $msg = PerformSubsts(Param("voteremovedmail"),
-                                    \%substs);
-
-            Bugzilla::BugMail::MessageToMTA($msg);
-        }
-        SendSQL("SELECT SUM(vote_count) FROM votes WHERE bug_id = $id");
-        my $v = FetchOneColumn();
-        $v ||= 0;
-        SendSQL("UPDATE bugs SET votes = $v WHERE bug_id = $id");
-    }
-}
-
 ###############################################################################
 
 # Constructs a format object from URL parameters. You most commonly call it