xref: /webtrees/app/Statistics/Google/ChartAge.php (revision 8d897fd11a9a8845d8456cb9eaad33b9c9d5a225)
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 */
17declare(strict_types=1);
18
19namespace Fisharebest\Webtrees\Statistics\Google;
20
21use Fisharebest\Webtrees\I18N;
22use Fisharebest\Webtrees\Statistics\Service\CenturyService;
23use Fisharebest\Webtrees\Tree;
24use Illuminate\Database\Capsule\Manager as DB;
25use Illuminate\Database\Query\Expression;
26use Illuminate\Database\Query\JoinClause;
27use stdClass;
28
29/**
30 * A chart showing the average age of individuals related to the death century.
31 */
32class ChartAge
33{
34    /**
35     * @var Tree
36     */
37    private $tree;
38
39    /**
40     * @var CenturyService
41     */
42    private $century_service;
43
44    /**
45     * Constructor.
46     *
47     * @param Tree $tree
48     */
49    public function __construct(Tree $tree)
50    {
51        $this->tree            = $tree;
52        $this->century_service = new CenturyService();
53    }
54
55    /**
56     * Returns the related database records.
57     *
58     * @return stdClass[]
59     */
60    private function queryRecords(): array
61    {
62        $prefix = DB::connection()->getTablePrefix();
63
64        return DB::table('individuals')
65            ->select([
66                new Expression('ROUND(AVG(' . $prefix . 'death.d_julianday2 - ' . $prefix . 'birth.d_julianday1) / 365.25, 1) AS age'),
67                new Expression('ROUND((' . $prefix . 'death.d_year + 49) / 100) AS century'),
68                'i_sex AS sex'
69            ])
70            ->join('dates AS birth', static function (JoinClause $join): void {
71                $join
72                    ->on('birth.d_file', '=', 'i_file')
73                    ->on('birth.d_gid', '=', 'i_id');
74            })
75            ->join('dates AS death', static function (JoinClause $join): void {
76                $join
77                    ->on('death.d_file', '=', 'i_file')
78                    ->on('death.d_gid', '=', 'i_id');
79            })
80            ->where('i_file', '=', $this->tree->id())
81            ->where('birth.d_fact', '=', 'BIRT')
82            ->where('death.d_fact', '=', 'DEAT')
83            ->whereIn('birth.d_type', ['@#DGREGORIAN@', '@#DJULIAN@'])
84            ->whereIn('death.d_type', ['@#DGREGORIAN@', '@#DJULIAN@'])
85            ->whereColumn('death.d_julianday1', '>=', 'birth.d_julianday2')
86            ->where('birth.d_julianday2', '<>', 0)
87            ->groupBy(['century', 'sex'])
88            ->orderBy('century')
89            ->orderBy('sex')
90            ->get()
91            ->all();
92    }
93
94    /**
95     * General query on ages.
96     *
97     * @return string
98     */
99    public function chartAge(): string
100    {
101        $out = [];
102        foreach ($this->queryRecords() as $record) {
103            $out[(int) $record->century][$record->sex] = (float) $record->age;
104        }
105
106        $data = [
107            [
108                I18N::translate('Century'),
109                I18N::translate('Males'),
110                I18N::translate('Females'),
111                I18N::translate('Average age'),
112            ]
113        ];
114
115        foreach ($out as $century => $values) {
116            $female_age  = $values['F'] ?? 0;
117            $male_age    = $values['M'] ?? 0;
118            $average_age = ($female_age + $male_age) / 2.0;
119
120            $data[] = [
121                $this->century_service->centuryName($century),
122                $male_age,
123                $female_age,
124                round($average_age, 1),
125            ];
126        }
127
128        $chart_title   = I18N::translate('Average age related to death century');
129        $chart_options = [
130            'title' => $chart_title,
131            'subtitle' => I18N::translate('Average age at death'),
132            'vAxis' => [
133                'title' => I18N::translate('Age'),
134            ],
135            'hAxis' => [
136                'title' => I18N::translate('Century'),
137            ],
138            'colors' => [
139                '#84beff',
140                '#ffd1dc',
141                '#ff0000',
142            ],
143        ];
144
145        return view(
146            'statistics/other/charts/combo',
147            [
148                'data'          => $data,
149                'chart_options' => $chart_options,
150                'chart_title'   => $chart_title,
151            ]
152        );
153    }
154}
155