xref: /webtrees/app/Statistics/Google/ChartMarriageAge.php (revision d7c2f5cc56f17801156c069987440ca1da2c5057)
18add1155SRico Sonntag<?php
23976b470SGreg Roach
38add1155SRico Sonntag/**
48add1155SRico Sonntag * webtrees: online genealogy
5d11be702SGreg Roach * Copyright (C) 2023 webtrees development team
68add1155SRico Sonntag * This program is free software: you can redistribute it and/or modify
78add1155SRico Sonntag * it under the terms of the GNU General Public License as published by
88add1155SRico Sonntag * the Free Software Foundation, either version 3 of the License, or
98add1155SRico Sonntag * (at your option) any later version.
108add1155SRico Sonntag * This program is distributed in the hope that it will be useful,
118add1155SRico Sonntag * but WITHOUT ANY WARRANTY; without even the implied warranty of
128add1155SRico Sonntag * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
138add1155SRico Sonntag * GNU General Public License for more details.
148add1155SRico Sonntag * You should have received a copy of the GNU General Public License
1589f7189bSGreg Roach * along with this program. If not, see <https://www.gnu.org/licenses/>.
168add1155SRico Sonntag */
17fcfa147eSGreg Roach
188add1155SRico Sonntagdeclare(strict_types=1);
198add1155SRico Sonntag
208add1155SRico Sonntagnamespace Fisharebest\Webtrees\Statistics\Google;
218add1155SRico Sonntag
226f4ec3caSGreg Roachuse Fisharebest\Webtrees\DB;
238add1155SRico Sonntaguse Fisharebest\Webtrees\I18N;
2493ccd686SRico Sonntaguse Fisharebest\Webtrees\Statistics\Service\CenturyService;
258add1155SRico Sonntaguse Fisharebest\Webtrees\Tree;
26a69f5655SGreg Roachuse Illuminate\Database\Query\Expression;
270892c7deSRico Sonntaguse Illuminate\Database\Query\JoinClause;
2834e3587bSGreg Roachuse Illuminate\Support\Collection;
298add1155SRico Sonntag
3034b20f29SGreg Roachuse function round;
3134b20f29SGreg Roachuse function view;
3234b20f29SGreg Roach
338add1155SRico Sonntag/**
3493ccd686SRico Sonntag * A chart showing the marriage ages by century.
358add1155SRico Sonntag */
3693ccd686SRico Sonntagclass ChartMarriageAge
378add1155SRico Sonntag{
3834b20f29SGreg Roach    private Tree $tree;
3993ccd686SRico Sonntag
4034b20f29SGreg Roach    private CenturyService $century_service;
418add1155SRico Sonntag
428add1155SRico Sonntag    /**
43f78da678SGreg Roach     * @param CenturyService $century_service
448add1155SRico Sonntag     * @param Tree           $tree
458add1155SRico Sonntag     */
46f78da678SGreg Roach    public function __construct(CenturyService $century_service, Tree $tree)
478add1155SRico Sonntag    {
4893ccd686SRico Sonntag        $this->tree            = $tree;
49f78da678SGreg Roach        $this->century_service = $century_service;
508add1155SRico Sonntag    }
518add1155SRico Sonntag
528add1155SRico Sonntag    /**
538add1155SRico Sonntag     * Returns the related database records.
548add1155SRico Sonntag     *
55*d7c2f5ccSGreg Roach     * @return Collection<array-key,object>
568add1155SRico Sonntag     */
5734e3587bSGreg Roach    private function queryRecords(): Collection
588add1155SRico Sonntag    {
590892c7deSRico Sonntag        $male = DB::table('dates as married')
600892c7deSRico Sonntag            ->select([
61211018abSGreg Roach                new Expression('AVG(' . DB::prefix('married.d_julianday2') . ' - ' . DB::prefix('birth.d_julianday1') . ' - 182.5) / 365.25 AS age'),
62211018abSGreg Roach                new Expression('ROUND((' . DB::prefix('married.d_year') . ' + 49) / 100, 0) AS century'),
63a69f5655SGreg Roach                new Expression("'M' as sex")
640892c7deSRico Sonntag            ])
650b5fd0a6SGreg Roach            ->join('families as fam', static function (JoinClause $join): void {
660892c7deSRico Sonntag                $join->on('fam.f_id', '=', 'married.d_gid')
670892c7deSRico Sonntag                    ->on('fam.f_file', '=', 'married.d_file');
680892c7deSRico Sonntag            })
690b5fd0a6SGreg Roach            ->join('dates as birth', static function (JoinClause $join): void {
700892c7deSRico Sonntag                $join->on('birth.d_gid', '=', 'fam.f_husb')
710892c7deSRico Sonntag                    ->on('birth.d_file', '=', 'fam.f_file');
720892c7deSRico Sonntag            })
730892c7deSRico Sonntag            ->whereIn('married.d_type', ['@#DGREGORIAN@', '@#DJULIAN@'])
740892c7deSRico Sonntag            ->where('married.d_file', '=', $this->tree->id())
750892c7deSRico Sonntag            ->where('married.d_fact', '=', 'MARR')
76211018abSGreg Roach            ->where('married.d_julianday1', '>', new Expression(DB::prefix('birth.d_julianday1')))
770892c7deSRico Sonntag            ->whereIn('birth.d_type', ['@#DGREGORIAN@', '@#DJULIAN@'])
780892c7deSRico Sonntag            ->where('birth.d_fact', '=', 'BIRT')
790892c7deSRico Sonntag            ->where('birth.d_julianday1', '<>', 0)
800892c7deSRico Sonntag            ->groupBy(['century', 'sex']);
810892c7deSRico Sonntag
820892c7deSRico Sonntag        $female = DB::table('dates as married')
830892c7deSRico Sonntag            ->select([
84211018abSGreg Roach                new Expression('ROUND(AVG(' . DB::prefix('married.d_julianday2') . ' - ' . DB::prefix('birth.d_julianday1') . ' - 182.5) / 365.25, 1) AS age'),
85211018abSGreg Roach                new Expression('ROUND((' . DB::prefix('married.d_year') . ' + 49) / 100, 0) AS century'),
86a69f5655SGreg Roach                new Expression("'F' as sex")
870892c7deSRico Sonntag            ])
880b5fd0a6SGreg Roach            ->join('families as fam', static function (JoinClause $join): void {
890892c7deSRico Sonntag                $join->on('fam.f_id', '=', 'married.d_gid')
900892c7deSRico Sonntag                    ->on('fam.f_file', '=', 'married.d_file');
910892c7deSRico Sonntag            })
920b5fd0a6SGreg Roach            ->join('dates as birth', static function (JoinClause $join): void {
930892c7deSRico Sonntag                $join->on('birth.d_gid', '=', 'fam.f_wife')
940892c7deSRico Sonntag                    ->on('birth.d_file', '=', 'fam.f_file');
950892c7deSRico Sonntag            })
960892c7deSRico Sonntag            ->whereIn('married.d_type', ['@#DGREGORIAN@', '@#DJULIAN@'])
970892c7deSRico Sonntag            ->where('married.d_file', '=', $this->tree->id())
980892c7deSRico Sonntag            ->where('married.d_fact', '=', 'MARR')
99211018abSGreg Roach            ->where('married.d_julianday1', '>', new Expression(DB::prefix('birth.d_julianday1')))
1000892c7deSRico Sonntag            ->whereIn('birth.d_type', ['@#DGREGORIAN@', '@#DJULIAN@'])
1010892c7deSRico Sonntag            ->where('birth.d_fact', '=', 'BIRT')
1020892c7deSRico Sonntag            ->where('birth.d_julianday1', '<>', 0)
1030892c7deSRico Sonntag            ->groupBy(['century', 'sex']);
1040892c7deSRico Sonntag
1050892c7deSRico Sonntag        return $male->unionAll($female)
1060892c7deSRico Sonntag            ->orderBy('century')
1070892c7deSRico Sonntag            ->get()
108f25fc0f9SGreg Roach            ->map(static fn (object $row): object => (object) [
10934e3587bSGreg Roach                'age'     => (float) $row->age,
11034e3587bSGreg Roach                'century' => (int) $row->century,
11134e3587bSGreg Roach                'sex'     => $row->sex,
112f25fc0f9SGreg Roach            ]);
1138add1155SRico Sonntag    }
1148add1155SRico Sonntag
1158add1155SRico Sonntag    /**
1168add1155SRico Sonntag     * General query on ages at marriage.
1178add1155SRico Sonntag     *
1188add1155SRico Sonntag     * @return string
1198add1155SRico Sonntag     */
12088de55fdSRico Sonntag    public function chartMarriageAge(): string
1218add1155SRico Sonntag    {
1228add1155SRico Sonntag        $out = [];
1238add1155SRico Sonntag
1240892c7deSRico Sonntag        foreach ($this->queryRecords() as $record) {
12534e3587bSGreg Roach            $out[$record->century][$record->sex] = $record->age;
1268add1155SRico Sonntag        }
1278add1155SRico Sonntag
12888de55fdSRico Sonntag        $data = [
12988de55fdSRico Sonntag            [
13088de55fdSRico Sonntag                I18N::translate('Century'),
13188de55fdSRico Sonntag                I18N::translate('Males'),
13288de55fdSRico Sonntag                I18N::translate('Females'),
13388de55fdSRico Sonntag                I18N::translate('Average age'),
13488de55fdSRico Sonntag            ]
13588de55fdSRico Sonntag        ];
13688de55fdSRico Sonntag
1378add1155SRico Sonntag        foreach ($out as $century => $values) {
13888de55fdSRico Sonntag            $female_age  = $values['F'] ?? 0;
13988de55fdSRico Sonntag            $male_age    = $values['M'] ?? 0;
14088de55fdSRico Sonntag            $average_age = ($female_age + $male_age) / 2.0;
1418add1155SRico Sonntag
14288de55fdSRico Sonntag            $data[] = [
14393ccd686SRico Sonntag                $this->century_service->centuryName($century),
14434e3587bSGreg Roach                round($male_age, 1),
14534e3587bSGreg Roach                round($female_age, 1),
14634e3587bSGreg Roach                round($average_age, 1),
14788de55fdSRico Sonntag            ];
1488add1155SRico Sonntag        }
1498add1155SRico Sonntag
1501b860509SRico Sonntag        $chart_title   = I18N::translate('Average age in century of marriage');
1511b860509SRico Sonntag        $chart_options = [
1521b860509SRico Sonntag            'title' => $chart_title,
1531b860509SRico Sonntag            'subtitle' => I18N::translate('Average age at marriage'),
1541b860509SRico Sonntag            'vAxis' => [
1551b860509SRico Sonntag                'title' => I18N::translate('Age'),
1561b860509SRico Sonntag            ],
1571b860509SRico Sonntag            'hAxis' => [
15876e7c249SGreg Roach                'showTextEvery' => 1,
15976e7c249SGreg Roach                'slantedText'   => false,
1601b860509SRico Sonntag                'title'         => I18N::translate('Century'),
1611b860509SRico Sonntag            ],
1621b860509SRico Sonntag            'colors' => [
1631b860509SRico Sonntag                '#84beff',
1641b860509SRico Sonntag                '#ffd1dc',
1651b860509SRico Sonntag                '#ff0000',
1661b860509SRico Sonntag            ],
1671b860509SRico Sonntag        ];
1681b860509SRico Sonntag
16990a2f718SGreg Roach        return view('statistics/other/charts/combo', [
17088de55fdSRico Sonntag            'data'          => $data,
1711b860509SRico Sonntag            'chart_options' => $chart_options,
1721b860509SRico Sonntag            'chart_title'   => $chart_title,
17365cf5706SGreg Roach            'language'      => I18N::languageTag(),
17490a2f718SGreg Roach        ]);
1758add1155SRico Sonntag    }
1768add1155SRico Sonntag}
177