1<?php 2/** 3 * webtrees: online genealogy 4 * Copyright (C) 2018 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\AbstractGoogle; 22use Fisharebest\Webtrees\Statistics\Helper\Century; 23use Fisharebest\Webtrees\Tree; 24 25/** 26 * 27 */ 28class ChartMarriageAge extends AbstractGoogle 29{ 30 /** 31 * @var Century 32 */ 33 private $centuryHelper; 34 35 /** 36 * Constructor. 37 * 38 * @param Tree $tree 39 */ 40 public function __construct(Tree $tree) 41 { 42 parent::__construct($tree); 43 44 $this->centuryHelper = new Century(); 45 } 46 47 /** 48 * Returns the related database records. 49 * 50 * @param string $sex 51 * 52 * @return \stdClass[] 53 */ 54 private function queryRecords(string $sex): array 55 { 56 // TODO 57 return $this->runSql( 58 'SELECT ' 59 . ' ROUND(AVG(married.d_julianday2-birth.d_julianday1-182.5)/365.25,1) AS age, ' 60 . ' ROUND((married.d_year - 50) / 100) AS century,' 61 . " 'M' AS sex " 62 . 'FROM `##dates` AS married ' 63 . 'JOIN `##families` AS fam ON (married.d_gid=fam.f_id AND married.d_file=fam.f_file) ' 64 . 'JOIN `##dates` AS birth ON (birth.d_gid=fam.f_husb AND birth.d_file=fam.f_file) ' 65 . 'WHERE ' 66 . " '{$sex}' IN ('M', 'BOTH') AND " 67 . " married.d_file={$this->tree->id()} AND married.d_type IN ('@#DGREGORIAN@', '@#DJULIAN@') AND married.d_fact='MARR' AND " 68 . " birth.d_type IN ('@#DGREGORIAN@', '@#DJULIAN@') AND birth.d_fact='BIRT' AND " 69 . ' married.d_julianday1>birth.d_julianday1 AND birth.d_julianday1<>0 ' 70 . 'GROUP BY century, sex ' 71 . 'UNION ALL ' 72 . 'SELECT ' 73 . ' ROUND(AVG(married.d_julianday2-birth.d_julianday1-182.5)/365.25,1) AS age, ' 74 . ' ROUND((married.d_year - 50) / 100) AS century,' 75 . " 'F' AS sex " 76 . 'FROM `##dates` AS married ' 77 . 'JOIN `##families` AS fam ON (married.d_gid=fam.f_id AND married.d_file=fam.f_file) ' 78 . 'JOIN `##dates` AS birth ON (birth.d_gid=fam.f_wife AND birth.d_file=fam.f_file) ' 79 . 'WHERE ' 80 . " '{$sex}' IN ('F', 'BOTH') AND " 81 . " married.d_file={$this->tree->id()} AND married.d_type IN ('@#DGREGORIAN@', '@#DJULIAN@') AND married.d_fact='MARR' AND " 82 . " birth.d_type IN ('@#DGREGORIAN@', '@#DJULIAN@') AND birth.d_fact='BIRT' AND " 83 . ' married.d_julianday1>birth.d_julianday1 AND birth.d_julianday1<>0 ' 84 . ' GROUP BY century, sex ORDER BY century' 85 ); 86 } 87 88 /** 89 * General query on ages at marriage. 90 * 91 * @return string 92 */ 93 public function chartMarriageAge(): string 94 { 95 $sex = 'BOTH'; 96 $out = []; 97 98 foreach ($this->queryRecords($sex) as $record) { 99 $out[(int) $record->century][$record->sex] = (float) $record->age; 100 } 101 102 $data = [ 103 [ 104 I18N::translate('Century'), 105 I18N::translate('Males'), 106 I18N::translate('Females'), 107 I18N::translate('Average age'), 108 ] 109 ]; 110 111 foreach ($out as $century => $values) { 112 $female_age = $values['F'] ?? 0; 113 $male_age = $values['M'] ?? 0; 114 $average_age = ($female_age + $male_age) / 2.0; 115 116 $data[] = [ 117 $this->centuryHelper->centuryName($century), 118 $male_age, 119 $female_age, 120 $average_age, 121 ]; 122 } 123 124 return view( 125 'statistics/other/charts/combo', 126 [ 127 'data' => $data, 128 'colors' => ['#84beff', '#ffd1dc', '#ff0000'], 129 'chart_title' => I18N::translate('Average age in century of marriage'), 130 'chart_sub_title' => I18N::translate('Average age at marriage'), 131 'hAxis_title' => I18N::translate('Century'), 132 'vAxis_title' => I18N::translate('Age'), 133 ] 134 ); 135 } 136} 137