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