Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
5.26% covered (danger)
5.26%
10 / 190
50.00% covered (danger)
50.00%
4 / 8
CRAP
0.00% covered (danger)
0.00%
0 / 1
OlzVereinParams
n/a
0 / 0
n/a
0 / 0
0
n/a
0 / 0
OlzVerein
5.26% covered (danger)
5.26%
10 / 190
50.00% covered (danger)
50.00%
4 / 8
848.11
0.00% covered (danger)
0.00%
0 / 1
 hasAccess
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 searchSqlWhenHasAccess
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
1
 getPageTitle
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getPageDescription
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getHtmlWhenHasAccess
0.00% covered (danger)
0.00%
0 / 25
0.00% covered (danger)
0.00%
0 / 1
12
 getStatistikDiagram
0.00% covered (danger)
0.00%
0 / 53
0.00% covered (danger)
0.00%
0 / 1
12
 getSektionRoles
0.00% covered (danger)
0.00%
0 / 9
0.00% covered (danger)
0.00%
0 / 1
6
 renderChart
0.00% covered (danger)
0.00%
0 / 93
0.00% covered (danger)
0.00%
0 / 1
380
1<?php
2
3namespace Olz\Roles\Components\OlzVerein;
4
5use Olz\Common\Components\OlzFooter\OlzFooter;
6use Olz\Common\Components\OlzHeader\OlzHeader;
7use Olz\Common\Components\OlzRootComponent;
8use Olz\Common\Utils\HttpParams;
9use Olz\Entity\Measurement;
10use Olz\Entity\Roles\Role;
11use Olz\Repository\Roles\PredefinedRole;
12use Olz\Roles\Components\OlzOrganigramm\OlzOrganigramm;
13
14/** @extends HttpParams<array{von?: ?string}> */
15class OlzVereinParams extends HttpParams {
16}
17
18/** @extends OlzRootComponent<array<string, mixed>> */
19class OlzVerein extends OlzRootComponent {
20    public function hasAccess(): bool {
21        return true;
22    }
23
24    public function searchSqlWhenHasAccess(array $terms): string|array|null {
25        $code_href = $this->envUtils()->getCodeHref();
26        return $this->searchUtils()->getStaticResultQuery([
27            'link' => "{$code_href}karten",
28            'icon' => "{$code_href}assets/icns/link_map_16.svg",
29            'title' => $this->getPageTitle(),
30            'text' => $this->getPageDescription(),
31        ], $terms);
32    }
33
34    public function getPageTitle(): string {
35        return "Verein";
36    }
37
38    public function getPageDescription(): string {
39        return "Die wichtigsten Kontaktadressen und eine Liste aller Vereinsorgane der OL Zimmerberg.";
40    }
41
42    public function getHtmlWhenHasAccess(mixed $args): string {
43        $this->httpUtils()->validateGetParams(OlzVereinParams::class);
44        $code_href = $this->envUtils()->getCodeHref();
45
46        $out = OlzHeader::render([
47            'title' => $this->getPageTitle(),
48            'description' => $this->getPageDescription(),
49            'canonical_url' => "{$code_href}verein",
50        ]);
51
52        $db = $this->dbUtils()->getDb();
53        $result = $db->query("SELECT id, name FROM roles WHERE featured_position IS NOT NULL ORDER BY featured_position ASC");
54        $featured_out = '';
55        // @phpstan-ignore-next-line
56        while ($row = $result->fetch_assoc()) {
57            $id = $row['id'];
58            $title = $row['name'];
59            $featured_out .= "<div><b><a href='javascript:olz.highlightOrganigramm(&quot;role-{$id}&quot;)' class='linkint'>{$title}</a></b></div>";
60        }
61        $out .= "<div class='content-full'><h1>Verein</h1><div id='organigramm'>";
62        $out .= <<<ZZZZZZZZZZ
63            <h2>Häufig gesucht</h2>
64            {$featured_out}
65            <div>
66                <br />
67                <div><b>PC-Konto</b></div>
68                <div><b>IBAN: </b>CH91 0900 0000 8525 6448 8</div>
69                <div><b>Empfänger: </b>OL Zimmerberg, 8800 Thalwil</div>
70            </div>
71            ZZZZZZZZZZ;
72
73        $out .= OlzOrganigramm::render();
74
75        $out .= "</div></div>";
76
77        if ($this->authUtils()->hasPermission('vorstand')) {
78            $out .= $this->getStatistikDiagram();
79        }
80
81        $out .= OlzFooter::render();
82        return $out;
83    }
84
85    private function getStatistikDiagram(): string {
86        $sektion_roles = $this->getSektionRoles();
87        $sektion_series = [];
88        foreach ($sektion_roles as $role) {
89            $username = $role->getUsername();
90            $has_match = preg_match('/^sektion-(.+)$/i', $username);
91            $name = $has_match ? substr($username, 8) : $username;
92            $name = substr("num_members_{$name}", 0, 31);
93            $sektion_series[$name] = ['label' => $role->getName()];
94        }
95        return implode('', [
96            $this->renderChart(
97                'Entwicklung Benutzer und Mitglieder',
98                'Anzahl Website-Benutzer, aktiver Benutzer, Vereins-Mitglieder und Benutzer mit Mitgliedschaft.',
99                [
100                    'num_members' => ['label' => 'Vereins-Mitglieder', 'color' => '#008800'],
101                    'num_users' => ['label' => 'Website-Benutzer', 'color' => '#0077cc'],
102                    'num_active_users' => ['label' => 'Aktive Benutzer', 'color' => '#cc0000'],
103                    'num_member_users' => ['label' => 'Benutzer mit Mitgliedschaft', 'color' => '#cc6600'],
104                ],
105            ),
106            $this->renderChart(
107                'Entwicklung Sektionen',
108                'Anzahl Vereins-Mitglieder nach Sektion.',
109                $sektion_series,
110            ),
111            $this->renderChart(
112                'Entwicklung Trainings',
113                'Durchschnittliche Anzahl Teilnehmer pro Training.',
114                [
115                    'avg_training_participants' => ['label' => 'Ø Trainings-Teilnahmen', 'color' => '#008800'],
116                ],
117            ),
118            $this->renderChart(
119                'Entwicklung Wettkämpfe',
120                'Anzahl Teilnahmen an Orientierungslauf-Wettkämpfen (SOLV).',
121                [
122                    'num_solv_participations' => ['label' => 'SOLV-Teilnahmen', 'color' => '#008800'],
123                ],
124            ),
125            $this->renderChart(
126                'Entwicklung Termine',
127                'Anzahl Termine auf der Website.',
128                [
129                    'num_termine' => ['label' => 'Termine', 'color' => '#008800'],
130                ],
131            ),
132            $this->renderChart(
133                'Entwicklung Website-Aktivität',
134                'Anzahl News-Einträge auf der Website.',
135                [
136                    'num_news' => ['label' => 'News', 'color' => '#008800'],
137                ],
138            ),
139        ]);
140    }
141
142    /** @return array<Role> */
143    private function getSektionRoles(): array {
144        $role_repo = $this->entityManager()->getRepository(Role::class);
145        $sektionen_role = $role_repo->getPredefinedRole(PredefinedRole::Sektionen);
146        if (!$sektionen_role) {
147            return [];
148        }
149        $sektion_roles = $role_repo->findBy([
150            'parent_role' => $sektionen_role->getId(),
151            'deleted_at' => null,
152        ], ['position_within_parent' => 'ASC']);
153        return [...$sektion_roles];
154    }
155
156    /** @param array<string, array{label: string, color?: string}> $series */
157    private function renderChart(
158        string $title,
159        string $description,
160        array $series,
161    ): string {
162        $default_colors = [
163            '#006600',
164            '#0033aa',
165            '#880000',
166            '#66aa00',
167            '#0077cc',
168            '#bb5500',
169            '#eeee00',
170            '#00bbee',
171            '#eeaa00',
172        ];
173        $current_year = intval($this->dateUtils()->getCurrentDateInFormat('Y'));
174        $current_month = intval($this->dateUtils()->getCurrentDateInFormat('m'));
175        $min_year = $current_year - 19;
176        $measurement_repo = $this->entityManager()->getRepository(Measurement::class);
177
178        // 0 = January of $min_year; the chart covers the last 20 years up to the current month.
179        $end_index = ($current_year - $min_year) * 12 + ($current_month - 1);
180
181        // name => [month index => value]
182        $values = [];
183        foreach (array_keys($series) as $name) {
184            $measurements = $measurement_repo->findBy(
185                ['name' => $name],
186                ['year' => 'ASC', 'month' => 'ASC'],
187            );
188            foreach ($measurements as $measurement) {
189                $year = $measurement->getYear();
190                $month = $measurement->getMonth();
191                $value = $measurement->getValue();
192                if ($year === null || $month === null || $value === null) {
193                    continue;
194                }
195                $index = ($year - $min_year) * 12 + ($month - 1);
196                if ($index < 0 || $index > $end_index) {
197                    continue;
198                }
199                $values[$name][$index] = $value;
200            }
201        }
202
203        $px_per_month = 3;
204        $height = 320;
205        $pad_left = 60;
206        $pad_right = 220;
207        $pad_top = 20;
208        $pad_bottom = 40;
209        $plot_height = $height - $pad_top - $pad_bottom;
210        $plot_width = max(1, $end_index) * $px_per_month;
211        $width = $pad_left + $plot_width + $pad_right;
212
213        $max_value = 1;
214        foreach ($values as $by_month) {
215            foreach ($by_month as $value) {
216                if ($value > $max_value) {
217                    $max_value = $value;
218                }
219            }
220        }
221        $step = max(1, intval(ceil($max_value / 4)));
222        $max_y = $step * 4;
223
224        $x_of = fn (int $index): float => $pad_left + $index * $px_per_month;
225        $y_of = fn (float $value): float => $pad_top + (1 - $value / $max_y) * $plot_height;
226
227        $svg = "<svg class='olz-verein-statistik' width='{$width}' height='{$height}' viewBox='0 0 {$width} {$height}' xmlns='http://www.w3.org/2000/svg' role='img' aria-label='{$title}'>";
228
229        // Horizontal grid lines and Y labels.
230        for ($i = 0; $i <= 4; $i++) {
231            $y = round($pad_top + (1 - $i / 4) * $plot_height, 1);
232            $tick_value = $step * $i;
233            $svg .= "<line x1='{$pad_left}' y1='{$y}' x2='".($pad_left + $plot_width)."' y2='{$y}' stroke='#dddddd' stroke-width='1' />";
234            $svg .= "<text x='".($pad_left - 8)."' y='".($y + 4)."' text-anchor='end' font-size='11' fill='#666666'>{$tick_value}</text>";
235        }
236
237        // Vertical grid lines at the start (January) of each year.
238        for ($year = $min_year; $year <= $current_year; $year++) {
239            $index = ($year - $min_year) * 12;
240            if ($index > $end_index) {
241                continue;
242            }
243            $x = round($x_of($index), 1);
244            $svg .= "<line x1='{$x}' y1='{$pad_top}' x2='{$x}' y2='".($pad_top + $plot_height)."' stroke='#dddddd' stroke-width='1' />";
245        }
246
247        // Axes.
248        $svg .= "<line x1='{$pad_left}' y1='{$pad_top}' x2='{$pad_left}' y2='".($pad_top + $plot_height)."' stroke='#999999' stroke-width='1' />";
249        $svg .= "<line x1='{$pad_left}' y1='".($pad_top + $plot_height)."' x2='".($pad_left + $plot_width)."' y2='".($pad_top + $plot_height)."' stroke='#999999' stroke-width='1' />";
250
251        // X labels at January of every year.
252        for ($year = $min_year; $year <= $current_year; $year++) {
253            $index = ($year - $min_year) * 12;
254            if ($index > $end_index) {
255                continue;
256            }
257            $x = round($x_of($index), 1);
258            $svg .= "<text x='{$x}' y='".($pad_top + $plot_height + 18)."' text-anchor='middle' font-size='10' fill='#666666'>{$year}</text>";
259        }
260
261        // Series lines, points and legend.
262        $legend_x = $pad_left + $plot_width + 20;
263        $legend_y = $pad_top + 10;
264        $i = 0;
265        foreach ($series as $name => $meta) {
266            $color = $meta['color'] ?? $default_colors[$i % count($default_colors)];
267            $label = $meta['label'];
268            $points = '';
269            foreach (($values[$name] ?? []) as $index => $value) {
270                $x = round($x_of($index), 1);
271                $y = round($y_of($value), 1);
272                $points .= "{$x},{$y} ";
273            }
274            if ($points !== '') {
275                $svg .= "<polyline points='".trim($points)."' fill='none' stroke='{$color}' stroke-width='2' />";
276            }
277            $svg .= "<rect x='{$legend_x}' y='{$legend_y}' width='14' height='14' fill='{$color}' />";
278            $svg .= "<text x='".($legend_x + 20)."' y='".($legend_y + 12)."' font-size='12' fill='#333333'>{$label}</text>";
279            $legend_y += 22;
280            $i++;
281        }
282
283        $svg .= "</svg>";
284
285        return "<div class='content-full'>"
286            ."<h2>{$title}</h2>"
287            ."<p>{$description}</p>"
288            ."<div style='overflow-x: auto;'>".$svg."</div>"
289            ."</div>";
290    }
291}