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