Issues (16)

app/Services/StatisticService.php (1 issue)

Severity
1
<?php
2
declare(strict_types = 1);
3
4
namespace App\Services;
5
6
use App\Models\Compilation;
7
8
class StatisticService
9
{
10
11
    /**
12
     * @var CompilationService
13
     */
14
    private $compilationService;
15
16
    public function __construct(CompilationService $compilationService)
17
    {
18
        $this->compilationService = $compilationService;
19
    }
20
21
    /**
22
     * Export a set of compilation to a statistic-compliant format.
23
     *
24
     * @param array|\Traversable $compilations
25
     *
26
     * @return array e.g. Array (
27
     *                      Array (
28
     *                        [stage_location_id] => 14
29
     *                        [stage_ward_id] => 47
30
     *                        [stage_academic_year] => 2017/2018
31
     *                        [stage_weeks] => 10
32
     *                        [student_gender] => female
33
     *                        [student_nationality] => IT
34
     *                        [q1] => 10
35
     *                        [q2] => 86
36
     *                        [q3] => 87
37
     *                        [q4] => 90
38
     *                        [q5] => 95
39
     *                        [q6] => 103
40
     *                        [q7] => 105
41
     *                        [q9] => 109
42
     *                        [q10] => 139
43
     *                        [q11] => 141
44
     *                        [q13] => 147
45
     *                        [q14] => 152
46
     *                        [q15] => 156
47
     *                        [q16] => 160
48
     *                        [q17] => 164
49
     *                        [q18] => 168
50
     *                        [q19] => 172
51
     *                        [q20] => 176
52
     *                        [q21] => 180
53
     *                        [q22] => 184
54
     *                        [q23] => 188
55
     *                        [q24] => 192
56
     *                        [q25] => 196
57
     *                        [q26] => 200
58
     *                        [q27] => 204
59
     *                        [q28] => 208
60
     *                        [q29] => 212
61
     *                        [q30] => 216
62
     *                        [q31] => 220
63
     *                        [q32] => 224
64
     *                        [q33] => 228
65
     *                        [q34] => 232
66
     *                        [q35] => 236
67
     *                      )
68
     *                      [...]
69
     *                    )
70
     */
71
    public function formatCompilations($compilations) : array
72
    {
73
74
        if (is_array($compilations) === false &&
75
            ($compilations instanceof \Traversable) === false
76
        ) {
77
            throw new \InvalidArgumentException("Compilation set must be an array or implement Traversable interface");
78
        }
79
80
        if ($compilations instanceof \Traversable) {
0 ignored issues
show
$compilations is always a sub-type of Traversable.
Loading history...
81
            $compilations = iterator_to_array($compilations);
82
        }
83
84
        return array_map([$this, "formatCompilation"], $compilations);
85
86
    }
87
88
    /**
89
     * Export a compilation to a statistic-compliant format.
90
     *
91
     * @param Compilation $compilation
92
     *
93
     * @return array e.g. Array (
94
     *                      [stage_location_id] => 14
95
     *                      [stage_ward_id] => 47
96
     *                      [stage_academic_year] => 2017/2018
97
     *                      [stage_weeks] => 10
98
     *                      [student_gender] => female
99
     *                      [student_nationality] => IT
100
     *                      [q1] => 10
101
     *                      [q2] => 86
102
     *                      [q3] => 87
103
     *                      [q4] => 90
104
     *                      [q5] => 95
105
     *                      [q6] => 103
106
     *                      [q7] => 105
107
     *                      [q9] => 109
108
     *                      [q10] => 139
109
     *                      [q11] => 141
110
     *                      [q13] => 147
111
     *                      [q14] => 152
112
     *                      [q15] => 156
113
     *                      [q16] => 160
114
     *                      [q17] => 164
115
     *                      [q18] => 168
116
     *                      [q19] => 172
117
     *                      [q20] => 176
118
     *                      [q21] => 180
119
     *                      [q22] => 184
120
     *                      [q23] => 188
121
     *                      [q24] => 192
122
     *                      [q25] => 196
123
     *                      [q26] => 200
124
     *                      [q27] => 204
125
     *                      [q28] => 208
126
     *                      [q29] => 212
127
     *                      [q30] => 216
128
     *                      [q31] => 220
129
     *                      [q32] => 224
130
     *                      [q33] => 228
131
     *                      [q34] => 232
132
     *                      [q35] => 236
133
     *                    )
134
     */
135
    public function formatCompilation(Compilation $compilation) : array
136
    {
137
138
        $formatted = [
139
            "stage_location_id" => $compilation->stage_location_id,
140
            "stage_ward_id" => $compilation->stage_ward_id,
141
            "stage_academic_year" => $compilation->stage_academic_year,
142
            "stage_weeks" => $compilation->stage_weeks,
143
            "student_gender" => $compilation->student->gender,
144
            "student_nationality" => $compilation->student->nationality,
145
        ];
146
147
        foreach ($compilation->items as $item) {
148
            // NULL answers are added during compilation creation for validation purposes.
149
            if ($item->answer !== null) {
150
                $formatted["q" . $item->question_id] = $item->answer;
151
            }
152
        }
153
154
        return $formatted;
155
    }
156
157
    /**
158
     * Count occurrences of each answer of each question of the provided compilations.
159
     *
160
     * @param array|\Traversable $formattedCompilations
161
     * @return array e.g. Array (
162
     *                      [stage_location_id] => Array (
163
     *                        [14] => 82
164
     *                        [21] => 11
165
     *                        [...]
166
     *                      )
167
     *                      [stage_ward_id] => Array (
168
     *                        [65] => 3
169
     *                        [3] => 7
170
     *                        [...]
171
     *                      )
172
     *                      [stage_academic_year] => Array (
173
     *                        [2017/2018] => 120
174
     *                        [2016/2017] => 1
175
     *                      )
176
     *                      [stage_weeks] => Array (
177
     *                        [8] => 11
178
     *                        [5] => 1
179
     *                        [...]
180
     *                      )
181
     *                      [student_gender] => Array (
182
     *                        [female] => 95
183
     *                        [male] => 26
184
     *                      )
185
     *                      [student_nationality] => Array (
186
     *                        [RO] => 4
187
     *                        [IT] => 111
188
     *                        [...]
189
     *                      )
190
     *                      [q1] => Array (
191
     *                        [5] => 35
192
     *                        [8] => 5
193
     *                        [...]
194
     *                      )
195
     *                      [q2] => Array (
196
     *                        [84] => 18
197
     *                        [85] => 12
198
     *                        [...]
199
     *                      )
200
     *                      [...]
201
     *                    )
202
     */
203
    public function getStatistics($formattedCompilations) : array
204
    {
205
        if (is_array($formattedCompilations) === false &&
206
            ($formattedCompilations instanceof \Traversable) === false
207
        ) {
208
            throw new \InvalidArgumentException("Compilation set must be an array or implement Traversable interface");
209
        }
210
211
        $statistics = [];
212
213
        foreach ($formattedCompilations as $formattedCompilation) {
214
            foreach ($formattedCompilation as $questionId => $answerId) {
215
                if (isset($statistics[$questionId]) === false) {
216
                    $statistics[$questionId] = [];
217
                }
218
                if (isset($statistics[$questionId][$answerId]) === false) {
219
                    $statistics[$questionId][$answerId] = 0;
220
                }
221
                $statistics[$questionId][$answerId]++;
222
            }
223
        }
224
225
        // Answers are sorted by record ID.
226
        // @todo sort by answer real sort value
227
        foreach ($statistics as $questionId => &$answers) {
228
            if (preg_match("/^q\d+$/", $questionId) === 1) {
229
                ksort($answers);
230
            }
231
        }
232
233
        return $statistics;
234
    }
235
236
}
237