GroupAnalysisExport::mergeCellColumnNext()   B
last analyzed

Complexity

Conditions 7
Paths 11

Size

Total Lines 31
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 21
c 0
b 0
f 0
nc 11
nop 0
dl 0
loc 31
rs 8.6506
1
<?php
2
3
namespace App\Exports;
4
5
use Maatwebsite\Excel\Concerns\FromCollection;
6
use Maatwebsite\Excel\Concerns\WithEvents;
7
use Maatwebsite\Excel\Concerns\WithStrictNullComparison;
8
use Maatwebsite\Excel\Events\AfterSheet;
9
10
class GroupAnalysisExport implements FromCollection, WithEvents, WithStrictNullComparison
11
{
12
    private $contest_data;
13
14
    private $tag_problems;
15
16
    private $member_data;
17
    private $config;
18
19
    public function __construct($data, $config=[])
20
    {
21
        if ($config['mode']=='contest') {
22
            $this->contest_data=$data['contest_data'];
23
            $this->member_data=$data['member_data'];
24
        } else {
25
            $this->member_data=$data['member_data'];
26
            $this->tag_problems=$data['tag_problems'];
27
        }
28
        $this->config=$config;
29
    }
30
31
    /**
32
     * @return array
33
     */
34
    public function registerEvents(): array
35
    {
36
        return [
37
            AfterSheet::class => function(AfterSheet $event) {
38
                if ($this->config['mode']==='contest') {
39
                    $mergeCell=['A1:A2', 'B1:E1'];
40
                    foreach ($this->contest_data as $c) {
41
                        array_push($mergeCell, $this->mergeCellColumnNext());
42
                    }
43
                    $event->sheet->getDelegate()->setMergeCells($mergeCell);
44
                }
45
            },
46
        ];
47
    }
48
49
    /**
50
     * @return \Illuminate\Support\Collection
51
     */
52
    public function collection()
53
    {
54
        $maxium=$this->config['maxium'] ?? false;
55
        $percent=$this->config['percent'] ?? false;
56
57
        if ($this->config['mode']=='contest') {
58
            $row_1=['Member', 'Total', '', '', ''];
59
            $row_2=['', 'Elo', 'Rank', 'Solved', 'Penalty'];
60
            foreach ($this->contest_data as $contest) {
61
                array_push($row_1, $contest['name'], '', '');
62
                array_push($row_2, 'Rank', 'Solved', 'Penalty');
63
            }
64
            $data=[$row_1, $row_2];
65
            foreach ($this->member_data as $member) {
66
                $display_name=$member['name'];
67
                if (!empty($member['nick_name'])) {
68
                    $display_name.=' ('.$member['nick_name'].')';
69
                }
70
                $row=[
71
                    $display_name,
72
                    $member['elo'],
73
                    !empty($member['rank_ave']) ? round($member['rank_ave'], 1) : '-',
74
                    $percent==='true' ? ($member['problem_all']!=0 ? round($member['solved_all'] / $member['problem_all'] * 100, 1) : '-').' %'
75
                            : ($maxium==='true' ? $member['solved_all'].' / '.$member['problem_all'] : $member['solved_all']),
76
                    round($member['penalty']),
77
                ];
78
                foreach ($this->contest_data as $contest) {
79
                    if (in_array($contest['cid'], array_keys($member['contest_detial']))) {
80
                        $contest_detial=$member['contest_detial'][$contest['cid']];
81
                        array_push(
82
                            $row,
83
                            $contest_detial['rank'],
84
                            $percent==='true' ? (round($contest_detial['solved'] / $contest_detial['problems'] * 100, 1).' %')
85
                                    : ($maxium==='true' ? $contest_detial['solved'].' / '.$contest_detial['problems'] : $contest_detial['solved']),
86
                            round($contest_detial['penalty'])
87
                        );
88
                    } else {
89
                        array_push(
90
                            $row,
91
                            '-',
92
                            $percent==='true' ? '- %'
93
                                    : ($maxium==='true' ? '- / -' : ' - '),
94
                            0
95
                        );
96
                    }
97
                }
98
                array_push($data, $row);
99
            }
100
        } else {
101
            $row_1=['Member'];
102
            $row_2=[''];
103
            foreach ($this->tag_problems as $tag => $tag_problem_set) {
104
                array_push($row_1, $tag);
105
                array_push($row_2, 'Solved');
106
            }
107
            $data=[$row_1, $row_2];
108
            foreach ($this->member_data as $member) {
109
                $display_name=$member['name'];
110
                if (!empty($member['nick_name'])) {
111
                    $display_name.=' ('.$member['nick_name'].')';
112
                }
113
                $row=[
114
                    $display_name,
115
                ];
116
                foreach ($member['completion'] as $tag => $tag_completion) {
117
                    $count=count($tag_completion);
118
                    $solved=eval('return '.join('+', array_values($tag_completion)).';');
0 ignored issues
show
introduced by
The use of eval() is discouraged.
Loading history...
119
                    array_push(
120
                        $row,
121
                        $percent==='true' ? (round($solved / $count * 100, 1).' %') : ($maxium==='true' ? $solved.' / '.$count : $solved)
122
                    );
123
                }
124
                array_push($data, $row);
125
            }
126
        }
127
        return collect($data);
128
    }
129
130
    private function mergeCellColumnNext()
131
    {
132
        static $columns=[
133
            [2], [4]
134
        ];
135
        $columns_str=['', ''];
136
        $chars=str_split('ABCDEFGHIJKLMNOPQRSTUVWXYZ');
137
        foreach ($columns as $key => &$column) {
138
            $column[0]+=3;
139
            if ($column[0]>25) {
140
                if (isset($column[1])) {
141
                    $column[1]+=1;
142
                    if ($column[1]>25) {
143
                        if (isset($column[2])) {
144
                            $column[2]+=1;
145
                        } else {
146
                            $column[2]=0;
147
                        }
148
                    }
149
                } else {
150
                    $column[1]=0;
151
                }
152
            }
153
            $columns_str[$key]='';
154
            $reverse_column=array_reverse($column);
155
            foreach ($reverse_column as $ord) {
156
                $columns_str[$key].=$chars[$ord];
157
            }
158
            $columns_str[$key].='1';
159
        }
160
        return $columns_str[0].':'.$columns_str[1];
161
    }
162
}
163