Enables addition of secondary educations.
authorStéphane Jacob <sj@m4x.org>
Tue, 31 May 2011 09:31:11 +0000 (11:31 +0200)
committerStéphane Jacob <sj@m4x.org>
Tue, 31 May 2011 14:07:28 +0000 (16:07 +0200)
Signed-off-by: Stéphane Jacob <sj@m4x.org>
classes/profile.php
modules/admin.php
templates/admin/add_secondary_edu.tpl [new file with mode: 0644]
templates/admin/index.tpl

index 988c1c6..d0435f5 100644 (file)
@@ -273,6 +273,20 @@ class Profile implements PlExportable
         }
     }
 
+    public static function educationDuration($education)
+    {
+        switch ($education) {
+          case self::DEGREE_X:
+            return 3;
+          case self::DEGREE_M:
+            return 2;
+          case self::DEGREE_D:
+            return 3;
+          default:
+            return 0;
+        }
+    }
+
     /** Number of years between the promotion year until the
      * graduation year. In standard schools it's 0, but for
      * Polytechnique the promo year is the entry year.
index 2318b3f..9778979 100644 (file)
@@ -54,7 +54,8 @@ class AdminModule extends PLModule
             'admin/xnet_without_group'     => $this->make_hook('xnet_without_group',     AUTH_MDP,    'admin'),
             'admin/jobs'                   => $this->make_hook('jobs',                   AUTH_MDP,    'admin,edit_directory'),
             'admin/profile'                => $this->make_hook('profile',                AUTH_MDP,    'admin,edit_directory'),
-            'admin/phd'                    => $this->make_hook('phd',                    AUTH_MDP,    'admin')
+            'admin/phd'                    => $this->make_hook('phd',                    AUTH_MDP,    'admin'),
+            'admin/add_secondary_edu'      => $this->make_hook('add_secondary_edu',      AUTH_MDP,    'admin')
         );
     }
 
@@ -1885,6 +1886,115 @@ class AdminModule extends PLModule
         $page->assign('list', $list);
         $page->assign('promo', $promo);
     }
+
+    function handler_add_secondary_edu($page)
+    {
+        $page->changeTpl('admin/add_secondary_edu.tpl');
+
+        if (!(Post::has('verify') || Post::has('add'))) {
+            return;
+        } elseif (!Post::has('people')) {
+            $page->trigWarning("Aucune information n'a été fournie.");
+            return;
+        }
+
+        require_once 'name.func.inc.php';
+        $lines = explode("\n", Post::t('people'));
+        $separator = Post::t('separator');
+        $degree = Post::v('degree');
+        $promotion = Post::i('promotion');
+        $schoolsList = array_flip(DirEnum::getOptions(DirEnum::EDUSCHOOLS));
+        $degreesList = array_flip(DirEnum::getOptions(DirEnum::EDUDEGREES));
+        $edu_id = $schoolsList[Profile::EDU_X];
+        $degree_id = $degreesList[$degree];
+
+        $res = array(
+            'incomplete' => array(),
+            'empty'      => array(),
+            'multiple'   => array(),
+            'already'    => array(),
+            'new'        => array()
+        );
+        $old_pids = array();
+        $new_pids = array();
+        foreach ($lines as $line) {
+            $line = trim($line);
+            $line_array = explode($separator, $line);
+            array_walk($line_array, 'trim');
+            if (count($line_array) != 3) {
+                $page->trigError("La ligne « $line » est incomplète.");
+                $res['incomplete'][] = $line;
+                continue;
+            }
+            $cond = new PFC_And(new UFC_NameTokens(split_name_for_search($line_array[0]), array(), false, false, Profile::LASTNAME));
+            $cond->addChild(new UFC_NameTokens(split_name_for_search($line_array[1]), array(), false, false, Profile::FIRSTNAME));
+            $cond->addChild(new UFC_Promo('=', UserFilter::DISPLAY, $line_array[2]));
+            $uf = new UserFilter($cond);
+            $pid = $uf->getPIDs();
+            $count = count($pid);
+            if ($count == 0) {
+                $page->trigError("La ligne « $line » ne correspond à aucun profil existant.");
+                $res['empty'][] = $line;
+                continue;
+            } elseif ($count > 1) {
+                $page->trigError("La ligne « $line » correspond à plusieurs profils existant.");
+                $res['multiple'][] = $line;
+                continue;
+            } else {
+                $count = XDB::fetchOneCell('SELECT  COUNT(*) AS count
+                                              FROM  profile_education
+                                             WHERE  pid = {?} AND eduid = {?} AND degreeid = {?}',
+                                      $pid, $edu_id, $degree_id);
+                if ($count == 1) {
+                    $res['already'][] = $line;
+                    $old_pids[] = $pid[0];
+                } else {
+                    $res['new'][] = $line;
+                    $new_pids[] = $pid[0];
+                }
+            }
+        }
+
+        $display = array();
+        foreach ($res as $type => $res_type) {
+            if (count($res_type) > 0) {
+                $display = array_merge($display, array('--------------------' . $type . ':'), $res_type);
+            }
+        }
+        $page->assign('people', implode("\n", $display));
+        $page->assign('promotion', $promotion);
+        $page->assign('degree', $degree);
+
+        if (Post::has('add')) {
+            $entry_year = $promotion - Profile::educationDuration($degree);
+
+            if (Post::b('force_addition')) {
+                $pids = array_unique(array_merge($old_pids, $new_pids));
+            } else {
+                $pids = array_unique($new_pids);
+
+                // Updates years.
+                XDB::execute('UPDATE  profile_education
+                                 SET  entry_year = {?}, grad_year = {?}, promo_year = {?}
+                               WHERE  pid IN {?} AND eduid = {?} AND degreeid = {?}',
+                             $entry_year, $promotion, $promotion, $old_pids, $edu_id, $degree_id);
+            }
+
+            // Precomputes values common to all users.
+            $select = XDB::format('MAX(id) + 1, pid, {?}, {?}, {?}, {?}, {?}, \'secondary\'',
+                                  $edu_id, $degree_id, $entry_year, $promotion, $promotion );
+            XDB::startTransaction();
+            foreach ($pids as $pid) {
+                XDB::execute('INSERT INTO  profile_education (id, pid, eduid, degreeid, entry_year, grad_year, promo_year, flags)
+                                   SELECT  ' . $select . '
+                                     FROM  profile_education
+                                    WHERE  pid = {?}',
+                             $pid);
+            }
+            XDB::commit();
+        }
+
+    }
 }
 
 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
diff --git a/templates/admin/add_secondary_edu.tpl b/templates/admin/add_secondary_edu.tpl
new file mode 100644 (file)
index 0000000..7616d46
--- /dev/null
@@ -0,0 +1,72 @@
+{**************************************************************************}
+{*                                                                        *}
+{*  Copyright (C) 2003-2011 Polytechnique.org                             *}
+{*  http://opensource.polytechnique.org/                                  *}
+{*                                                                        *}
+{*  This program is free software; you can redistribute it and/or modify  *}
+{*  it under the terms of the GNU General Public License as published by  *}
+{*  the Free Software Foundation; either version 2 of the License, or     *}
+{*  (at your option) any later version.                                   *}
+{*                                                                        *}
+{*  This program is distributed in the hope that it will be useful,       *}
+{*  but WITHOUT ANY WARRANTY; without even the implied warranty of        *}
+{*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *}
+{*  GNU General Public License for more details.                          *}
+{*                                                                        *}
+{*  You should have received a copy of the GNU General Public License     *}
+{*  along with this program; if not, write to the Free Software           *}
+{*  Foundation, Inc.,                                                     *}
+{*  59 Temple Place, Suite 330, Boston, MA  02111-1307  USA               *}
+{*                                                                        *}
+{**************************************************************************}
+
+<h1>Ajout de formations secondaires</h1>
+
+<form action="{$platal->pl_self()}" method="post">
+<table class="tinybicol" style="margin-bottom: 1em">
+  <tr>
+    <td>
+      <strong>Promotion&nbsp;:</strong>
+    </td>
+    <td>
+      <input type="text" name="promotion" size="4" maxlength="4" {if t($promotion)}value="{$promotion}"{/if} />
+    </td>
+  </tr>
+  <tr>
+    <td>
+      <strong>Formation&nbsp;:</strong>
+    </td>
+    <td>
+      <label><input type="radio" name="degree" value="Master" {if !t($degree) || $degree eq "Master"}checked="checked"{/if} /> master</label> -
+      <label><input type="radio" name="degree" value="Doctorat" {if t($degree) && $degree eq "Doctorat"}checked="checked"{/if} /> doctorat</label>
+    </td>
+  </tr>
+  <tr>
+    <td>
+      <strong>Forcer l'ajout&nbsp;:</strong><br /><small>(en cas de formation du même niveau préexistante)</small>
+    </td>
+    <td>
+      <input type="checkbox" name="force_addition" />
+    </td>
+  </tr>
+</table>
+
+<table class="bicol">
+  <tr>
+    <td>Nom</td>
+    <td>Prénom</td>
+    <td>Promotion principale</td>
+  </tr>
+  <tr>
+    <td colspan="3"><textarea name="people" rows="20" cols="80">{if t($people)}{$people}{/if}</textarea></td>
+  </tr>
+</table>
+
+<p class="center">
+  <strong>Séparateur&nbsp;:</strong>
+  <input type="text" name="separator" value=";" size="1" maxlength="1" /><br /><br />
+  <input type="submit" name="verify" value="Vérifier" />&nbsp;&nbsp;&nbsp;&nbsp;<input type="submit" name="add" value="Ajouter" />
+</p>
+</form>
+
+{* vim:set et sws=2 sts=2 sw=2 enc=utf-8: *}
index 42d066d..7132a26 100644 (file)
     </td>
   </tr>
   <tr class="impair">
-    <td class="titre">Promotions</td>
+    <td class="titre">Formations</td>
     <td>
       <a href="admin/phd">Doctorants</a>
+      &nbsp;&nbsp;|&nbsp;&nbsp;
+      <a href="admin/add_secondary_edu">Ajout de formation</a>
     </td>
   </tr>