2003-03-27 03:07:02 +03:00
|
|
|
#!/usr/bin/perl -wT
|
2002-04-05 11:13:55 +04:00
|
|
|
# -*- Mode: perl; indent-tabs-mode: nil -*-
|
|
|
|
#
|
|
|
|
# The contents of this file are subject to the Mozilla Public
|
|
|
|
# License Version 1.1 (the "License"); you may not use this file
|
|
|
|
# except in compliance with the License. You may obtain a copy of
|
|
|
|
# the License at http://www.mozilla.org/MPL/
|
|
|
|
#
|
|
|
|
# Software distributed under the License is distributed on an "AS
|
|
|
|
# IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
|
|
|
|
# implied. See the License for the specific language governing
|
|
|
|
# rights and limitations under the License.
|
|
|
|
#
|
|
|
|
# The Original Code is the Bugzilla Bug Tracking System.
|
|
|
|
#
|
|
|
|
# The Initial Developer of the Original Code is Netscape Communications
|
|
|
|
# Corporation. Portions created by Netscape are
|
|
|
|
# Copyright (C) 1998 Netscape Communications Corporation. All
|
|
|
|
# Rights Reserved.
|
|
|
|
#
|
|
|
|
# Contributor(s): Terry Weissman <terry@mozilla.org>
|
|
|
|
# Stephan Niemz <st.n@gmx.net>
|
|
|
|
# Christopher Aillon <christopher@aillon.com>
|
|
|
|
# Gervase Markham <gerv@gerv.net>
|
|
|
|
|
|
|
|
use strict;
|
|
|
|
use lib ".";
|
|
|
|
|
2003-05-05 05:15:38 +04:00
|
|
|
use Bugzilla;
|
2004-03-27 06:51:44 +03:00
|
|
|
use Bugzilla::Constants;
|
2005-04-20 03:43:00 +04:00
|
|
|
use Bugzilla::Bug;
|
2002-04-05 11:13:55 +04:00
|
|
|
|
2005-08-10 05:30:41 +04:00
|
|
|
require "globals.pl";
|
2002-04-05 11:13:55 +04:00
|
|
|
|
|
|
|
# Use global template variables
|
|
|
|
use vars qw($template $vars);
|
|
|
|
|
2003-05-05 05:15:38 +04:00
|
|
|
my $cgi = Bugzilla->cgi;
|
|
|
|
|
2002-04-05 11:13:55 +04:00
|
|
|
# If the action is show_bug, you need a bug_id.
|
|
|
|
# If the action is show_user, you can supply a userid to show the votes for
|
|
|
|
# another user, otherwise you see your own.
|
|
|
|
# If the action is vote, your votes are set to those encoded in the URL as
|
|
|
|
# <bug_id>=<votes>.
|
|
|
|
#
|
|
|
|
# If no action is defined, we default to show_bug if a bug_id is given,
|
|
|
|
# otherwise to show_user.
|
2004-04-13 22:34:59 +04:00
|
|
|
my $bug_id = $cgi->param('bug_id');
|
|
|
|
my $action = $cgi->param('action') || ($bug_id ? "show_bug" : "show_user");
|
2002-04-05 11:13:55 +04:00
|
|
|
|
|
|
|
if ($action eq "show_bug" ||
|
2004-04-13 22:34:59 +04:00
|
|
|
($action eq "show_user" && defined $cgi->param('user')))
|
2002-04-05 11:13:55 +04:00
|
|
|
{
|
2004-03-27 06:51:44 +03:00
|
|
|
Bugzilla->login();
|
2002-04-05 11:13:55 +04:00
|
|
|
}
|
|
|
|
else {
|
2004-03-27 06:51:44 +03:00
|
|
|
Bugzilla->login(LOGIN_REQUIRED);
|
2002-04-05 11:13:55 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
################################################################################
|
|
|
|
# Begin Data/Security Validation
|
|
|
|
################################################################################
|
|
|
|
|
|
|
|
# Make sure the bug ID is a positive integer representing an existing
|
|
|
|
# bug that the user is authorized to access.
|
2004-04-13 22:34:59 +04:00
|
|
|
|
|
|
|
ValidateBugID($bug_id) if defined $bug_id;
|
2002-04-05 11:13:55 +04:00
|
|
|
|
|
|
|
################################################################################
|
|
|
|
# End Data/Security Validation
|
|
|
|
################################################################################
|
|
|
|
|
|
|
|
if ($action eq "show_bug") {
|
|
|
|
show_bug();
|
|
|
|
}
|
|
|
|
elsif ($action eq "show_user") {
|
|
|
|
show_user();
|
|
|
|
}
|
|
|
|
elsif ($action eq "vote") {
|
2005-01-27 22:12:54 +03:00
|
|
|
record_votes() if Param('usevotes');
|
2002-04-05 11:13:55 +04:00
|
|
|
show_user();
|
|
|
|
}
|
|
|
|
else {
|
2002-10-06 15:52:37 +04:00
|
|
|
ThrowCodeError("unknown_action", {action => $action});
|
2002-04-05 11:13:55 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
exit;
|
|
|
|
|
|
|
|
# Display the names of all the people voting for this one bug.
|
|
|
|
sub show_bug {
|
2003-05-05 05:15:38 +04:00
|
|
|
my $cgi = Bugzilla->cgi;
|
|
|
|
|
2004-04-13 22:34:59 +04:00
|
|
|
ThrowCodeError("missing_bug_id") unless defined $bug_id;
|
|
|
|
|
2002-04-05 11:13:55 +04:00
|
|
|
my $total = 0;
|
|
|
|
my @users;
|
|
|
|
|
2003-04-27 11:20:39 +04:00
|
|
|
SendSQL("SELECT profiles.login_name, votes.who, votes.vote_count
|
2005-04-05 01:52:06 +04:00
|
|
|
FROM votes INNER JOIN profiles
|
|
|
|
ON profiles.userid = votes.who
|
|
|
|
WHERE votes.bug_id = $bug_id");
|
2002-04-05 11:13:55 +04:00
|
|
|
|
|
|
|
while (MoreSQLData()) {
|
|
|
|
my ($name, $userid, $count) = (FetchSQLData());
|
|
|
|
push (@users, { name => $name, id => $userid, count => $count });
|
|
|
|
$total += $count;
|
|
|
|
}
|
|
|
|
|
|
|
|
$vars->{'bug_id'} = $bug_id;
|
|
|
|
$vars->{'users'} = \@users;
|
|
|
|
$vars->{'total'} = $total;
|
|
|
|
|
2003-05-05 05:15:38 +04:00
|
|
|
print $cgi->header();
|
2002-04-24 11:24:50 +04:00
|
|
|
$template->process("bug/votes/list-for-bug.html.tmpl", $vars)
|
|
|
|
|| ThrowTemplateError($template->error());
|
2002-04-05 11:13:55 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
# Display all the votes for a particular user. If it's the user
|
|
|
|
# doing the viewing, give them the option to edit them too.
|
|
|
|
sub show_user {
|
|
|
|
GetVersionTable();
|
2005-02-18 00:57:27 +03:00
|
|
|
|
2003-05-05 05:15:38 +04:00
|
|
|
my $cgi = Bugzilla->cgi;
|
2005-02-18 00:57:27 +03:00
|
|
|
my $dbh = Bugzilla->dbh;
|
2003-05-05 05:15:38 +04:00
|
|
|
|
2002-04-05 11:13:55 +04:00
|
|
|
# If a bug_id is given, and we're editing, we'll add it to the votes list.
|
2004-04-13 22:34:59 +04:00
|
|
|
$bug_id ||= "";
|
|
|
|
|
|
|
|
my $name = $cgi->param('user') || Bugzilla->user->login;
|
2003-11-03 06:20:49 +03:00
|
|
|
my $who = DBNameToIdAndCheck($name);
|
2004-07-31 06:27:26 +04:00
|
|
|
my $userid = Bugzilla->user->id;
|
2002-04-05 11:13:55 +04:00
|
|
|
|
2005-01-27 22:12:54 +03:00
|
|
|
my $canedit = (Param('usevotes') && $userid == $who) ? 1 : 0;
|
2005-02-18 00:57:27 +03:00
|
|
|
|
|
|
|
$dbh->bz_lock_tables('bugs READ', 'products READ', 'votes WRITE',
|
|
|
|
'cc READ', 'bug_group_map READ', 'user_group_map READ',
|
|
|
|
'cc AS selectVisible_cc READ', 'groups READ');
|
|
|
|
|
2002-04-05 11:13:55 +04:00
|
|
|
if ($canedit && $bug_id) {
|
|
|
|
# Make sure there is an entry for this bug
|
|
|
|
# in the vote table, just so that things display right.
|
2003-04-27 11:20:39 +04:00
|
|
|
SendSQL("SELECT votes.vote_count FROM votes
|
2002-04-05 11:13:55 +04:00
|
|
|
WHERE votes.bug_id = $bug_id AND votes.who = $who");
|
|
|
|
if (!FetchOneColumn()) {
|
2003-04-27 11:20:39 +04:00
|
|
|
SendSQL("INSERT INTO votes (who, bug_id, vote_count)
|
2002-04-05 11:13:55 +04:00
|
|
|
VALUES ($who, $bug_id, 0)");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
# 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) {
|
2002-08-12 09:43:05 +04:00
|
|
|
SendSQL("SELECT products.name, products.maxvotesperbug
|
2002-04-05 11:13:55 +04:00
|
|
|
FROM products");
|
|
|
|
while (MoreSQLData()) {
|
|
|
|
my ($prod, $max) = FetchSQLData();
|
|
|
|
$maxvotesperbug{$prod} = $max;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
my @products;
|
|
|
|
|
|
|
|
# Read the votes data for this user for each product
|
|
|
|
foreach my $product (sort(keys(%::prodmaxvotes))) {
|
|
|
|
next if $::prodmaxvotes{$product} <= 0;
|
|
|
|
|
|
|
|
my @bugs;
|
|
|
|
my $total = 0;
|
|
|
|
my $onevoteonly = 0;
|
|
|
|
|
2003-04-27 11:20:39 +04:00
|
|
|
SendSQL("SELECT votes.bug_id, votes.vote_count, bugs.short_desc,
|
2002-04-05 11:13:55 +04:00
|
|
|
bugs.bug_status
|
2005-04-05 01:52:06 +04:00
|
|
|
FROM votes
|
|
|
|
INNER JOIN bugs ON votes.bug_id = bugs.bug_id
|
|
|
|
INNER JOIN products ON bugs.product_id = products.id
|
2002-04-05 11:13:55 +04:00
|
|
|
WHERE votes.who = $who
|
2002-08-12 09:43:05 +04:00
|
|
|
AND products.name = " . SqlQuote($product) .
|
2002-04-05 11:13:55 +04:00
|
|
|
"ORDER BY votes.bug_id");
|
|
|
|
|
|
|
|
while (MoreSQLData()) {
|
|
|
|
my ($id, $count, $summary, $status) = FetchSQLData();
|
|
|
|
next if !defined($status);
|
|
|
|
$total += $count;
|
|
|
|
|
|
|
|
# Next if user can't see this bug. So, the totals will be correct
|
|
|
|
# and they can see there are votes 'missing', but not on what bug
|
|
|
|
# they are. This seems a reasonable compromise; the alternative is
|
|
|
|
# to lie in the totals.
|
2004-08-04 20:17:10 +04:00
|
|
|
next if !Bugzilla->user->can_see_bug($id);
|
2002-04-05 11:13:55 +04:00
|
|
|
|
|
|
|
push (@bugs, { id => $id,
|
|
|
|
summary => $summary,
|
|
|
|
count => $count,
|
|
|
|
opened => IsOpenedState($status) });
|
|
|
|
}
|
|
|
|
|
2003-06-28 04:14:56 +04:00
|
|
|
# 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;
|
|
|
|
|
2002-04-05 11:13:55 +04:00
|
|
|
$onevoteonly = 1 if (min($::prodmaxvotes{$product},
|
|
|
|
$maxvotesperbug{$product}) == 1);
|
|
|
|
|
|
|
|
# Only add the product for display if there are any bugs in it.
|
|
|
|
if ($#bugs > -1) {
|
|
|
|
push (@products, { name => $product,
|
|
|
|
bugs => \@bugs,
|
|
|
|
onevoteonly => $onevoteonly,
|
|
|
|
total => $total,
|
|
|
|
maxvotes => $::prodmaxvotes{$product},
|
|
|
|
maxperbug => $maxvotesperbug{$product} });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-04-27 11:20:39 +04:00
|
|
|
SendSQL("DELETE FROM votes WHERE vote_count <= 0");
|
2005-02-18 00:57:27 +03:00
|
|
|
$dbh->bz_unlock_tables();
|
2005-01-27 22:12:54 +03:00
|
|
|
|
|
|
|
$vars->{'canedit'} = $canedit;
|
2002-05-08 01:28:52 +04:00
|
|
|
$vars->{'voting_user'} = { "login" => $name };
|
2002-04-05 11:13:55 +04:00
|
|
|
$vars->{'products'} = \@products;
|
2005-02-08 18:49:57 +03:00
|
|
|
$vars->{'bug_id'} = $bug_id;
|
2002-04-05 11:13:55 +04:00
|
|
|
|
2003-05-05 05:15:38 +04:00
|
|
|
print $cgi->header();
|
2002-04-24 11:24:50 +04:00
|
|
|
$template->process("bug/votes/list-for-user.html.tmpl", $vars)
|
|
|
|
|| ThrowTemplateError($template->error());
|
2002-04-05 11:13:55 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
# Update the user's votes in the database.
|
|
|
|
sub record_votes {
|
|
|
|
############################################################################
|
|
|
|
# Begin Data/Security Validation
|
|
|
|
############################################################################
|
|
|
|
|
2003-05-05 05:15:38 +04:00
|
|
|
my $cgi = Bugzilla->cgi;
|
2005-02-18 00:57:27 +03:00
|
|
|
my $dbh = Bugzilla->dbh;
|
2003-05-05 05:15:38 +04:00
|
|
|
|
2002-04-05 11:13:55 +04:00
|
|
|
# Build a list of bug IDs for which votes have been submitted. Votes
|
|
|
|
# are submitted in form fields in which the field names are the bug
|
|
|
|
# IDs and the field values are the number of votes.
|
2004-04-13 22:34:59 +04:00
|
|
|
|
|
|
|
my @buglist = grep {/^[1-9][0-9]*$/} $cgi->param();
|
2002-04-05 11:13:55 +04:00
|
|
|
|
|
|
|
# If no bugs are in the buglist, let's make sure the user gets notified
|
|
|
|
# that their votes will get nuked if they continue.
|
|
|
|
if (scalar(@buglist) == 0) {
|
2004-04-13 22:34:59 +04:00
|
|
|
if (!defined $cgi->param('delete_all_votes')) {
|
2003-05-05 05:15:38 +04:00
|
|
|
print $cgi->header();
|
2002-04-24 11:24:50 +04:00
|
|
|
$template->process("bug/votes/delete-all.html.tmpl", $vars)
|
|
|
|
|| ThrowTemplateError($template->error());
|
2002-04-05 11:13:55 +04:00
|
|
|
exit();
|
|
|
|
}
|
2004-04-13 22:34:59 +04:00
|
|
|
elsif ($cgi->param('delete_all_votes') == 0) {
|
2003-05-05 05:15:38 +04:00
|
|
|
print $cgi->redirect("votes.cgi");
|
2002-04-05 11:13:55 +04:00
|
|
|
exit();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
# Call ValidateBugID on each bug ID to make sure it is a positive
|
|
|
|
# integer representing an existing bug that the user is authorized
|
|
|
|
# to access, and make sure the number of votes submitted is also
|
|
|
|
# a non-negative integer (a series of digits not preceded by a
|
|
|
|
# minus sign).
|
2004-04-13 22:34:59 +04:00
|
|
|
my %votes;
|
2002-04-05 11:13:55 +04:00
|
|
|
foreach my $id (@buglist) {
|
|
|
|
ValidateBugID($id);
|
2004-04-13 22:34:59 +04:00
|
|
|
$votes{$id} = $cgi->param($id);
|
|
|
|
detaint_natural($votes{$id})
|
2002-10-06 15:52:37 +04:00
|
|
|
|| ThrowUserError("votes_must_be_nonnegative");
|
2002-04-05 11:13:55 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
############################################################################
|
|
|
|
# End Data/Security Validation
|
|
|
|
############################################################################
|
|
|
|
|
|
|
|
GetVersionTable();
|
|
|
|
|
2003-06-03 13:48:15 +04:00
|
|
|
my $who = Bugzilla->user->id;
|
2002-04-05 11:13:55 +04:00
|
|
|
|
|
|
|
# If the user is voting for bugs, make sure they aren't overstuffing
|
|
|
|
# the ballot box.
|
|
|
|
if (scalar(@buglist)) {
|
2002-08-12 09:43:05 +04:00
|
|
|
SendSQL("SELECT bugs.bug_id, products.name, products.maxvotesperbug
|
2005-04-05 01:52:06 +04:00
|
|
|
FROM bugs
|
|
|
|
INNER JOIN products
|
|
|
|
ON products.id = bugs.product_id
|
|
|
|
WHERE bugs.bug_id IN (" . join(", ", @buglist) . ")");
|
2002-04-05 11:13:55 +04:00
|
|
|
|
|
|
|
my %prodcount;
|
|
|
|
|
|
|
|
while (MoreSQLData()) {
|
|
|
|
my ($id, $prod, $max) = FetchSQLData();
|
|
|
|
$prodcount{$prod} ||= 0;
|
2004-04-13 22:34:59 +04:00
|
|
|
$prodcount{$prod} += $votes{$id};
|
2002-04-05 11:13:55 +04:00
|
|
|
|
|
|
|
# Make sure we haven't broken the votes-per-bug limit
|
2004-04-13 22:34:59 +04:00
|
|
|
($votes{$id} <= $max)
|
2002-10-06 15:52:37 +04:00
|
|
|
|| ThrowUserError("too_many_votes_for_bug",
|
|
|
|
{max => $max,
|
|
|
|
product => $prod,
|
2004-04-13 22:34:59 +04:00
|
|
|
votes => $votes{$id}});
|
2002-04-05 11:13:55 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
# Make sure we haven't broken the votes-per-product limit
|
|
|
|
foreach my $prod (keys(%prodcount)) {
|
2002-10-06 15:52:37 +04:00
|
|
|
($prodcount{$prod} <= $::prodmaxvotes{$prod})
|
|
|
|
|| ThrowUserError("too_many_votes_for_product",
|
|
|
|
{max => $::prodmaxvotes{$prod},
|
|
|
|
product => $prod,
|
|
|
|
votes => $prodcount{$prod}});
|
2002-04-05 11:13:55 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
# Update the user's votes in the database. If the user did not submit
|
|
|
|
# any votes, they may be using a form with checkboxes to remove all their
|
|
|
|
# votes (checkboxes are not submitted along with other form data when
|
|
|
|
# they are not checked, and Bugzilla uses them to represent single votes
|
|
|
|
# for products that only allow one vote per bug). In that case, we still
|
|
|
|
# need to clear the user's votes from the database.
|
|
|
|
my %affected;
|
2005-02-18 00:57:27 +03:00
|
|
|
$dbh->bz_lock_tables('bugs WRITE', 'bugs_activity WRITE',
|
|
|
|
'votes WRITE', 'longdescs WRITE', 'profiles READ',
|
|
|
|
'products READ', 'components READ', 'cc READ',
|
|
|
|
'dependencies READ', 'groups READ', 'fielddefs READ',
|
|
|
|
'namedqueries READ', 'whine_queries READ', 'watch READ',
|
|
|
|
'profiles AS watchers READ', 'profiles AS watched READ',
|
2005-04-20 03:43:00 +04:00
|
|
|
'user_group_map READ', 'bug_group_map READ',
|
|
|
|
'email_setting READ');
|
2002-04-05 11:13:55 +04:00
|
|
|
|
|
|
|
# Take note of, and delete the user's old votes from the database.
|
|
|
|
SendSQL("SELECT bug_id FROM votes WHERE who = $who");
|
|
|
|
while (MoreSQLData()) {
|
|
|
|
my $id = FetchOneColumn();
|
|
|
|
$affected{$id} = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
SendSQL("DELETE FROM votes WHERE who = $who");
|
|
|
|
|
|
|
|
# Insert the new values in their place
|
|
|
|
foreach my $id (@buglist) {
|
2004-04-13 22:34:59 +04:00
|
|
|
if ($votes{$id} > 0) {
|
2003-04-27 11:20:39 +04:00
|
|
|
SendSQL("INSERT INTO votes (who, bug_id, vote_count)
|
2004-04-13 22:34:59 +04:00
|
|
|
VALUES ($who, $id, ".$votes{$id}.")");
|
2002-04-05 11:13:55 +04:00
|
|
|
}
|
|
|
|
$affected{$id} = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
# Update the cached values in the bugs table
|
2005-01-08 21:22:45 +03:00
|
|
|
print $cgi->header();
|
2002-04-05 11:13:55 +04:00
|
|
|
foreach my $id (keys %affected) {
|
2003-04-27 11:20:39 +04:00
|
|
|
SendSQL("SELECT sum(vote_count) FROM votes WHERE bug_id = $id");
|
2005-01-08 21:22:45 +03:00
|
|
|
my $v = FetchOneColumn() || 0;
|
2005-02-08 19:51:03 +03:00
|
|
|
SendSQL("UPDATE bugs SET votes = $v WHERE bug_id = $id");
|
2005-01-08 21:22:45 +03:00
|
|
|
my $confirmed = CheckIfVotedConfirmed($id, $who);
|
|
|
|
$vars->{'header_done'} = 1 if $confirmed;
|
2002-04-05 11:13:55 +04:00
|
|
|
}
|
|
|
|
|
2005-02-18 00:57:27 +03:00
|
|
|
$dbh->bz_unlock_tables();
|
2002-04-05 11:13:55 +04:00
|
|
|
|
|
|
|
$vars->{'votes_recorded'} = 1;
|
|
|
|
}
|