Completed
Push — master ( 94717f...b0ff97 )
by Jonathan
06:05
created
src/Webtrees/Globals.php 1 patch
Indentation   +36 added lines, -36 removed lines patch added patch discarded remove patch
@@ -16,48 +16,48 @@
 block discarded – undo
16 16
  */
17 17
 class Globals {
18 18
     
19
-    /**
20
-     * Get global $WT_TREE variable.
21
-     * 
22
-     * @return \Fisharebest\Webtrees\Tree
23
-     */
24
-    public static function getTree() {
25
-        global $WT_TREE;
19
+	/**
20
+	 * Get global $WT_TREE variable.
21
+	 * 
22
+	 * @return \Fisharebest\Webtrees\Tree
23
+	 */
24
+	public static function getTree() {
25
+		global $WT_TREE;
26 26
         
27
-        return $WT_TREE;
28
-    }
27
+		return $WT_TREE;
28
+	}
29 29
     
30
-    /**
31
-     * Check whether the visitor is a bot.
32
-     * 
33
-     * @return boolean
34
-     */
35
-    public static function isSearchSpider() {
36
-        global $SEARCH_SPIDER;
30
+	/**
31
+	 * Check whether the visitor is a bot.
32
+	 * 
33
+	 * @return boolean
34
+	 */
35
+	public static function isSearchSpider() {
36
+		global $SEARCH_SPIDER;
37 37
         
38
-        return $SEARCH_SPIDER;
39
-    }
38
+		return $SEARCH_SPIDER;
39
+	}
40 40
     
41
-    /**
42
-     * Get the current controller.
43
-     * 
44
-     * @return \Fisharebest\Webtrees\BaseController
45
-     */
46
-    public static function getController() {
47
-        global $controller;
41
+	/**
42
+	 * Get the current controller.
43
+	 * 
44
+	 * @return \Fisharebest\Webtrees\BaseController
45
+	 */
46
+	public static function getController() {
47
+		global $controller;
48 48
         
49
-        return $controller;
50
-    }
49
+		return $controller;
50
+	}
51 51
     
52
-    /**
53
-     * Get the global facts
54
-     * 
55
-     * @return array
56
-     */
57
-    public static function getGlobalFacts() {
58
-        global $global_facts;
52
+	/**
53
+	 * Get the global facts
54
+	 * 
55
+	 * @return array
56
+	 */
57
+	public static function getGlobalFacts() {
58
+		global $global_facts;
59 59
         
60
-        return $global_facts;
61
-    }
60
+		return $global_facts;
61
+	}
62 62
     
63 63
 }
Please login to merge, or discard this patch.
src/Webtrees/Module/Sosa/Model/SosaProvider.php 1 patch
Indentation   +613 added lines, -613 removed lines patch added patch discarded remove patch
@@ -22,389 +22,389 @@  discard block
 block discarded – undo
22 22
  */
23 23
 class SosaProvider {
24 24
     
25
-    /**
26
-     * Maximum number of generation the database is able to hold.
27
-     * @var int MAX_DB_GENERATIONS
28
-     */
29
-    const MAX_DB_GENERATIONS = 64;
30
-    
31
-    /**
32
-     * System's default user (ID -1 in the database
33
-     * @var User $default_user
34
-     */
35
-    protected static $default_user;
36
-    
37
-    /**
38
-     * Reference user
39
-     * @var User $user
40
-     */
41
-    protected $user;
42
-    
43
-    /**
44
-     * Reference tree
45
-     * @var Tree $tree
46
-     */
47
-    protected $tree;
48
-    
49
-    /**
50
-     * Cached list of Sosa Individuals by generation
51
-     * Format: key = generation, value = array ( sosa => Individual ID)
52
-     * @var array $sosa_list_by_gen
53
-     */
54
-    protected $sosa_list_by_gen;
55
-    
56
-    /**
57
-     * Cached list of Sosa Families by generation
58
-     * Format: key = generation, value = array ( sosa => Family ID)
59
-     * @var unknown $sosa_fam_list_by_gen
60
-     */
61
-    protected $sosa_fam_list_by_gen;
62
-    
63
-    /**
64
-     * Cached array of statistics by generation
65
-     * Format:  key = generation, 
66
-     *          value = array(
67
-     *              sosaCount, sosaTotalCount, diffSosaTotalCount, firstBirth, lastBirth, avgBirth
68
-     *           )
69
-     * @var array $statistics_tab
70
-     */
71
-    protected $statistics_tab;
72
-    
73
-    /**
74
-     * Has the provider's initialisation completed
75
-     * @var bool $is_setup
76
-     */
77
-    protected $is_setup;
78
-    
79
-    /**
80
-     * Constructor for Sosa Provider.
81
-     * A provider is defined in relation to a specific tree and reference user.
82
-     * 
83
-     * @param Tree $tree
84
-     * @param User $user
85
-     */
86
-    public function __construct(Tree $tree, User $user = null) {
87
-        if(self::$default_user === null) 
88
-            self::$default_user = User::find(-1);
25
+	/**
26
+	 * Maximum number of generation the database is able to hold.
27
+	 * @var int MAX_DB_GENERATIONS
28
+	 */
29
+	const MAX_DB_GENERATIONS = 64;
30
+    
31
+	/**
32
+	 * System's default user (ID -1 in the database
33
+	 * @var User $default_user
34
+	 */
35
+	protected static $default_user;
36
+    
37
+	/**
38
+	 * Reference user
39
+	 * @var User $user
40
+	 */
41
+	protected $user;
42
+    
43
+	/**
44
+	 * Reference tree
45
+	 * @var Tree $tree
46
+	 */
47
+	protected $tree;
48
+    
49
+	/**
50
+	 * Cached list of Sosa Individuals by generation
51
+	 * Format: key = generation, value = array ( sosa => Individual ID)
52
+	 * @var array $sosa_list_by_gen
53
+	 */
54
+	protected $sosa_list_by_gen;
55
+    
56
+	/**
57
+	 * Cached list of Sosa Families by generation
58
+	 * Format: key = generation, value = array ( sosa => Family ID)
59
+	 * @var unknown $sosa_fam_list_by_gen
60
+	 */
61
+	protected $sosa_fam_list_by_gen;
62
+    
63
+	/**
64
+	 * Cached array of statistics by generation
65
+	 * Format:  key = generation, 
66
+	 *          value = array(
67
+	 *              sosaCount, sosaTotalCount, diffSosaTotalCount, firstBirth, lastBirth, avgBirth
68
+	 *           )
69
+	 * @var array $statistics_tab
70
+	 */
71
+	protected $statistics_tab;
72
+    
73
+	/**
74
+	 * Has the provider's initialisation completed
75
+	 * @var bool $is_setup
76
+	 */
77
+	protected $is_setup;
78
+    
79
+	/**
80
+	 * Constructor for Sosa Provider.
81
+	 * A provider is defined in relation to a specific tree and reference user.
82
+	 * 
83
+	 * @param Tree $tree
84
+	 * @param User $user
85
+	 */
86
+	public function __construct(Tree $tree, User $user = null) {
87
+		if(self::$default_user === null) 
88
+			self::$default_user = User::find(-1);
89 89
         
90
-        $this->tree = $tree;
91
-        $this->user = $user;
92
-        $this->is_setup = true;
93
-        if($this->user === null) $this->user = Auth::user();
94
-        if(strlen($this->user->getUserId()) == 0) $this->user = self::$default_user;
90
+		$this->tree = $tree;
91
+		$this->user = $user;
92
+		$this->is_setup = true;
93
+		if($this->user === null) $this->user = Auth::user();
94
+		if(strlen($this->user->getUserId()) == 0) $this->user = self::$default_user;
95 95
         
96
-        // Check if the user, or the default user, has a root already setup;
97
-        if(empty($this->getRootIndiId())) {
98
-            if($this->user == self::$default_user) {  // If the default user is not setup
99
-                $this->is_setup = false;
100
-            }
101
-            else {
102
-                $this->user = self::$default_user;
103
-                $this->is_setup = $this->getRootIndiId() === null;
104
-            }            
105
-        }
106
-    }
107
-    
108
-    /**
109
-     * Returns is the Provider has been successfully set up
110
-     * @return bool
111
-     */
112
-    public function isSetup() {
113
-        return $this->is_setup;
114
-    }
115
-    
116
-    /**
117
-     * Return the root individual ID for the reference tree and user.
118
-     * @return string Individual ID
119
-     */
120
-    public function getRootIndiId() {
121
-        return $this->tree->getUserPreference($this->user, 'MAJ_SOSA_ROOT_ID');
122
-    }
123
-    
124
-    /**
125
-     * Return the root individual for the reference tree and user.
126
-     * @return Individual Individual
127
-     */
128
-    public function getRootIndi() {
129
-        $root_indi_id = $this->getRootIndiId();
130
-        if(!empty($root_indi_id)) {
131
-            return Individual::getInstance($root_indi_id, $this->tree);
132
-        }
133
-        return null;
134
-    }
96
+		// Check if the user, or the default user, has a root already setup;
97
+		if(empty($this->getRootIndiId())) {
98
+			if($this->user == self::$default_user) {  // If the default user is not setup
99
+				$this->is_setup = false;
100
+			}
101
+			else {
102
+				$this->user = self::$default_user;
103
+				$this->is_setup = $this->getRootIndiId() === null;
104
+			}            
105
+		}
106
+	}
107
+    
108
+	/**
109
+	 * Returns is the Provider has been successfully set up
110
+	 * @return bool
111
+	 */
112
+	public function isSetup() {
113
+		return $this->is_setup;
114
+	}
115
+    
116
+	/**
117
+	 * Return the root individual ID for the reference tree and user.
118
+	 * @return string Individual ID
119
+	 */
120
+	public function getRootIndiId() {
121
+		return $this->tree->getUserPreference($this->user, 'MAJ_SOSA_ROOT_ID');
122
+	}
123
+    
124
+	/**
125
+	 * Return the root individual for the reference tree and user.
126
+	 * @return Individual Individual
127
+	 */
128
+	public function getRootIndi() {
129
+		$root_indi_id = $this->getRootIndiId();
130
+		if(!empty($root_indi_id)) {
131
+			return Individual::getInstance($root_indi_id, $this->tree);
132
+		}
133
+		return null;
134
+	}
135 135
        
136
-    /*****************
136
+	/*****************
137 137
      * DATA CRUD LAYER
138 138
      *****************/
139 139
     
140
-    /**
141
-     * Remove all Sosa entries related to the gedcom file and user
142
-     */
143
-    public function deleteAll() {
144
-        if(!$this->is_setup) return;
145
-        Database::prepare(
146
-            'DELETE FROM `##maj_sosa`'.
147
-            ' WHERE majs_gedcom_id= :tree_id and majs_user_id = :user_id ')
148
-            ->execute(array(
149
-                'tree_id' => $this->tree->getTreeId(), 
150
-                'user_id' => $this->user->getUserId()                
151
-            ));
152
-    }
153
-    
154
-    /**
155
-     * Remove all ancestors of a sosa number
156
-     * 
157
-     * @param int $sosa
158
-     */
159
-    public function deleteAncestors($sosa) {
160
-        if(!$this->is_setup) return;
161
-        $gen = Functions::getGeneration($sosa);
162
-        Database::prepare(
163
-            'DELETE FROM `##maj_sosa`'.
164
-            ' WHERE majs_gedcom_id=:tree_id and majs_user_id = :user_id' .
165
-            ' AND majs_gen >= :gen' .
166
-            ' AND FLOOR(majs_sosa / (POW(2, (majs_gen - :gen)))) = :sosa'
167
-        )->execute(array(
168
-            'tree_id' => $this->tree->getTreeId(), 
169
-            'user_id' => $this->user->getUserId(),
170
-            'gen' => $gen,
171
-            'sosa' => $sosa
172
-        ));
173
-    }    
174
-    
175
-    /**
176
-     * Insert (or update if already existing) a list of Sosa individuals
177
-     * @param array $sosa_records
178
-     */
179
-    public function insertOrUpdate($sosa_records) {
180
-        if(!$this->is_setup) return;
140
+	/**
141
+	 * Remove all Sosa entries related to the gedcom file and user
142
+	 */
143
+	public function deleteAll() {
144
+		if(!$this->is_setup) return;
145
+		Database::prepare(
146
+			'DELETE FROM `##maj_sosa`'.
147
+			' WHERE majs_gedcom_id= :tree_id and majs_user_id = :user_id ')
148
+			->execute(array(
149
+				'tree_id' => $this->tree->getTreeId(), 
150
+				'user_id' => $this->user->getUserId()                
151
+			));
152
+	}
153
+    
154
+	/**
155
+	 * Remove all ancestors of a sosa number
156
+	 * 
157
+	 * @param int $sosa
158
+	 */
159
+	public function deleteAncestors($sosa) {
160
+		if(!$this->is_setup) return;
161
+		$gen = Functions::getGeneration($sosa);
162
+		Database::prepare(
163
+			'DELETE FROM `##maj_sosa`'.
164
+			' WHERE majs_gedcom_id=:tree_id and majs_user_id = :user_id' .
165
+			' AND majs_gen >= :gen' .
166
+			' AND FLOOR(majs_sosa / (POW(2, (majs_gen - :gen)))) = :sosa'
167
+		)->execute(array(
168
+			'tree_id' => $this->tree->getTreeId(), 
169
+			'user_id' => $this->user->getUserId(),
170
+			'gen' => $gen,
171
+			'sosa' => $sosa
172
+		));
173
+	}    
174
+    
175
+	/**
176
+	 * Insert (or update if already existing) a list of Sosa individuals
177
+	 * @param array $sosa_records
178
+	 */
179
+	public function insertOrUpdate($sosa_records) {
180
+		if(!$this->is_setup) return;
181 181
         
182
-        $treeid = $this->tree->getTreeId();
183
-        $userid = $this->user->getUserId();
184
-        $questionmarks_table = array();
185
-        $values_table = array();
182
+		$treeid = $this->tree->getTreeId();
183
+		$userid = $this->user->getUserId();
184
+		$questionmarks_table = array();
185
+		$values_table = array();
186 186
         
187
-        $i = 0;
188
-        foreach  ($sosa_records as $row) {
189
-            $gen = Functions::getGeneration($row['sosa']);
190
-            if($gen <= self::MAX_DB_GENERATIONS) {
191
-                $questionmarks_table[] = 
192
-                    '(:tree_id'.$i.', :user_id'.$i.', :sosa'.$i.', :indi_id'.$i.', :gen'.$i.', :byear'.$i.', :dyear'.$i.')';
193
-                $values_table = array_merge(
194
-                    $values_table, 
195
-                    array(
196
-                        'tree_id'.$i => $treeid, 
197
-                        'user_id'.$i => $userid, 
198
-                        'sosa'.$i => $row['sosa'], 
199
-                        'indi_id'.$i => $row['indi'], 
200
-                        'gen'.$i => Functions::getGeneration($row['sosa']),
201
-                        'byear'.$i => $row['birth_year'],
202
-                        'dyear'.$i => $row['death_year']
203
-                    )
204
-                );
205
-            }
206
-            $i++;
207
-        }
187
+		$i = 0;
188
+		foreach  ($sosa_records as $row) {
189
+			$gen = Functions::getGeneration($row['sosa']);
190
+			if($gen <= self::MAX_DB_GENERATIONS) {
191
+				$questionmarks_table[] = 
192
+					'(:tree_id'.$i.', :user_id'.$i.', :sosa'.$i.', :indi_id'.$i.', :gen'.$i.', :byear'.$i.', :dyear'.$i.')';
193
+				$values_table = array_merge(
194
+					$values_table, 
195
+					array(
196
+						'tree_id'.$i => $treeid, 
197
+						'user_id'.$i => $userid, 
198
+						'sosa'.$i => $row['sosa'], 
199
+						'indi_id'.$i => $row['indi'], 
200
+						'gen'.$i => Functions::getGeneration($row['sosa']),
201
+						'byear'.$i => $row['birth_year'],
202
+						'dyear'.$i => $row['death_year']
203
+					)
204
+				);
205
+			}
206
+			$i++;
207
+		}
208 208
         
209
-        $sql = 'REPLACE INTO `##maj_sosa`' .
210
-            ' (majs_gedcom_id, majs_user_id, majs_sosa, majs_i_id, majs_gen, majs_birth_year, majs_death_year)' .
211
-            ' VALUES '. implode(',', $questionmarks_table);
212
-        Database::prepare($sql)->execute($values_table);
213
-    }
209
+		$sql = 'REPLACE INTO `##maj_sosa`' .
210
+			' (majs_gedcom_id, majs_user_id, majs_sosa, majs_i_id, majs_gen, majs_birth_year, majs_death_year)' .
211
+			' VALUES '. implode(',', $questionmarks_table);
212
+		Database::prepare($sql)->execute($values_table);
213
+	}
214 214
     
215
-    /****************
215
+	/****************
216 216
      * SIMPLE QUERIES
217 217
      ****************/
218 218
     
219
-    /**
220
-     * Returns the list of Sosa numbers to which an individual is related.
221
-     * Format: key = sosa number, value = generation for the Sosa number
222
-     * 
223
-     * @param Individual $indi
224
-     * @return array Array of sosa numbers
225
-     */
226
-    public function getSosaNumbers(Individual $indi) {
227
-        if(!$this->is_setup) return array();
228
-        return Database::prepare(
229
-                'SELECT majs_sosa, majs_gen FROM `##maj_sosa`'.
230
-                ' WHERE majs_i_id=:indi_id AND majs_gedcom_id=:tree_id AND majs_user_id=:user_id'
231
-            )->execute(array(
232
-                'indi_id' => $indi->getXref(), 
233
-                'tree_id' => $this->tree->getTreeId(), 
234
-                'user_id' => $this->user->getUserId()
235
-            ))->fetchAssoc();
236
-    }
237
-    
238
-    /**
239
-     * Get the last generation of Sosa ancestors
240
-     *
241
-     * @return number Last generation if found, 1 otherwise
242
-     */
243
-    public function getLastGeneration() {
244
-        if(!$this->is_setup) return;
245
-        return Database::prepare(
246
-                'SELECT MAX(majs_gen) FROM `##maj_sosa`'.
247
-                ' WHERE majs_gedcom_id=:tree_id AND majs_user_id=:user_id'
248
-            )->execute(array(
249
-                'tree_id' => $this->tree->getTreeId(), 
250
-                'user_id' => $this->user->getUserId()                
251
-            ))->fetchOne() ?: 1;
252
-    }
253
-    
254
-    /*************
219
+	/**
220
+	 * Returns the list of Sosa numbers to which an individual is related.
221
+	 * Format: key = sosa number, value = generation for the Sosa number
222
+	 * 
223
+	 * @param Individual $indi
224
+	 * @return array Array of sosa numbers
225
+	 */
226
+	public function getSosaNumbers(Individual $indi) {
227
+		if(!$this->is_setup) return array();
228
+		return Database::prepare(
229
+				'SELECT majs_sosa, majs_gen FROM `##maj_sosa`'.
230
+				' WHERE majs_i_id=:indi_id AND majs_gedcom_id=:tree_id AND majs_user_id=:user_id'
231
+			)->execute(array(
232
+				'indi_id' => $indi->getXref(), 
233
+				'tree_id' => $this->tree->getTreeId(), 
234
+				'user_id' => $this->user->getUserId()
235
+			))->fetchAssoc();
236
+	}
237
+    
238
+	/**
239
+	 * Get the last generation of Sosa ancestors
240
+	 *
241
+	 * @return number Last generation if found, 1 otherwise
242
+	 */
243
+	public function getLastGeneration() {
244
+		if(!$this->is_setup) return;
245
+		return Database::prepare(
246
+				'SELECT MAX(majs_gen) FROM `##maj_sosa`'.
247
+				' WHERE majs_gedcom_id=:tree_id AND majs_user_id=:user_id'
248
+			)->execute(array(
249
+				'tree_id' => $this->tree->getTreeId(), 
250
+				'user_id' => $this->user->getUserId()                
251
+			))->fetchOne() ?: 1;
252
+	}
253
+    
254
+	/*************
255 255
      * SOSA LISTS
256 256
      *************/
257 257
     
258
-    /**
259
-     * Return the list of all sosas, with the generations it belongs to
260
-     *
261
-     * @return array Associative array of Sosa ancestors, with their generation, comma separated
262
-     */
263
-    public function getAllSosaWithGenerations(){
264
-        if(!$this->is_setup) return array();
265
-        return Database::prepare(
266
-            'SELECT majs_i_id AS indi,' .
267
-            ' GROUP_CONCAT(DISTINCT majs_gen ORDER BY majs_gen ASC SEPARATOR ",") AS generations' .
268
-            ' FROM `##maj_sosa`' .
269
-            ' WHERE majs_gedcom_id=:tree_id AND majs_user_id=:user_id' .
270
-            ' GROUP BY majs_i_id'
271
-        )->execute(array(
272
-            'tree_id' => $this->tree->getTreeId(),
273
-            'user_id' => $this->user->getUserId()
274
-        ))->fetchAssoc();
275
-    }
276
-    
277
-    /**
278
-     * Get an associative array of Sosa individuals in generation G. Keys are Sosa numbers, values individuals.
279
-     *
280
-     * @param number $gen Generation
281
-     * @return array Array of Sosa individuals
282
-     */
283
-    public function getSosaListAtGeneration($gen){
284
-        if(!$this->is_setup) return array();
285
-        if(!$this->sosa_list_by_gen)
286
-            $this->sosa_list_by_gen = array();
258
+	/**
259
+	 * Return the list of all sosas, with the generations it belongs to
260
+	 *
261
+	 * @return array Associative array of Sosa ancestors, with their generation, comma separated
262
+	 */
263
+	public function getAllSosaWithGenerations(){
264
+		if(!$this->is_setup) return array();
265
+		return Database::prepare(
266
+			'SELECT majs_i_id AS indi,' .
267
+			' GROUP_CONCAT(DISTINCT majs_gen ORDER BY majs_gen ASC SEPARATOR ",") AS generations' .
268
+			' FROM `##maj_sosa`' .
269
+			' WHERE majs_gedcom_id=:tree_id AND majs_user_id=:user_id' .
270
+			' GROUP BY majs_i_id'
271
+		)->execute(array(
272
+			'tree_id' => $this->tree->getTreeId(),
273
+			'user_id' => $this->user->getUserId()
274
+		))->fetchAssoc();
275
+	}
276
+    
277
+	/**
278
+	 * Get an associative array of Sosa individuals in generation G. Keys are Sosa numbers, values individuals.
279
+	 *
280
+	 * @param number $gen Generation
281
+	 * @return array Array of Sosa individuals
282
+	 */
283
+	public function getSosaListAtGeneration($gen){
284
+		if(!$this->is_setup) return array();
285
+		if(!$this->sosa_list_by_gen)
286
+			$this->sosa_list_by_gen = array();
287 287
         
288
-        if($gen){
289
-            if(!isset($this->sosa_list_by_gen[$gen])){
290
-                $this->sosa_list_by_gen[$gen] = Database::prepare(
291
-                    'SELECT majs_sosa AS sosa, majs_i_id AS indi'.
292
-                    ' FROM `##maj_sosa`'.
293
-                    ' WHERE majs_gedcom_id=:tree_id AND majs_user_id=:user_id'.
294
-                    ' AND majs_gen = :gen'.
295
-                    ' ORDER BY majs_sosa ASC')
296
-                ->execute(array(
297
-                    'tree_id' => $this->tree->getTreeId(),
298
-                    'user_id' => $this->user->getUserId(),
299
-                    'gen' => $gen
300
-                ))
301
-                ->fetchAssoc();
302
-            }
303
-            return $this->sosa_list_by_gen[$gen];
304
-        }
305
-        return array();
306
-    }
307
-    
308
-    /**
309
-     * Get an associative array of Sosa families in generation G. Keys are Sosa numbers for the husband, values families.
310
-     *
311
-     * @param number $gen Generation
312
-     * @return array Array of Sosa families
313
-     */
314
-    public function getFamilySosaListAtGeneration($gen){
315
-        if(!$this->is_setup) return array();
316
-        if(!$this->sosa_fam_list_by_gen)
317
-            $this->sosa_fam_list_by_gen = array();
288
+		if($gen){
289
+			if(!isset($this->sosa_list_by_gen[$gen])){
290
+				$this->sosa_list_by_gen[$gen] = Database::prepare(
291
+					'SELECT majs_sosa AS sosa, majs_i_id AS indi'.
292
+					' FROM `##maj_sosa`'.
293
+					' WHERE majs_gedcom_id=:tree_id AND majs_user_id=:user_id'.
294
+					' AND majs_gen = :gen'.
295
+					' ORDER BY majs_sosa ASC')
296
+				->execute(array(
297
+					'tree_id' => $this->tree->getTreeId(),
298
+					'user_id' => $this->user->getUserId(),
299
+					'gen' => $gen
300
+				))
301
+				->fetchAssoc();
302
+			}
303
+			return $this->sosa_list_by_gen[$gen];
304
+		}
305
+		return array();
306
+	}
307
+    
308
+	/**
309
+	 * Get an associative array of Sosa families in generation G. Keys are Sosa numbers for the husband, values families.
310
+	 *
311
+	 * @param number $gen Generation
312
+	 * @return array Array of Sosa families
313
+	 */
314
+	public function getFamilySosaListAtGeneration($gen){
315
+		if(!$this->is_setup) return array();
316
+		if(!$this->sosa_fam_list_by_gen)
317
+			$this->sosa_fam_list_by_gen = array();
318 318
         
319
-        if($gen){
320
-            if(!isset($this->sosa_fam_list_by_gen[$gen])){
321
-                $this->sosa_fam_list_by_gen[$gen] = Database::prepare(
322
-                    'SELECT s1.majs_sosa AS sosa, f_id AS fam'.
323
-                    ' FROM `##families`'.
324
-                    ' INNER JOIN `##maj_sosa` AS s1 ON (`##families`.f_husb = s1.majs_i_id AND `##families`.f_file = s1.majs_gedcom_id)'.
325
-                    ' INNER JOIN `##maj_sosa` AS s2 ON (`##families`.f_wife = s2.majs_i_id AND `##families`.f_file = s2.majs_gedcom_id)'.
326
-                    ' WHERE s1.majs_sosa + 1 = s2.majs_sosa'.
327
-                    ' AND s1.majs_gedcom_id= :tree_id AND s1.majs_user_id=:user_id'.
328
-                    ' AND s2.majs_gedcom_id= :tree_id AND s2.majs_user_id=:user_id'.
329
-                    ' AND s1.majs_gen = :gen'.
330
-                    ' ORDER BY s1.majs_sosa ASC'
331
-                    )
332
-                    ->execute(array(
333
-                        'tree_id' => $this->tree->getTreeId(),
334
-                        'user_id' => $this->user->getUserId(),
335
-                        'gen' => $gen
336
-                    ))
337
-                    ->fetchAssoc();
338
-            }
339
-            return $this->sosa_fam_list_by_gen[$gen];
340
-        }
341
-        return array();
342
-    }
343
-    
344
-    /**
345
-     * Get an associative array of Sosa individuals in generation G who are missing parents. Keys are Sosa numbers, values individuals.
346
-     *
347
-     * @param number $gen Generation
348
-     * @return array Array of Sosa individuals
349
-     */
350
-    public function getMissingSosaListAtGeneration($gen){
351
-        if(!$this->is_setup) return array();    
352
-        if($gen){
353
-            return $this->sosa_list_by_gen[$gen] = Database::prepare(
354
-                'SELECT schild.majs_sosa sosa, schild.majs_i_id indi, sfat.majs_sosa IS NOT NULL has_father, smot.majs_sosa IS NOT NULL has_mother'.
355
-                ' FROM `##maj_sosa` schild'.
356
-                ' LEFT JOIN `##maj_sosa` sfat ON ((schild.majs_sosa * 2) = sfat.majs_sosa AND schild.majs_gedcom_id = sfat.majs_gedcom_id AND schild.majs_user_id = sfat.majs_user_id)'.
357
-                ' LEFT JOIN `##maj_sosa` smot ON ((schild.majs_sosa * 2 + 1) = smot.majs_sosa AND schild.majs_gedcom_id = smot.majs_gedcom_id AND schild.majs_user_id = smot.majs_user_id)'.
358
-                ' WHERE schild.majs_gedcom_id = :tree_id AND schild.majs_user_id = :user_id'.
359
-                ' AND schild.majs_gen = :gen'.
360
-                ' AND (sfat.majs_sosa IS NULL OR smot.majs_sosa IS NULL)'.
361
-                ' ORDER BY schild.majs_sosa ASC')
362
-                ->execute(array(
363
-                    'tree_id' => $this->tree->getTreeId(),
364
-                    'user_id' => $this->user->getUserId(),
365
-                    'gen' => $gen - 1
366
-                ))->fetchAll(\PDO::FETCH_ASSOC);
367
-        }
368
-        return array();
369
-    }
370
-    
371
-    
372
-    
373
-    /*************
319
+		if($gen){
320
+			if(!isset($this->sosa_fam_list_by_gen[$gen])){
321
+				$this->sosa_fam_list_by_gen[$gen] = Database::prepare(
322
+					'SELECT s1.majs_sosa AS sosa, f_id AS fam'.
323
+					' FROM `##families`'.
324
+					' INNER JOIN `##maj_sosa` AS s1 ON (`##families`.f_husb = s1.majs_i_id AND `##families`.f_file = s1.majs_gedcom_id)'.
325
+					' INNER JOIN `##maj_sosa` AS s2 ON (`##families`.f_wife = s2.majs_i_id AND `##families`.f_file = s2.majs_gedcom_id)'.
326
+					' WHERE s1.majs_sosa + 1 = s2.majs_sosa'.
327
+					' AND s1.majs_gedcom_id= :tree_id AND s1.majs_user_id=:user_id'.
328
+					' AND s2.majs_gedcom_id= :tree_id AND s2.majs_user_id=:user_id'.
329
+					' AND s1.majs_gen = :gen'.
330
+					' ORDER BY s1.majs_sosa ASC'
331
+					)
332
+					->execute(array(
333
+						'tree_id' => $this->tree->getTreeId(),
334
+						'user_id' => $this->user->getUserId(),
335
+						'gen' => $gen
336
+					))
337
+					->fetchAssoc();
338
+			}
339
+			return $this->sosa_fam_list_by_gen[$gen];
340
+		}
341
+		return array();
342
+	}
343
+    
344
+	/**
345
+	 * Get an associative array of Sosa individuals in generation G who are missing parents. Keys are Sosa numbers, values individuals.
346
+	 *
347
+	 * @param number $gen Generation
348
+	 * @return array Array of Sosa individuals
349
+	 */
350
+	public function getMissingSosaListAtGeneration($gen){
351
+		if(!$this->is_setup) return array();    
352
+		if($gen){
353
+			return $this->sosa_list_by_gen[$gen] = Database::prepare(
354
+				'SELECT schild.majs_sosa sosa, schild.majs_i_id indi, sfat.majs_sosa IS NOT NULL has_father, smot.majs_sosa IS NOT NULL has_mother'.
355
+				' FROM `##maj_sosa` schild'.
356
+				' LEFT JOIN `##maj_sosa` sfat ON ((schild.majs_sosa * 2) = sfat.majs_sosa AND schild.majs_gedcom_id = sfat.majs_gedcom_id AND schild.majs_user_id = sfat.majs_user_id)'.
357
+				' LEFT JOIN `##maj_sosa` smot ON ((schild.majs_sosa * 2 + 1) = smot.majs_sosa AND schild.majs_gedcom_id = smot.majs_gedcom_id AND schild.majs_user_id = smot.majs_user_id)'.
358
+				' WHERE schild.majs_gedcom_id = :tree_id AND schild.majs_user_id = :user_id'.
359
+				' AND schild.majs_gen = :gen'.
360
+				' AND (sfat.majs_sosa IS NULL OR smot.majs_sosa IS NULL)'.
361
+				' ORDER BY schild.majs_sosa ASC')
362
+				->execute(array(
363
+					'tree_id' => $this->tree->getTreeId(),
364
+					'user_id' => $this->user->getUserId(),
365
+					'gen' => $gen - 1
366
+				))->fetchAll(\PDO::FETCH_ASSOC);
367
+		}
368
+		return array();
369
+	}
370
+    
371
+    
372
+    
373
+	/*************
374 374
      * STATISTICS
375 375
      *************/
376
-    /**
377
-     * Get the statistic array detailed by generation.
378
-     * Statistics for each generation are:
379
-     * 	- The number of Sosa in generation
380
-     * 	- The number of Sosa up to generation
381
-     *  - The number of distinct Sosa up to generation
382
-     *  - The year of the first birth in generation
383
-     *  - The year of the last birth in generation
384
-     *  - The average year of birth in generation
385
-     *
386
-     * @return array Statistics array
387
-     */
388
-    public function getStatisticsByGeneration() {
389
-        if(!$this->is_setup) return array();
390
-        if(!$this->statistics_tab) {
391
-            $this->statistics_tab = array();
392
-            if($maxGeneration = $this->getLastGeneration()) {
393
-                for ($gen = 1; $gen <= $maxGeneration; $gen++) {
394
-                    $birthStats = $this->getStatsBirthYearInGeneration($gen);
395
-                    $this->statistics_tab[$gen] = array(
396
-                        'sosaCount'				=>	$this->getSosaCountAtGeneration($gen),
397
-                        'sosaTotalCount'		=>	$this->getSosaCountUpToGeneration($gen),
398
-                        'diffSosaTotalCount'	=>	$this->getDifferentSosaCountUpToGeneration($gen),
399
-                        'firstBirth'			=>	$birthStats['first'],
400
-                        'lastBirth'				=>	$birthStats['last'],
401
-                        'avgBirth'				=>	$birthStats['avg']
402
-                    );
403
-                }
404
-            }
405
-        }
406
-        return $this->statistics_tab;        
407
-    }
376
+	/**
377
+	 * Get the statistic array detailed by generation.
378
+	 * Statistics for each generation are:
379
+	 * 	- The number of Sosa in generation
380
+	 * 	- The number of Sosa up to generation
381
+	 *  - The number of distinct Sosa up to generation
382
+	 *  - The year of the first birth in generation
383
+	 *  - The year of the last birth in generation
384
+	 *  - The average year of birth in generation
385
+	 *
386
+	 * @return array Statistics array
387
+	 */
388
+	public function getStatisticsByGeneration() {
389
+		if(!$this->is_setup) return array();
390
+		if(!$this->statistics_tab) {
391
+			$this->statistics_tab = array();
392
+			if($maxGeneration = $this->getLastGeneration()) {
393
+				for ($gen = 1; $gen <= $maxGeneration; $gen++) {
394
+					$birthStats = $this->getStatsBirthYearInGeneration($gen);
395
+					$this->statistics_tab[$gen] = array(
396
+						'sosaCount'				=>	$this->getSosaCountAtGeneration($gen),
397
+						'sosaTotalCount'		=>	$this->getSosaCountUpToGeneration($gen),
398
+						'diffSosaTotalCount'	=>	$this->getDifferentSosaCountUpToGeneration($gen),
399
+						'firstBirth'			=>	$birthStats['first'],
400
+						'lastBirth'				=>	$birthStats['last'],
401
+						'avgBirth'				=>	$birthStats['avg']
402
+					);
403
+				}
404
+			}
405
+		}
406
+		return $this->statistics_tab;        
407
+	}
408 408
     
409 409
 	/**
410 410
 	 * How many individuals exist in the tree.
@@ -412,258 +412,258 @@  discard block
 block discarded – undo
412 412
 	 * @return int
413 413
 	 */
414 414
 	public function getTotalIndividuals() {
415
-	    if(!$this->is_setup) return 0;
416
-	    return Database::prepare(
417
-	        'SELECT SQL_CACHE COUNT(*) FROM `##individuals`' .
418
-	        ' WHERE i_file = :tree_id')
419
-	        ->execute(array('tree_id' => $this->tree->getTreeId()))
420
-	        ->fetchOne() ?: 0;
415
+		if(!$this->is_setup) return 0;
416
+		return Database::prepare(
417
+			'SELECT SQL_CACHE COUNT(*) FROM `##individuals`' .
418
+			' WHERE i_file = :tree_id')
419
+			->execute(array('tree_id' => $this->tree->getTreeId()))
420
+			->fetchOne() ?: 0;
421
+	}
422
+    
423
+	/**
424
+	 * Get the total Sosa count for all generations
425
+	 *
426
+	 * @return number Number of Sosas
427
+	 */
428
+	public function getSosaCount(){
429
+		if(!$this->is_setup) return 0;
430
+		return Database::prepare(
431
+			'SELECT SQL_CACHE COUNT(majs_sosa) FROM `##maj_sosa`' .
432
+			' WHERE majs_gedcom_id=:tree_id AND majs_user_id=:user_id')
433
+			->execute(array(
434
+				'tree_id' => $this->tree->getTreeId(), 
435
+				'user_id' => $this->user->getUserId() 
436
+			))->fetchOne() ?: 0;
437
+	}
438
+    
439
+	/**
440
+	 * Get the number of Sosa in a specific generation.
441
+	 *
442
+	 * @param number $gen Generation
443
+	 * @return number Number of Sosas in generation
444
+	 */
445
+	public function getSosaCountAtGeneration($gen){
446
+		if(!$this->is_setup) return 0;
447
+		return Database::prepare(
448
+			'SELECT SQL_CACHE COUNT(majs_sosa) FROM `##maj_sosa`' .
449
+			' WHERE majs_gedcom_id=:tree_id AND majs_user_id=:user_id'.
450
+			' AND majs_gen= :gen')
451
+		->execute(array(
452
+				'tree_id' => $this->tree->getTreeId(), 
453
+				'user_id' => $this->user->getUserId(),
454
+				'gen' => $gen            
455
+		))->fetchOne() ?: 0;
421 456
 	}
422 457
     
423
-    /**
424
-     * Get the total Sosa count for all generations
425
-     *
426
-     * @return number Number of Sosas
427
-     */
428
-    public function getSosaCount(){
429
-        if(!$this->is_setup) return 0;
430
-        return Database::prepare(
431
-            'SELECT SQL_CACHE COUNT(majs_sosa) FROM `##maj_sosa`' .
432
-            ' WHERE majs_gedcom_id=:tree_id AND majs_user_id=:user_id')
433
-            ->execute(array(
434
-                'tree_id' => $this->tree->getTreeId(), 
435
-                'user_id' => $this->user->getUserId() 
436
-            ))->fetchOne() ?: 0;
437
-    }
438
-    
439
-    /**
440
-     * Get the number of Sosa in a specific generation.
441
-     *
442
-     * @param number $gen Generation
443
-     * @return number Number of Sosas in generation
444
-     */
445
-    public function getSosaCountAtGeneration($gen){
446
-        if(!$this->is_setup) return 0;
447
-        return Database::prepare(
448
-            'SELECT SQL_CACHE COUNT(majs_sosa) FROM `##maj_sosa`' .
449
-            ' WHERE majs_gedcom_id=:tree_id AND majs_user_id=:user_id'.
450
-            ' AND majs_gen= :gen')
451
-        ->execute(array(
452
-                'tree_id' => $this->tree->getTreeId(), 
453
-                'user_id' => $this->user->getUserId(),
454
-                'gen' => $gen            
455
-        ))->fetchOne() ?: 0;
456
-    }
457
-    
458
-    /**
459
-     * Get the total number of Sosa up to a specific generation.
460
-     *
461
-     * @param number $gen Generation
462
-     * @return number Total number of Sosas up to generation
463
-     */
464
-    public function getSosaCountUpToGeneration($gen){
465
-        if(!$this->is_setup) return 0;
466
-        return Database::prepare(
467
-            'SELECT SQL_CACHE COUNT(majs_sosa) FROM `##maj_sosa`' .
468
-            ' WHERE majs_gedcom_id=:tree_id AND majs_user_id=:user_id'.
469
-            ' AND majs_gen <= :gen')
470
-        ->execute(array(
471
-                'tree_id' => $this->tree->getTreeId(), 
472
-                'user_id' => $this->user->getUserId(),
473
-                'gen' => $gen 
474
-        ))->fetchOne() ?: 0;
475
-    }
476
-    
477
-    /**
478
-     * Get the total number of distinct Sosa individual for all generations.
479
-     *
480
-     * @return number Total number of distinct individual
481
-     */
482
-    public function getDifferentSosaCount(){
483
-        if(!$this->is_setup) return 0;
484
-        return Database::prepare(
485
-            'SELECT SQL_CACHE COUNT(DISTINCT majs_i_id) FROM `##maj_sosa`' .
486
-            ' WHERE majs_gedcom_id=:tree_id AND majs_user_id=:user_id')
487
-        ->execute(array(
488
-                'tree_id' => $this->tree->getTreeId(), 
489
-                'user_id' => $this->user->getUserId()
490
-        ))->fetchOne() ?: 0;
491
-    }
492
-    
493
-    /**
494
-     * Get the number of distinct Sosa individual up to a specific generation.
495
-     *
496
-     * @param number $gen Generation
497
-     * @return number Number of distinct Sosa individuals up to generation
498
-     */
499
-    public function getDifferentSosaCountUpToGeneration($gen){
500
-        if(!$this->is_setup) return 0;
501
-        return Database::prepare(
502
-            'SELECT SQL_CACHE COUNT(DISTINCT majs_i_id) FROM `##maj_sosa`' .
503
-            ' WHERE majs_gedcom_id=:tree_id AND majs_user_id=:user_id'.
504
-            ' AND majs_gen <= :gen')
505
-        ->execute(array(
506
-                'tree_id' => $this->tree->getTreeId(), 
507
-                'user_id' => $this->user->getUserId(),
508
-                'gen' => $gen 
509
-        ))->fetchOne() ?: 0;
510
-    }
511
-    
512
-    /**
513
-     * Get an array of birth statistics for a specific generation
514
-     * Statistics are :
515
-     * 	- first : First birth year in generation
516
-     *  - last : Last birth year in generation
517
-     *  - avg : Average birth year
518
-     *
519
-     * @param number $gen Generation
520
-     * @return array Birth statistics array
521
-     */
522
-    public function getStatsBirthYearInGeneration($gen){
523
-        if(!$this->is_setup) return array('first' => 0, 'avg' => 0, 'last' => 0);
524
-        return Database::prepare(
525
-            'SELECT MIN(majs_birth_year) AS first, AVG(majs_birth_year) AS avg, MAX(majs_birth_year) AS last'.
526
-            ' FROM `##maj_sosa`' .
527
-            ' WHERE majs_gedcom_id=:tree_id AND majs_user_id=:user_id'.
528
-            ' AND majs_gen=:gen AND NOT majs_birth_year = :birth_year')
529
-            ->execute(array(
530
-                'tree_id' => $this->tree->getTreeId(), 
531
-                'user_id' => $this->user->getUserId(),
532
-                'gen' => $gen,
533
-                'birth_year' => 0))
534
-            ->fetchOneRow(\PDO::FETCH_ASSOC) ?: array('first' => 0, 'avg' => 0, 'last' => 0);
535
-    }
536
-    
537
-    /**
538
-     * Get the mean generation time, based on a linear regression of birth years and generations
539
-     *
540
-     * @return number|NULL Mean generation time
541
-     */
542
-    public function getMeanGenerationTime(){
543
-        if(!$this->is_setup) return;
544
-        if(!$this->statistics_tab){
545
-            $this->getStatisticsByGeneration();
546
-        }
547
-        //Linear regression on x=generation and y=birthdate
548
-        $sum_xy = 0;
549
-        $sum_x=0;
550
-        $sum_y=0;
551
-        $sum_x2=0;
552
-        $n=count($this->statistics_tab);
553
-        foreach($this->statistics_tab as $gen=>$stats){
554
-            $sum_xy+=$gen*$stats['avgBirth'];
555
-            $sum_x+=$gen;
556
-            $sum_y+=$stats['avgBirth'];
557
-            $sum_x2+=$gen*$gen;
558
-        }
559
-        $denom=($n*$sum_x2)-($sum_x*$sum_x);
560
-        if($denom!=0){
561
-            return -(($n*$sum_xy)-($sum_x*$sum_y))/($denom);
562
-        }
563
-        return null;
564
-    }
565
-    
566
-    /**
567
-     * Return a computed array of statistics about the dispersion of ancestors across the ancestors
568
-     * at a specified generation.
569
-     * This statistics cannot be used for generations above 11, as it would cause a out of range in MySQL
570
-     * 
571
-     * Format: 
572
-     *  - key : a base-2 representation of the ancestor at generation G for which exclusive ancestors have been found,
573
-     *          -1 is used for shared ancestors
574
-     *          For instance base2(0100) = base10(4) represent the maternal grand father
575
-     *  - values: number of ancestors exclusively in the ancestors of the ancestor in key
576
-     *  
577
-     *  For instance a result at generation 3 could be :
578
-     *      array (   -1        =>  12      -> 12 ancestors are shared by the grand-parents
579
-     *                base10(1) =>  32      -> 32 ancestors are exclusive to the paternal grand-father
580
-     *                base10(2) =>  25      -> 25 ancestors are exclusive to the paternal grand-mother
581
-     *                base10(4) =>  12      -> 12 ancestors are exclusive to the maternal grand-father
582
-     *                base10(8) =>  30      -> 30 ancestors are exclusive to the maternal grand-mother
583
-     *            )
584
-     *  
585
-     * @param int $gen Reference generation
586
-     * @return array
587
-     */
588
-    public function getAncestorDispersionForGen($gen) {
589
-        if(!$this->is_setup || $gen > 11) return array();  // Going further than 11 gen will be out of range in the query
590
-        return Database::prepare(
591
-            'SELECT branches, count(i_id)'.
592
-            ' FROM ('.
593
-            '   SELECT i_id,'.
594
-            '       CASE'.
595
-            '           WHEN CEIL(LOG2(SUM(branch))) = LOG2(SUM(branch)) THEN SUM(branch)'.
596
-            '           ELSE -1'.   // We put all ancestors shared between some branches in the same bucket
597
-            '       END branches'.
598
-            '   FROM ('.
599
-            '       SELECT DISTINCT majs_i_id i_id,'.
600
-            '           POW(2, FLOOR(majs_sosa / POW(2, (majs_gen - :gen))) - POW(2, :gen -1)) branch'.
601
-            '       FROM `##maj_sosa`'.
602
-            '       WHERE majs_gedcom_id = :tree_id AND majs_user_id = :user_id'.
603
-            '           AND majs_gen >= :gen'.
604
-            '   ) indistat'.
605
-            '   GROUP BY i_id'.
606
-            ') grouped'.
607
-            ' GROUP BY branches')
608
-            ->execute(array(
609
-                'tree_id' => $this->tree->getTreeId(), 
610
-                'user_id' => $this->user->getUserId(),
611
-                'gen' => $gen
612
-            ))->fetchAssoc() ?: array();
613
-    }
614
-    
615
-    /**
616
-     * Return an array of the most duplicated root Sosa ancestors.
617
-     * The number of ancestors to return is limited by the parameter $limit.
618
-     * If several individuals are tied when reaching the limit, none of them are returned,
619
-     * which means that there can be less individuals returned than requested.
620
-     * 
621
-     * Format: 
622
-     *  - key : root Sosa individual
623
-     *  - value: number of duplications of the ancestor (e.g. 3 if it appears 3 times)
624
-     * 
625
-     * @param number $limit Maximum number of individuals to return
626
-     * @return array 
627
-     */
628
-    public function getTopMultiSosaAncestorsNoTies($limit) {
629
-        if(!$this->is_setup) return array();
630
-        return Database::prepare(
631
-            'SELECT sosa_i_id, sosa_count FROM ('.
632
-            '   SELECT'.
633
-            '       top_sosa.sosa_i_id, top_sosa.sosa_count, top_sosa.sosa_min,'.
634
-            '       @keep := IF(@prev_count = 0 OR sosa_count = @prev_count, @keep, 1) AS keep,'.
635
-            '       @prev_count := top_sosa.sosa_count AS prev_count'.
636
-            '   FROM ('.
637
-            '       SELECT'.
638
-            '           sosa.majs_i_id sosa_i_id,'.
639
-            '           COUNT(sosa.majs_sosa) sosa_count,'.
640
-            '           MIN(sosa.majs_sosa) sosa_min'.
641
-            '       FROM ##maj_sosa AS sosa'.
642
-            '       LEFT JOIN ##maj_sosa AS sosa_fat ON sosa_fat.majs_sosa = 2 * sosa.majs_sosa'.   // Link to sosa's father
643
-            '           AND sosa.majs_gedcom_id = sosa_fat.majs_gedcom_id'.
644
-            '           AND sosa.majs_user_id = sosa_fat.majs_user_id'.
645
-            '       LEFT JOIN ##maj_sosa AS sosa_mot on sosa_mot.majs_sosa = (2 * sosa.majs_sosa + 1)'.  // Link to sosa's mother
646
-            '           AND sosa.majs_gedcom_id = sosa_fat.majs_gedcom_id'.
647
-            '           AND sosa.majs_user_id = sosa_fat.majs_user_id'.
648
-            '       WHERE sosa.majs_gedcom_id = :tree_id'.
649
-            '       AND sosa.majs_user_id = :user_id'.
650
-            '       AND sosa_fat.majs_sosa IS NULL'.    // We keep only root individuals, i.e. those with no father or mother
651
-            '       AND sosa_mot.majs_sosa IS NULL'. 
652
-            '       GROUP BY sosa.majs_i_id'.
653
-            '       HAVING COUNT(sosa.majs_sosa) > 1'.   // Limit to the duplicate sosas.
654
-            '       ORDER BY COUNT(sosa.majs_sosa) DESC'.
655
-            '       LIMIT ' . ($limit + 1) . // We want to select one more than required
656
-            '   ) AS top_sosa,'.
657
-            '   (SELECT @prev_count := 0, @keep := 0) x'.
658
-            '   ORDER BY top_sosa.sosa_count ASC'.
659
-            ' ) top_sosa_list'.
660
-            ' WHERE keep = 1'.
661
-            ' ORDER BY sosa_count DESC, sosa_min ASC'
662
-            )->execute(array(
663
-                'tree_id' => $this->tree->getTreeId(),
664
-                'user_id' => $this->user->getUserId()
665
-            ))->fetchAssoc() ?: array();
666
-    }
458
+	/**
459
+	 * Get the total number of Sosa up to a specific generation.
460
+	 *
461
+	 * @param number $gen Generation
462
+	 * @return number Total number of Sosas up to generation
463
+	 */
464
+	public function getSosaCountUpToGeneration($gen){
465
+		if(!$this->is_setup) return 0;
466
+		return Database::prepare(
467
+			'SELECT SQL_CACHE COUNT(majs_sosa) FROM `##maj_sosa`' .
468
+			' WHERE majs_gedcom_id=:tree_id AND majs_user_id=:user_id'.
469
+			' AND majs_gen <= :gen')
470
+		->execute(array(
471
+				'tree_id' => $this->tree->getTreeId(), 
472
+				'user_id' => $this->user->getUserId(),
473
+				'gen' => $gen 
474
+		))->fetchOne() ?: 0;
475
+	}
476
+    
477
+	/**
478
+	 * Get the total number of distinct Sosa individual for all generations.
479
+	 *
480
+	 * @return number Total number of distinct individual
481
+	 */
482
+	public function getDifferentSosaCount(){
483
+		if(!$this->is_setup) return 0;
484
+		return Database::prepare(
485
+			'SELECT SQL_CACHE COUNT(DISTINCT majs_i_id) FROM `##maj_sosa`' .
486
+			' WHERE majs_gedcom_id=:tree_id AND majs_user_id=:user_id')
487
+		->execute(array(
488
+				'tree_id' => $this->tree->getTreeId(), 
489
+				'user_id' => $this->user->getUserId()
490
+		))->fetchOne() ?: 0;
491
+	}
492
+    
493
+	/**
494
+	 * Get the number of distinct Sosa individual up to a specific generation.
495
+	 *
496
+	 * @param number $gen Generation
497
+	 * @return number Number of distinct Sosa individuals up to generation
498
+	 */
499
+	public function getDifferentSosaCountUpToGeneration($gen){
500
+		if(!$this->is_setup) return 0;
501
+		return Database::prepare(
502
+			'SELECT SQL_CACHE COUNT(DISTINCT majs_i_id) FROM `##maj_sosa`' .
503
+			' WHERE majs_gedcom_id=:tree_id AND majs_user_id=:user_id'.
504
+			' AND majs_gen <= :gen')
505
+		->execute(array(
506
+				'tree_id' => $this->tree->getTreeId(), 
507
+				'user_id' => $this->user->getUserId(),
508
+				'gen' => $gen 
509
+		))->fetchOne() ?: 0;
510
+	}
511
+    
512
+	/**
513
+	 * Get an array of birth statistics for a specific generation
514
+	 * Statistics are :
515
+	 * 	- first : First birth year in generation
516
+	 *  - last : Last birth year in generation
517
+	 *  - avg : Average birth year
518
+	 *
519
+	 * @param number $gen Generation
520
+	 * @return array Birth statistics array
521
+	 */
522
+	public function getStatsBirthYearInGeneration($gen){
523
+		if(!$this->is_setup) return array('first' => 0, 'avg' => 0, 'last' => 0);
524
+		return Database::prepare(
525
+			'SELECT MIN(majs_birth_year) AS first, AVG(majs_birth_year) AS avg, MAX(majs_birth_year) AS last'.
526
+			' FROM `##maj_sosa`' .
527
+			' WHERE majs_gedcom_id=:tree_id AND majs_user_id=:user_id'.
528
+			' AND majs_gen=:gen AND NOT majs_birth_year = :birth_year')
529
+			->execute(array(
530
+				'tree_id' => $this->tree->getTreeId(), 
531
+				'user_id' => $this->user->getUserId(),
532
+				'gen' => $gen,
533
+				'birth_year' => 0))
534
+			->fetchOneRow(\PDO::FETCH_ASSOC) ?: array('first' => 0, 'avg' => 0, 'last' => 0);
535
+	}
536
+    
537
+	/**
538
+	 * Get the mean generation time, based on a linear regression of birth years and generations
539
+	 *
540
+	 * @return number|NULL Mean generation time
541
+	 */
542
+	public function getMeanGenerationTime(){
543
+		if(!$this->is_setup) return;
544
+		if(!$this->statistics_tab){
545
+			$this->getStatisticsByGeneration();
546
+		}
547
+		//Linear regression on x=generation and y=birthdate
548
+		$sum_xy = 0;
549
+		$sum_x=0;
550
+		$sum_y=0;
551
+		$sum_x2=0;
552
+		$n=count($this->statistics_tab);
553
+		foreach($this->statistics_tab as $gen=>$stats){
554
+			$sum_xy+=$gen*$stats['avgBirth'];
555
+			$sum_x+=$gen;
556
+			$sum_y+=$stats['avgBirth'];
557
+			$sum_x2+=$gen*$gen;
558
+		}
559
+		$denom=($n*$sum_x2)-($sum_x*$sum_x);
560
+		if($denom!=0){
561
+			return -(($n*$sum_xy)-($sum_x*$sum_y))/($denom);
562
+		}
563
+		return null;
564
+	}
565
+    
566
+	/**
567
+	 * Return a computed array of statistics about the dispersion of ancestors across the ancestors
568
+	 * at a specified generation.
569
+	 * This statistics cannot be used for generations above 11, as it would cause a out of range in MySQL
570
+	 * 
571
+	 * Format: 
572
+	 *  - key : a base-2 representation of the ancestor at generation G for which exclusive ancestors have been found,
573
+	 *          -1 is used for shared ancestors
574
+	 *          For instance base2(0100) = base10(4) represent the maternal grand father
575
+	 *  - values: number of ancestors exclusively in the ancestors of the ancestor in key
576
+	 *  
577
+	 *  For instance a result at generation 3 could be :
578
+	 *      array (   -1        =>  12      -> 12 ancestors are shared by the grand-parents
579
+	 *                base10(1) =>  32      -> 32 ancestors are exclusive to the paternal grand-father
580
+	 *                base10(2) =>  25      -> 25 ancestors are exclusive to the paternal grand-mother
581
+	 *                base10(4) =>  12      -> 12 ancestors are exclusive to the maternal grand-father
582
+	 *                base10(8) =>  30      -> 30 ancestors are exclusive to the maternal grand-mother
583
+	 *            )
584
+	 *  
585
+	 * @param int $gen Reference generation
586
+	 * @return array
587
+	 */
588
+	public function getAncestorDispersionForGen($gen) {
589
+		if(!$this->is_setup || $gen > 11) return array();  // Going further than 11 gen will be out of range in the query
590
+		return Database::prepare(
591
+			'SELECT branches, count(i_id)'.
592
+			' FROM ('.
593
+			'   SELECT i_id,'.
594
+			'       CASE'.
595
+			'           WHEN CEIL(LOG2(SUM(branch))) = LOG2(SUM(branch)) THEN SUM(branch)'.
596
+			'           ELSE -1'.   // We put all ancestors shared between some branches in the same bucket
597
+			'       END branches'.
598
+			'   FROM ('.
599
+			'       SELECT DISTINCT majs_i_id i_id,'.
600
+			'           POW(2, FLOOR(majs_sosa / POW(2, (majs_gen - :gen))) - POW(2, :gen -1)) branch'.
601
+			'       FROM `##maj_sosa`'.
602
+			'       WHERE majs_gedcom_id = :tree_id AND majs_user_id = :user_id'.
603
+			'           AND majs_gen >= :gen'.
604
+			'   ) indistat'.
605
+			'   GROUP BY i_id'.
606
+			') grouped'.
607
+			' GROUP BY branches')
608
+			->execute(array(
609
+				'tree_id' => $this->tree->getTreeId(), 
610
+				'user_id' => $this->user->getUserId(),
611
+				'gen' => $gen
612
+			))->fetchAssoc() ?: array();
613
+	}
614
+    
615
+	/**
616
+	 * Return an array of the most duplicated root Sosa ancestors.
617
+	 * The number of ancestors to return is limited by the parameter $limit.
618
+	 * If several individuals are tied when reaching the limit, none of them are returned,
619
+	 * which means that there can be less individuals returned than requested.
620
+	 * 
621
+	 * Format: 
622
+	 *  - key : root Sosa individual
623
+	 *  - value: number of duplications of the ancestor (e.g. 3 if it appears 3 times)
624
+	 * 
625
+	 * @param number $limit Maximum number of individuals to return
626
+	 * @return array 
627
+	 */
628
+	public function getTopMultiSosaAncestorsNoTies($limit) {
629
+		if(!$this->is_setup) return array();
630
+		return Database::prepare(
631
+			'SELECT sosa_i_id, sosa_count FROM ('.
632
+			'   SELECT'.
633
+			'       top_sosa.sosa_i_id, top_sosa.sosa_count, top_sosa.sosa_min,'.
634
+			'       @keep := IF(@prev_count = 0 OR sosa_count = @prev_count, @keep, 1) AS keep,'.
635
+			'       @prev_count := top_sosa.sosa_count AS prev_count'.
636
+			'   FROM ('.
637
+			'       SELECT'.
638
+			'           sosa.majs_i_id sosa_i_id,'.
639
+			'           COUNT(sosa.majs_sosa) sosa_count,'.
640
+			'           MIN(sosa.majs_sosa) sosa_min'.
641
+			'       FROM ##maj_sosa AS sosa'.
642
+			'       LEFT JOIN ##maj_sosa AS sosa_fat ON sosa_fat.majs_sosa = 2 * sosa.majs_sosa'.   // Link to sosa's father
643
+			'           AND sosa.majs_gedcom_id = sosa_fat.majs_gedcom_id'.
644
+			'           AND sosa.majs_user_id = sosa_fat.majs_user_id'.
645
+			'       LEFT JOIN ##maj_sosa AS sosa_mot on sosa_mot.majs_sosa = (2 * sosa.majs_sosa + 1)'.  // Link to sosa's mother
646
+			'           AND sosa.majs_gedcom_id = sosa_fat.majs_gedcom_id'.
647
+			'           AND sosa.majs_user_id = sosa_fat.majs_user_id'.
648
+			'       WHERE sosa.majs_gedcom_id = :tree_id'.
649
+			'       AND sosa.majs_user_id = :user_id'.
650
+			'       AND sosa_fat.majs_sosa IS NULL'.    // We keep only root individuals, i.e. those with no father or mother
651
+			'       AND sosa_mot.majs_sosa IS NULL'. 
652
+			'       GROUP BY sosa.majs_i_id'.
653
+			'       HAVING COUNT(sosa.majs_sosa) > 1'.   // Limit to the duplicate sosas.
654
+			'       ORDER BY COUNT(sosa.majs_sosa) DESC'.
655
+			'       LIMIT ' . ($limit + 1) . // We want to select one more than required
656
+			'   ) AS top_sosa,'.
657
+			'   (SELECT @prev_count := 0, @keep := 0) x'.
658
+			'   ORDER BY top_sosa.sosa_count ASC'.
659
+			' ) top_sosa_list'.
660
+			' WHERE keep = 1'.
661
+			' ORDER BY sosa_count DESC, sosa_min ASC'
662
+			)->execute(array(
663
+				'tree_id' => $this->tree->getTreeId(),
664
+				'user_id' => $this->user->getUserId()
665
+			))->fetchAssoc() ?: array();
666
+	}
667 667
     
668 668
                
669 669
 }
Please login to merge, or discard this patch.
src/Webtrees/Module/Sosa/Views/SosaStatsView.php 1 patch
Indentation   +11 added lines, -11 removed lines patch added patch discarded remove patch
@@ -23,8 +23,8 @@  discard block
 block discarded – undo
23 23
 	 * {@inhericDoc}
24 24
 	 * @see \MyArtJaub\Webtrees\Mvc\View\AbstractView::renderContent()
25 25
 	 */
26
-    protected function renderContent() {      
27
-        ?>                
26
+	protected function renderContent() {      
27
+		?>                
28 28
         <div id="maj-sosa-stats-page">
29 29
 			<h2><?php echo $this->data->get('title'); ?></h2>
30 30
 			
@@ -36,7 +36,7 @@  discard block
 block discarded – undo
36 36
 			<?php } ?>
37 37
 			
38 38
 			<?php  if($this->data->get('is_setup')) {  
39
-			    $general_stats = $this->data->get('general_stats'); ?>
39
+				$general_stats = $this->data->get('general_stats'); ?>
40 40
 			<h3><?php echo I18N::translate('General statistics'); ?></h3>
41 41
 			<div class="maj-table">
42 42
 				<div class="maj-row">
@@ -140,13 +140,13 @@  discard block
 block discarded – undo
140 140
 			    <div class="maj-row">
141 141
 			    	<div class="label"><?= I18N::translate('%s times', I18N::number($count)); ?></div>
142 142
 			    	<div class="value"><?php 
143
-			    	echo implode(
144
-			    	    I18N::$list_separator, 
145
-			    	    array_map(function(Individual $indi) {
146
-			    	        return '<a href="'. $indi->getHtmlUrl(). '">'. $indi->getFullName() . '&nbsp;' . $indi->getSexImage() . '</a>';
147
-			    	    }, $indis)
148
-			    	);
149
-			    	?></div>
143
+					echo implode(
144
+						I18N::$list_separator, 
145
+						array_map(function(Individual $indi) {
146
+							return '<a href="'. $indi->getHtmlUrl(). '">'. $indi->getFullName() . '&nbsp;' . $indi->getSexImage() . '</a>';
147
+						}, $indis)
148
+					);
149
+					?></div>
150 150
 				</div>
151 151
 			<?php } ?>
152 152
 			</div>
@@ -162,6 +162,6 @@  discard block
 block discarded – undo
162 162
 			<?php   } else { ?>
163 163
 			<div class="center warning"><?php echo I18N::translate('No Sosa root individual has been defined.'); ?></div>
164 164
 			<?php }     
165
-    }
165
+	}
166 166
     
167 167
 }
168 168
\ No newline at end of file
Please login to merge, or discard this patch.
src/Webtrees/Module/Sosa/SosaStatsController.php 1 patch
Indentation   +192 added lines, -192 removed lines patch added patch discarded remove patch
@@ -27,229 +27,229 @@
 block discarded – undo
27 27
  */
28 28
 class SosaStatsController extends MvcController
29 29
 {
30
-    /**
31
-     * Sosa Provider for the controller
32
-     * @var SosaProvider $sosa_provider
33
-     */
34
-    protected $sosa_provider;
30
+	/**
31
+	 * Sosa Provider for the controller
32
+	 * @var SosaProvider $sosa_provider
33
+	 */
34
+	protected $sosa_provider;
35 35
     
36
-    /**
37
-     * Constructor for SosaStatsController
38
-     * @param AbstractModule $module
39
-     */
40
-    public function __construct(AbstractModule $module) {
41
-        parent::__construct($module);
36
+	/**
37
+	 * Constructor for SosaStatsController
38
+	 * @param AbstractModule $module
39
+	 */
40
+	public function __construct(AbstractModule $module) {
41
+		parent::__construct($module);
42 42
         
43
-        $this->sosa_provider = new SosaProvider(Globals::getTree(), Auth::user());
44
-    }
43
+		$this->sosa_provider = new SosaProvider(Globals::getTree(), Auth::user());
44
+	}
45 45
     
46
-    /**
47
-     * Pages
48
-     */
46
+	/**
47
+	 * Pages
48
+	 */
49 49
     
50
-    /**
51
-     * SosaStats@index
52
-     */
53
-    public function index() {
54
-        $wt_tree = Globals::getTree();
55
-        $controller = new PageController();
56
-        $controller
57
-            ->setPageTitle(I18N::translate('Sosa Statistics'))
58
-            ->addInlineJavascript('$(".help_tooltip").tooltip();')
59
-        ;
50
+	/**
51
+	 * SosaStats@index
52
+	 */
53
+	public function index() {
54
+		$wt_tree = Globals::getTree();
55
+		$controller = new PageController();
56
+		$controller
57
+			->setPageTitle(I18N::translate('Sosa Statistics'))
58
+			->addInlineJavascript('$(".help_tooltip").tooltip();')
59
+		;
60 60
 
61
-        $view_bag = new ViewBag();
62
-        $view_bag->set('title', $controller->getPageTitle());
63
-        $view_bag->set('is_setup', false);
61
+		$view_bag = new ViewBag();
62
+		$view_bag->set('title', $controller->getPageTitle());
63
+		$view_bag->set('is_setup', false);
64 64
         
65
-        if($this->sosa_provider->isSetup()) {
66
-            $view_bag->set('is_setup', true);
65
+		if($this->sosa_provider->isSetup()) {
66
+			$view_bag->set('is_setup', true);
67 67
             
68
-            $view_bag->set('root_indi', $this->sosa_provider->getRootIndi());
68
+			$view_bag->set('root_indi', $this->sosa_provider->getRootIndi());
69 69
             
70
-            $sosaCount = $this->sosa_provider->getSosaCount();
71
-            $diffSosaCount = $this->sosa_provider->getDifferentSosaCount();
70
+			$sosaCount = $this->sosa_provider->getSosaCount();
71
+			$diffSosaCount = $this->sosa_provider->getDifferentSosaCount();
72 72
             
73
-            $general_stats = array(
74
-                'sosa_count' => $sosaCount,
75
-                'distinct_count' => $diffSosaCount,
76
-                'sosa_rate' => Functions::safeDivision($diffSosaCount, $this->sosa_provider->getTotalIndividuals()),
77
-                'pedi_collapse' => 1 - Functions::safeDivision($diffSosaCount, $sosaCount),
78
-                'mean_gen_time' => $this->sosa_provider->getMeanGenerationTime()
79
-            );
80
-            $view_bag->set('general_stats', $general_stats);
73
+			$general_stats = array(
74
+				'sosa_count' => $sosaCount,
75
+				'distinct_count' => $diffSosaCount,
76
+				'sosa_rate' => Functions::safeDivision($diffSosaCount, $this->sosa_provider->getTotalIndividuals()),
77
+				'pedi_collapse' => 1 - Functions::safeDivision($diffSosaCount, $sosaCount),
78
+				'mean_gen_time' => $this->sosa_provider->getMeanGenerationTime()
79
+			);
80
+			$view_bag->set('general_stats', $general_stats);
81 81
             
82
-            $stats_gen = $this->sosa_provider->getStatisticsByGeneration();
83
-            $view_bag->set('missinganc_url', 'module.php?mod='.$this->module->getName().'&mod_action=SosaList@missing&ged='.$wt_tree->getNameUrl().'&gen=');
84
-            $view_bag->set('sosaanc_url', 'module.php?mod='.$this->module->getName().'&mod_action=SosaList&ged='.$wt_tree->getNameUrl().'&gen=');
82
+			$stats_gen = $this->sosa_provider->getStatisticsByGeneration();
83
+			$view_bag->set('missinganc_url', 'module.php?mod='.$this->module->getName().'&mod_action=SosaList@missing&ged='.$wt_tree->getNameUrl().'&gen=');
84
+			$view_bag->set('sosaanc_url', 'module.php?mod='.$this->module->getName().'&mod_action=SosaList&ged='.$wt_tree->getNameUrl().'&gen=');
85 85
             
86
-            $gen_theoretical=1;
87
-            $total_theoretical=0;
88
-            $prev_diff=0;
89
-            $prev_known=0.5;
90
-            $gen_equiv=0;            
91
-            $generation_stats = array();
86
+			$gen_theoretical=1;
87
+			$total_theoretical=0;
88
+			$prev_diff=0;
89
+			$prev_known=0.5;
90
+			$gen_equiv=0;            
91
+			$generation_stats = array();
92 92
             
93
-            foreach($stats_gen as $gen => $tab){
94
-                $genY1= I18N::translate('-');
95
-                $genY2= I18N::translate('-');
96
-                if($tab['firstBirth']>0) $genY1=$tab['firstBirth'];
97
-                if($tab['lastBirth']>0) $genY2=$tab['lastBirth'];
98
-                $total_theoretical += $gen_theoretical;
99
-                $perc_sosa_count_theor = Functions::safeDivision($tab['sosaCount'], $gen_theoretical);
100
-                $gen_equiv += $perc_sosa_count_theor;
101
-                $missing=2*$prev_known - $tab['sosaCount'];
102
-                $gen_diff=$tab['diffSosaTotalCount']-$prev_diff;
93
+			foreach($stats_gen as $gen => $tab){
94
+				$genY1= I18N::translate('-');
95
+				$genY2= I18N::translate('-');
96
+				if($tab['firstBirth']>0) $genY1=$tab['firstBirth'];
97
+				if($tab['lastBirth']>0) $genY2=$tab['lastBirth'];
98
+				$total_theoretical += $gen_theoretical;
99
+				$perc_sosa_count_theor = Functions::safeDivision($tab['sosaCount'], $gen_theoretical);
100
+				$gen_equiv += $perc_sosa_count_theor;
101
+				$missing=2*$prev_known - $tab['sosaCount'];
102
+				$gen_diff=$tab['diffSosaTotalCount']-$prev_diff;
103 103
                 
104
-                $generation_stats[$gen] = array(
105
-                    'gen_min_birth' => $genY1,
106
-                    'gen_max_birth' => $genY2,
107
-                    'theoretical' => $gen_theoretical,
108
-                    'known' => $tab['sosaCount'],
109
-                    'perc_known' => $perc_sosa_count_theor,
110
-                    'missing' => $missing,
111
-                    'perc_missing' => 1-Functions::safeDivision($tab['sosaCount'], 2*$prev_known),
112
-                    'total_known' => $tab['sosaTotalCount'],
113
-                    'perc_total_known' => Functions::safeDivision($tab['sosaTotalCount'], $total_theoretical),
114
-                    'different' => $gen_diff,
115
-                    'perc_different' => Functions::safeDivision($gen_diff, $tab['sosaCount']),
116
-                    'total_different' => $tab['diffSosaTotalCount'],
117
-                    'pedi_collapse' => 1 - Functions::safeDivision($tab['diffSosaTotalCount'], $tab['sosaTotalCount'])
118
-                );
104
+				$generation_stats[$gen] = array(
105
+					'gen_min_birth' => $genY1,
106
+					'gen_max_birth' => $genY2,
107
+					'theoretical' => $gen_theoretical,
108
+					'known' => $tab['sosaCount'],
109
+					'perc_known' => $perc_sosa_count_theor,
110
+					'missing' => $missing,
111
+					'perc_missing' => 1-Functions::safeDivision($tab['sosaCount'], 2*$prev_known),
112
+					'total_known' => $tab['sosaTotalCount'],
113
+					'perc_total_known' => Functions::safeDivision($tab['sosaTotalCount'], $total_theoretical),
114
+					'different' => $gen_diff,
115
+					'perc_different' => Functions::safeDivision($gen_diff, $tab['sosaCount']),
116
+					'total_different' => $tab['diffSosaTotalCount'],
117
+					'pedi_collapse' => 1 - Functions::safeDivision($tab['diffSosaTotalCount'], $tab['sosaTotalCount'])
118
+				);
119 119
                 
120
-                $gen_theoretical = $gen_theoretical * 2;
121
-                $prev_known=$tab['sosaCount'];
122
-                $prev_diff=$tab['diffSosaTotalCount'];
123
-            }
120
+				$gen_theoretical = $gen_theoretical * 2;
121
+				$prev_known=$tab['sosaCount'];
122
+				$prev_diff=$tab['diffSosaTotalCount'];
123
+			}
124 124
             
125
-            $view_bag->set('generation_stats', $generation_stats);
126
-            $view_bag->set('equivalent_gen', $gen_equiv);
125
+			$view_bag->set('generation_stats', $generation_stats);
126
+			$view_bag->set('equivalent_gen', $gen_equiv);
127 127
             
128
-            $top10multiancestors = $this->sosa_provider->getTopMultiSosaAncestorsNoTies(10);
129
-            $top10ancestors = array();
130
-            if($top10multiancestors !== null && count($top10multiancestors)) {
131
-                foreach($top10multiancestors as $pid => $count) {
132
-                    $indi = Individual::getInstance($pid, $wt_tree);
133
-                    if($indi !== null && $indi->canShowName()) {
134
-                        array_key_exists($count, $top10ancestors) ?
135
-                            $top10ancestors[$count][] = $indi:
136
-                            $top10ancestors[$count] = array($count => $indi);
137
-                    }
138
-                }
139
-            }
140
-            $view_bag->set('top10multiancestors', $top10ancestors);
128
+			$top10multiancestors = $this->sosa_provider->getTopMultiSosaAncestorsNoTies(10);
129
+			$top10ancestors = array();
130
+			if($top10multiancestors !== null && count($top10multiancestors)) {
131
+				foreach($top10multiancestors as $pid => $count) {
132
+					$indi = Individual::getInstance($pid, $wt_tree);
133
+					if($indi !== null && $indi->canShowName()) {
134
+						array_key_exists($count, $top10ancestors) ?
135
+							$top10ancestors[$count][] = $indi:
136
+							$top10ancestors[$count] = array($count => $indi);
137
+					}
138
+				}
139
+			}
140
+			$view_bag->set('top10multiancestors', $top10ancestors);
141 141
             
142
-            $view_bag->set('chart_img_g2', $this->htmlAncestorDispersionG2());
143
-            $view_bag->set('chart_img_g3', $this->htmlAncestorDispersionG3());
142
+			$view_bag->set('chart_img_g2', $this->htmlAncestorDispersionG2());
143
+			$view_bag->set('chart_img_g3', $this->htmlAncestorDispersionG3());
144 144
             
145
-        }
145
+		}
146 146
         
147
-        ViewFactory::make('SosaStats', $this, $controller, $view_bag)->render();   
148
-    }
147
+		ViewFactory::make('SosaStats', $this, $controller, $view_bag)->render();   
148
+	}
149 149
     
150
-    /**
151
-     * Returns HTML code for a graph showing the dispersion of ancestors across father & mother
152
-     * @return string HTML code
153
-     */
154
-     private function htmlAncestorDispersionG2()
155
-    {
156
-        $ancestorsDispGen2 = $this->sosa_provider->getAncestorDispersionForGen(2);
157
-        if(count($ancestorsDispGen2) == 0) return;
150
+	/**
151
+	 * Returns HTML code for a graph showing the dispersion of ancestors across father & mother
152
+	 * @return string HTML code
153
+	 */
154
+	 private function htmlAncestorDispersionG2()
155
+	{
156
+		$ancestorsDispGen2 = $this->sosa_provider->getAncestorDispersionForGen(2);
157
+		if(count($ancestorsDispGen2) == 0) return;
158 158
         
159
-        $size = '600x300';
159
+		$size = '600x300';
160 160
         
161
-        $total = array_sum($ancestorsDispGen2);
162
-        $father_count = array_key_exists(1, $ancestorsDispGen2) ? $ancestorsDispGen2[1] : 0;
163
-        $father = array (
164
-            'color' => '84beff', 
165
-            'count' => $father_count, 
166
-            'perc' => Functions::safeDivision($father_count, $total), 
167
-            'name' => \Fisharebest\Webtrees\Functions\Functions::getRelationshipNameFromPath('fat')            
168
-        );
169
-        $mother_count = array_key_exists(2, $ancestorsDispGen2) ? $ancestorsDispGen2[2] : 0;
170
-        $mother = array (
171
-            'color' => 'ffd1dc', 
172
-            'count' => $mother_count, 
173
-            'perc' => Functions::safeDivision($mother_count, $total),
174
-            'name' => \Fisharebest\Webtrees\Functions\Functions::getRelationshipNameFromPath('mot')
175
-        );
176
-        $shared_count = array_key_exists(-1, $ancestorsDispGen2) ? $ancestorsDispGen2[-1] : 0;
177
-        $shared = array (
178
-            'color' => '777777', 
179
-            'count' => $shared_count, 
180
-            'perc' => Functions::safeDivision($shared_count, $total),
181
-            'name' => I18N::translate('Shared')
182
-        );
161
+		$total = array_sum($ancestorsDispGen2);
162
+		$father_count = array_key_exists(1, $ancestorsDispGen2) ? $ancestorsDispGen2[1] : 0;
163
+		$father = array (
164
+			'color' => '84beff', 
165
+			'count' => $father_count, 
166
+			'perc' => Functions::safeDivision($father_count, $total), 
167
+			'name' => \Fisharebest\Webtrees\Functions\Functions::getRelationshipNameFromPath('fat')            
168
+		);
169
+		$mother_count = array_key_exists(2, $ancestorsDispGen2) ? $ancestorsDispGen2[2] : 0;
170
+		$mother = array (
171
+			'color' => 'ffd1dc', 
172
+			'count' => $mother_count, 
173
+			'perc' => Functions::safeDivision($mother_count, $total),
174
+			'name' => \Fisharebest\Webtrees\Functions\Functions::getRelationshipNameFromPath('mot')
175
+		);
176
+		$shared_count = array_key_exists(-1, $ancestorsDispGen2) ? $ancestorsDispGen2[-1] : 0;
177
+		$shared = array (
178
+			'color' => '777777', 
179
+			'count' => $shared_count, 
180
+			'perc' => Functions::safeDivision($shared_count, $total),
181
+			'name' => I18N::translate('Shared')
182
+		);
183 183
         
184
-        $chd = $this->arrayToExtendedEncoding(array(4095 * $father['perc'], 4095 * $shared['perc'], 4095 * $mother['perc']));
185
-        $chart_title = I18N::translate('Known Sosa ancestors\' dispersion');
186
-        $chl = 
187
-             $father['name'] . ' - ' . I18N::percentage($father['perc'], 1) . '|' .
188
-             $shared['name'] . ' - ' . I18N::percentage($shared['perc'], 1) . '|' .
189
-             $mother['name'] . ' - ' . I18N::percentage($mother['perc'], 1);
190
-        return "<img src=\"https://chart.googleapis.com/chart?cht=p&chp=1.5708&amp;chd=e:{$chd}&amp;chs={$size}&amp;chco={$father['color']},{$shared['color']},{$mother['color']}&amp;chf=bg,s,ffffff00&amp;chl={$chl}\" alt=\"" . $chart_title . "\" title=\"" . $chart_title . "\" />";
191
-    }
184
+		$chd = $this->arrayToExtendedEncoding(array(4095 * $father['perc'], 4095 * $shared['perc'], 4095 * $mother['perc']));
185
+		$chart_title = I18N::translate('Known Sosa ancestors\' dispersion');
186
+		$chl = 
187
+			 $father['name'] . ' - ' . I18N::percentage($father['perc'], 1) . '|' .
188
+			 $shared['name'] . ' - ' . I18N::percentage($shared['perc'], 1) . '|' .
189
+			 $mother['name'] . ' - ' . I18N::percentage($mother['perc'], 1);
190
+		return "<img src=\"https://chart.googleapis.com/chart?cht=p&chp=1.5708&amp;chd=e:{$chd}&amp;chs={$size}&amp;chco={$father['color']},{$shared['color']},{$mother['color']}&amp;chf=bg,s,ffffff00&amp;chl={$chl}\" alt=\"" . $chart_title . "\" title=\"" . $chart_title . "\" />";
191
+	}
192 192
     
193
-    /**
194
-     * Returns HTML code for a graph showing the dispersion of ancestors across grand-parents
195
-     * @return string HTML code
196
-     */
197
-    private function htmlAncestorDispersionG3()
198
-    {
199
-        $ancestorsDispGen2 = $this->sosa_provider->getAncestorDispersionForGen(3);
193
+	/**
194
+	 * Returns HTML code for a graph showing the dispersion of ancestors across grand-parents
195
+	 * @return string HTML code
196
+	 */
197
+	private function htmlAncestorDispersionG3()
198
+	{
199
+		$ancestorsDispGen2 = $this->sosa_provider->getAncestorDispersionForGen(3);
200 200
         
201
-        $size = '700x300';
201
+		$size = '700x300';
202 202
         
203
-        $color_motmot = 'ffd1dc';
204
-        $color_motfat = 'b998a0';
205
-        $color_fatfat = '577292';
206
-        $color_fatmot = '84beff';
207
-        $color_shared = '777777';
203
+		$color_motmot = 'ffd1dc';
204
+		$color_motfat = 'b998a0';
205
+		$color_fatfat = '577292';
206
+		$color_fatmot = '84beff';
207
+		$color_shared = '777777';
208 208
     
209
-        $total_fatfat = array_key_exists(1, $ancestorsDispGen2) ? $ancestorsDispGen2[1] : 0;
210
-        $total_fatmot = array_key_exists(2, $ancestorsDispGen2) ? $ancestorsDispGen2[2] : 0;
211
-        $total_motfat = array_key_exists(4, $ancestorsDispGen2) ? $ancestorsDispGen2[4] : 0;
212
-        $total_motmot = array_key_exists(8, $ancestorsDispGen2) ? $ancestorsDispGen2[8] : 0;
213
-        $total_sha = array_key_exists(-1, $ancestorsDispGen2) ? $ancestorsDispGen2[-1] : 0;
214
-        $total = $total_fatfat + $total_fatmot + $total_motfat+ $total_motmot + $total_sha;
209
+		$total_fatfat = array_key_exists(1, $ancestorsDispGen2) ? $ancestorsDispGen2[1] : 0;
210
+		$total_fatmot = array_key_exists(2, $ancestorsDispGen2) ? $ancestorsDispGen2[2] : 0;
211
+		$total_motfat = array_key_exists(4, $ancestorsDispGen2) ? $ancestorsDispGen2[4] : 0;
212
+		$total_motmot = array_key_exists(8, $ancestorsDispGen2) ? $ancestorsDispGen2[8] : 0;
213
+		$total_sha = array_key_exists(-1, $ancestorsDispGen2) ? $ancestorsDispGen2[-1] : 0;
214
+		$total = $total_fatfat + $total_fatmot + $total_motfat+ $total_motmot + $total_sha;
215 215
     
216
-        $chd = $this->arrayToExtendedEncoding(array(
217
-	    4095 * Functions::safeDivision($total_fatfat, $total), 
218
-            4095 * Functions::safeDivision($total_fatmot, $total),
219
-            4095 * Functions::safeDivision($total_sha, $total), 
220
-            4095 * Functions::safeDivision($total_motfat, $total),
221
-            4095 * Functions::safeDivision($total_motmot, $total)          
222
-        ));
223
-        $chart_title = I18N::translate('Known Sosa ancestors\' dispersion - G3');
224
-        $chl =
225
-            \Fisharebest\Webtrees\Functions\Functions::getRelationshipNameFromPath('fatfat') . ' - ' . I18N::percentage(Functions::safeDivision($total_fatfat, $total), 1) . '|' .
226
-            \Fisharebest\Webtrees\Functions\Functions::getRelationshipNameFromPath('fatmot') . ' - ' . I18N::percentage(Functions::safeDivision($total_fatmot, $total), 1) . '|' .
227
-            I18N::translate('Shared') . ' - ' . I18N::percentage(Functions::safeDivision($total_sha, $total), 1) . '|' .
228
-            \Fisharebest\Webtrees\Functions\Functions::getRelationshipNameFromPath('motfat') . ' - ' . I18N::percentage(Functions::safeDivision($total_motfat, $total), 1) . '|' .
229
-            \Fisharebest\Webtrees\Functions\Functions::getRelationshipNameFromPath('motmot') . ' - ' . I18N::percentage(Functions::safeDivision($total_motmot, $total), 1);
230
-         return "<img src=\"https://chart.googleapis.com/chart?cht=p&chp=1.5708&amp;chd=e:{$chd}&amp;chs={$size}&amp;chco={$color_fatfat},{$color_fatmot},{$color_shared},{$color_motfat},{$color_motmot}&amp;chf=bg,s,ffffff00&amp;chl={$chl}\" alt=\"" . $chart_title . "\" title=\"" . $chart_title . "\" />";
231
-    }
216
+		$chd = $this->arrayToExtendedEncoding(array(
217
+		4095 * Functions::safeDivision($total_fatfat, $total), 
218
+			4095 * Functions::safeDivision($total_fatmot, $total),
219
+			4095 * Functions::safeDivision($total_sha, $total), 
220
+			4095 * Functions::safeDivision($total_motfat, $total),
221
+			4095 * Functions::safeDivision($total_motmot, $total)          
222
+		));
223
+		$chart_title = I18N::translate('Known Sosa ancestors\' dispersion - G3');
224
+		$chl =
225
+			\Fisharebest\Webtrees\Functions\Functions::getRelationshipNameFromPath('fatfat') . ' - ' . I18N::percentage(Functions::safeDivision($total_fatfat, $total), 1) . '|' .
226
+			\Fisharebest\Webtrees\Functions\Functions::getRelationshipNameFromPath('fatmot') . ' - ' . I18N::percentage(Functions::safeDivision($total_fatmot, $total), 1) . '|' .
227
+			I18N::translate('Shared') . ' - ' . I18N::percentage(Functions::safeDivision($total_sha, $total), 1) . '|' .
228
+			\Fisharebest\Webtrees\Functions\Functions::getRelationshipNameFromPath('motfat') . ' - ' . I18N::percentage(Functions::safeDivision($total_motfat, $total), 1) . '|' .
229
+			\Fisharebest\Webtrees\Functions\Functions::getRelationshipNameFromPath('motmot') . ' - ' . I18N::percentage(Functions::safeDivision($total_motmot, $total), 1);
230
+		 return "<img src=\"https://chart.googleapis.com/chart?cht=p&chp=1.5708&amp;chd=e:{$chd}&amp;chs={$size}&amp;chco={$color_fatfat},{$color_fatmot},{$color_shared},{$color_motfat},{$color_motmot}&amp;chf=bg,s,ffffff00&amp;chl={$chl}\" alt=\"" . $chart_title . "\" title=\"" . $chart_title . "\" />";
231
+	}
232 232
 
233
-    /**
234
-     * Convert an array to Google Chart encoding
235
-     * @param arrat $a Array to encode
236
-     * @return string
237
-     */
238
-    private function arrayToExtendedEncoding($a) {
239
-        $xencoding = WT_GOOGLE_CHART_ENCODING;
233
+	/**
234
+	 * Convert an array to Google Chart encoding
235
+	 * @param arrat $a Array to encode
236
+	 * @return string
237
+	 */
238
+	private function arrayToExtendedEncoding($a) {
239
+		$xencoding = WT_GOOGLE_CHART_ENCODING;
240 240
     
241
-        $encoding = '';
242
-        foreach ($a as $value) {
243
-            if ($value < 0) {
244
-                $value = 0;
245
-            }
246
-            $first  = (int) ($value / 64);
247
-            $second = $value % 64;
248
-            $encoding .= $xencoding[(int) $first] . $xencoding[(int) $second];
249
-        }
241
+		$encoding = '';
242
+		foreach ($a as $value) {
243
+			if ($value < 0) {
244
+				$value = 0;
245
+			}
246
+			$first  = (int) ($value / 64);
247
+			$second = $value % 64;
248
+			$encoding .= $xencoding[(int) $first] . $xencoding[(int) $second];
249
+		}
250 250
     
251
-        return $encoding;
252
-    }
251
+		return $encoding;
252
+	}
253 253
     
254 254
     
255 255
 }
256 256
\ No newline at end of file
Please login to merge, or discard this patch.