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