From 7e735012ce134d53e532d2d9b23ea5437127c5b8 Mon Sep 17 00:00:00 2001 From: Florent Bruneau Date: Wed, 11 Feb 2009 00:16:52 +0100 Subject: [PATCH] Can get the number of notification. Signed-off-by: Florent Bruneau --- bin/cron/notifs.birthday.php | 22 ++++++++---------- classes/userfilter.php | 45 ++++++++++++++++++++++++++++++++---- classes/xorgsession.php | 2 +- include/notifs.inc.php | 47 ++++++++++++++++++++++++++++++-------- modules/carnet.php | 4 ++-- modules/events.php | 3 +++ upgrade/account/01_profiles.sql | 1 + upgrade/account/99_insertion.sql | 1 + upgrade/account/birthday.php | 1 + upgrade/account/connect.db.inc.php | 1 + upgrade/account/upgrade.sh | 7 ++++++ 11 files changed, 105 insertions(+), 29 deletions(-) create mode 120000 upgrade/account/birthday.php create mode 120000 upgrade/account/connect.db.inc.php create mode 100644 upgrade/account/upgrade.sh diff --git a/bin/cron/notifs.birthday.php b/bin/cron/notifs.birthday.php index 46c2ae0..96516b8 100755 --- a/bin/cron/notifs.birthday.php +++ b/bin/cron/notifs.birthday.php @@ -22,18 +22,16 @@ 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: ?> diff --git a/classes/userfilter.php b/classes/userfilter.php index 8ba722b..332f16a 100644 --- a/classes/userfilter.php +++ b/classes/userfilter.php @@ -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; } diff --git a/classes/xorgsession.php b/classes/xorgsession.php index ae4d524..f091753 100644 --- a/classes/xorgsession.php +++ b/classes/xorgsession.php @@ -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 diff --git a/include/notifs.inc.php b/include/notifs.inc.php index 8efb108..0cdfdf5 100644 --- a/include/notifs.inc.php +++ b/include/notifs.inc.php @@ -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: ?> diff --git a/modules/carnet.php b/modules/carnet.php index 7cd9320..7afc6bc 100644 --- a/modules/carnet.php +++ b/modules/carnet.php @@ -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(); diff --git a/modules/events.php b/modules/events.php index 063ffc4..1a03dc3 100644 --- a/modules/events.php +++ b/modules/events.php @@ -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(); diff --git a/upgrade/account/01_profiles.sql b/upgrade/account/01_profiles.sql index 8248cd4..02fa34b 100644 --- a/upgrade/account/01_profiles.sql +++ b/upgrade/account/01_profiles.sql @@ -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, diff --git a/upgrade/account/99_insertion.sql b/upgrade/account/99_insertion.sql index 3af9373..f0cb142 100644 --- a/upgrade/account/99_insertion.sql +++ b/upgrade/account/99_insertion.sql @@ -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 index 0000000..cae0f40 --- /dev/null +++ b/upgrade/account/birthday.php @@ -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 index 0000000..442fab7 --- /dev/null +++ b/upgrade/account/connect.db.inc.php @@ -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 index 0000000..e9658df --- /dev/null +++ b/upgrade/account/upgrade.sh @@ -0,0 +1,7 @@ +#!/bin/sh + +echo "Upgrading database" +mysql x4dat < *.sql + +echo "Updating birthday date" +php birthday.php -- 2.1.4