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