Fix AX ID updating in admin/add_accounts page
authorNicolas Iooss <nicolas.iooss_git@polytechnique.org>
Mon, 26 May 2014 22:42:32 +0000 (00:42 +0200)
committerNicolas Iooss <nicolas.iooss_git@polytechnique.org>
Sun, 1 Jun 2014 19:56:32 +0000 (21:56 +0200)
This page creates new accounts instead of using existing ones when
updating AX IDs.  Fix this by looking for profiles in the database.

modules/admin.php
templates/admin/add_accounts.tpl

index a220bcc..9cf1e7e 100644 (file)
@@ -971,12 +971,37 @@ class AdminModule extends PLModule
             } else if (Env::t('add_type') == 'ax_id') {
                 $type = 'x';
                 foreach ($lines as $line) {
-                    if ($infos = self::formatNewUser($page, $line, $separator, $promotion, 3)) {
-                        XDB::execute('UPDATE  profiles
-                                         SET  ax_id = {?}
-                                       WHERE  hrpid = {?}',
-                                     $infos[2], $infos['hrid']);
+                    $infos = explode($separator, $line);
+                    if (sizeof($infos) > 3 || sizeof($infos) < 2) {
+                        $page->trigError("La ligne $line n'a pas été ajoutée : mauvais nombre de champs.");
+                        continue;
                     }
+                    $infos = array_map('trim', $infos);
+                    if (sizeof($infos) == 3) {
+                        // Get human readable ID with first name and last name
+                        $hrid = User::makeHrid($infos[1], $infos[0], $promotion);
+                        $user = User::getSilent($hrid);
+                    } else {
+                        // The first column is the hrid, possibly without the promotion
+                        $user = User::getSilent($infos[0] . '.' . $promotion);
+                        if (is_null($user)) {
+                            $user = User::getSilent($infos[0]);
+                        }
+                    }
+                    if (is_null($user)) {
+                        $page->trigError("La ligne $line n'a pas été ajoutée : aucun compte trouvé.");
+                        continue;
+                    }
+                    $profile = $user->profile();
+                    if ($profile->ax_id) {
+                        $page->trigError("Le profil " . $profile->hrpid . " a déjà l'ID AX " . $profile->ax_id);
+                        continue;
+                    }
+                    XDB::execute('UPDATE  profiles
+                                     SET  ax_id = {?}
+                                   WHERE  pid = {?}',
+                                 $infos[2], $profile->id());
+
                 }
             }
 
index ef20ea6..d72ec79 100644 (file)
 <br />
 <table class="bicol">
   <tr>
-    <td>Nom</td>
-    <td>Prénom</td>
+    <td>Nom Prénom ou Identifiant (hrid)</td>
     <td>Matricule AX</td>
   </tr>
   <tr>
-    <td colspan="4"><textarea name="people" rows="20" cols="80"></textarea></td>
+    <td colspan="2"><textarea name="people" rows="20" cols="80"></textarea></td>
   </tr>
 </table>
 {/if}