Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
96.23% covered (success)
96.23%
51 / 53
66.67% covered (warning)
66.67%
2 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
CommentsRenderer
96.23% covered (success)
96.23%
51 / 53
66.67% covered (warning)
66.67%
2 / 3
13
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 renderComment
92.86% covered (success)
92.86%
26 / 28
0.00% covered (danger)
0.00%
0 / 1
7.02
 render
100.00% covered (success)
100.00%
22 / 22
100.00% covered (success)
100.00%
1 / 1
5
1<?php
2
3class CommentsRenderer
4{
5    public function __construct(string $name, ?int $userID, $urlParams)
6    {
7        $this->name = $name;
8        $this->userID = $userID;
9        $this->params = $urlParams;
10    }
11
12    private function renderComment($comment, $index)
13    {
14        echo '<div class="sandboxComment">';
15        $commentColor = $comment['from_admin'] ? 'admin-text' : '';
16        echo '<table class="sandboxTable2" width="100%" border="0">';
17        echo '<tr><td width="73%"><div style="padding-bottom:7px;"><b>#' . ($index + 1) . '</b> | ';
18        if ($comment['set_connection_id'])
19        {
20            echo '<a href="/' . $comment['set_connection_id'] . '?search=topics">';
21            echo $comment['set_title'] . ' - ' . $comment['set_num'] . '</a><br>';
22        }
23        else
24            echo '<i>Problem (id=' . $comment['tsumego_id'] . 'doesn\'t have set connection.</i>';
25
26        echo '<br></div>';
27        echo '<div class="' . $commentColor . '">';
28        echo $comment['from_name'] . ':<br>';
29        echo '</div>';
30        echo '<div ';
31        if($comment['from_admin'])
32            echo 'class="commentBox2" ';
33        echo 'id="comment_' . $comment['id'] . '"';
34        if (TsumegoUtil::isSolvedStatus($comment['status']) || Auth::isAdmin())
35            echo '>' . $comment['message'] . '</div>';
36        else
37            echo ' class="grey-text">[You need to solve this problem to see the comment]</div>';
38        $date = new DateTime($comment['created']);
39        echo '</td><td class="sandboxTable2time" align="right">' . $date->format('Y-m-d') . '<br>' . $date->format('H:i') . '</td>';
40        echo '</tr>';
41        echo '<tr><td colspan="2"><div width="100%"><div align="center">';
42
43        if ($comment['set_connection_id'])
44            new TsumegoButton($comment['tsumego_id'], $comment['set_connection_id'], $comment['set_num'], $comment['status'])->render();
45        echo '</div></td></tr>';
46        echo '</table>';
47        echo '</div>';
48    }
49
50    public function render()
51    {
52        $parameters = [];
53        $parameters[] = Auth::getUserID();
54
55        $queryCondition = "tsumego_comment.deleted = 0";
56        if ($this->userID)
57            Util::addSqlCondition($queryCondition, "tsumego_comment.user_id = " . $this->userID);
58
59        $querySelects = "
60SELECT
61    tsumego_comment.message AS message,
62    tsumego_comment.tsumego_id AS tsumego_id,
63    tsumego_comment.created AS created,
64    tsumego_status.status AS status,
65    user.isAdmin AS from_admin,
66    user.name as from_name,
67    tsumego_comment.id as id,
68    set_connection.id AS set_connection_id,
69    CONCAT(`set`.title, ' ', `set`.title2) as set_title,
70    set_connection.num as set_num";
71        $queryFrom = "
72FROM
73    tsumego_comment
74    JOIN tsumego ON tsumego_comment.tsumego_id = tsumego.id
75    JOIN user ON tsumego_comment.user_id = user.id
76    LEFT JOIN tsumego_status ON tsumego_status.tsumego_id = tsumego.id AND tsumego_status.user_id = ?
77    LEFT JOIN set_connection ON set_connection.tsumego_id = tsumego.id
78    LEFT JOIN `set` ON set_connection.set_id = `set`.id";
79
80        if (!empty($queryCondition))
81            $queryFrom .= " WHERE " . $queryCondition;
82        $queryFrom .= ' ORDER BY tsumego_comment.created DESC';
83        $queryFrom .= ' LIMIT ' . self::$PAGE_SIZE;
84
85        $pageIndex = isset($this->params[$this->name]) ? max(1, (int) $this->params[$this->name]) : 1;
86        $count = Util::query("SELECT COUNT(*) " . $queryFrom, $parameters)[0]['COUNT(*)'];
87
88        $offset = ($pageIndex - 1) * self::$PAGE_SIZE;
89        $queryFrom .= ' OFFSET ' . $offset;
90        $comments = Util::query($querySelects . $queryFrom, $parameters);
91        echo PaginationHelper::render($pageIndex, intval(ceil($count / self::$PAGE_SIZE)), $this->name);
92        foreach ($comments as $index => $comment)
93        {
94            $this->renderComment($comment, $index + $offset);
95            echo '<div class="space"><br></div>';
96        }
97    }
98    public ?int $userID;
99    public static int $PAGE_SIZE = 10;
100    public string $name;
101    public array $params;
102}