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 marriage ages by century. 31 */ 32class ChartMarriageAge 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 $male = DB::table('dates as married') 65 ->select([ 66 new Expression('ROUND(AVG(' . $prefix . 'married.d_julianday2 - ' . $prefix . 'birth.d_julianday1 - 182.5) / 365.25, 1) AS age'), 67 new Expression('ROUND((' . $prefix . 'married.d_year + 49) / 100) AS century'), 68 new Expression("'M' as sex") 69 ]) 70 ->join('families as fam', static function (JoinClause $join): void { 71 $join->on('fam.f_id', '=', 'married.d_gid') 72 ->on('fam.f_file', '=', 'married.d_file'); 73 }) 74 ->join('dates as birth', static function (JoinClause $join): void { 75 $join->on('birth.d_gid', '=', 'fam.f_husb') 76 ->on('birth.d_file', '=', 'fam.f_file'); 77 }) 78 ->whereIn('married.d_type', ['@#DGREGORIAN@', '@#DJULIAN@']) 79 ->where('married.d_file', '=', $this->tree->id()) 80 ->where('married.d_fact', '=', 'MARR') 81 ->where('married.d_julianday1', '>', 'birth.d_julianday1') 82 ->whereIn('birth.d_type', ['@#DGREGORIAN@', '@#DJULIAN@']) 83 ->where('birth.d_fact', '=', 'BIRT') 84 ->where('birth.d_julianday1', '<>', 0) 85 ->groupBy(['century', 'sex']); 86 87 $female = DB::table('dates as married') 88 ->select([ 89 new Expression('ROUND(AVG(' . $prefix . 'married.d_julianday2 - ' . $prefix . 'birth.d_julianday1 - 182.5) / 365.25, 1) AS age'), 90 new Expression('ROUND((' . $prefix . 'married.d_year + 49) / 100) AS century'), 91 new Expression("'F' as sex") 92 ]) 93 ->join('families as fam', static function (JoinClause $join): void { 94 $join->on('fam.f_id', '=', 'married.d_gid') 95 ->on('fam.f_file', '=', 'married.d_file'); 96 }) 97 ->join('dates as birth', static function (JoinClause $join): void { 98 $join->on('birth.d_gid', '=', 'fam.f_wife') 99 ->on('birth.d_file', '=', 'fam.f_file'); 100 }) 101 ->whereIn('married.d_type', ['@#DGREGORIAN@', '@#DJULIAN@']) 102 ->where('married.d_file', '=', $this->tree->id()) 103 ->where('married.d_fact', '=', 'MARR') 104 ->where('married.d_julianday1', '>', 'birth.d_julianday1') 105 ->whereIn('birth.d_type', ['@#DGREGORIAN@', '@#DJULIAN@']) 106 ->where('birth.d_fact', '=', 'BIRT') 107 ->where('birth.d_julianday1', '<>', 0) 108 ->groupBy(['century', 'sex']); 109 110 return $male->unionAll($female) 111 ->orderBy('century') 112 ->get() 113 ->all(); 114 } 115 116 /** 117 * General query on ages at marriage. 118 * 119 * @return string 120 */ 121 public function chartMarriageAge(): string 122 { 123 $out = []; 124 125 foreach ($this->queryRecords() as $record) { 126 $out[(int) $record->century][$record->sex] = (float) $record->age; 127 } 128 129 $data = [ 130 [ 131 I18N::translate('Century'), 132 I18N::translate('Males'), 133 I18N::translate('Females'), 134 I18N::translate('Average age'), 135 ] 136 ]; 137 138 foreach ($out as $century => $values) { 139 $female_age = $values['F'] ?? 0; 140 $male_age = $values['M'] ?? 0; 141 $average_age = ($female_age + $male_age) / 2.0; 142 143 $data[] = [ 144 $this->century_service->centuryName($century), 145 $male_age, 146 $female_age, 147 $average_age, 148 ]; 149 } 150 151 $chart_title = I18N::translate('Average age in century of marriage'); 152 $chart_options = [ 153 'title' => $chart_title, 154 'subtitle' => I18N::translate('Average age at marriage'), 155 'vAxis' => [ 156 'title' => I18N::translate('Age'), 157 ], 158 'hAxis' => [ 159 'title' => I18N::translate('Century'), 160 ], 161 'colors' => [ 162 '#84beff', 163 '#ffd1dc', 164 '#ff0000', 165 ], 166 ]; 167 168 return view( 169 'statistics/other/charts/combo', 170 [ 171 'data' => $data, 172 'chart_options' => $chart_options, 173 'chart_title' => $chart_title, 174 ] 175 ); 176 } 177} 178