More robust $profile->yearpromo(): it does relies on displayed promotion as the lates...
authorStéphane Jacob <sj@m4x.org>
Wed, 20 Apr 2011 08:29:35 +0000 (10:29 +0200)
committerStéphane Jacob <sj@m4x.org>
Thu, 21 Apr 2011 16:33:53 +0000 (18:33 +0200)
Signed-off-by: Stéphane Jacob <sj@m4x.org>
classes/profile.php
modules/admin.php
modules/profile/general.inc.php
modules/register.php
upgrade/1.1.1/07_yearpromo.sql [new file with mode: 0644]

index 73a9fff..5e113a4 100644 (file)
@@ -188,7 +188,7 @@ class Profile implements PlExportable
 
     public function yearpromo()
     {
-        return intval(substr($this->promo, 1, 4));
+        return $this->promo_year;
     }
 
     /** Check if user is an orange (associated with several promos)
@@ -880,7 +880,7 @@ class Profile implements PlExportable
                                      IF ({?}, p.cv, NULL) AS cv, p.medals_pub, p.alias_pub, p.email_directory,
                                      p.last_change, p.nationality1, p.nationality2, p.nationality3,
                                      IF (p.freetext_pub IN {?}, p.freetext, NULL) AS freetext,
-                                     pe.entry_year, pe.grad_year,
+                                     pe.entry_year, pe.grad_year, pe.promo_year,
                                      IF ({?}, pse.text, NULL) AS section,
                                      pn_f.name AS firstname, pn_l.name AS lastname,
                                      IF( {?}, pn_n.name, NULL) AS nickname,
index 34c3f44..0ce61e7 100644 (file)
@@ -890,9 +890,9 @@ class AdminModule extends PLModule
                                                                         directory_name, short_name, sort_name, promo)
                                                VALUES  ({?}, {?}, {?}, {?}, {?}, {?}, {?}, {?})',
                                          $pid, $infos[1], $fullName, $fullName, $directoryName, $fullName, $directoryName, $promo);
-                            XDB::execute('INSERT INTO  profile_education (id, pid, eduid, degreeid, entry_year, grad_year, flags)
-                                               VALUES  (100, {?}, {?}, {?}, {?}, {?}, \'primary\')',
-                                         $pid, $eduSchools[Profile::EDU_X], $degreeid, $entry_year, $grad_year);
+                            XDB::execute('INSERT INTO  profile_education (id, pid, eduid, degreeid, entry_year, grad_year, promo_year, flags)
+                                               VALUES  (100, {?}, {?}, {?}, {?}, {?}, {?}, \'primary\')',
+                                         $pid, $eduSchools[Profile::EDU_X], $degreeid, $entry_year, $grad_year, $promotion);
                             XDB::execute('INSERT INTO  accounts (hruid, type, is_admin, state, full_name, directory_name,
                                                                  display_name, lastname, firstname, sex)
                                                VALUES  ({?}, {?}, {?}, {?}, {?}, {?}, {?}, {?}, {?}, {?})',
index 2051f16..85c3dd5 100644 (file)
@@ -490,10 +490,7 @@ class ProfileSettingPromo implements ProfileSetting
     {
         $entryYear = $page->profile->entry_year;
         $gradYear  = $page->profile->grad_year;
-        $yearpromo = $page->profile->grad_year;
-        if ($page->profile->mainEducation() == 'X') {
-            $yearpromo -= $page->profile->mainEducationDuration();
-        }
+        $yearpromo = $page->profile->yearpromo();
         $success   = true;
         if (is_null($value) || $value == $yearpromo) {
             if ($gradYear != $entryYear + $page->profile->mainEducationDuration()) {
@@ -675,14 +672,18 @@ class ProfilePageGeneral extends ProfilePage
         }
         if ($this->changed['promo_display']) {
             if ($this->values['promo_display']{0} == $this->profile->mainEducation()) {
-                if (($this->profile->mainEducation() == 'X'
-                     && intval(substr($this->values['promo_display'], 1, 4)) >= $this->profile->entry_year)
+                $yearpromo = intval(substr($this->values['promo_display'], 1, 4));
+                if (($this->profile->mainEducation() == 'X' && $yearpromo >= $this->profile->entry_year)
                     || ($this->profile->mainEducation() != 'X'
-                        && intval(substr($this->values['promo_display'], 1, 4)) >= $this->profile->entry_year + $this->profile->mainEducationDuration())) {
+                        && $yearpromo >= $this->profile->entry_year + $this->profile->mainEducationDuration())) {
                     XDB::execute('UPDATE  profile_display
                                      SET  promo = {?}
                                    WHERE  pid = {?}',
                                  $this->values['promo_display'], $this->pid());
+                    XDB::execute('UPDATE  profile_education
+                                     SET  promo_year = {?}
+                                   WHERE  pid = {?} AND FIND_IN_SET(\'primary\', flags)',
+                                 $yearpromo, $this->pid());
                 }
             }
         }
index 9b0aafc..3511138 100644 (file)
@@ -51,12 +51,14 @@ class RegisterModule extends PLModule
         if ($hash) {
             $nameTypes = DirEnum::getOptions(DirEnum::NAMETYPES);
             $nameTypes = array_flip($nameTypes);
-            $res = XDB::query("SELECT  a.uid, a.hruid, pnl.name AS lastname, pnf.name AS firstname, p.xorg_id AS xorgid,
+            $res = XDB::query("SELECT  a.uid, a.hruid, pnl.name AS lastname, pnf.name AS firstname, p.xorg_id AS xorgid, pd.promo, pe.promo_year AS yearpromo
                                        p.birthdate_ref AS birthdateRef, FIND_IN_SET('watch', a.flags) AS watch, m.hash, a.type
                                  FROM  register_marketing AS m
                            INNER JOIN  accounts           AS a   ON (m.uid = a.uid)
                            INNER JOIN  account_profiles   AS ap  ON (a.uid = ap.uid AND FIND_IN_SET('owner', ap.perms))
                            INNER JOIN  profiles           AS p   ON (p.pid = ap.pid)
+                           INNER JOIN  profile_display    AS pd  ON (p.pid = pd.pid)
+                           INNER JOIN  profile_education  AS pe  ON (pe.pid = p.pid AND FIND_IN_SET('primary', pe.flags))
                            INNER JOIN  profile_name       AS pnl ON (p.pid = pnl.pid AND pnl.typeid = {?})
                            INNER JOIN  profile_name       AS pnf ON (p.pid = pnf.pid AND pnf.typeid = {?})
                                 WHERE  m.hash = {?} AND a.state = 'pending'",
@@ -65,7 +67,6 @@ class RegisterModule extends PLModule
             if ($res->numRows() == 1) {
                 $subState->merge($res->fetchOneRow());
                 $subState->set('main_mail_domain', User::$sub_mail_domains[$subState->v('type')]);
-                $subState->set('yearpromo', substr($subState->s('hruid'), -4));
 
                 XDB::execute('INSERT INTO  register_mstats (uid, sender, success)
                                    SELECT  m.uid, m.sender, 0
@@ -95,13 +96,12 @@ class RegisterModule extends PLModule
                     $edu_type = Post::t('edu_type');
                     $yearpromo = Post::i('yearpromo');
                     $promo = $edu_type . $yearpromo;
-                    $year = ($edu_type == 'X') ? 'entry_year' : 'grad_year';
                     $res = XDB::query("SELECT  COUNT(*)
                                          FROM  accounts         AS a
                                    INNER JOIN  account_profiles AS ap ON (a.uid = ap.uid AND FIND_IN_SET('owner', ap.perms))
                                    INNER JOIN  profiles         AS p  ON (p.pid = ap.pid)
                                    INNER JOIN  profile_education AS pe ON (pe.pid = p.pid AND FIND_IN_SET('primary', pe.flags))
-                                        WHERE  a.state = 'pending' AND p.deathdate IS NULL AND pe.$year = {?}",
+                                        WHERE  a.state = 'pending' AND p.deathdate IS NULL AND pe.promo_year = {?}",
                                       $yearpromo);
 
                     if (!$res->fetchOneCell()) {
@@ -169,8 +169,7 @@ class RegisterModule extends PLModule
                             $birth[2] += 1900;
                         }
                         $year  = $birth[2];
-                        $promo = $subState->i('yearpromo');
-                        if ($year > $promo - 15 || $year < $promo - 30) {
+                        if ($year > $subState->i('yearpromo') - 15 || $subState->i('yearpromo') < $promo - 30) {
                             $error[] = "La 'Date de naissance' n'est pas correcte.";
                             $alert = "Date de naissance incorrecte à l'inscription - ";
                             $subState->set('wrong_birthdate', $birth);
@@ -282,7 +281,7 @@ class RegisterModule extends PLModule
         // authentication token.
         $res = XDB::query("SELECT  r.uid, p.pid, r.forlife, r.bestalias, r.mailorg2,
                                    r.password, r.email, r.services, r.naissance,
-                                   pnl.name AS lastname, pnf.name AS firstname,
+                                   pnl.name AS lastname, pnf.name AS firstname, pe.promo_year AS yearpromo,
                                    pd.promo, p.sex, p.birthdate_ref, a.type AS eduType
                              FROM  register_pending AS r
                        INNER JOIN  accounts         AS a   ON (r.uid = a.uid)
@@ -291,6 +290,7 @@ class RegisterModule extends PLModule
                        INNER JOIN  profile_name     AS pnl ON (p.pid = pnl.pid AND pnl.typeid = {?})
                        INNER JOIN  profile_name     AS pnf ON (p.pid = pnf.pid AND pnf.typeid = {?})
                        INNER JOIN  profile_display  AS pd  ON (p.pid = pd.pid)
+                       INNER JOIN  profile_education AS pe ON (pe.pid = p.pid AND FIND_IN_SET('primary', pe.flags))
                             WHERE  hash = {?} AND hash != 'INSCRIT' AND a.state = 'pending'",
                           $nameTypes['name_ini'], $nameTypes['firstname_ini'], $hash);
         if (!$hash || $res->numRows() == 0) {
@@ -311,7 +311,7 @@ class RegisterModule extends PLModule
         list($uid, $pid, $forlife, $bestalias, $emailXorg2, $password, $email, $services,
              $birthdate, $lastname, $firstname, $promo, $sex, $birthdate_ref, $eduType) = $res->fetchOneRow();
         $isX = ($eduType == 'x');
-        $yearpromo = substr($forlife, -4);
+        // We need the expected promotion here. Thus we remove pending display.
         $promo = str_replace(' (en cours)', $yearpromo, $promo);
         $mail_domain = User::$sub_mail_domains[$eduType] . $globals->mail->domain;
 
diff --git a/upgrade/1.1.1/07_yearpromo.sql b/upgrade/1.1.1/07_yearpromo.sql
new file mode 100644 (file)
index 0000000..8ee0485
--- /dev/null
@@ -0,0 +1,18 @@
+ALTER TABLE profile_education ADD COLUMN promo_year INT(4) DEFAULT NULL AFTER grad_year;
+
+    UPDATE  profile_education AS pe
+INNER JOIN  profile_display   AS pd ON (pd.pid = pe.pid)
+       SET  pe.promo_year = REPLACE(pd.promo, 'X', '')
+     WHERE  pe.degreeid = 2 AND FIND_IN_SET('primary', pe.flags);
+
+    UPDATE  profile_education AS pe
+INNER JOIN  profile_display   AS pd ON (pd.pid = pe.pid)
+       SET  pe.promo_year = REPLACE(pd.promo, 'M', '')
+     WHERE  pe.degreeid = 33 AND FIND_IN_SET('primary', pe.flags);
+
+    UPDATE  profile_education AS pe
+INNER JOIN  profiles          AS p ON (p.pid = pe.pid)
+       SET  pe.promo_year = REPLACE(SUBSTRING_INDEX(p.hrpid, '.', -1), 'd', '')
+     WHERE  pe.degreeid = 5 AND FIND_IN_SET('primary', pe.flags);
+
+-- vim:set syntax=mysql: