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; 2744cdc21eSGreg Roachuse 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 average age of individuals related to the death century. 358add1155SRico Sonntag */ 3693ccd686SRico Sonntagclass ChartAge 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 { 48f78da678SGreg Roach $this->century_service = $century_service; 4993ccd686SRico Sonntag $this->tree = $tree; 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 { 5944cdc21eSGreg Roach return DB::table('individuals') 6088de55fdSRico Sonntag ->select([ 61211018abSGreg Roach new Expression('AVG(' . DB::prefix('death.d_julianday2') . ' - ' . DB::prefix('birth.d_julianday1') . ') / 365.25 AS age'), 62211018abSGreg Roach new Expression('ROUND((' . DB::prefix('death.d_year') . ' + 49) / 100, 0) AS century'), 6388de55fdSRico Sonntag 'i_sex AS sex' 6488de55fdSRico Sonntag ]) 650b5fd0a6SGreg Roach ->join('dates AS birth', static function (JoinClause $join): void { 6644cdc21eSGreg Roach $join 6744cdc21eSGreg Roach ->on('birth.d_file', '=', 'i_file') 6844cdc21eSGreg Roach ->on('birth.d_gid', '=', 'i_id'); 6944cdc21eSGreg Roach }) 700b5fd0a6SGreg Roach ->join('dates AS death', static function (JoinClause $join): void { 7144cdc21eSGreg Roach $join 7244cdc21eSGreg Roach ->on('death.d_file', '=', 'i_file') 7344cdc21eSGreg Roach ->on('death.d_gid', '=', 'i_id'); 7444cdc21eSGreg Roach }) 7544cdc21eSGreg Roach ->where('i_file', '=', $this->tree->id()) 7644cdc21eSGreg Roach ->where('birth.d_fact', '=', 'BIRT') 7744cdc21eSGreg Roach ->where('death.d_fact', '=', 'DEAT') 7844cdc21eSGreg Roach ->whereIn('birth.d_type', ['@#DGREGORIAN@', '@#DJULIAN@']) 7944cdc21eSGreg Roach ->whereIn('death.d_type', ['@#DGREGORIAN@', '@#DJULIAN@']) 8044cdc21eSGreg Roach ->whereColumn('death.d_julianday1', '>=', 'birth.d_julianday2') 8144cdc21eSGreg Roach ->where('birth.d_julianday2', '<>', 0) 8244cdc21eSGreg Roach ->groupBy(['century', 'sex']) 8344cdc21eSGreg Roach ->orderBy('century') 8444cdc21eSGreg Roach ->orderBy('sex') 8544cdc21eSGreg Roach ->get() 86f25fc0f9SGreg Roach ->map(static fn (object $row): object => (object) [ 8734e3587bSGreg Roach 'age' => (float) $row->age, 8834e3587bSGreg Roach 'century' => (int) $row->century, 8934e3587bSGreg Roach 'sex' => $row->sex, 90f25fc0f9SGreg Roach ]); 918add1155SRico Sonntag } 928add1155SRico Sonntag 938add1155SRico Sonntag /** 948add1155SRico Sonntag * General query on ages. 958add1155SRico Sonntag * 968add1155SRico Sonntag * @return string 978add1155SRico Sonntag */ 9888de55fdSRico Sonntag public function chartAge(): string 998add1155SRico Sonntag { 1008add1155SRico Sonntag $out = []; 10188de55fdSRico Sonntag foreach ($this->queryRecords() as $record) { 10234e3587bSGreg Roach $out[$record->century][$record->sex] = $record->age; 1038add1155SRico Sonntag } 1048add1155SRico Sonntag 10588de55fdSRico Sonntag $data = [ 10688de55fdSRico Sonntag [ 10788de55fdSRico Sonntag I18N::translate('Century'), 10888de55fdSRico Sonntag I18N::translate('Males'), 10988de55fdSRico Sonntag I18N::translate('Females'), 11088de55fdSRico Sonntag I18N::translate('Average age'), 11188de55fdSRico Sonntag ] 11288de55fdSRico Sonntag ]; 11388de55fdSRico Sonntag 1148add1155SRico Sonntag foreach ($out as $century => $values) { 1158add1155SRico Sonntag $female_age = $values['F'] ?? 0; 1168add1155SRico Sonntag $male_age = $values['M'] ?? 0; 11788de55fdSRico Sonntag $average_age = ($female_age + $male_age) / 2.0; 1188add1155SRico Sonntag 11988de55fdSRico Sonntag $data[] = [ 12093ccd686SRico Sonntag $this->century_service->centuryName($century), 12134e3587bSGreg Roach round($male_age, 1), 12234e3587bSGreg Roach round($female_age, 1), 1231b860509SRico Sonntag round($average_age, 1), 12488de55fdSRico Sonntag ]; 1258add1155SRico Sonntag } 1268add1155SRico Sonntag 1271b860509SRico Sonntag $chart_title = I18N::translate('Average age related to death century'); 1281b860509SRico Sonntag $chart_options = [ 1291b860509SRico Sonntag 'title' => $chart_title, 1301b860509SRico Sonntag 'subtitle' => I18N::translate('Average age at death'), 1311b860509SRico Sonntag 'vAxis' => [ 1321b860509SRico Sonntag 'title' => I18N::translate('Age'), 1331b860509SRico Sonntag ], 1341b860509SRico Sonntag 'hAxis' => [ 13576e7c249SGreg Roach 'showTextEvery' => 1, 13676e7c249SGreg Roach 'slantedText' => false, 1371b860509SRico Sonntag 'title' => I18N::translate('Century'), 1381b860509SRico Sonntag ], 1391b860509SRico Sonntag 'colors' => [ 1401b860509SRico Sonntag '#84beff', 1411b860509SRico Sonntag '#ffd1dc', 1421b860509SRico Sonntag '#ff0000', 1431b860509SRico Sonntag ], 1441b860509SRico Sonntag ]; 1451b860509SRico Sonntag 14690a2f718SGreg Roach return view('statistics/other/charts/combo', [ 14788de55fdSRico Sonntag 'data' => $data, 1481b860509SRico Sonntag 'chart_options' => $chart_options, 1491b860509SRico Sonntag 'chart_title' => $chart_title, 15065cf5706SGreg Roach 'language' => I18N::languageTag(), 15190a2f718SGreg Roach ]); 1528add1155SRico Sonntag } 1538add1155SRico Sonntag} 154