Can get the number of notification.
authorFlorent Bruneau <florent.bruneau@polytechnique.org>
Tue, 10 Feb 2009 23:16:52 +0000 (00:16 +0100)
committerFlorent Bruneau <florent.bruneau@polytechnique.org>
Tue, 10 Feb 2009 23:16:52 +0000 (00:16 +0100)
Signed-off-by: Florent Bruneau <florent.bruneau@polytechnique.org>
bin/cron/notifs.birthday.php
classes/userfilter.php
classes/xorgsession.php
include/notifs.inc.php
modules/carnet.php
modules/events.php
upgrade/account/01_profiles.sql
upgrade/account/99_insertion.sql
upgrade/account/birthday.php [new symlink]
upgrade/account/connect.db.inc.php [new symlink]
upgrade/account/upgrade.sh [new file with mode: 0644]

index 46c2ae0..96516b8 100755 (executable)
 
 require('./connect.db.inc.php');
 
-$date  = date('Y-m-d', time() + 7 * 24*60*60);
-$stamp = date('Ymd000000');
-$like  = date('%-m-d', time() + 7 * 24*60*60);
-
-XDB::execute("DELETE FROM  watch_ops
-                    WHERE  cid = 4 AND date < CURDATE()");
-
-XDB::execute("INSERT INTO  watch_ops (uid, cid, known, date)
-                   SELECT  user_id, 4, $stamp, '$date'
-                     FROM  auth_user_md5
-                    WHERE  naissance LIKE '$like' AND deces=0");
-
+$it = 0;
+do {
+    XDB::execute('UPDATE  profiles
+                     SET  next_birthday = DATE_ADD(next_birthday, INTERVAL 1 YEAR)
+                   WHERE  (next_birthday != 0 AND next_birthday IS NOT NULL AND next_birthday < CURDATE())
+                           AND deathdate IS NULL');
+    ++$it;
+    $affected = XDB::affectedRows();
+    echo "Iteration $it => $affected changes\n";
+} while ($affected > 0);
 
 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
 ?>
index 8ba722b..332f16a 100644 (file)
@@ -292,6 +292,40 @@ class UFC_Registered implements UserFilterCondition
     }
 }
 
+class UFC_ProfileUpdated implements UserFilterCondition
+{
+    private $comparison;
+    private $date;
+
+    public function __construct($comparison = null, $date = null)
+    {
+        $this->comparison = $comparison;
+        $this->date = $date;
+    }
+
+    public function buildCondition(UserFilter &$uf)
+    {
+        return 'p.last_change ' . $this->comparison . XDB::format(' {?}', date('Y-m-d H:i:s', $this->date));
+    }
+}
+
+class UFC_Birthday implements UserFilterCondition
+{
+    private $comparison;
+    private $date;
+
+    public function __construct($comparison = null, $date = null)
+    {
+        $this->comparison = $comparison;
+        $this->date = $date;
+    }
+
+    public function buildCondition(UserFilter &$uf)
+    {
+        return 'p.next_birthday ' . $this->comparison . XDB::format(' {?}', date('Y-m-d', $this->date));
+    }
+}
+
 class UFC_Sex implements UserFilterCondition
 {
     private $sex;
@@ -428,7 +462,8 @@ class UFC_WatchRegistration extends UFC_UserRelated
     public function buildCondition(UserFilter &$uf)
     {
         $sub = $uf->addWatchRegistrationFilter($this->uid);
-        return 'wn' . $sub . '.uid IS NOT NULL';
+        $su  = $uf->addWatchFilter($this->uid);
+        return 'FIND_IN_SET(\'registration\', w' . $su . '.flags) OR wn' . $sub . '.uid IS NOT NULL';
     }
 }
 
@@ -455,11 +490,12 @@ class UFC_WatchContacts extends UFC_Contact
     public function buildCondition(UserFilter &$uf)
     {
         $sub = $uf->addWatchFilter($this->uid);
-        return 'FIND_IN_SET(\'contacts\' w' . $sub . '.flags) AND ' . parent::buildCondition($uf);
+        return 'FIND_IN_SET(\'contacts\', w' . $sub . '.flags) AND ' . parent::buildCondition($uf);
     }
 }
 
 
+
 /******************
  * ORDERS
  ******************/
@@ -716,9 +752,8 @@ class UserFilter
     {
         if (is_null($this->lastcount)) {
             $this->buildQuery();
-            return (int)XDB::fetchOneCell('SELECT  COUNT(*)
-                                          ' . $this->query . '
-                                         GROUP BY  a.uid');
+            return (int)XDB::fetchOneCell('SELECT  COUNT(DISTINCT a.uid)
+                                          ' . $this->query);
         } else {
             return $this->lastcount;
         }
index ae4d524..f091753 100644 (file)
@@ -197,7 +197,7 @@ class XorgSession extends PlSession
         $res  = XDB::query("SELECT  a.uid, a.hruid, a.display_name, a.full_name,
                                     a.sex = 'female' AS femme, a.email_format,
                                     a.token, FIND_IN_SET('watch', a.flags) AS watch_account,
-                                    UNIX_TIMESTAMP(fp.last_seen) AS banana_last, w.last AS watch_last,
+                                    UNIX_TIMESTAMP(fp.last_seen) AS banana_last, UNIX_TIMESTAMP(w.last) AS watch_last,
                                     a.last_version, g.g_account_name IS NOT NULL AS googleapps,
                                     UNIX_TIMESTAMP(s.start) AS lastlogin, s.host,
                                     a.is_admin, at.perms
index 8efb108..0cdfdf5 100644 (file)
@@ -30,9 +30,9 @@ class WatchProfileUpdate
                      $profile->id(), $field);
     }
 
-    public static function getCondition(PlUser &$user)
+    public function getCondition(PlUser &$user)
     {
-        return new UFC_And(new UFC_ProfileUpdated('>=', $user->watch_last),
+        return new UFC_And(new UFC_ProfileUpdated('>', $user->watch_last),
                            new UFC_WatchContacts($user->id()));
     }
 }
@@ -41,10 +41,12 @@ class WatchRegistration
 {
     const ID = 2;
 
-    public static function getCondition(PlUser &$user)
+    public function getCondition(PlUser &$user)
     {
-        return new UFC_And(new UFC_Registered(false, '>=', $user->watch_last),
-                           new UFC_WatchRegistration($user->id()));
+        return new UFC_And(new UFC_Registered(false, '>', $user->watch_last),
+                           new UFC_Or(new UFC_WatchContacts($user->id()),
+                                      new UFC_WatchPromo($user->id()),
+                                      new UFC_WatchRegistration($user->id())));
     }
 }
 
@@ -52,9 +54,9 @@ class WatchDeath
 {
     const ID = 3;
 
-    public static function getCondition(PlUser &$user)
+    public function getCondition(PlUser &$user)
     {
-        return new UFC_And(new UFC_Dead('>=', $user->watch_last, true),
+        return new UFC_And(new UFC_Dead('>', $user->watch_last, true),
                            new UFC_Or(new UFC_WatchPromo($user->id()),
                                       new UFC_WatchContacts($user->id())));
     }
@@ -64,12 +66,39 @@ class WatchBirthday
 {
     const ID = 4;
 
-    public static function getCondition(PlUser &$user)
+    public function getCondition(PlUser &$user)
     {
-        return new UFC_And(new UFC_Birthday(),
+        return new UFC_And(new UFC_OR(new UFC_Birthday('=', time()),
+                                      new UFC_And(new UFC_Birthday('<=', time() + 864000),
+                                                  new UFC_Birthday('>', $user->watch_last + 864000))),
                            new UFC_Or(new UFC_WatchPromo($user->id()),
                                       new UFC_WatchContacts($user->id())));
     }
 }
 
+class Watch
+{
+    private static  $classes = array('WatchRegistration',
+                                     'WatchProfileUpdate',
+                                     'WatchDeath',
+                                     'WatchBirthday');
+
+    private static function fetchCount(PlUser &$user, $class)
+    {
+        $obj = new $class();
+        $uf = new UserFilter($obj->getCondition($user));
+        return $uf->getTotalCount();
+    }
+
+    public static function getCount(PlUser &$user)
+    {
+        $count = 0;
+        foreach (self::$classes as $class) {
+            $count += self::fetchCount($user, $class);
+        }
+        return $count;
+    }
+}
+
+// vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
 ?>
index 7cd9320..7afc6bc 100644 (file)
@@ -201,14 +201,14 @@ class CarnetModule extends PLModule
             S::assert_xsrf_token();
             XDB::execute('UPDATE  watch
                              SET  ' . XDB::changeFlag('flags', 'contacts', Env::b('contacts')) . '
-                               WHERE  uid = {?}', S::i('uid'));
+                           WHERE  uid = {?}', S::i('uid'));
         }
 
         if (Env::has('flags_mail')) {
             S::assert_xsrf_token();
             XDB::execute('UPDATE  watch
                              SET  ' . XDB::changeFlag('flags', 'mail', Env::b('mail')) . '
-                               WHERE  uid = {?}', S::i('uid'));
+                           WHERE  uid = {?}', S::i('uid'));
         }
 
         $user = S::user();
index 063ffc4..1a03dc3 100644 (file)
@@ -157,6 +157,9 @@ class EventsModule extends PLModule
             pl_redirect('events#newsid'.$eid);
         }
 
+        require_once 'notifs.inc.php';
+        echo Watch::getCount(S::user());
+
         function next_event(PlIterator &$it)
         {
             $user = S::user();
index 8248cd4..02fa34b 100644 (file)
@@ -11,6 +11,7 @@ create table profiles (
   # birthdate and birthdate given by reference directory (library, school...)
   birthdate date default null,
   birthdate_ref date default null,
+  next_birthday date default null,
   deathdate date default null,
   deathdate_rec date default null,
 
index 3af9373..f0cb142 100644 (file)
@@ -50,6 +50,7 @@ insert into carvas
 insert into profiles
      select u.user_id AS pid, u.hruid AS hrpid, u.matricule AS xorg_id,
             u.matricule_ax AS ax_id, u.naissance AS birthdate, u.naissance_ini AS birthdate_ref,
+            u.naissance AS next_birthday,
             IF(u.deces = 0, NULL, u.deces) AS deathdate,
             IF(u.deces = 0, NULL, u.deces) AS deathdate_rec,
             IF(FIND_IN_SET('femme', flags), 'female', 'male') AS sex,
diff --git a/upgrade/account/birthday.php b/upgrade/account/birthday.php
new file mode 120000 (symlink)
index 0000000..cae0f40
--- /dev/null
@@ -0,0 +1 @@
+../../bin/cron/notifs.birthday.php
\ No newline at end of file
diff --git a/upgrade/account/connect.db.inc.php b/upgrade/account/connect.db.inc.php
new file mode 120000 (symlink)
index 0000000..442fab7
--- /dev/null
@@ -0,0 +1 @@
+../../bin/connect.db.inc.php
\ No newline at end of file
diff --git a/upgrade/account/upgrade.sh b/upgrade/account/upgrade.sh
new file mode 100644 (file)
index 0000000..e9658df
--- /dev/null
@@ -0,0 +1,7 @@
+#!/bin/sh
+
+echo "Upgrading database"
+mysql x4dat < *.sql
+
+echo "Updating birthday date"
+php birthday.php