Skip to content

Commit a2f5a5e

Browse files
committedJul 24, 2021
Refactor Credits page
1 parent d9d40cd commit a2f5a5e

File tree

4 files changed

+149
-60
lines changed

4 files changed

+149
-60
lines changed
 

‎src/controllers/Credits.php

+2
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ public function &run(Router &$router, View &$view, array &$args) {
2424
protected function getCredits(CreditsModel &$model) {
2525
$credits = new CreditsLib();
2626
$model->total_users = CreditsLib::getTotalUsers();
27+
$model->top_contributors_by_comments
28+
= $credits->getTopContributorsByComments();
2729
$model->top_contributors_by_documents
2830
= $credits->getTopContributorsByDocuments();
2931
$model->top_contributors_by_news_posts

‎src/libraries/Credits.php

+34
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,40 @@ public static function getTotalUsers() {
2323
return (int) $obj->sum;
2424
}
2525

26+
public function getTopContributorsByComments()
27+
{
28+
if (!isset(Common::$database))
29+
{
30+
Common::$database = DatabaseDriver::getDatabaseObject();
31+
}
32+
$q = Common::$database->prepare('
33+
SELECT
34+
`u`.`id` AS `user_id`,
35+
IFNULL(
36+
IFNULL(`u`.`display_name`, `u`.`username`), \'Anonymous\'
37+
) AS `name`,
38+
COUNT(`c`.`id`) AS `comments_made`
39+
FROM
40+
`users` AS `u`
41+
RIGHT JOIN
42+
`comments` AS `c` ON `c`.`user_id` = `u`.`id`
43+
GROUP BY
44+
`u`.`id`
45+
ORDER BY
46+
`comments_made` DESC,
47+
`c`.`created_datetime` ASC
48+
LIMIT 5;
49+
');
50+
$r = $q->execute();
51+
if (!$r) return $r;
52+
$r = [];
53+
while ($o = $q->fetch(PDO::FETCH_OBJ))
54+
{
55+
$r[] = $o;
56+
}
57+
return $r;
58+
}
59+
2660
public function getTopContributorsByDocuments() {
2761
if (!isset(Common::$database)) {
2862
Common::$database = DatabaseDriver::getDatabaseObject();

‎src/models/Credits.php

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
class Credits extends Model {
88

99
public $total_users;
10+
public $top_contributors_by_comments;
1011
public $top_contributors_by_documents;
1112
public $top_contributors_by_news_posts;
1213
public $top_contributors_by_packets;

‎src/templates/Credits.phtml

+112-60
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ $title = 'Contributors';
77
$description = 'The top contributors to BNETDocs grouped by different types of content.';
88
$this->opengraph->attach(new Pair('url', '/credits'));
99
$total_users = $this->getContext()->total_users;
10+
$top_contributors_by_comments = $this->getContext()->top_contributors_by_comments;
1011
$top_contributors_by_documents = $this->getContext()->top_contributors_by_documents;
1112
$top_contributors_by_news_posts = $this->getContext()->top_contributors_by_news_posts;
1213
$top_contributors_by_packets = $this->getContext()->top_contributors_by_packets;
@@ -18,127 +19,178 @@ require('./header.inc.phtml');
1819
<h2>Contributors</h2>
1920
<p>We have a total of <strong><?=number_format($total_users)?> users</strong>, of which the following are our top contributors grouped by content.</p>
2021

22+
<h3>Comments</h3>
23+
<p>Top contributors by most comments made.</p>
24+
<table class="table table-hover table-striped"><thead>
25+
<tr><th>Rank</th><th>Who</th><th>Sum</th></tr>
26+
</thead><tbody>
27+
<? $rank = 0;
28+
foreach ($top_contributors_by_comments as $item)
29+
{
30+
++$rank;
31+
$item_name = filter_var($item->name, FILTER_SANITIZE_FULL_SPECIAL_CHARS);
32+
$item_sum = number_format($item->comments_made);
33+
$item_sum_str = sprintf('%d comment%s', $item_sum, ($item_sum != 1 ? 's' : ''));
34+
if (is_null($item->user_id))
35+
{
36+
$item_user = null;
37+
$item_user_string = $item_name;
38+
}
39+
else
40+
{
41+
$item_user = $users[$item->user_id] ?? null;
42+
if (!$item_user)
43+
{
44+
$users[$item->user_id] = $item_user = new User($item->user_id);
45+
}
46+
47+
$item_user_string = sprintf('<a href="%s"><img class="rounded mr-2" src="%s"/>%s</a>',
48+
$users[$item->user_id]->getURI(),
49+
$users[$item->user_id]->getAvatarURI(22),
50+
$item_name
51+
);
52+
}
53+
printf('<tr><td>%s</td><td>%s</td><td>%s</td></tr>%s', $rank, $item_user_string, $item_sum_str, PHP_EOL);
54+
} ?>
55+
</tbody></table>
56+
2157
<h3>Documents</h3>
2258
<p>Top contributors by most documents authored.</p>
2359
<table class="table table-hover table-striped"><thead>
24-
<tr>
25-
<th class="rank">Rank</th>
26-
<th class="who">Who</th>
27-
<th class="sum">Sum</th>
28-
</tr>
60+
<tr><th>Rank</th><th>Who</th><th>Sum</th></tr>
2961
</thead><tbody>
3062
<? $rank = 0;
31-
foreach ($top_contributors_by_documents as $item) {
63+
foreach ($top_contributors_by_documents as $item)
64+
{
3265
++$rank;
3366
$item_name = filter_var($item->name, FILTER_SANITIZE_FULL_SPECIAL_CHARS);
3467
$item_sum = number_format($item->documents_authored);
35-
$item_sum_str = sprintf('%d document', $item_sum);
36-
if ($item_sum != 1) $item_sum_str .= 's';
37-
if (is_null($item->user_id)) {
38-
echo '<tr><td>' . $rank . '</td><td>' . $item_name . '</td><td>' . $item_sum_str . '</td></tr>' . PHP_EOL;
39-
} else {
40-
if (!isset($users[$item->user_id])) $users[$item->user_id] = new User($item->user_id);
41-
$item_user = sprintf(
42-
'<a href="%s"><img src="%s"/> %s</a>',
68+
$item_sum_str = sprintf('%d document%s', $item_sum, ($item_sum != 1 ? 's' : ''));
69+
if (is_null($item->user_id))
70+
{
71+
$item_user = null;
72+
$item_user_string = $item_name;
73+
}
74+
else
75+
{
76+
$item_user = $users[$item->user_id] ?? null;
77+
if (!$item_user)
78+
{
79+
$users[$item->user_id] = $item_user = new User($item->user_id);
80+
}
81+
82+
$item_user_string = sprintf('<a href="%s"><img class="rounded mr-2" src="%s"/>%s</a>',
4383
$users[$item->user_id]->getURI(),
4484
$users[$item->user_id]->getAvatarURI(22),
4585
$item_name
4686
);
4787
}
48-
echo '<tr><td>' . $rank . '</td><td>' . $item_user . '</td><td>' . $item_sum_str . '</td></tr>' . PHP_EOL;
88+
printf('<tr><td>%s</td><td>%s</td><td>%s</td></tr>%s', $rank, $item_user_string, $item_sum_str, PHP_EOL);
4989
} ?>
5090
</tbody></table>
5191

5292
<h3>News Posts</h3>
5393
<p>Top contributors by most news posts created.</p>
5494
<table class="table table-hover table-striped"><thead>
55-
<tr>
56-
<th class="rank">Rank</th>
57-
<th class="who">Who</th>
58-
<th class="sum">Sum</th>
59-
</tr>
95+
<tr><th>Rank</th><th>Who</th><th>Sum</th></tr>
6096
</thead><tbody>
6197
<? $rank = 0;
62-
foreach ($top_contributors_by_news_posts as $item) {
98+
foreach ($top_contributors_by_news_posts as $item)
99+
{
63100
++$rank;
64101
$item_name = filter_var($item->name, FILTER_SANITIZE_FULL_SPECIAL_CHARS);
65102
$item_sum = number_format($item->news_posts_created);
66-
$item_sum_str = sprintf('%d news post', $item_sum);
67-
if ($item_sum != 1) $item_sum_str .= 's';
68-
if (is_null($item->user_id)) {
69-
echo '<tr><td>' . $rank . '</td><td>' . $item_name . '</td><td>' . $item_sum_str . '</td></tr>' . PHP_EOL;
70-
} else {
71-
if (!isset($users[$item->user_id])) $users[$item->user_id] = new User($item->user_id);
72-
$item_user = sprintf(
73-
'<a href="%s"><img src="%s"/> %s</a>',
103+
$item_sum_str = sprintf('%d news post%s', $item_sum, ($item_sum != 1 ? 's' : ''));
104+
if (is_null($item->user_id))
105+
{
106+
$item_user = null;
107+
$item_user_string = $item_name;
108+
}
109+
else
110+
{
111+
$item_user = $users[$item->user_id] ?? null;
112+
if (!$item_user)
113+
{
114+
$users[$item->user_id] = $item_user = new User($item->user_id);
115+
}
116+
117+
$item_user_string = sprintf('<a href="%s"><img class="rounded mr-2" src="%s"/>%s</a>',
74118
$users[$item->user_id]->getURI(),
75119
$users[$item->user_id]->getAvatarURI(22),
76120
$item_name
77121
);
78-
echo '<tr><td>' . $rank . '</td><td>' . $item_user . '</td><td>' . $item_sum_str . '</td></tr>' . PHP_EOL;
79122
}
123+
printf('<tr><td>%s</td><td>%s</td><td>%s</td></tr>%s', $rank, $item_user_string, $item_sum_str, PHP_EOL);
80124
} ?>
81125
</tbody></table>
82126

83127
<h3>Packets</h3>
84128
<p>Top contributors by most packets authored.</p>
85129
<table class="table table-hover table-striped"><thead>
86-
<tr>
87-
<th class="rank">Rank</th>
88-
<th class="who">Who</th>
89-
<th class="sum">Sum</th>
90-
</tr>
130+
<tr><th>Rank</th><th>Who</th><th>Sum</th></tr>
91131
</thead><tbody>
92132
<? $rank = 0;
93-
foreach ($top_contributors_by_packets as $item) {
133+
foreach ($top_contributors_by_packets as $item)
134+
{
94135
++$rank;
95136
$item_name = filter_var($item->name, FILTER_SANITIZE_FULL_SPECIAL_CHARS);
96137
$item_sum = number_format($item->packets_authored);
97-
$item_sum_str = sprintf('%d packet', $item_sum);
98-
if ($item_sum != 1) $item_sum_str .= 's';
99-
if (is_null($item->user_id)) {
100-
echo '<tr><td>' . $rank . '</td><td>' . $item_name . '</td><td>' . $item_sum_str . '</td></tr>' . PHP_EOL;
101-
} else {
102-
if (!isset($users[$item->user_id])) $users[$item->user_id] = new User($item->user_id);
103-
$item_user = sprintf(
104-
'<a href="%s"><img src="%s"/> %s</a>',
138+
$item_sum_str = sprintf('%d packet%s', $item_sum, ($item_sum != 1 ? 's' : ''));
139+
if (is_null($item->user_id))
140+
{
141+
$item_user = null;
142+
$item_user_string = $item_name;
143+
}
144+
else
145+
{
146+
$item_user = $users[$item->user_id] ?? null;
147+
if (!$item_user)
148+
{
149+
$users[$item->user_id] = $item_user = new User($item->user_id);
150+
}
151+
152+
$item_user_string = sprintf('<a href="%s"><img class="rounded mr-2" src="%s"/>%s</a>',
105153
$users[$item->user_id]->getURI(),
106154
$users[$item->user_id]->getAvatarURI(22),
107155
$item_name
108156
);
109-
echo '<tr><td>' . $rank . '</td><td>' . $item_user . '</td><td>' . $item_sum_str . '</td></tr>' . PHP_EOL;
110157
}
158+
printf('<tr><td>%s</td><td>%s</td><td>%s</td></tr>%s', $rank, $item_user_string, $item_sum_str, PHP_EOL);
111159
} ?>
112160
</tbody></table>
113161

114162
<h3>Servers</h3>
115163
<p>Top contributors by most servers owned.</p>
116164
<table class="table table-hover table-striped"><thead>
117-
<tr>
118-
<th class="rank">Rank</th>
119-
<th class="who">Who</th>
120-
<th class="sum">Sum</th>
121-
</tr>
165+
<tr><th>Rank</th><th>Who</th><th>Sum</th></tr>
122166
</thead><tbody>
123167
<? $rank = 0;
124-
foreach ($top_contributors_by_servers as $item) {
168+
foreach ($top_contributors_by_servers as $item)
169+
{
125170
++$rank;
126171
$item_name = filter_var($item->name, FILTER_SANITIZE_FULL_SPECIAL_CHARS);
127172
$item_sum = number_format($item->servers_owned);
128-
$item_sum_str = sprintf('%d server', $item_sum);
129-
if ($item_sum != 1) $item_sum_str .= 's';
130-
if (is_null($item->user_id)) {
131-
echo '<tr><td>' . $rank . '</td><td>' . $item_name . '</td><td>' . $item_sum_str . '</td></tr>' . PHP_EOL;
132-
} else {
133-
if (!isset($users[$item->user_id])) $users[$item->user_id] = new User($item->user_id);
134-
$item_user = sprintf(
135-
'<a href="%s"><img src="%s"/> %s</a>',
173+
$item_sum_str = sprintf('%d server%s', $item_sum, ($item_sum != 1 ? 's' : ''));
174+
if (is_null($item->user_id))
175+
{
176+
$item_user = null;
177+
$item_user_string = $item_name;
178+
}
179+
else
180+
{
181+
$item_user = $users[$item->user_id] ?? null;
182+
if (!$item_user)
183+
{
184+
$users[$item->user_id] = $item_user = new User($item->user_id);
185+
}
186+
187+
$item_user_string = sprintf('<a href="%s"><img class="rounded mr-2" src="%s"/>%s</a>',
136188
$users[$item->user_id]->getURI(),
137189
$users[$item->user_id]->getAvatarURI(22),
138190
$item_name
139191
);
140-
echo '<tr><td>' . $rank . '</td><td>' . $item_user . '</td><td>' . $item_sum_str . '</td></tr>' . PHP_EOL;
141192
}
193+
printf('<tr><td>%s</td><td>%s</td><td>%s</td></tr>%s', $rank, $item_user_string, $item_sum_str, PHP_EOL);
142194
} ?>
143195
</tbody></table>
144196
</div>

0 commit comments

Comments
 (0)
Please sign in to comment.