Issues (1963)

html/ops/badge_assign.php (1 issue)

1
#!/usr/bin/env php
2
<?php
3
// This file is part of BOINC.
4
// http://boinc.berkeley.edu
5
// Copyright (C) 2013 University of California
6
//
7
// BOINC is free software; you can redistribute it and/or modify it
8
// under the terms of the GNU Lesser General Public License
9
// as published by the Free Software Foundation,
10
// either version 3 of the License, or (at your option) any later version.
11
//
12
// BOINC is distributed in the hope that it will be useful,
13
// but WITHOUT ANY WARRANTY; without even the implied warranty of
14
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
15
// See the GNU Lesser General Public License for more details.
16
//
17
// You should have received a copy of the GNU Lesser General Public License
18
// along with BOINC.  If not, see <http://www.gnu.org/licenses/>.
19
20
// Assign badges based on RAC percentile.
21
// Customize this to grant other types of badges
22
23
require_once("../inc/util_ops.inc");
24
25
// thresholds for the various badges
26
// (i.e. gold badge is for top 1% of active users/teams)
27
//
28
$badge_pctiles = array(1, 5, 25);
29
$badge_images = array("pct_1.png", "pct_5.png", "pct_25.png");
30
31
// get the records for percentile badges; create them if needed
32
//
33
function get_pct_badges($badge_name_prefix, $badge_pctiles, $badge_images) {
34
    $badges = array();
35
    for ($i=0; $i<3; $i++) {
36
        $badges[$i] = get_badge($badge_name_prefix."_".$i, "Top ".$badge_pctiles[$i]."% in average credit", $badge_images[$i]);
37
    }
38
    return $badges;
39
}
40
41
// get the RAC percentiles from the database
42
//
43
function get_percentiles($is_user, $badge_pctiles) {
44
    $percentiles = array();
45
    for ($i=0; $i<3; $i++) {
46
        if ($is_user) {
47
            $percentiles[$i] = BoincUser::percentile("expavg_credit", "expavg_credit>1", 100-$badge_pctiles[$i]);
48
        } else {
49
            $percentiles[$i] = BoincTeam::percentile("expavg_credit", "expavg_credit>1", 100-$badge_pctiles[$i]);
50
        }
51
        if ($percentiles[$i] === false) {
52
            die("Can't get percentiles\n");
0 ignored issues
show
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
53
        }
54
    }
55
    return $percentiles;
56
}
57
58
// decide which badge to assign, if any.
59
// Unassign other badges.
60
//
61
function assign_pct_badge($is_user, $item, $percentiles, $badges) {
62
    for ($i=0; $i<3; $i++) {
63
        if ($item->expavg_credit >= $percentiles[$i]) {
64
            assign_badge($is_user, $item, $badges[$i]);
65
            unassign_badges($is_user, $item, $badges, $i);
66
            return;
67
        }
68
    }
69
    unassign_badges($is_user, $item, $badges, -1);
70
}
71
72
// Scan through all the users/teams, 1000 at a time,
73
// and assign/unassign RAC badges
74
//
75
function assign_badges($is_user, $badge_pctiles, $badge_images) {
76
    $kind = $is_user?"user":"team";
77
    $badges = get_pct_badges($kind."_pct", $badge_pctiles, $badge_images);
78
    $pctiles = get_percentiles($is_user, $badge_pctiles);
79
    //echo "thresholds for $kind badges: $pctiles[0] $pctiles[1] $pctiles[2]\n";
80
81
    $n = 0;
82
    $maxid = $is_user?BoincUser::max("id"):BoincTeam::max("id");
83
    while ($n <= $maxid) {
84
        $m = $n + 1000;
85
        if ($is_user) {
86
            $items = BoincUser::enum_fields("id, expavg_credit", "id>=$n and id<$m and total_credit>0");
87
        } else {
88
            $items = BoincTeam::enum_fields("id, expavg_credit", "id>=$n and id<$m and total_credit>0");
89
        }
90
        foreach ($items as $item) {
91
            assign_pct_badge($is_user, $item, $pctiles, $badges);
92
            // ... assign other types of badges
93
        }
94
        $n = $m;
95
    }
96
}
97
98
echo "Starting: ", time_str(time()), "\n";
99
100
assign_badges(true, $badge_pctiles, $badge_images);
101
assign_badges(false, $badge_pctiles, $badge_images);
102
103
echo "Finished: ", time_str(time()), "\n";
104
105
?>
106