18add1155SRico Sonntag<?php 28add1155SRico Sonntag/** 38add1155SRico Sonntag * webtrees: online genealogy 4242a7862SGreg Roach * Copyright (C) 2019 webtrees development team 58add1155SRico Sonntag * This program is free software: you can redistribute it and/or modify 68add1155SRico Sonntag * it under the terms of the GNU General Public License as published by 78add1155SRico Sonntag * the Free Software Foundation, either version 3 of the License, or 88add1155SRico Sonntag * (at your option) any later version. 98add1155SRico Sonntag * This program is distributed in the hope that it will be useful, 108add1155SRico Sonntag * but WITHOUT ANY WARRANTY; without even the implied warranty of 118add1155SRico Sonntag * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 128add1155SRico Sonntag * GNU General Public License for more details. 138add1155SRico Sonntag * You should have received a copy of the GNU General Public License 148add1155SRico Sonntag * along with this program. If not, see <http://www.gnu.org/licenses/>. 158add1155SRico Sonntag */ 168add1155SRico Sonntagdeclare(strict_types=1); 178add1155SRico Sonntag 188add1155SRico Sonntagnamespace Fisharebest\Webtrees\Statistics\Google; 198add1155SRico Sonntag 208add1155SRico Sonntaguse Fisharebest\Webtrees\I18N; 2193ccd686SRico Sonntaguse Fisharebest\Webtrees\Statistics\Service\CenturyService; 228add1155SRico Sonntaguse Fisharebest\Webtrees\Tree; 238add1155SRico Sonntaguse Illuminate\Database\Capsule\Manager as DB; 248add1155SRico Sonntaguse Illuminate\Database\Query\JoinClause; 25*6ccdf4f0SGreg Roachuse stdClass; 268add1155SRico Sonntag 278add1155SRico Sonntag/** 2893ccd686SRico Sonntag * A chart showing the average number of children by century. 298add1155SRico Sonntag */ 3093ccd686SRico Sonntagclass ChartChildren 318add1155SRico Sonntag{ 328add1155SRico Sonntag /** 3393ccd686SRico Sonntag * @var Tree 348add1155SRico Sonntag */ 3593ccd686SRico Sonntag private $tree; 3693ccd686SRico Sonntag 3793ccd686SRico Sonntag /** 3893ccd686SRico Sonntag * @var CenturyService 3993ccd686SRico Sonntag */ 4093ccd686SRico Sonntag private $century_service; 418add1155SRico Sonntag 428add1155SRico Sonntag /** 438add1155SRico Sonntag * Constructor. 448add1155SRico Sonntag * 458add1155SRico Sonntag * @param Tree $tree 468add1155SRico Sonntag */ 478add1155SRico Sonntag public function __construct(Tree $tree) 488add1155SRico Sonntag { 4993ccd686SRico Sonntag $this->tree = $tree; 5093ccd686SRico Sonntag $this->century_service = new CenturyService(); 518add1155SRico Sonntag } 528add1155SRico Sonntag 538add1155SRico Sonntag /** 548add1155SRico Sonntag * Returns the related database records. 558add1155SRico Sonntag * 56*6ccdf4f0SGreg Roach * @return stdClass[] 578add1155SRico Sonntag */ 588add1155SRico Sonntag private function queryRecords(): array 598add1155SRico Sonntag { 608add1155SRico Sonntag $query = DB::table('families') 618add1155SRico Sonntag ->selectRaw('ROUND(AVG(f_numchil), 2) AS num') 623413ec75SRico Sonntag ->selectRaw('ROUND((d_year + 49) / 100) AS century') 630b5fd0a6SGreg Roach ->join('dates', static function (JoinClause $join): void { 648add1155SRico Sonntag $join->on('d_file', '=', 'f_file') 658add1155SRico Sonntag ->on('d_gid', '=', 'f_id'); 668add1155SRico Sonntag }) 678add1155SRico Sonntag ->where('f_file', '=', $this->tree->id()) 688add1155SRico Sonntag ->where('d_julianday1', '<>', 0) 698add1155SRico Sonntag ->where('d_fact', '=', 'MARR') 708add1155SRico Sonntag ->whereIn('d_type', ['@#DGREGORIAN@', '@#DJULIAN@']) 718add1155SRico Sonntag ->groupBy(['century']) 728add1155SRico Sonntag ->orderBy('century'); 738add1155SRico Sonntag 748add1155SRico Sonntag return $query->get()->all(); 758add1155SRico Sonntag } 768add1155SRico Sonntag 778add1155SRico Sonntag /** 7888de55fdSRico Sonntag * Creates a children per family chart. 798add1155SRico Sonntag * 808add1155SRico Sonntag * @return string 818add1155SRico Sonntag */ 8288de55fdSRico Sonntag public function chartChildren(): string 838add1155SRico Sonntag { 8488de55fdSRico Sonntag $data = [ 8588de55fdSRico Sonntag [ 8688de55fdSRico Sonntag I18N::translate('Century'), 8788de55fdSRico Sonntag I18N::translate('Average number') 8888de55fdSRico Sonntag ] 8988de55fdSRico Sonntag ]; 908add1155SRico Sonntag 9188de55fdSRico Sonntag foreach ($this->queryRecords() as $record) { 9288de55fdSRico Sonntag $data[] = [ 9393ccd686SRico Sonntag $this->century_service->centuryName((int) $record->century), 9488de55fdSRico Sonntag (float) $record->num 9588de55fdSRico Sonntag ]; 968add1155SRico Sonntag } 978add1155SRico Sonntag 98a81e5019SRico Sonntag $chart_title = I18N::translate('Average number of children per family'); 99a81e5019SRico Sonntag $chart_options = [ 100a81e5019SRico Sonntag 'title' => $chart_title, 101a81e5019SRico Sonntag 'subtitle' => '', 102a81e5019SRico Sonntag 'legend' => [ 103a81e5019SRico Sonntag 'position' => 'none', 104a81e5019SRico Sonntag ], 105a81e5019SRico Sonntag 'vAxis' => [ 106a81e5019SRico Sonntag 'title' => I18N::translate('Number of children'), 107a81e5019SRico Sonntag ], 108a81e5019SRico Sonntag 'hAxis' => [ 109a81e5019SRico Sonntag 'title' => I18N::translate('Century'), 110a81e5019SRico Sonntag ], 111a81e5019SRico Sonntag 'colors' => [ 112a81e5019SRico Sonntag '#84beff' 113a81e5019SRico Sonntag ], 114a81e5019SRico Sonntag ]; 115a81e5019SRico Sonntag 1168add1155SRico Sonntag return view( 11788de55fdSRico Sonntag 'statistics/other/charts/column', 1188add1155SRico Sonntag [ 11988de55fdSRico Sonntag 'data' => $data, 120a81e5019SRico Sonntag 'chart_options' => $chart_options, 121a81e5019SRico Sonntag 'chart_title' => $chart_title, 1228add1155SRico Sonntag ] 1238add1155SRico Sonntag ); 1248add1155SRico Sonntag } 1258add1155SRico Sonntag} 126