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