Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
92.68% covered (success)
92.68%
38 / 41
0.00% covered (danger)
0.00%
0 / 1
CRAP
0.00% covered (danger)
0.00%
0 / 1
AdminActivity
92.68% covered (success)
92.68%
38 / 41
0.00% covered (danger)
0.00%
0 / 1
17.11
0.00% covered (danger)
0.00%
0 / 1
 renderChange
92.68% covered (success)
92.68%
38 / 41
0.00% covered (danger)
0.00%
0 / 1
17.11
1<?php
2
3class AdminActivity extends AppModel
4{
5    public $useTable = 'admin_activity';
6    public $actsAs = ['Containable'];
7
8    public $belongsTo = [
9        'AdminActivityType' => [
10            'className' => 'AdminActivityType',
11            'foreignKey' => 'type',
12            'fields' => ['name']
13        ]
14    ];
15
16    public static function renderChange($adminActivity)
17    {
18        switch ($adminActivity['type'])
19        {
20            case AdminActivityType::ACCEPT_TAG: return 'Accepted tag ' . $adminActivity['new_value'];
21            case AdminActivityType::REJECT_TAG: return 'Rejected tag ' . $adminActivity['old_value'];
22            case AdminActivityType::ADD_TAG: return 'Added tag ' . $adminActivity['new_value'];
23            case AdminActivityType::DELETE_USER: return 'Deleted user ' . $adminActivity['old_value'];
24            case AdminActivityType::ACCEPT_PROPOSAL: return 'Accepted proposal by ' . $adminActivity['new_value'];
25            case AdminActivityType::REJECT_PROPOSAL: return 'Rejected proposal by ' . $adminActivity['new_value'];
26            case AdminActivityType::TSUMEGO_MERGE:
27                {
28                    $decoded = json_decode($adminActivity['old_value'], true);
29                    if (json_last_error() !== JSON_ERROR_NONE
30                        || !is_array($decoded)
31                        || !isset($decoded['master_sgf']))
32                            return 'Merged tsumego ' . $adminActivity['old_value'];
33
34                    $masterCorrectMoves = SgfBoard::decodePositionString($decoded['master_correct_moves']);
35                    $masterSgf = SgfParser::process($decoded['master_sgf'], $masterCorrectMoves);
36                    $slaveCorrectMoves = SgfBoard::decodePositionString($decoded['slave_correct_moves']);
37                    $slaveSgf = SgfParser::process($decoded['slave_sgf'], $slaveCorrectMoves);
38                    $comparisonResult = BoardComparator::compare(
39                        $masterSgf->stones,
40                        $decoded['master_first_move_color'],
41                        $masterCorrectMoves,
42                        $slaveSgf->stones,
43                        $decoded['slave_first_move_color'],
44                        $slaveCorrectMoves);
45
46                    $onMouseOver = 'if (this.querySelector(\'svg\')) return;';
47                    $onMouseOver .= TsumegoButton::createBoardFromSgf($decoded['master_sgf'], 'this', 'createPreviewBoard');
48                    $onMouseOver .= TsumegoButton::createBoardFromSgf($decoded['slave_sgf'], 'this', 'createPreviewBoard', $comparisonResult->diff);
49                    $result = 'Merged ';
50                    $result .= '<a style="position: relative;" onmouseover="' . $onMouseOver . '">tsumego<span class="tooltip-box"></span></a>';
51                    return $result;
52                }
53            default:
54                if (!empty($adminActivity['old_value']) && !empty($adminActivity['new_value']))
55                    return $adminActivity['readable_type'] . ': ' . h($adminActivity['old_value']) . ' → ' . h($adminActivity['new_value']);
56                if (!empty($adminActivity['new_value']))
57                {
58                    if ($adminActivity['new_value'] === '1')
59                        return $adminActivity['readable_type'] . ' → enabled';
60                    /** @phpstan-ignore-next-line */
61                    if ($adminActivity['new_value'] === '0')
62                        return $adminActivity['readable_type'] . ' → disabled';
63                    return $adminActivity['readable_type'] . ': [Empty]  → ' . $adminActivity['new_value'];
64                }
65                return $adminActivity['readable_type'];
66        }
67    }
68}