Bug 332522: Remove $::prodmaxvotes - Patch by Fr�d�ric Buclin <LpSolit@gmail.com> r=mkanat a=myk

This commit is contained in:
lpsolit%gmail.com 2006-05-21 22:15:05 +00:00
parent 52d7cd4f61
commit 8842500aa4
3 changed files with 46 additions and 72 deletions

View File

@ -32,7 +32,7 @@ use strict;
use vars qw(@legal_platform use vars qw(@legal_platform
@legal_priority @legal_severity @legal_opsys @legal_bug_status @legal_priority @legal_severity @legal_opsys @legal_bug_status
@settable_resolution %prodmaxvotes); @settable_resolution);
use CGI::Carp qw(fatalsToBrowser); use CGI::Carp qw(fatalsToBrowser);
@ -526,8 +526,9 @@ sub use_votes {
my ($self) = @_; my ($self) = @_;
return 0 if $self->{'error'}; return 0 if $self->{'error'};
return Param('usevotes') $self->{'prod_obj'} ||= new Bugzilla::Product({name => $self->{'product'}});
&& $::prodmaxvotes{$self->{product}} > 0;
return Param('usevotes') && $self->{'prod_obj'}->votes_per_user > 0;
} }
sub groups { sub groups {

View File

@ -49,7 +49,6 @@ sub globals_pl_sillyness {
$zz = @main::legal_platform; $zz = @main::legal_platform;
$zz = @main::legal_priority; $zz = @main::legal_priority;
$zz = @main::legal_severity; $zz = @main::legal_severity;
$zz = @main::prodmaxvotes;
} }
# #
@ -99,16 +98,8 @@ $::SIG{PIPE} = 'IGNORE';
sub GenerateVersionTable { sub GenerateVersionTable {
my $dbh = Bugzilla->dbh; my $dbh = Bugzilla->dbh;
my @line;
SendSQL("SELECT name, votesperuser " .
"FROM products ORDER BY name");
while (@line = FetchSQLData()) {
my ($p, $votesperuser) = (@line);
$::prodmaxvotes{$p} = $votesperuser;
}
@::log_columns = $dbh->bz_table_columns('bugs'); @::log_columns = $dbh->bz_table_columns('bugs');
foreach my $i ("bug_id", "creation_ts", "delta_ts", "lastdiffed") { foreach my $i ("bug_id", "creation_ts", "delta_ts", "lastdiffed") {
my $w = lsearch(\@::log_columns, $i); my $w = lsearch(\@::log_columns, $i);
if ($w >= 0) { if ($w >= 0) {
@ -161,8 +152,8 @@ sub GenerateVersionTable {
'*::legal_platform', '*::legal_opsys', '*::legal_platform', '*::legal_opsys',
'*::legal_bug_status', '*::legal_resolution'])); '*::legal_bug_status', '*::legal_resolution']));
print $fh (Data::Dumper->Dump([\@::settable_resolution, \%::prodmaxvotes], print $fh (Data::Dumper->Dump([\@::settable_resolution],
['*::settable_resolution', '*::prodmaxvotes'])); ['*::settable_resolution']));
print $fh "1;\n"; print $fh "1;\n";
close $fh; close $fh;

View File

@ -22,6 +22,7 @@
# Stephan Niemz <st.n@gmx.net> # Stephan Niemz <st.n@gmx.net>
# Christopher Aillon <christopher@aillon.com> # Christopher Aillon <christopher@aillon.com>
# Gervase Markham <gerv@gerv.net> # Gervase Markham <gerv@gerv.net>
# Frédéric Buclin <LpSolit@gmail.com>
use strict; use strict;
use lib "."; use lib ".";
@ -30,6 +31,7 @@ use Bugzilla;
use Bugzilla::Constants; use Bugzilla::Constants;
use Bugzilla::Bug; use Bugzilla::Bug;
use Bugzilla::User; use Bugzilla::User;
use Bugzilla::Product;
require "globals.pl"; require "globals.pl";
@ -127,7 +129,7 @@ sub show_user {
$dbh->bz_lock_tables('bugs READ', 'products READ', 'votes WRITE', $dbh->bz_lock_tables('bugs READ', 'products READ', 'votes WRITE',
'cc READ', 'bug_group_map READ', 'user_group_map READ', 'cc READ', 'bug_group_map READ', 'user_group_map READ',
'group_group_map READ', 'groups READ'); 'group_group_map READ', 'groups READ', 'group_control_map READ');
if ($canedit && $bug_id) { if ($canedit && $bug_id) {
# Make sure there is an entry for this bug # Make sure there is an entry for this bug
@ -140,38 +142,27 @@ sub show_user {
VALUES (?, ?, 0)', undef, ($who, $bug_id)); VALUES (?, ?, 0)', undef, ($who, $bug_id));
} }
} }
# Calculate the max votes per bug for each product; doing it here means
# we can do it all in one query.
my %maxvotesperbug;
if($canedit) {
my $products = $dbh->selectall_arrayref('SELECT name, maxvotesperbug
FROM products');
foreach (@$products) {
my ($prod, $max) = @$_;
$maxvotesperbug{$prod} = $max;
}
}
my @products; my @products;
my $products = $user->get_selectable_products;
# Read the votes data for this user for each product # Read the votes data for this user for each product.
foreach my $product (sort(keys(%::prodmaxvotes))) { foreach my $product (@$products) {
next if $::prodmaxvotes{$product} <= 0; next unless ($product->votes_per_user > 0);
my @bugs; my @bugs;
my $total = 0; my $total = 0;
my $onevoteonly = 0; my $onevoteonly = 0;
my $vote_list = my $vote_list =
$dbh->selectall_arrayref('SELECT votes.bug_id, votes.vote_count, $dbh->selectall_arrayref('SELECT votes.bug_id, votes.vote_count,
bugs.short_desc, bugs.bug_status bugs.short_desc, bugs.bug_status
FROM votes FROM votes
INNER JOIN bugs ON votes.bug_id = bugs.bug_id INNER JOIN bugs
INNER JOIN products ON bugs.product_id = products.id ON votes.bug_id = bugs.bug_id
WHERE votes.who = ? AND products.name = ? WHERE votes.who = ?
AND bugs.product_id = ?
ORDER BY votes.bug_id', ORDER BY votes.bug_id',
undef, ($who, $product)); undef, ($who, $product->id));
foreach (@$vote_list) { foreach (@$vote_list) {
my ($id, $count, $summary, $status) = @$_; my ($id, $count, $summary, $status) = @$_;
@ -181,29 +172,25 @@ sub show_user {
# and they can see there are votes 'missing', but not on what bug # and they can see there are votes 'missing', but not on what bug
# they are. This seems a reasonable compromise; the alternative is # they are. This seems a reasonable compromise; the alternative is
# to lie in the totals. # to lie in the totals.
next if !$user->can_see_bug($id); next if !$user->can_see_bug($id);
push (@bugs, { id => $id, push (@bugs, { id => $id,
summary => $summary, summary => $summary,
count => $count, count => $count,
opened => is_open_state($status) }); opened => is_open_state($status) });
} }
# In case we didn't populate this earlier (i.e. an error, or
# a not logged in user viewing a users votes)
$maxvotesperbug{$product} ||= 0;
$onevoteonly = 1 if (min($::prodmaxvotes{$product}, $onevoteonly = 1 if (min($product->votes_per_user,
$maxvotesperbug{$product}) == 1); $product->max_votes_per_bug) == 1);
# Only add the product for display if there are any bugs in it. # Only add the product for display if there are any bugs in it.
if ($#bugs > -1) { if ($#bugs > -1) {
push (@products, { name => $product, push (@products, { name => $product->name,
bugs => \@bugs, bugs => \@bugs,
onevoteonly => $onevoteonly, onevoteonly => $onevoteonly,
total => $total, total => $total,
maxvotes => $::prodmaxvotes{$product}, maxvotes => $product->votes_per_user,
maxperbug => $maxvotesperbug{$product} }); maxperbug => $product->max_votes_per_bug });
} }
} }
@ -274,35 +261,30 @@ sub record_votes {
# If the user is voting for bugs, make sure they aren't overstuffing # If the user is voting for bugs, make sure they aren't overstuffing
# the ballot box. # the ballot box.
if (scalar(@buglist)) { if (scalar(@buglist)) {
my $product_vote_settings =
$dbh->selectall_arrayref('SELECT bugs.bug_id, products.name,
products.maxvotesperbug
FROM bugs
INNER JOIN products
ON products.id = bugs.product_id
WHERE bugs.bug_id IN
(' . join(', ', @buglist) . ')');
my %prodcount; my %prodcount;
foreach (@$product_vote_settings) { my %products = {};
my ($id, $prod, $max) = @$_; # XXX - We really need a $bug->product() method.
foreach my $bug_id (@buglist) {
my $bug = new Bugzilla::Bug($bug_id, $who);
my $prod = $bug->{'product'};
$products{$prod} ||= new Bugzilla::Product({name => $prod});
$prodcount{$prod} ||= 0; $prodcount{$prod} ||= 0;
$prodcount{$prod} += $votes{$id}; $prodcount{$prod} += $votes{$bug_id};
# Make sure we haven't broken the votes-per-bug limit # Make sure we haven't broken the votes-per-bug limit
($votes{$id} <= $max) ($votes{$bug_id} <= $products{$prod}->max_votes_per_bug)
|| ThrowUserError("too_many_votes_for_bug", || ThrowUserError("too_many_votes_for_bug",
{max => $max, {max => $products{$prod}->max_votes_per_bug,
product => $prod, product => $prod,
votes => $votes{$id}}); votes => $votes{$bug_id}});
} }
# Make sure we haven't broken the votes-per-product limit # Make sure we haven't broken the votes-per-product limit
foreach my $prod (keys(%prodcount)) { foreach my $prod (keys(%prodcount)) {
($prodcount{$prod} <= $::prodmaxvotes{$prod}) ($prodcount{$prod} <= $products{$prod}->votes_per_user)
|| ThrowUserError("too_many_votes_for_product", || ThrowUserError("too_many_votes_for_product",
{max => $::prodmaxvotes{$prod}, {max => $products{$prod}->votes_per_user,
product => $prod, product => $prod,
votes => $prodcount{$prod}}); votes => $prodcount{$prod}});
} }
} }