Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
92.00% |
46 / 50 |
|
0.00% |
0 / 2 |
CRAP | |
0.00% |
0 / 1 |
Maturity | |
92.00% |
46 / 50 |
|
0.00% |
0 / 2 |
15.12 | |
0.00% |
0 / 1 |
generateMaturityByDomain | |
93.18% |
41 / 44 |
|
0.00% |
0 / 1 |
12.05 | |||
getGlobalScore | |
83.33% |
5 / 6 |
|
0.00% |
0 / 1 |
3.04 |
1 | <?php |
2 | |
3 | /** |
4 | * This file is part of the MADIS - RGPD Management application. |
5 | * |
6 | * @copyright Copyright (c) 2018-2019 Soluris - Solutions Numériques Territoriales Innovantes |
7 | * |
8 | * This program is free software: you can redistribute it and/or modify |
9 | * it under the terms of the GNU Affero General Public License as published by |
10 | * the Free Software Foundation, either version 3 of the License, or |
11 | * (at your option) any later version. |
12 | * |
13 | * This program is distributed in the hope that it will be useful, |
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16 | * GNU Affero General Public License for more details. |
17 | * |
18 | * You should have received a copy of the GNU Affero General Public License |
19 | * along with this program. If not, see <https://www.gnu.org/licenses/>. |
20 | */ |
21 | |
22 | declare(strict_types=1); |
23 | |
24 | namespace App\Domain\Maturity\Calculator; |
25 | |
26 | use App\Domain\Maturity\Model; |
27 | |
28 | class Maturity |
29 | { |
30 | /** |
31 | * Compute maturity indice for each linked survey maturity. |
32 | * |
33 | * @param Model\Survey $survey The survey maturities object to compute |
34 | * |
35 | * @throws \Exception |
36 | * |
37 | * @return Model\Maturity[] |
38 | */ |
39 | public function generateMaturityByDomain(Model\Survey $survey): array |
40 | { |
41 | $domains = []; |
42 | $points = []; |
43 | $maturityList = []; |
44 | |
45 | // Get all existant maturity to update it |
46 | /** @var Model\Maturity $item */ |
47 | foreach ($survey->getMaturity() as $item) { |
48 | $did = $item->getDomain()->getId()->toString(); |
49 | $maturityList[$did] = $item; |
50 | |
51 | // Get all domain in specific array |
52 | if (!isset($domains[$did])) { |
53 | $domains[$did] = $item->getDomain(); |
54 | } |
55 | } |
56 | |
57 | /* @var Model\Answer $answer */ |
58 | // Begin calculation |
59 | foreach ($survey->getAnswerSurveys() as $answerSurvey) { |
60 | /** @var Model\AnswerSurvey $answerSurvey */ |
61 | /** @var Model\Answer $answer */ |
62 | $answer = $answerSurvey->getAnswer(); |
63 | $domain = $answer->getQuestion()->getDomain(); |
64 | $domainId = $domain->getId()->toString(); |
65 | |
66 | // Get all domain in specific array |
67 | if (!isset($domains[$domainId])) { |
68 | $domains[$domainId] = $domain; |
69 | } |
70 | // Make an addition with answer response by domain |
71 | $w = $answer->getQuestion()->getWeight(); |
72 | |
73 | $v = $answer->getPosition(); |
74 | $ac = count($answer->getQuestion()->getAnswers()) - 1; |
75 | |
76 | $val = 0; |
77 | if ($ac > 0) { |
78 | $val = $v / $ac; |
79 | } elseif (0 === $ac) { |
80 | $val = $v; |
81 | } |
82 | |
83 | if (isset($points[$domainId])) { |
84 | $points[$domainId]['value'] += $val * 5 * $w; |
85 | } else { |
86 | $points[$domainId] = [ |
87 | 'value' => $val * 5 * $w, |
88 | 'nbItem' => 0, |
89 | ]; |
90 | } |
91 | $points[$domainId]['nbItem'] += $w; |
92 | } |
93 | |
94 | $removedMaturities = $maturityList; |
95 | // Update maturityList with new points |
96 | // If maturity doesn't exists for related domain, create it |
97 | foreach ($points as $key => $point) { |
98 | if (!isset($maturityList[$key])) { |
99 | $maturityList[$key] = new Model\Maturity(); |
100 | } |
101 | $maturityList[$key]->setReferentiel($survey->getReferentiel()); |
102 | $maturityList[$key]->setDomain($domains[$key]); |
103 | $maturityList[$key]->setSurvey($survey); |
104 | // Prevent division by 0 for question with no answers |
105 | if (0 === $point['nbItem']) { |
106 | $score = 0; |
107 | } else { |
108 | // * 10 to keep int data in order to display a {int}/10 in report |
109 | $score = \intval(\ceil($point['value'] / $point['nbItem'] * 10)); |
110 | } |
111 | |
112 | $maturityList[$key]->setScore($score); |
113 | // This maturity still exists, so remove it from the removed maturities array |
114 | unset($removedMaturities[$key]); |
115 | } |
116 | |
117 | foreach ($removedMaturities as $k => $removedMaturity) { |
118 | unset($maturityList[$k]); |
119 | } |
120 | |
121 | return $maturityList; |
122 | } |
123 | |
124 | /** |
125 | * Get the global score of every maturity provided as parameter |
126 | * Make an average of maturity indice of each Maturity. |
127 | * |
128 | * @param Model\Maturity[] $maturityList |
129 | */ |
130 | public function getGlobalScore(array $maturityList = []): int |
131 | { |
132 | if (0 === count($maturityList)) { |
133 | return 0; |
134 | } |
135 | |
136 | $points = 0; |
137 | foreach ($maturityList as $i => $maturity) { |
138 | $points += $maturity->getScore(); |
139 | } |
140 | |
141 | return \intval(\ceil($points / \count($maturityList))); |
142 | } |
143 | } |