Adds education verifications before merge.
[platal.git] / modules / fusionax.php
1 <?php
2 /***************************************************************************
3 * Copyright (C) 2003-2010 Polytechnique.org *
4 * http://opensource.polytechnique.org/ *
5 * *
6 * This program is free software; you can redistribute it and/or modify *
7 * it under the terms of the GNU General Public License as published by *
8 * the Free Software Foundation; either version 2 of the License, or *
9 * (at your option) any later version. *
10 * *
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 * *
16 * You should have received a copy of the GNU General Public License *
17 * along with this program; if not, write to the Free Software *
18 * Foundation, Inc., *
19 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *
20 ***************************************************************************/
21
22 /**
23 * Module to merge data from AX database: this will only be used once on
24 * production site and should be removed afterwards.
25 *
26 * Module to import data from another database of alumni that had
27 * different schemas. The organization that used this db is called AX
28 * hence the name of this module.
29 *
30 * Datas are stored in an export file.
31 */
32 class FusionAxModule extends PLModule
33 {
34 function handlers()
35 {
36 if (Platal::globals()->merge->state == 'pending') {
37 return array(
38 'fusionax' => $this->make_hook('index', AUTH_MDP, 'admin'),
39 'fusionax/import' => $this->make_hook('import', AUTH_MDP, 'admin'),
40 'fusionax/view' => $this->make_hook('view', AUTH_MDP, 'admin'),
41 'fusionax/ids' => $this->make_hook('ids', AUTH_MDP, 'admin'),
42 'fusionax/deceased' => $this->make_hook('deceased', AUTH_MDP, 'admin'),
43 'fusionax/promo' => $this->make_hook('promo', AUTH_MDP, 'admin'),
44 'fusionax/names' => $this->make_hook('names', AUTH_MDP, 'admin'),
45 'fusionax/edu' => $this->make_hook('edu', AUTH_MDP, 'admin')
46 );
47 } elseif (Platal::globals()->merge->state == 'done') {
48 return array(
49 'fusionax' => $this->make_hook('index', AUTH_MDP, 'admin,edit_directory'),
50 'fusionax/issues' => $this->make_hook('issues', AUTH_MDP, 'admin,edit_directory'),
51 'fusionax/issues/deathdate' => $this->make_hook('issues_deathdate', AUTH_MDP, 'admin,edit_directory'),
52 'fusionax/issues/promo' => $this->make_hook('issues_promo', AUTH_MDP, 'admin,edit_directory'),
53 );
54 }
55 }
56
57
58 function handler_index(&$page)
59 {
60 if (Platal::globals()->merge->state == 'pending') {
61 $page->changeTpl('fusionax/index.tpl');
62 } elseif (Platal::globals()->merge->state == 'done') {
63 $issueList = array(
64 'name' => 'noms',
65 'job' => 'emplois',
66 'address' => 'adresses',
67 'promo' => 'promotions',
68 'deathdate' => 'dates de décès',
69 'phone' => 'téléphones',
70 'education' => 'formations',
71 );
72 $issues = XDB::rawFetchOneAssoc("SELECT COUNT(*) AS total,
73 SUM(FIND_IN_SET('name', issues)) DIV 1 AS name,
74 SUM(FIND_IN_SET('job', issues)) DIV 2 AS job,
75 SUM(FIND_IN_SET('address', issues)) DIV 3 AS address,
76 SUM(FIND_IN_SET('promo', issues)) DIV 4 AS promo,
77 SUM(FIND_IN_SET('deathdate', issues)) DIV 5 AS deathdate,
78 SUM(FIND_IN_SET('phone', issues)) DIV 6 AS phone,
79 SUM(FIND_IN_SET('education', issues)) DIV 7 AS education
80 FROM profile_merge_issues
81 WHERE issues IS NOT NULL OR issues != ''");
82 $page->changeTpl('fusionax/issues.tpl');
83 $page->assign('issues', $issues);
84 $page->assign('issueList', $issueList);
85 }
86 }
87
88 /** Import de l'annuaire de l'AX depuis l'export situé dans le home de jacou */
89 function handler_import(&$page, $action = 'index', $file = '')
90 {
91 if ($action == 'index') {
92 $page->changeTpl('fusionax/import.tpl');
93 return;
94 }
95
96 // toutes les actions sont faites en ajax en utilisant jquery
97 header('Content-type: text/javascript; charset=utf-8');
98
99 // log des actions
100 $report = array();
101
102 $modulepath = realpath(dirname(__FILE__) . '/fusionax/') . '/';
103 $spoolpath = realpath(dirname(__FILE__) . '/../spool/fusionax/') . '/';
104
105 if ($action == 'launch') {
106 if ($file == '') {
107 $report[] = 'Nom de fichier non renseigné.';
108 } elseif (!file_exists(dirname(__FILE__) . '/../spool/fusionax/' . $file)) {
109 $report[] = 'Le fichier ne se situe pas au bon endroit.';
110 } else {
111 // séparation de l'archive en fichiers par tables
112 $file = $spoolpath . $file;
113 // Removes master and doctorate students
114 exec('grep -v "^[A-Z]\{2\}.[0-9]\{4\}[MD][0-9]\{3\}" ' . $file . ' > ' . $file . '.tmp');
115 exec('mv -f ' . $file . '.tmp ' . $file);
116 // Split export into specialised files
117 exec('grep "^AD" ' . $file . ' > ' . $spoolpath . 'Adresses.txt');
118 exec('grep "^AN" ' . $file . ' > ' . $spoolpath . 'Anciens.txt');
119 exec('grep "^FO" ' . $file . ' > ' . $spoolpath . 'Formations.txt');
120 exec('grep "^AC" ' . $file . ' > ' . $spoolpath . 'Activites.txt');
121 exec('grep "^EN" ' . $file . ' > ' . $spoolpath . 'Entreprises.txt');
122 exec($modulepath . 'formation.pl');
123 exec('mv -f ' . $spoolpath . 'Formations_out.txt ' . $spoolpath . 'Formations.txt');
124 $report[] = 'Fichier parsé.';
125 $report[] = 'Import dans la base en cours...';
126 $next = 'integrateSQL';
127 }
128 } elseif ($action == 'integrateSQL') {
129 // intégration des données dans la base MySQL
130 // liste des fichiers sql à exécuter
131 $filesSQL = array(
132 0 => 'Activites.sql',
133 1 => 'Adresses.sql',
134 2 => 'Anciens.sql',
135 3 => 'Formations.sql',
136 4 => 'Entreprises.sql'
137 );
138 if ($file != '') {
139 // récupère le contenu du fichier sql
140 $queries = explode(';', file_get_contents($modulepath . $filesSQL[$file]));
141 foreach ($queries as $q) {
142 if (trim($q)) {
143 // coupe le fichier en requêtes individuelles
144 if (substr($q, 0, 2) == '--') {
145 // affiche les commentaires dans le report
146 $lines = explode("\n", $q);
147 $l = $lines[0];
148 $report[] = addslashes($l);
149 }
150 // exécute la requête
151 XDB::execute(str_replace('{?}', $spoolpath, $q));
152 }
153 }
154 // trouve le prochain fichier à exécuter
155 $nextfile = $file + 1;
156 } else {
157 $nextfile = 0;
158 }
159 if ($nextfile > 4) {
160 // tous les fichiers ont été exécutés, on passe à l'étape suivante
161 $next = 'adds1920';
162 } else {
163 // on passe au fichier suivant
164 $next = 'integrateSQL/' . $nextfile;
165 }
166 } elseif ($action == 'adds1920') {
167 // Adds promotion 1920 from AX db.
168 $report[] = 'Ajout de la promotion 1920';
169 $res = XDB::iterator('SELECT prenom, Nom_complet, ax_id
170 FROM fusionax_anciens
171 WHERE promotion_etude = 1920;');
172
173 $nameTypes = DirEnum::getOptions(DirEnum::NAMETYPES);
174 $nameTypes = array_flip($nameTypes);
175 $eduSchools = DirEnum::getOptions(DirEnum::EDUSCHOOLS);
176 $eduSchools = array_flip($eduSchools);
177 $eduDegrees = DirEnum::getOptions(DirEnum::EDUDEGREES);
178 $eduDegrees = array_flip($eduDegrees);
179 $degreeid = $eduDegrees[Profile::DEGREE_X];
180 $entry_year = 1920;
181 $grad_year = 1923;
182 $promo = 'X1920';
183 $sex = PlUser::GENDER_MALE;
184 $xorgId = 19200000;
185 $type = 'x';
186
187 while (list($firstname, $lastname, $ax_id) = $res->next()) {
188 $hrid = self::getHrid($firstname, $lastname, $promo);
189 $res1 = XDB::query('SELECT COUNT(*)
190 FROM accounts
191 WHERE hruid = {?}', $hrid);
192 $res2 = XDB::query('SELECT COUNT(*)
193 FROM profiles
194 WHERE hrpid = {?}', $hrid);
195 if (is_null($hrid) || $res1->fetchOneCell() > 0 || $res2->fetchOneCell() > 0) {
196 $report[] = $ax_id . ' non ajouté';
197 }
198 $fullName = $firstname . ' ' . $lastname;
199 $directoryName = $lastname . ' ' . $firstname;
200 ++$xorgId;
201
202 XDB::execute('REPLACE INTO profiles (hrpid, xorg_id, ax_id, sex)
203 VALUES ({?}, {?}, {?}, {?})',
204 $hrid, $xorgId, $ax_id, $sex);
205 $pid = XDB::insertId();
206 XDB::execute('REPLACE INTO profile_name (pid, name, typeid)
207 VALUES ({?}, {?}, {?})',
208 $pid, $lastname, $nameTypes['name_ini']);
209 XDB::execute('REPLACE INTO profile_name (pid, name, typeid)
210 VALUES ({?}, {?}, {?})',
211 $pid, $firstname, $nameTypes['firstname_ini']);
212 XDB::execute('REPLACE INTO profile_display (pid, yourself, public_name, private_name,
213 directory_name, short_name, sort_name, promo)
214 VALUES ({?}, {?}, {?}, {?}, {?}, {?}, {?}, {?})',
215 $pid, $firstname, $fullName, $fullName, $directoryName, $fullName, $directoryName, $promo);
216 XDB::execute('REPLACE INTO profile_education (pid, eduid, degreeid, entry_year, grad_year, flags)
217 VALUES ({?}, {?}, {?}, {?}, {?}, {?})',
218 $pid, $eduSchools[Profile::EDU_X], $degreeid, $entry_year, $grad_year, 'primary');
219 XDB::execute('REPLACE INTO accounts (hruid, type, is_admin, state, full_name, directory_name, display_name, sex)
220 VALUES ({?}, {?}, {?}, {?}, {?}, {?}, {?})',
221 $hrid, $type, 0, 'active', $fullName, $directoryName, $lastname, $sex);
222 $uid = XDB::insertId();
223 XDB::execute('REPLACE INTO account_profiles (uid, pid, perms)
224 VALUES ({?}, {?}, {?})',
225 $uid, $pid, 'owner');
226 }
227 $report[] = 'Promo 1920 ajoutée.';
228 $next = 'view';
229 } elseif ($action == 'view') {
230 XDB::execute('CREATE OR REPLACE ALGORITHM=MERGE VIEW fusionax_xorg_anciens AS
231 SELECT p.pid, p.ax_id, pd.promo, pd.private_name, pd.public_name,
232 pd.sort_name, pd.short_name, pd.directory_name
233 FROM profiles AS p
234 INNER JOIN profile_display AS pd USING(pid)');
235 $next = 'clean';
236 } elseif ($action == 'clean') {
237 // nettoyage du fichier temporaire
238 //exec('rm -Rf ' . $spoolpath);
239 $report[] = 'Import finit.';
240 }
241 foreach($report as $t) {
242 // affiche les lignes de report
243 echo "$('#fusionax').append('" . $t . "<br/>');\n";
244 }
245 if (isset($next)) {
246 // lance le prochain script s'il y en a un
247 echo "$.getScript('fusionax/import/" . $next . "');";
248 }
249 // exit pour ne pas afficher la page template par défaut
250 exit;
251 }
252
253 function handler_view(&$page, $action = '')
254 {
255 $page->changeTpl('fusionax/view.tpl');
256 if ($action == 'create') {
257 XDB::execute('DROP VIEW IF EXISTS fusionax_deceased');
258 XDB::execute('CREATE VIEW fusionax_deceased AS
259 SELECT p.pid, a.ax_id, pd.private_name, pd.promo, p.deathdate AS deces_xorg, a.Date_deces AS deces_ax
260 FROM profiles AS p
261 INNER JOIN profile_display AS pd ON (p.pid = pd.pid)
262 INNER JOIN fusionax_anciens AS a ON (a.ax_id = p.ax_id)
263 WHERE p.deathdate != a.Date_deces');
264 XDB::execute('DROP VIEW IF EXISTS fusionax_promo');
265 XDB::execute('CREATE VIEW fusionax_promo AS
266 SELECT p.pid, p.ax_id, pd.private_name, pd.promo, pe.entry_year AS promo_etude_xorg,
267 f.promotion_etude AS promo_etude_ax, pe.grad_year AS promo_sortie_xorg
268 FROM profiles AS p
269 INNER JOIN profile_display AS pd ON (p.pid = pd.pid)
270 INNER JOIN profile_education AS pe ON (p.pid = pe.pid)
271 INNER JOIN fusionax_anciens AS f ON (p.ax_id = f.ax_id)
272 WHERE pd.promo != CONCAT(\'X\', f.promotion_etude)
273 AND !(f.promotion_etude = pe.entry_year + 1 AND pe.grad_year = pe.entry_year + 4)');
274 $page->trigSuccess('Les VIEW ont bien été créées.');
275 }
276 }
277
278 /* Mets à NULL le matricule_ax de ces camarades pour marquer le fait qu'ils ne figurent pas dans l'annuaire de l'AX */
279 private static function clear_wrong_in_xorg($pid)
280 {
281 $res = XDB::execute('UPDATE fusionax_xorg_anciens
282 SET ax_id = NULL
283 WHERE pid = {?}', $pid);
284 if (!$res) {
285 return 0;
286 }
287 return XDB::affectedRows() / 2;
288 }
289
290 /* Cherche les les anciens présents dans Xorg avec un matricule_ax ne correspondant à rien dans la base de l'AX
291 * (mises à part les promo 1921 et 1923 qui ne figurent pas dans les données de l'AX)*/
292 private static function find_wrong_in_xorg($limit = 10)
293 {
294 return XDB::iterator('SELECT u.promo, u.pid, u.private_name
295 FROM fusionax_xorg_anciens AS u
296 WHERE NOT EXISTS (SELECT *
297 FROM fusionax_anciens AS f
298 WHERE f.ax_id = u.ax_id)
299 AND u.ax_id IS NOT NULL AND promo != \'X1921\' AND promo != \'X1923\'');
300 }
301
302 /** Lier les identifiants d'un ancien dans les deux annuaires
303 * @param user_id identifiant dans l'annuaire X.org
304 * @param matricule_ax identifiant dans l'annuaire de l'AX
305 * @return 0 si la liaison a échoué, 1 sinon
306 */
307 private static function link_by_ids($pid, $ax_id)
308 {
309 $res = XDB::execute('UPDATE fusionax_import AS i
310 INNER JOIN fusionax_xorg_anciens AS u
311 SET u.ax_id = i.ax_id,
312 i.pid = u.pid,
313 i.date_match_id = NOW()
314 WHERE i.ax_id = {?} AND u.pid = {?}
315 AND (u.ax_id != {?} OR u.ax_id IS NULL
316 OR i.pid != {?} OR i.pid IS NULL)',
317 $ax_id, $pid, $ax_id, $pid);
318 if (!$res) {
319 return 0;
320 }
321 return XDB::affectedRows() / 2;
322 }
323
324 /** Recherche automatique d'anciens à lier entre les deux annuaires
325 * @param limit nombre d'anciens à trouver au max
326 * @param sure si true, ne trouve que des anciens qui sont quasi sûrs
327 * @return un XOrgDBIterator sur les entrées avec display_name, promo,
328 * pid, ax_id et display_name_ax
329 */
330 private static function find_easy_to_link($limit = 10, $sure = false)
331 {
332 $easy_to_link = XDB::iterator("
333 SELECT u.private_name, u.promo, u.pid, ax.ax_id,
334 CONCAT(ax.prenom, ' ', ax.nom_complet, ' (X', ax.promotion_etude, ')') AS display_name_ax,
335 COUNT(*) AS nbMatches
336 FROM fusionax_anciens AS ax
337 INNER JOIN fusionax_import AS i ON (i.ax_id = ax.ax_id AND i.pid IS NULL)
338 LEFT JOIN fusionax_xorg_anciens AS u ON (u.ax_id IS NULL
339 AND u.promo = CONCAT('X', ax.promotion_etude)
340 AND (CONCAT(ax.prenom, ' ', ax.nom_complet) = u.private_name
341 OR CONCAT(ax.prenom, ' ', ax.nom_complet) = u.public_name
342 OR CONCAT(ax.prenom, ' ', ax.nom_complet) = u.short_name))
343 GROUP BY u.pid
344 HAVING u.pid IS NOT NULL AND nbMatches = 1" . ($limit ? (' LIMIT ' . $limit) : ''));
345 if ($easy_to_link->total() > 0 || $sure) {
346 return $easy_to_link;
347 }
348 return XDB::iterator("
349 SELECT u.private_name, u.promo, u.pid, ax.ax_id,
350 CONCAT(ax.prenom, ' ', ax.nom_complet, ' (X', ax.promotion_etude, ')') AS display_name_ax,
351 COUNT(*) AS nbMatches
352 FROM fusionax_anciens AS ax
353 INNER JOIN fusionax_import AS i ON (i.ax_id = ax.ax_id AND i.pid IS NULL)
354 LEFT JOIN fusionax_xorg_anciens AS u ON (u.ax_id IS NULL
355 AND (CONCAT(ax.prenom, ' ', ax.nom_complet) = u.private_name
356 OR CONCAT(ax.prenom, ' ', ax.nom_complet) = u.public_name
357 OR CONCAT(ax.prenom, ' ', ax.nom_complet) = u.short_name)
358 AND u.promo < CONCAT('X', ax.promotion_etude + 2)
359 AND u.promo > CONCAT('X', ax.promotion_etude - 2))
360 GROUP BY u.pid
361 HAVING u.pid IS NOT NULL AND nbMatches = 1" . ($limit ? (' LIMIT ' . $limit) : ''));
362 }
363
364 /** Module de mise en correspondance les ids */
365 function handler_ids(&$page, $part = 'main', $pid = null, $ax_id = null)
366 {
367 $nbToLink = 100;
368 $page->assign('xorg_title', 'Polytechnique.org - Fusion - Mise en correspondance simple');
369
370 if ($part == 'missingInAX') {
371 // locate all persons from this database that are not in AX's
372 $page->changeTpl('fusionax/idsMissingInAx.tpl');
373 $missingInAX = XDB::iterator('SELECT promo, pid, private_name
374 FROM fusionax_xorg_anciens
375 WHERE ax_id IS NULL');
376 $page->assign('missingInAX', $missingInAX);
377 return;
378 }
379 if ($part == 'missingInXorg') {
380 // locate all persons from AX's database that are not here
381 $page->changeTpl('fusionax/idsMissingInXorg.tpl');
382 $missingInXorg = XDB::iterator("SELECT CONCAT(a.prenom, ' ', a.Nom_usuel) AS private_name,
383 a.promotion_etude AS promo, a.ax_id
384 FROM fusionax_import
385 INNER JOIN fusionax_anciens AS a USING (ax_id)
386 WHERE fusionax_import.pid IS NULL");
387 $page->assign('missingInXorg', $missingInXorg);
388 return;
389 }
390 if ($part == 'wrongInXorg') {
391 // locate all persons from Xorg database that have a bad AX id
392 $page->changeTpl('fusionax/idswrongInXorg.tpl');
393 $wrongInXorg = FusionAxModule::find_wrong_in_xorg($nbToLink);
394 $page->assign('wrongInXorg', $wrongInXorg);
395 return;
396 }
397 if ($part == 'cleanwronginxorg') {
398 $linksToDo = FusionAxModule::find_wrong_in_xorg($nbToLink);
399 while ($l = $linksToDo->next()) {
400 FusionAxModule::clear_wrong_in_xorg($l['pid']);
401 }
402 pl_redirect('fusionax/ids/wrongInXorg');
403 }
404 if ($part == 'lier') {
405 if (Post::has('user_id') && Post::has('matricule_ax')) {
406 FusionAxModule::link_by_ids(Post::i('pid'), Post::v('ax_id'));
407 }
408 }
409 if ($part == 'link') {
410 FusionAxModule::link_by_ids($pid, $ax_id);
411 exit;
412 }
413 if ($part == 'linknext') {
414 $linksToDo = FusionAxModule::find_easy_to_link($nbToLink);
415 while ($l = $linksToDo->next()) {
416 FusionAxModule::link_by_ids($l['pid'], $l['ax_id']);
417 }
418 pl_redirect('fusionax/ids#autolink');
419 }
420 if ($part == 'linkall') {
421 $linksToDo = FusionAxModule::find_easy_to_link(0);
422 while ($l = $linksToDo->next()) {
423 FusionAxModule::link_by_ids($l['pid'], $l['ax_id']);
424 }
425 }
426 {
427 $page->changeTpl('fusionax/ids.tpl');
428 $missingInAX = XDB::query('SELECT COUNT(*)
429 FROM fusionax_xorg_anciens
430 WHERE ax_id IS NULL');
431 if ($missingInAX) {
432 $page->assign('nbMissingInAX', $missingInAX->fetchOneCell());
433 }
434 $missingInXorg = XDB::query('SELECT COUNT(*)
435 FROM fusionax_import
436 WHERE pid IS NULL');
437 if ($missingInXorg) {
438 $page->assign('nbMissingInXorg', $missingInXorg->fetchOneCell());
439 }
440 $wrongInXorg = FusionAxModule::find_wrong_in_xorg($nbToLink);
441 if ($wrongInXorg->total() > 0) {
442 $page->assign('wrongInXorg', $wrongInXorg->total());
443 }
444 $easyToLink = FusionAxModule::find_easy_to_link($nbToLink);
445 if ($easyToLink->total() > 0) {
446 $page->assign('nbMatch', $easyToLink->total());
447 $page->assign('easyToLink', $easyToLink);
448 }
449 }
450 }
451
452 function handler_deceased(&$page, $action = '')
453 {
454 if ($action == 'updateXorg') {
455 XDB::execute('UPDATE fusionax_deceased
456 SET deces_xorg = deces_ax
457 WHERE deces_xorg = "0000-00-00"');
458 }
459 if ($action == 'updateAX') {
460 XDB::execute('UPDATE fusionax_deceased
461 SET deces_ax = deces_xorg
462 WHERE deces_ax = "0000-00-00"');
463 }
464 if ($action == 'update') {
465 if (Post::has('pid') && Post::has('date')) {
466 XDB::execute('UPDATE fusionax_deceased
467 SET deces_ax = {?}, deces_xorg = {?}
468 WHERE pid = {?}',
469 Post::v('date'), Post::v('date'), Post::i('pid'));
470 }
471 }
472 $page->changeTpl('fusionax/deceased.tpl');
473 // deceased
474 $deceasedErrorsSql = XDB::query('SELECT COUNT(*) FROM fusionax_deceased');
475 $page->assign('deceasedErrors', $deceasedErrorsSql->fetchOneCell());
476 $res = XDB::iterator('SELECT pid, ax_id, promo, private_name, deces_ax
477 FROM fusionax_deceased
478 WHERE deces_xorg = "0000-00-00"
479 LIMIT 10');
480 $page->assign('nbDeceasedMissingInXorg', $res->total());
481 $page->assign('deceasedMissingInXorg', $res);
482 $res = XDB::iterator('SELECT pid, ax_id, promo, private_name, deces_xorg
483 FROM fusionax_deceased
484 WHERE deces_ax = "0000-00-00"
485 LIMIT 10');
486 $page->assign('nbDeceasedMissingInAX', $res->total());
487 $page->assign('deceasedMissingInAX', $res);
488 $res = XDB::iterator('SELECT pid, ax_id, promo, private_name, deces_xorg, deces_ax
489 FROM fusionax_deceased
490 WHERE deces_xorg != "0000-00-00" AND deces_ax != "0000-00-00"');
491 $page->assign('nbDeceasedDifferent', $res->total());
492 $page->assign('deceasedDifferent', $res);
493 }
494
495 function handler_promo(&$page, $action = '')
496 {
497 $page->changeTpl('fusionax/promo.tpl');
498 $res = XDB::iterator('SELECT pid, private_name, promo_etude_xorg, promo_sortie_xorg, promo_etude_ax, promo
499 FROM fusionax_promo
500 WHERE !(promo_etude_ax + 1 = promo_etude_xorg AND promo_etude_xorg + 3 = promo_sortie_xorg)
501 AND !(promo_etude_ax + 1 = promo_etude_xorg AND promo_etude_xorg + 4 = promo_sortie_xorg)
502 AND !(promo_etude_ax = promo_etude_xorg + 1)
503 ORDER BY promo_etude_xorg');
504 $nbMissmatchingPromos = $res->total();
505 $page->assign('nbMissmatchingPromos', $res->total());
506 $page->assign('missmatchingPromos', $res);
507
508 $res = XDB::iterator('SELECT pid, private_name, promo_etude_xorg, promo_sortie_xorg, promo_etude_ax, promo
509 FROM fusionax_promo
510 WHERE promo_etude_ax = promo_etude_xorg + 1
511 ORDER BY promo_etude_xorg');
512 $nbMissmatchingPromos += $res->total();
513 $page->assign('nbMissmatchingPromos1', $res->total());
514 $page->assign('missmatchingPromos1', $res);
515
516 $res = XDB::iterator('SELECT pid, private_name, promo_etude_xorg, promo_sortie_xorg, promo_etude_ax, promo
517 FROM fusionax_promo
518 WHERE promo_etude_ax + 1 = promo_etude_xorg AND promo_etude_xorg + 3 = promo_sortie_xorg
519 ORDER BY promo_etude_xorg');
520 $nbMissmatchingPromos += $res->total();
521 $page->assign('nbMissmatchingPromos2', $res->total());
522 $page->assign('missmatchingPromos2', $res);
523
524 $res = XDB::iterator('SELECT pid, private_name, promo_etude_xorg, promo_sortie_xorg, promo_etude_ax, promo
525 FROM fusionax_promo
526 WHERE promo_etude_ax + 1 = promo_etude_xorg AND promo_etude_xorg + 4 = promo_sortie_xorg
527 ORDER BY promo_etude_xorg');
528 $nbMissmatchingPromos += $res->total();
529 $page->assign('nbMissmatchingPromos3', $res->total());
530 $page->assign('missmatchingPromos3', $res);
531
532 $page->assign('nbMissmatchingPromosTotal', $nbMissmatchingPromos);
533 }
534
535 function handler_names(&$page, $action = '')
536 {
537 $page->changeTpl('fusionax/names.tpl');
538
539 $res = XDB::query('SELECT COUNT(*)
540 FROM fusionax_anciens AS f
541 INNER JOIN profiles AS p ON (f.ax_id = p.ax_id)');
542 $page->assign('total', $res->fetchOneCell());
543
544 // To be checked:
545 // | lastname | 1 |
546 // | lastname_marital | 2 |
547 // | lastname_ordinary | 3 |
548 // | firstname | 4 |
549 // | firstname_ordinary | 7 |
550 // | firstname_other | 8 |
551 // | name_other | 9 |
552 // | name_ini | 10 |
553 // | firstname_ini | 11 |
554 $res = XDB::query("SELECT COUNT(*)
555 FROM fusionax_anciens AS f
556 INNER JOIN profiles AS p ON (f.ax_id = p.ax_id)
557 LEFT JOIN profile_name AS pnp ON (p.pid = pnp.pid AND pnp.typeid = 1)
558 LEFT JOIN profile_name AS pnm ON (p.pid = pnm.pid AND pnm.typeid = 2)
559 LEFT JOIN profile_name AS pno ON (p.pid = pno.pid AND pno.typeid = 3)
560 LEFT JOIN profile_name AS pne ON (p.pid = pne.pid AND pne.typeid = 9)
561 LEFT JOIN profile_name AS pni ON (p.pid = pni.pid AND pni.typeid = 10)
562 WHERE IF(f.partic_patro, CONCAT(f.partic_patro, CONCAT(' ', f.Nom_patronymique)), f.Nom_patronymique) NOT IN (pnp.name, pno.name, pnm.name, pne.name, pni.name)
563 OR IF(f.partic_nom, CONCAT(f.partic_nom, CONCAT(' ', f.Nom_usuel)), f.Nom_usuel) NOT IN (pnp.name, pno.name, pnm.name, pne.name, pni.name)
564 OR f.Nom_complet NOT IN (pnp.name, pno.name, pnm.name, pne.name, pni.name)");
565 $page->assign('lastnameIssues', $res->fetchOneCell());
566
567 $res = XDB::query('SELECT COUNT(*)
568 FROM fusionax_anciens AS f
569 INNER JOIN profiles AS p ON (f.ax_id = p.ax_id)
570 LEFT JOIN profile_name AS pnf ON (p.pid = pnf.pid AND pnf.typeid = 4)
571 LEFT JOIN profile_name AS pno ON (p.pid = pno.pid AND pno.typeid = 7)
572 LEFT JOIN profile_name AS pne ON (p.pid = pne.pid AND pne.typeid = 8)
573 LEFT JOIN profile_name AS pni ON (p.pid = pni.pid AND pni.typeid = 11)
574 WHERE f.prenom NOT IN (pnf.name, pno.name, pne.name, pni.name)');
575 $page->assign('firstnameIssues', $res->fetchOneCell());
576
577 }
578
579 function handler_edu(&$page, $action = '')
580 {
581 $page->changeTpl('fusionax/education.tpl');
582
583 $missingEducation = XDB::rawIterator("SELECT DISTINCT(f.Intitule_diplome)
584 FROM fusionax_formations AS f
585 WHERE f.Intitule_diplome != '' AND NOT EXISTS (SELECT *
586 FROM profile_education_enum AS e
587 WHERE f.Intitule_diplome = e.name)");
588 $missingDegree = XDB::rawIterator("SELECT DISTINCT(f.Intitule_formation)
589 FROM fusionax_formations AS f
590 WHERE f.Intitule_formation != '' AND NOT EXISTS (SELECT *
591 FROM profile_education_degree_enum AS e
592 WHERE f.Intitule_formation = e.abbreviation)");
593 $missingCouple = XDB::rawIterator("SELECT DISTINCT(f.Intitule_diplome) AS edu, f.Intitule_formation AS degree, ee.id AS eduid, de.id AS degreeid
594 FROM fusionax_formations AS f
595 INNER JOIN profile_education_enum AS ee ON (f.Intitule_diplome = ee.name)
596 INNER JOIN profile_education_degree_enum AS de ON (f.Intitule_formation = de.abbreviation)
597 WHERE f.Intitule_diplome != '' AND f.Intitule_formation != ''
598 AND NOT EXISTS (SELECT *
599 FROM profile_education_degree AS d
600 WHERE ee.id = d.eduid AND de.id = d.degreeid)");
601
602 $page->assign('missingEducation', $missingEducation);
603 $page->assign('missingDegree', $missingDegree);
604 $page->assign('missingCouple', $missingCouple);
605 $page->assign('missingEducationCount', $missingEducation->total());
606 $page->assign('missingDegreeCount', $missingDegree->total());
607 $page->assign('missingCoupleCount', $missingCouple->total());
608 }
609
610 function handler_issues_deathdate(&$page, $action = '')
611 {
612 $page->changeTpl('fusionax/deathdate_issues.tpl');
613 if ($action == 'edit') {
614 S::assert_xsrf_token();
615
616 $issues = XDB::rawIterRow('SELECT p.pid, pd.directory_name, pd.promo, pm.deathdate_ax, p.deathdate
617 FROM profile_merge_issues AS pm
618 INNER JOIN profiles AS p ON (pm.pid = p.pid)
619 INNER JOIN profile_display AS pd ON (pd.pid = p.pid)
620 WHERE FIND_IN_SET(\'deathdate\', pm.issues)
621 ORDER BY pd.directory_name');
622 while (list($pid, $name, $promo, $deathAX, $deathXorg) = $issues->next()) {
623 $choiceAX = Post::has('AX_' . $pid);
624 $choiceXorg = Post::has('XORG_' . $pid);
625 if (!($choiceAX || $choiceXorg)) {
626 continue;
627 }
628
629 if ($choiceAX) {
630 XDB::execute('UPDATE profiles AS p
631 INNER JOIN profile_merge_issues AS pm ON (pm.pid = p.pid)
632 SET p.deathdate = pm.deathdate_ax, p.deathdate_rec = NOW()
633 WHERE p.pid = {?}', $pid);
634 }
635 XDB::execute("UPDATE profile_merge_issues
636 SET issues = REPLACE(issues, 'deathdate', '')
637 WHERE pid = {?}", $pid());
638 $page->trigSuccess("La date de décès de $name ($promo) a bien été corrigée.");
639 }
640 }
641
642 $issues = XDB::rawFetchAllAssoc('SELECT p.pid, p.hrpid, pd.directory_name, pd.promo, pm.deathdate_ax, p.deathdate
643 FROM profile_merge_issues AS pm
644 INNER JOIN profiles AS p ON (pm.pid = p.pid)
645 INNER JOIN profile_display AS pd ON (pd.pid = p.pid)
646 WHERE FIND_IN_SET(\'deathdate\', pm.issues)
647 ORDER BY pd.directory_name');
648 $page->assign('issues', $issues);
649 $page->assign('total', count($issues));
650 }
651
652 function handler_issues_promo(&$page, $action = '')
653 {
654 $page->changeTpl('fusionax/promo_issues.tpl');
655 if ($action == 'edit') {
656 S::assert_xsrf_token();
657
658 $issues = XDB::rawIterRow('SELECT p.pid, pd.directory_name, pd.promo, pm.entry_year_ax, pe.entry_year, pe.grad_year
659 FROM profile_merge_issues AS pm
660 INNER JOIN profiles AS p ON (pm.pid = p.pid)
661 INNER JOIN profile_display AS pd ON (pd.pid = p.pid)
662 INNER JOIN profile_education AS pe ON (pe.pid = p.pid AND FIND_IN_SET(\'primary\', pe.flags))
663 WHERE FIND_IN_SET(\'promo\', pm.issues)
664 ORDER BY pd.directory_name');
665 while (list($pid, $name, $promo, $deathAX, $deathXorgEntry, $deathXorgGrad) = $issues->next()) {
666 $choiceXorg = Post::has('XORG_' . $pid);
667 if (!(Post::has('display_' . $pid) && Post::has('entry_' . $pid) && Post::has('grad_' . $pid))) {
668 continue;
669 }
670
671 $display = Post::i('display_' . $pid);
672 $entry = Post::i('entry_' . $pid);
673 $grad = Post::i('grad_' . $pid);
674 if (!(($grad <= $entry + 5 && $grad >= $entry + 3) && ($display >= $entry && $display <= $grad - 3))) {
675 $page->trigError("La promotion de $name n'a pas été corrigée.");
676 continue;
677 }
678 XDB::execute('UPDATE profile_display
679 SET promo = {?}
680 WHERE pid = {?}', 'X' . $display, $pid);
681 XDB::execute('UPDATE profile_education
682 SET entry_year = {?}, grad_year = {?}
683 WHERE pid = {?} AND FIND_IN_SET(\'primary\', flags)', $entry, $grad, $pid);
684 $page->trigSuccess("La promotion de $name a bien été corrigée.");
685 }
686 }
687
688 $issues = XDB::rawFetchAllAssoc('SELECT p.pid, p.hrpid, pd.directory_name, pd.promo, pm.entry_year_ax, pe.entry_year, pe.grad_year
689 FROM profile_merge_issues AS pm
690 INNER JOIN profiles AS p ON (pm.pid = p.pid)
691 INNER JOIN profile_display AS pd ON (pd.pid = p.pid)
692 INNER JOIN profile_education AS pe ON (pe.pid = p.pid AND FIND_IN_SET(\'primary\', pe.flags))
693 WHERE FIND_IN_SET(\'promo\', pm.issues)
694 ORDER BY pd.directory_name');
695 $page->assign('issues', $issues);
696 $page->assign('total', count($issues));
697 }
698
699 function handler_issues(&$page, $action = '')
700 {
701 static $issueList = array(
702 'name' => 'noms',
703 'phone' => 'téléphones',
704 'education' => 'formations',
705 'address' => 'adresses',
706 'job' => 'emplois'
707 );
708 static $typeList = array(
709 'name' => 'general',
710 'phone' => 'general',
711 'education' => 'general',
712 'address' => 'adresses',
713 'job' => 'emploi'
714 );
715
716 if (!array_key_exists($action, $issueList)) {
717 pl_redirect('fusionax');
718 } else {
719 $total = XDB::fetchOneCell('SELECT COUNT(*)
720 FROM profile_merge_issues
721 WHERE FIND_IN_SET({?}, issues)', $action);
722 if ($total == 0) {
723 pl_redirect('fusionax');
724 }
725
726 $issues = XDB::fetchAllAssoc('SELECT p.hrpid, pd.directory_name, pd.promo
727 FROM profile_merge_issues AS pm
728 INNER JOIN profiles AS p ON (pm.pid = p.pid)
729 INNER JOIN profile_display AS pd ON (pd.pid = p.pid)
730 WHERE FIND_IN_SET({?}, pm.issues)
731 ORDER BY pd.directory_name
732 LIMIT 100', $action);
733
734 $page->changeTpl('fusionax/other_issues.tpl');
735 $page->assign('issues', $issues);
736 $page->assign('issue', $issueList[$action]);
737 $page->assign('type', $typeList[$action]);
738 $page->assign('total', $total);
739 }
740 }
741 }
742
743 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
744 ?>