Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
96.00% covered (success)
96.00%
48 / 50
66.67% covered (warning)
66.67%
2 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
CommentsRenderer
96.00% covered (success)
96.00%
48 / 50
66.67% covered (warning)
66.67%
2 / 3
12
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.00% covered (success)
92.00%
23 / 25
0.00% covered (danger)
0.00%
0 / 1
6.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 id="comment_' . $comment['id'] . '"';
31        if (TsumegoUtil::isSolvedStatus($comment['status']) || Auth::isAdmin())
32            echo '>' . $comment['message'] . '</div>';
33        else
34            echo ' class="grey-text">[You need to solve this problem to see the comment]</div>';
35        $date = new DateTime($comment['created']);
36        echo '</td><td class="sandboxTable2time" align="right">' . $date->format('Y-m-d') . '<br>' . $date->format('H:i') . '</td>';
37        echo '</tr>';
38        echo '<tr><td colspan="2"><div width="100%"><div align="center">';
39
40        if ($comment['set_connection_id'])
41            new TsumegoButton($comment['tsumego_id'], $comment['set_connection_id'], $comment['set_num'], $comment['status'])->render();
42        echo '</div></td></tr>';
43        echo '</table>';
44        echo '</div>';
45    }
46
47    public function render()
48    {
49        $parameters = [];
50        $parameters[] = Auth::getUserID();
51
52        $queryCondition = "tsumego_comment.deleted = 0";
53        if ($this->userID)
54            Util::addSqlCondition($queryCondition, "tsumego_comment.user_id = " . $this->userID);
55
56        $querySelects = "
57SELECT
58    tsumego_comment.message AS message,
59    tsumego_comment.tsumego_id AS tsumego_id,
60    tsumego_comment.created AS created,
61    tsumego_status.status AS status,
62    user.isAdmin AS from_admin,
63    user.name as from_name,
64    tsumego_comment.id as id,
65    set_connection.id AS set_connection_id,
66    CONCAT(`set`.title, ' ', `set`.title2) as set_title,
67    set_connection.num as set_num";
68        $queryFrom = "
69FROM
70    tsumego_comment
71    JOIN tsumego ON tsumego_comment.tsumego_id = tsumego.id
72    JOIN user ON tsumego_comment.user_id = user.id
73    LEFT JOIN tsumego_status ON tsumego_status.tsumego_id = tsumego.id AND tsumego_status.user_id = ?
74    LEFT JOIN set_connection ON set_connection.tsumego_id = tsumego.id
75    LEFT JOIN `set` ON set_connection.set_id = `set`.id";
76
77        if (!empty($queryCondition))
78            $queryFrom .= " WHERE " . $queryCondition;
79        $queryFrom .= ' ORDER BY tsumego_comment.created DESC';
80        $queryFrom .= ' LIMIT ' . self::$PAGE_SIZE;
81
82        $pageIndex = isset($this->params[$this->name]) ? max(1, (int) $this->params[$this->name]) : 1;
83        $count = Util::query("SELECT COUNT(*) " . $queryFrom, $parameters)[0]['COUNT(*)'];
84
85        $offset = ($pageIndex - 1) * self::$PAGE_SIZE;
86        $queryFrom .= ' OFFSET ' . $offset;
87        $comments = Util::query($querySelects . $queryFrom, $parameters);
88        echo PaginationHelper::render($pageIndex, intval(ceil($count / self::$PAGE_SIZE)), $this->name);
89        foreach ($comments as $index => $comment)
90        {
91            $this->renderComment($comment, $index + $offset);
92            echo '<div class="space"><br></div>';
93        }
94    }
95    public ?int $userID;
96    public static int $PAGE_SIZE = 10;
97    public string $name;
98    public array $params;
99}