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