xref: /webtrees/app/Module/FamilyTreeStatisticsModule.php (revision 3d7a8a4ca809135634f38216b734b15acff479f7)
1<?php
2namespace Fisharebest\Webtrees\Module;
3
4/**
5 * webtrees: online genealogy
6 * Copyright (C) 2015 webtrees development team
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18use Fisharebest\Webtrees\Auth;
19use Fisharebest\Webtrees\Filter;
20use Fisharebest\Webtrees\Functions\FunctionsDb;
21use Fisharebest\Webtrees\Functions\FunctionsEdit;
22use Fisharebest\Webtrees\I18N;
23use Fisharebest\Webtrees\Stats;
24use Fisharebest\Webtrees\Theme;
25
26/**
27 * Class FamilyTreeStatisticsModule
28 */
29class FamilyTreeStatisticsModule extends AbstractModule implements ModuleBlockInterface {
30	/** {@inheritdoc} */
31	public function getTitle() {
32		return /* I18N: Name of a module */ I18N::translate('Statistics');
33	}
34
35	/** {@inheritdoc} */
36	public function getDescription() {
37		return /* I18N: Description of “Statistics” module */ I18N::translate('The size of the family tree, earliest and latest events, common names, etc.');
38	}
39
40	/** {@inheritdoc} */
41	public function getBlock($block_id, $template = true, $cfg = null) {
42		global $WT_TREE, $ctype;
43
44		$show_last_update     = $this->getBlockSetting($block_id, 'show_last_update', '1');
45		$show_common_surnames = $this->getBlockSetting($block_id, 'show_common_surnames', '1');
46		$stat_indi            = $this->getBlockSetting($block_id, 'stat_indi', '1');
47		$stat_fam             = $this->getBlockSetting($block_id, 'stat_fam', '1');
48		$stat_sour            = $this->getBlockSetting($block_id, 'stat_sour', '1');
49		$stat_media           = $this->getBlockSetting($block_id, 'stat_media', '1');
50		$stat_repo            = $this->getBlockSetting($block_id, 'stat_repo', '1');
51		$stat_surname         = $this->getBlockSetting($block_id, 'stat_surname', '1');
52		$stat_events          = $this->getBlockSetting($block_id, 'stat_events', '1');
53		$stat_users           = $this->getBlockSetting($block_id, 'stat_users', '1');
54		$stat_first_birth     = $this->getBlockSetting($block_id, 'stat_first_birth', '1');
55		$stat_last_birth      = $this->getBlockSetting($block_id, 'stat_last_birth', '1');
56		$stat_first_death     = $this->getBlockSetting($block_id, 'stat_first_death', '1');
57		$stat_last_death      = $this->getBlockSetting($block_id, 'stat_last_death', '1');
58		$stat_long_life       = $this->getBlockSetting($block_id, 'stat_long_life', '1');
59		$stat_avg_life        = $this->getBlockSetting($block_id, 'stat_avg_life', '1');
60		$stat_most_chil       = $this->getBlockSetting($block_id, 'stat_most_chil', '1');
61		$stat_avg_chil        = $this->getBlockSetting($block_id, 'stat_avg_chil', '1');
62
63		// This can be overriden when embedding in an HTML block
64		$block     = '0';
65		$stat_link = '1';
66
67		if ($cfg) {
68			foreach (array('show_common_surnames', 'stat_indi', 'stat_fam', 'stat_sour', 'stat_media', 'stat_surname', 'stat_events', 'stat_users', 'stat_first_birth', 'stat_last_birth', 'stat_first_death', 'stat_last_death', 'stat_long_life', 'stat_avg_life', 'stat_most_chil', 'stat_avg_chil', 'stat_link', 'block') as $name) {
69				if (array_key_exists($name, $cfg)) {
70					$$name = $cfg[$name];
71				}
72			}
73		}
74
75		$id    = $this->getName() . $block_id;
76		$class = $this->getName() . '_block';
77		if ($ctype === 'gedcom' && Auth::isManager($WT_TREE) || $ctype === 'user' && Auth::check()) {
78			$title = '<a class="icon-admin" title="' . I18N::translate('Configure') . '" href="block_edit.php?block_id=' . $block_id . '&amp;ged=' . $WT_TREE->getNameHtml() . '&amp;ctype=' . $ctype . '"></a>';
79		} else {
80			$title = '';
81		}
82		$title .= $this->getTitle();
83
84		$stats = new Stats($WT_TREE);
85
86		$content = '<b>' . $WT_TREE->getTitleHtml() . '</b><br>';
87
88		if ($show_last_update) {
89			$content .= '<div>' . /* I18N: %s is a date */ I18N::translate('This family tree was last updated on %s.', strip_tags($stats->gedcomUpdated())) . '</div>';
90		}
91	/** Responsive Design */
92
93	$content .= '<div class="stat-table1">';
94		if ($stat_indi) {
95			$content .= '<div class="stat-row"><div class="facts_label stat-cell">' . I18N::translate('Individuals') . '</div><div class="facts_value stats_value stat-cell"><a href="' . "indilist.php?surname_sublist=no&amp;ged=" . $WT_TREE->getNameUrl() . '">' . $stats->totalIndividuals() . '</a></div></div>';
96			$content .= '<div class="stat-row"><div class="facts_label stat-cell">' . I18N::translate('Males') . '</div><div class="facts_value stats_value stat-cell">' . $stats->totalSexMales() . '<br>' . $stats->totalSexMalesPercentage() . '</a></div></div>';
97			$content .= '<div class="stat-row"><div class="facts_label stat-cell">' . I18N::translate('Females') . '</div><div class="facts_value stats_value stat-cell">' . $stats->totalSexFemales() . '<br>' . $stats->totalSexFemalesPercentage() . '</a></div></div>';
98		}
99		if ($stat_surname) {
100			$content .= '<div class="stat-row"><div class="facts_label stat-cell">' . I18N::translate('Total surnames') . '</div><div class="facts_value stats_value stat-cell"><a href="indilist.php?show_all=yes&amp;surname_sublist=yes&amp;ged=' . $WT_TREE->getNameUrl() . '">' . $stats->totalSurnames() . '</a></div></div>';
101		}
102		if ($stat_fam) {
103			$content .= '<div class="stat-row"><div class="facts_label stat-cell">' . I18N::translate('Families') . '</div><div class="facts_value stats_value stat-cell"><a href="famlist.php?ged=' . $WT_TREE->getNameUrl() . '">' . $stats->totalFamilies() . '</a></div></div>';
104		}
105		if ($stat_sour) {
106			$content .= '<div class="stat-row"><div class="facts_label stat-cell">' . I18N::translate('Sources') . '</div><div class="facts_value stats_value stat-cell"><a href="sourcelist.php?ged=' . $WT_TREE->getNameUrl() . '">' . $stats->totalSources() . '</a></div></div>';
107		}
108		if ($stat_media) {
109			$content .= '<div class="stat-row"><div class="facts_label stat-cell">' . I18N::translate('Media objects') . '</div><div class="facts_value stats_value stat-cell"><a href="medialist.php?ged=' . $WT_TREE->getNameUrl() . '">' . $stats->totalMedia() . '</a></div></div>';
110		}
111		if ($stat_repo) {
112			$content .= '<div class="stat-row"><div class="facts_label stat-cell">' . I18N::translate('Repositories') . '</div><div class="facts_value stats_value stat-cell"><a href="repolist.php?ged=' . $WT_TREE->getNameUrl() . '">' . $stats->totalRepositories() . '</a></div></div>';
113		}
114		if ($stat_events) {
115			$content .= '<div class="stat-row"><div class="facts_label stat-cell">' . I18N::translate('Total events') . '</div><div class="facts_value stats_value stat-cell">' . $stats->totalEvents() . '</div></div>';
116		}
117		if ($stat_users) {
118			$content .= '<div class="stat-row"><div class="facts_label stat-cell">' . I18N::translate('Total users') . '</div><div class="facts_value stats_value stat-cell">';
119			if (Auth::isManager($WT_TREE)) {
120				$content .= '<a href="admin_users.php">' . $stats->totalUsers() . '</a>';
121			} else {
122				$content .= $stats->totalUsers();
123			}
124			$content .= '</div></div>';
125		}
126		if (!$block) {
127			$content .= '</div><div class="facts_table stat-table2">';
128		}
129		if ($stat_first_birth) {
130			$content .= '<div class="stat-row"><div class="facts_label stat-cell">' . I18N::translate('Earliest birth year') . '</div><div class="facts_value stats_value stat-cell">' . $stats->firstBirthYear() . '</div>';
131			if (!$block) {
132				$content .= '<div class="facts_value stat-cell left">' . $stats->firstBirth() . '</div>';
133			}
134			$content .= '</div>';
135		}
136		if ($stat_last_birth) {
137			$content .= '<div class="stat-row"><div class="facts_label stat-cell">' . I18N::translate('Latest birth year') . '</div><div class="facts_value stats_value stat-cell">' . $stats->lastBirthYear() . '</div>';
138			if (!$block) {
139				$content .= '<div class="facts_value stat-cell left">' . $stats->lastBirth() . '</div>';
140			}
141			$content .= '</div>';
142		}
143		if ($stat_first_death) {
144			$content .= '<div class="stat-row"><div class="facts_label stat-cell">' . I18N::translate('Earliest death year') . '</div><div class="facts_value stats_value stat-cell">' . $stats->firstDeathYear() . '</div>';
145			if (!$block) {
146				$content .= '<div class="facts_value stat-cell left">' . $stats->firstDeath() . '</div>';
147			}
148			$content .= '</div>';
149		}
150		if ($stat_last_death) {
151			$content .= '<div class="stat-row"><div class="facts_label stat-cell">' . I18N::translate('Latest death year') . '</div><div class="facts_value stats_value stat-cell">' . $stats->lastDeathYear() . '</div>';
152			if (!$block) {
153				$content .= '<div class="facts_value stat-cell left">' . $stats->lastDeath() . '</div>';
154			}
155			$content .= '</div>';
156		}
157		if ($stat_long_life) {
158			$content .= '<div class="stat-row"><div class="facts_label stat-cell">' . I18N::translate('Individual who lived the longest') . '</div><div class="facts_value stats_value stat-cell">' . $stats->LongestLifeAge() . '</div>';
159			if (!$block) {
160				$content .= '<div class="facts_value stat-cell left">' . $stats->LongestLife() . '</div>';
161			}
162			$content .= '</div>';
163		}
164		if ($stat_avg_life) {
165			$content .= '<div class="stat-row"><div class="facts_label stat-cell">' . I18N::translate('Average age at death') . '</div><div class="facts_value stats_value stat-cell">' . $stats->averageLifespan() . '</div>';
166			if (!$block) {
167				$content .= '<div class="facts_value stat-cell left">' . I18N::translate('Males') . ':&nbsp;' . $stats->averageLifespanMale();
168				$content .= '&nbsp;&nbsp;&nbsp;' . I18N::translate('Females') . ':&nbsp;' . $stats->averageLifespanFemale() . '</div>';
169			}
170			$content .= '</div>';
171		}
172
173		if ($stat_most_chil && !$block) {
174			$content .= '<div class="stat-row"><div class="facts_label stat-cell">' . I18N::translate('Family with the most children') . '</div><div class="facts_value stats_value  stat-cell">' . $stats->largestFamilySize() . '</div>';
175			if (!$block) {
176				$content .= '<div class="facts_value stat-cell left">' . $stats->largestFamily() . '</div>';
177			}
178			$content .= '</div>';
179		}
180		if ($stat_avg_chil) {
181			$content .= '<div class="stat-row"><div class="facts_label stat-cell">' . I18N::translate('Average number of children per family') . '</div><div class="facts_value stats_value  stat-cell">' . $stats->averageChildren() . '</div>';
182			if (!$block) {
183				$content .= '<div class="facts_value stat-cell left"></div>';
184			}
185			$content .= '</div>';
186		}
187		if ($stat_link) {
188			$content .= '</div><div class="clearfloat"><a href="statistics.php?ged=' . $WT_TREE->getNameUrl() . '"><b>' . I18N::translate('View statistics as graphs') . '</b></a></div>';
189		}
190		// NOTE: Print the most common surnames
191		if ($show_common_surnames) {
192			$surnames = FunctionsDb::getCommonSurnames($WT_TREE->getPreference('COMMON_NAMES_THRESHOLD'), $WT_TREE);
193			if (count($surnames) > 0) {
194				$content .= '<p><b>' . I18N::translate('Most common surnames') . '</b></p>';
195				$content .= '<div class="common_surnames">';
196				$i = 0;
197				foreach ($surnames as $indexval => $surname) {
198					if (stristr($surname['name'], '@N.N') === false) {
199						if ($i > 0) {
200							$content .= ', ';
201						}
202						$content .= '<a href="' . "indilist.php?ged=" . $WT_TREE->getNameUrl() . "&amp;surname=" . rawurlencode($surname['name']) . '">' . $surname['name'] . '</a>';
203						$i++;
204					}
205				}
206			}
207		}
208
209		if ($template) {
210			return Theme::theme()->formatBlock($id, $title, $class, $content);
211		} else {
212			return $content;
213		}
214	}
215
216	/** {@inheritdoc} */
217	public function loadAjax() {
218		return true;
219	}
220
221	/** {@inheritdoc} */
222	public function isUserBlock() {
223		return true;
224	}
225
226	/** {@inheritdoc} */
227	public function isGedcomBlock() {
228		return true;
229	}
230
231	/** {@inheritdoc} */
232	public function configureBlock($block_id) {
233		if (Filter::postBool('save') && Filter::checkCsrf()) {
234			$this->setBlockSetting($block_id, 'show_last_update', Filter::postBool('show_last_update'));
235			$this->setBlockSetting($block_id, 'show_common_surnames', Filter::postBool('show_common_surnames'));
236			$this->setBlockSetting($block_id, 'stat_indi', Filter::postBool('stat_indi'));
237			$this->setBlockSetting($block_id, 'stat_fam', Filter::postBool('stat_fam'));
238			$this->setBlockSetting($block_id, 'stat_sour', Filter::postBool('stat_sour'));
239			$this->setBlockSetting($block_id, 'stat_other', Filter::postBool('stat_other'));
240			$this->setBlockSetting($block_id, 'stat_media', Filter::postBool('stat_media'));
241			$this->setBlockSetting($block_id, 'stat_repo', Filter::postBool('stat_repo'));
242			$this->setBlockSetting($block_id, 'stat_surname', Filter::postBool('stat_surname'));
243			$this->setBlockSetting($block_id, 'stat_events', Filter::postBool('stat_events'));
244			$this->setBlockSetting($block_id, 'stat_users', Filter::postBool('stat_users'));
245			$this->setBlockSetting($block_id, 'stat_first_birth', Filter::postBool('stat_first_birth'));
246			$this->setBlockSetting($block_id, 'stat_last_birth', Filter::postBool('stat_last_birth'));
247			$this->setBlockSetting($block_id, 'stat_first_death', Filter::postBool('stat_first_death'));
248			$this->setBlockSetting($block_id, 'stat_last_death', Filter::postBool('stat_last_death'));
249			$this->setBlockSetting($block_id, 'stat_long_life', Filter::postBool('stat_long_life'));
250			$this->setBlockSetting($block_id, 'stat_avg_life', Filter::postBool('stat_avg_life'));
251			$this->setBlockSetting($block_id, 'stat_most_chil', Filter::postBool('stat_most_chil'));
252			$this->setBlockSetting($block_id, 'stat_avg_chil', Filter::postBool('stat_avg_chil'));
253		}
254
255		$show_last_update     = $this->getBlockSetting($block_id, 'show_last_update', '1');
256		$show_common_surnames = $this->getBlockSetting($block_id, 'show_common_surnames', '1');
257		$stat_indi            = $this->getBlockSetting($block_id, 'stat_indi', '1');
258		$stat_fam             = $this->getBlockSetting($block_id, 'stat_fam', '1');
259		$stat_sour            = $this->getBlockSetting($block_id, 'stat_sour', '1');
260		$stat_media           = $this->getBlockSetting($block_id, 'stat_media', '1');
261		$stat_repo            = $this->getBlockSetting($block_id, 'stat_repo', '1');
262		$stat_surname         = $this->getBlockSetting($block_id, 'stat_surname', '1');
263		$stat_events          = $this->getBlockSetting($block_id, 'stat_events', '1');
264		$stat_users           = $this->getBlockSetting($block_id, 'stat_users', '1');
265		$stat_first_birth     = $this->getBlockSetting($block_id, 'stat_first_birth', '1');
266		$stat_last_birth      = $this->getBlockSetting($block_id, 'stat_last_birth', '1');
267		$stat_first_death     = $this->getBlockSetting($block_id, 'stat_first_death', '1');
268		$stat_last_death      = $this->getBlockSetting($block_id, 'stat_last_death', '1');
269		$stat_long_life       = $this->getBlockSetting($block_id, 'stat_long_life', '1');
270		$stat_avg_life        = $this->getBlockSetting($block_id, 'stat_avg_life', '1');
271		$stat_most_chil       = $this->getBlockSetting($block_id, 'stat_most_chil', '1');
272		$stat_avg_chil        = $this->getBlockSetting($block_id, 'stat_avg_chil', '1');
273
274		echo '<tr><td class="descriptionbox wrap width33">';
275		echo /* I18N: label for yes/no option */ I18N::translate('Show date of last update?');
276		echo '</td><td class="optionbox">';
277		echo FunctionsEdit::editFieldYesNo('show_last_update', $show_last_update);
278		echo '</td></tr>';
279
280		echo '<tr><td class="descriptionbox wrap width33">';
281		echo I18N::translate('Show common surnames?');
282		echo '</td><td class="optionbox">';
283		echo FunctionsEdit::editFieldYesNo('show_common_surnames', $show_common_surnames);
284		echo '</td></tr>';
285
286?>
287	<tr>
288	<td class="descriptionbox wrap width33"><?php echo I18N::translate('Select the stats to show in this block'); ?></td>
289	<td class="optionbox">
290	<table>
291		<tbody>
292			<tr>
293				<td>
294					<label>
295						<input type="checkbox" value="yes" name="stat_indi" <?php echo $stat_indi ? 'checked' : ''; ?>>
296						<?php echo I18N::translate('Individuals'); ?>
297					</label>
298				</td>
299				<td>
300					<label>
301						<input type="checkbox" value="yes" name="stat_first_birth" <?php echo $stat_first_birth ? 'checked' : ''; ?>>
302						<?php echo I18N::translate('Earliest birth year'); ?>
303					</label>
304				</td>
305			</tr>
306			<tr>
307				<td>
308					<label>
309						<input type="checkbox" value="yes" name="stat_surname" <?php echo $stat_surname ? 'checked' : ''; ?>>
310						<?php echo I18N::translate('Total surnames'); ?>
311					</label>
312				</td>
313				<td>
314					<label>
315						<input type="checkbox" value="yes" name="stat_last_birth" <?php echo $stat_last_birth ? 'checked' : ''; ?>>
316						<?php echo I18N::translate('Latest birth year'); ?>
317					</label>
318				</td>
319			</tr>
320			<tr>
321				<td>
322					<label>
323						<input type="checkbox" value="yes" name="stat_fam" <?php echo $stat_fam ? 'checked' : ''; ?>>
324						<?php echo I18N::translate('Families'); ?>
325					</label>
326				</td>
327				<td>
328					<label>
329						<input type="checkbox" value="yes" name="stat_first_death" <?php echo $stat_first_death ? 'checked' : ''; ?>>
330						<?php echo I18N::translate('Earliest death year'); ?>
331					</label>
332				</td>
333			</tr>
334			<tr>
335				<td>
336					<label>
337						<input type="checkbox" value="yes" name="stat_sour" <?php echo $stat_sour ? 'checked' : ''; ?>>
338						<?php echo I18N::translate('Sources'); ?>
339					</label>
340				</td>
341				<td>
342					<label>
343						<input type="checkbox" value="yes" name="stat_last_death" <?php echo $stat_last_death ? 'checked' : ''; ?>>
344						<?php echo I18N::translate('Latest death year'); ?>
345					</label>
346				</td>
347			</tr>
348			<tr>
349				<td>
350					<label>
351						<input type="checkbox" value="yes" name="stat_media" <?php echo $stat_media ? 'checked' : ''; ?>>
352						<?php echo I18N::translate('Media objects'); ?>
353					</label>
354				</td>
355				<td>
356					<label>
357						<input type="checkbox" value="yes" name="stat_long_life" <?php echo $stat_long_life ? 'checked' : ''; ?>>
358						<?php echo I18N::translate('Individual who lived the longest'); ?>
359					</label>
360				</td>
361			</tr>
362			<tr>
363				<td>
364					<label>
365						<input type="checkbox" value="yes" name="stat_repo" <?php echo $stat_repo ? 'checked' : ''; ?>>
366						<?php echo I18N::translate('Repositories'); ?>
367					</label>
368				</td>
369				<td>
370					<label>
371						<input type="checkbox" value="yes" name="stat_avg_life" <?php echo $stat_avg_life ? 'checked' : ''; ?>>
372						<?php echo I18N::translate('Average age at death'); ?>
373					</label>
374				</td>
375			</tr>
376			<tr>
377				<td>
378					<label>
379						<input type="checkbox" value="yes" name="stat_events" <?php echo $stat_events ? 'checked' : ''; ?>>
380						<?php echo I18N::translate('Total events'); ?>
381					</label>
382				</td>
383				<td>
384					<label>
385						<input type="checkbox" value="yes" name="stat_most_chil" <?php echo $stat_most_chil ? 'checked' : ''; ?>>
386						<?php echo I18N::translate('Family with the most children'); ?>
387					</label>
388				</td>
389			</tr>
390			<tr>
391				<td>
392					<label>
393						<input type="checkbox" value="yes" name="stat_users" <?php echo $stat_users ? 'checked' : ''; ?>>
394						<?php echo I18N::translate('Total users'); ?>
395					</label>
396				</td>
397				<td>
398					<label>
399						<input type="checkbox" value="yes" name="stat_avg_chil" <?php echo $stat_avg_chil ? 'checked' : ''; ?>>
400						<?php echo I18N::translate('Average number of children per family'); ?>
401					</label>
402				</td>
403			</tr>
404		</tbody>
405	</table>
406	</td>
407	</tr>
408	<?php
409	}
410}
411