xref: /webtrees/app/Statistics/Google/ChartMarriageAge.php (revision f18701c6beaba57391730a7417c1848d9af57865)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2019 webtrees development team
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18declare(strict_types=1);
19
20namespace Fisharebest\Webtrees\Statistics\Google;
21
22use Fisharebest\Webtrees\I18N;
23use Fisharebest\Webtrees\Statistics\Service\CenturyService;
24use Fisharebest\Webtrees\Tree;
25use Illuminate\Database\Capsule\Manager as DB;
26use Illuminate\Database\Query\Expression;
27use Illuminate\Database\Query\JoinClause;
28use stdClass;
29
30/**
31 * A chart showing the marriage ages by century.
32 */
33class ChartMarriageAge
34{
35    /**
36     * @var Tree
37     */
38    private $tree;
39
40    /**
41     * @var CenturyService
42     */
43    private $century_service;
44
45    /**
46     * Constructor.
47     *
48     * @param Tree $tree
49     */
50    public function __construct(Tree $tree)
51    {
52        $this->tree            = $tree;
53        $this->century_service = new CenturyService();
54    }
55
56    /**
57     * Returns the related database records.
58     *
59     * @return stdClass[]
60     */
61    private function queryRecords(): array
62    {
63        $prefix = DB::connection()->getTablePrefix();
64
65        $male = DB::table('dates as married')
66            ->select([
67                new Expression('ROUND(AVG(' . $prefix . 'married.d_julianday2 - ' . $prefix . 'birth.d_julianday1 - 182.5) / 365.25, 1) AS age'),
68                new Expression('ROUND((' . $prefix . 'married.d_year + 49) / 100) AS century'),
69                new Expression("'M' as sex")
70            ])
71            ->join('families as fam', static function (JoinClause $join): void {
72                $join->on('fam.f_id', '=', 'married.d_gid')
73                    ->on('fam.f_file', '=', 'married.d_file');
74            })
75            ->join('dates as birth', static function (JoinClause $join): void {
76                $join->on('birth.d_gid', '=', 'fam.f_husb')
77                    ->on('birth.d_file', '=', 'fam.f_file');
78            })
79            ->whereIn('married.d_type', ['@#DGREGORIAN@', '@#DJULIAN@'])
80            ->where('married.d_file', '=', $this->tree->id())
81            ->where('married.d_fact', '=', 'MARR')
82            ->where('married.d_julianday1', '>', 'birth.d_julianday1')
83            ->whereIn('birth.d_type', ['@#DGREGORIAN@', '@#DJULIAN@'])
84            ->where('birth.d_fact', '=', 'BIRT')
85            ->where('birth.d_julianday1', '<>', 0)
86            ->groupBy(['century', 'sex']);
87
88        $female = DB::table('dates as married')
89            ->select([
90                new Expression('ROUND(AVG(' . $prefix . 'married.d_julianday2 - ' . $prefix . 'birth.d_julianday1 - 182.5) / 365.25, 1) AS age'),
91                new Expression('ROUND((' . $prefix . 'married.d_year + 49) / 100) AS century'),
92                new Expression("'F' as sex")
93            ])
94            ->join('families as fam', static function (JoinClause $join): void {
95                $join->on('fam.f_id', '=', 'married.d_gid')
96                    ->on('fam.f_file', '=', 'married.d_file');
97            })
98            ->join('dates as birth', static function (JoinClause $join): void {
99                $join->on('birth.d_gid', '=', 'fam.f_wife')
100                    ->on('birth.d_file', '=', 'fam.f_file');
101            })
102            ->whereIn('married.d_type', ['@#DGREGORIAN@', '@#DJULIAN@'])
103            ->where('married.d_file', '=', $this->tree->id())
104            ->where('married.d_fact', '=', 'MARR')
105            ->where('married.d_julianday1', '>', 'birth.d_julianday1')
106            ->whereIn('birth.d_type', ['@#DGREGORIAN@', '@#DJULIAN@'])
107            ->where('birth.d_fact', '=', 'BIRT')
108            ->where('birth.d_julianday1', '<>', 0)
109            ->groupBy(['century', 'sex']);
110
111        return $male->unionAll($female)
112           ->orderBy('century')
113           ->get()
114           ->all();
115    }
116
117    /**
118     * General query on ages at marriage.
119     *
120     * @return string
121     */
122    public function chartMarriageAge(): string
123    {
124        $out = [];
125
126        foreach ($this->queryRecords() as $record) {
127            $out[(int) $record->century][$record->sex] = (float) $record->age;
128        }
129
130        $data = [
131            [
132                I18N::translate('Century'),
133                I18N::translate('Males'),
134                I18N::translate('Females'),
135                I18N::translate('Average age'),
136            ]
137        ];
138
139        foreach ($out as $century => $values) {
140            $female_age  = $values['F'] ?? 0;
141            $male_age    = $values['M'] ?? 0;
142            $average_age = ($female_age + $male_age) / 2.0;
143
144            $data[] = [
145                $this->century_service->centuryName($century),
146                $male_age,
147                $female_age,
148                $average_age,
149            ];
150        }
151
152        $chart_title   = I18N::translate('Average age in century of marriage');
153        $chart_options = [
154            'title' => $chart_title,
155            'subtitle' => I18N::translate('Average age at marriage'),
156            'vAxis' => [
157                'title' => I18N::translate('Age'),
158            ],
159            'hAxis' => [
160                'title' => I18N::translate('Century'),
161            ],
162            'colors' => [
163                '#84beff',
164                '#ffd1dc',
165                '#ff0000',
166            ],
167        ];
168
169        return view(
170            'statistics/other/charts/combo',
171            [
172                'data'          => $data,
173                'chart_options' => $chart_options,
174                'chart_title'   => $chart_title,
175            ]
176        );
177    }
178}
179