Skip to content

Commit 6ed5694

Browse files
committed
Show comments user has made on the site
1 parent 4e19225 commit 6ed5694

File tree

5 files changed

+76
-14
lines changed

5 files changed

+76
-14
lines changed

src/controllers/User/View.php

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
namespace BNETDocs\Controllers\User;
33

44
use \BNETDocs\Libraries\Authentication;
5+
use \BNETDocs\Libraries\Comment;
56
use \BNETDocs\Libraries\Credits;
67
use \BNETDocs\Libraries\Document;
78
use \BNETDocs\Libraries\Exceptions\UserNotFoundException;
@@ -47,27 +48,24 @@ protected function getUserInfo(UserViewModel &$model)
4748
$model->user_profile = ($model->user ? $model->user->getUserProfile() : null);
4849

4950
// Summary of contributions
50-
$model->sum_documents = Credits::getTotalDocumentsByUserId(
51-
$model->user_id
52-
);
53-
$model->sum_news_posts = Credits::getTotalNewsPostsByUserId(
54-
$model->user_id
55-
);
56-
$model->sum_packets = Credits::getTotalPacketsByUserId(
57-
$model->user_id
58-
);
59-
$model->sum_servers = Credits::getTotalServersByUserId(
60-
$model->user_id
61-
);
51+
$model->sum_comments = Credits::getTotalCommentsByUserId($model->user_id);
52+
$model->sum_documents = Credits::getTotalDocumentsByUserId($model->user_id);
53+
$model->sum_news_posts = Credits::getTotalNewsPostsByUserId($model->user_id);
54+
$model->sum_packets = Credits::getTotalPacketsByUserId($model->user_id);
55+
$model->sum_servers = Credits::getTotalServersByUserId($model->user_id);
6256

6357
// Total number of contributions
6458
$model->contributions = 0;
59+
$model->contributions += $model->sum_comments;
6560
$model->contributions += $model->sum_documents;
6661
$model->contributions += $model->sum_news_posts;
6762
$model->contributions += $model->sum_packets;
6863
$model->contributions += $model->sum_servers;
6964

7065
// References to the contributions
66+
$model->comments = ($model->sum_comments ?
67+
Comment::getCommentsByUserId($model->user_id, true) : null
68+
);
7169
$model->documents = ($model->sum_documents ?
7270
Document::getDocumentsByUserId($model->user_id) : null
7371
);

src/libraries/Comment.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,47 @@ public static function getAll($parent_type, $parent_id) {
148148
return null;
149149
}
150150

151+
public static function getCommentsByUserId(int $user_id, bool $descending)
152+
{
153+
$o = ($descending ? 'DESC' : 'ASC');
154+
if (!isset(Common::$database)) {
155+
Common::$database = DatabaseDriver::getDatabaseObject();
156+
}
157+
try
158+
{
159+
$stmt = Common::$database->prepare(sprintf('
160+
SELECT
161+
`content`,
162+
`created_datetime`,
163+
`edited_count`,
164+
`edited_datetime`,
165+
`id`,
166+
`parent_id`,
167+
`parent_type`,
168+
`user_id`
169+
FROM `comments`
170+
WHERE `user_id` = :id ORDER BY `created_datetime` %s, `id` %s;
171+
', $o, $o));
172+
$stmt->bindParam(':id', $user_id, PDO::PARAM_INT);
173+
if (!$stmt->execute())
174+
{
175+
throw new QueryException('Cannot get comments by user id');
176+
}
177+
$objects = [];
178+
while ($row = $stmt->fetch(PDO::FETCH_OBJ))
179+
{
180+
$objects[] = new self($row);
181+
}
182+
$stmt->closeCursor();
183+
return $objects;
184+
}
185+
catch (PDOException $e)
186+
{
187+
throw new QueryException('Cannot get comments by user id', $e);
188+
}
189+
return null;
190+
}
191+
151192
public function getContent($prepare) {
152193
if (!$prepare) {
153194
return $this->content;

src/libraries/Credits.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,20 @@ public function getTopContributorsByServers() {
151151
return $result;
152152
}
153153

154+
public static function getTotalCommentsByUserId($user_id) {
155+
if (!isset(Common::$database)) {
156+
Common::$database = DatabaseDriver::getDatabaseObject();
157+
}
158+
$stmt = Common::$database->prepare("
159+
SELECT COUNT(*) AS `sum` FROM `comments` WHERE `user_id` = :id;
160+
");
161+
$stmt->bindParam(":id", $user_id, PDO::PARAM_INT);
162+
$stmt->execute();
163+
$obj = $stmt->fetch(PDO::FETCH_OBJ);
164+
$stmt->closeCursor();
165+
return (int) $obj->sum;
166+
}
167+
154168
public static function getTotalDocumentsByUserId($user_id) {
155169
if (!isset(Common::$database)) {
156170
Common::$database = DatabaseDriver::getDatabaseObject();

src/templates/Comment/Section.inc.phtml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,20 +28,22 @@ $c_delete_visible_admin = ($active_user && $active_user->getOption(User::OPTION_
2828
$c_user_string = ($c_user ? sprintf('<a href="%s"><img class="rounded" src="%s"/> %s</a>', $c_user_url, $c_user_avatar, $c_user_name) : $c_user_name);
2929
$c_time_string = sprintf('<time class="small text-muted" datetime="%s">%s</time>', $c_created_dt->format('c'), $c_created_dt->format('D M j, Y g:ia T'));
3030

31+
$c_context_visible = (isset($comment_show_parent) && $comment_show_parent);
3132
$c_edit_visible = ($c_user_id === $active_user_id || $c_edit_visible_admin);
3233
$c_delete_visible = ($c_user_id === $active_user_id || $c_delete_visible_admin);
3334

3435
$c_content = (
3536
($c_delete_visible ? '<a class="btn btn-sm btn-danger float-right m-1" href="' . Common::relativeUrlToAbsolute('/comment/delete?id=' . $c_id) . '" title="Delete">❌</a>' : '') .
3637
($c_edit_visible ? '<a class="btn btn-sm btn-secondary float-right m-1" href="' . Common::relativeUrlToAbsolute('/comment/edit?id=' . $c_id) . '" title="Edit">📝</a>' : '') .
38+
($c_context_visible ? '<a class="btn btn-sm btn-secondary float-right m-1" href="' . $c_parent_url . '" title="Context">🔎</a>' : '') .
3739
$c->getContent(true)
3840
);
3941

4042
printf('<div class="row bg-primary border border-dark mt-2 py-3 rounded"><div class="col-3">%s<br/>%s</div><div class="col-9">%s</div></div>', $c_user_string, $c_time_string, $c_content);
4143
}} ?>
4244
</div>
4345
</div>
44-
<? if ($active_user) { ?>
46+
<? if (isset($active_user) && !is_null($active_user) && isset($comment_parent_type) && isset($comment_parent_id)) { ?>
4547
<div class="row mb-3 justify-content-center">
4648
<div class="col-md-8">
4749
<form method="POST" action="<?=Common::relativeUrlToAbsolute('/comment/create')?>">

src/templates/User/View.phtml

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
<?php /* vim: set colorcolumn= expandtab shiftwidth=2 softtabstop=2 tabstop=4 smarttab: */
22
namespace BNETDocs\Templates\User;
3-
use \BNETDocs\Libraries\Document;
3+
use \BNETDocs\Libraries\Comment;
44
use \CarlBennett\MVC\Libraries\Common;
55
use \CarlBennett\MVC\Libraries\Pair;
66
$active_user = $this->getContext()->active_user;
7+
$comments = $this->getContext()->comments;
78
$contributions = $this->getContext()->contributions;
89
$documents = $this->getContext()->documents;
910
$news_posts = $this->getContext()->news_posts;
1011
$packets = $this->getContext()->packets;
1112
$servers = $this->getContext()->servers;
13+
$sum_comments = $this->getContext()->sum_comments;
1214
$sum_documents = $this->getContext()->sum_documents;
1315
$sum_news_posts = $this->getContext()->sum_news_posts;
1416
$sum_packets = $this->getContext()->sum_packets;
@@ -102,6 +104,7 @@ require('./header.inc.phtml'); ?>
102104
</tr>
103105
<? } ?>
104106
<tr class="text-muted"><th scope="row" class="align-middle text-right">Member for</th><td><?=$user_est?></td></tr>
107+
<tr class="text-muted"><th scope="row" class="align-middle text-right">Comments made</th><td><?=number_format($sum_comments)?></td></tr>
105108
<tr class="text-muted"><th scope="row" class="align-middle text-right">Documents authored</th><td><?=number_format($sum_documents)?></td></tr>
106109
<tr class="text-muted"><th scope="row" class="align-middle text-right">News posted</th><td><?=number_format($sum_news_posts)?></td></tr>
107110
<tr class="text-muted"><th scope="row" class="align-middle text-right">Packets authored</th><td><?=number_format($sum_packets)?></td></tr>
@@ -204,5 +207,9 @@ require('./header.inc.phtml'); ?>
204207
</div>
205208
<? } ?>
206209
</div>
210+
<? if (!empty($comments)) {
211+
$comment_show_parent = true;
212+
require('./Comment/Section.inc.phtml');
213+
} ?>
207214
</div>
208215
<? require('./footer.inc.phtml'); ?>

0 commit comments

Comments
 (0)