summaryrefslogtreecommitdiffstats
path: root/extensions/Review/lib/Util.pm
blob: 7304f9ba653dbf636f6b911eb128d3b720e56203 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
#
# This Source Code Form is "Incompatible With Secondary Licenses", as
# defined by the Mozilla Public License, v. 2.0.

package Bugzilla::Extension::Review::Util;
use strict;
use warnings;

use base qw(Exporter);
use Bugzilla;

our @EXPORT = qw( rebuild_review_counters );

sub rebuild_review_counters {
    my ($callback) = @_;
    my $dbh = Bugzilla->dbh;

    $dbh->bz_start_transaction;
    my $rows = $dbh->selectall_arrayref("
        SELECT flags.requestee_id AS user_id,
               flagtypes.name AS flagtype,
               COUNT(*) as count
          FROM flags
               INNER JOIN profiles ON profiles.userid = flags.requestee_id
               INNER JOIN flagtypes ON flagtypes.id = flags.type_id
         WHERE flags.status = '?'
               AND flagtypes.name IN ('review', 'feedback', 'needinfo')
         GROUP BY flags.requestee_id, flagtypes.name
    ", { Slice => {} });

    my ($count, $total, $current) = (1, scalar(@$rows), { id => 0 });
    foreach my $row (@$rows) {
        $callback->($count++, $total) if $callback;
        if ($row->{user_id} != $current->{id}) {
            _update_profile($dbh, $current) if $current->{id};
            $current = { id => $row->{user_id} };
        }
        $current->{$row->{flagtype}} = $row->{count};
    }
    _update_profile($dbh, $current) if $current->{id};
    $dbh->bz_commit_transaction;
}

sub _update_profile {
    my ($dbh, $data) = @_;
    $dbh->do("
        UPDATE profiles
           SET review_request_count = ?,
               feedback_request_count = ?,
               needinfo_request_count = ?
         WHERE userid = ?",
        undef,
        $data->{review} || 0,
        $data->{feedback} || 0,
        $data->{needinfo} || 0,
        $data->{id}
    );
}

1;