Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
39.02% covered (danger)
39.02%
32 / 82
50.00% covered (danger)
50.00%
2 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
AchievementsController
38.27% covered (danger)
38.27%
31 / 81
50.00% covered (danger)
50.00%
2 / 4
172.01
0.00% covered (danger)
0.00%
0 / 1
 index
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
2
 user
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
2
 renderAchievementsPage
86.21% covered (warning)
86.21%
25 / 29
0.00% covered (danger)
0.00%
0 / 1
8.17
 view
0.00% covered (danger)
0.00%
0 / 46
0.00% covered (danger)
0.00%
0 / 1
182
1<?php
2
3App::uses('NotFoundException', 'Routing/Error');
4
5class AchievementsController extends AppController
6{
7    /**
8     * @return void
9     */
10    public function index()
11    {
12        $this->renderAchievementsPage(Auth::isLoggedIn() ? Auth::getUser() : null);
13    }
14
15    /**
16     * @param string|int $userId
17     * @return void
18     */
19    public function user($userId)
20    {
21        $user = $this->User->findById($userId);
22        if (!$user)
23            throw new NotFoundException('User not found');
24
25        $this->renderAchievementsPage($user['User']);
26        $this->render('index');
27    }
28
29    /**
30     * @param array|null $viewedUser
31     * @return void
32     */
33    private function renderAchievementsPage($viewedUser)
34    {
35        $this->set('_page', 'user');
36        $this->set('_title', 'Tsumego Hero - Achievements');
37        $this->loadModel('AchievementStatus');
38        $existingAs = [];
39        $unlockedCounter2 = 0;
40
41        $a = $this->Achievement->find('all', ['order' => 'order ASC']);
42        if (!$a)
43            $a = [];
44
45        if ($viewedUser)
46        {
47            $this->set('viewedUser', $viewedUser);
48            $as = $this->AchievementStatus->find('all', ['conditions' => ['user_id' => $viewedUser['id']]]);
49            if (!$as)
50                $as = [];
51
52            foreach ($as as $item)
53                $existingAs[$item['AchievementStatus']['achievement_id']] = $item;
54        }
55
56        $aCount = count($a);
57        for ($i = 0; $i < $aCount; $i++)
58        {
59            $a[$i]['Achievement']['unlocked'] = false;
60            $a[$i]['Achievement']['created'] = '';
61            if (isset($existingAs[$a[$i]['Achievement']['id']]))
62            {
63                if ($a[$i]['Achievement']['id'] == 46)
64                {
65                    $a[$i]['Achievement']['a46value'] = $existingAs[$a[$i]['Achievement']['id']]['AchievementStatus']['value'];
66                    $unlockedCounter2 = $existingAs[$a[$i]['Achievement']['id']]['AchievementStatus']['value'] - 1;
67                }
68                $a[$i]['Achievement']['unlocked'] = true;
69                $a[$i]['Achievement']['created'] = $existingAs[$a[$i]['Achievement']['id']]['AchievementStatus']['created'];
70                $date = date_create($a[$i]['Achievement']['created']);
71                $a[$i]['Achievement']['created'] = date_format($date, 'd.m.Y H:i');
72            }
73        }
74        $this->set('a', $a);
75        $this->set('unlockedCounter2', $unlockedCounter2);
76    }
77
78    /**
79     * @param string|int|null $id
80     * @return void
81     */
82    public function view($id = null)
83    {
84        $this->set('_page', 'user');
85        $this->set('_title', 'Tsumego Hero - Achievements');
86        $this->loadModel('AchievementCondition');
87        $this->loadModel('AchievementStatus');
88        $this->loadModel('User');
89        $a = $this->Achievement->findById($id);
90        if (!$a)
91            throw new NotFoundException('Achievement not found');
92
93        $as = [];
94        $asAll = $this->AchievementStatus->find('all', ['order' => 'created DESC', 'conditions' => ['achievement_id' => $id]]);
95        if (!$asAll)
96            $asAll = [];
97        $aCount = count($asAll);
98        if (Auth::isLoggedIn())
99            $as = $this->AchievementStatus->find('first', ['conditions' => ['achievement_id' => $id, 'user_id' => Auth::getUserID()]]);
100        if ($as)
101        {
102            $date = date_create($as['AchievementStatus']['created']);
103            $as['AchievementStatus']['created'] = date_format($date, 'd.m.Y H:i');
104        }
105        $asAll2 = [];
106        $count = 10;
107        if (count($asAll) < 10)
108            $count = count($asAll);
109        if (count($asAll) > 10)
110            $andMore = ' and more.';
111        else
112            $andMore = '.';
113        for ($i = 0; $i < $count; $i++)
114        {
115            $u = $this->User->findById($asAll[$i]['AchievementStatus']['user_id']);
116            $asAll[$i]['AchievementStatus']['name'] = $this->checkPicture($u);
117            $asAll2[] = $asAll[$i];
118        }
119        $asAll = $asAll2;
120
121        if (Auth::isLoggedIn())
122        {
123            $acGolden = $this->AchievementCondition->find('all', ['conditions' => ['user_id' => Auth::getUserID(), 'category' => 'golden']]);
124            if (!$acGolden)
125                $acGolden = [];
126            if (count($acGolden) == 0)
127                $acGoldenCount = 0;
128            else
129                $acGoldenCount = $acGolden[0]['AchievementCondition']['value'];
130            if ($as)
131                $acGoldenCount = 10;
132            if ($a['Achievement']['id'] == 97)
133                $a['Achievement']['additionalDescription'] = 'Progress: ' . $acGoldenCount . '/10';
134        }
135
136        $this->set('a', $a);
137        $this->set('as', $as);
138        $this->set('asAll', $asAll);
139        $this->set('aCount', $aCount);
140        $this->set('andMore', $andMore);
141    }
142
143}