X-Git-Url: http://git.polytechnique.org/?a=blobdiff_plain;f=include%2Fnotifs.inc.php;h=4d8013cb5574c3804718debeee1eef09fe97fecc;hb=0f5f1b707d93b4dc8c5875c2129bb38fcd186563;hp=d48f010a2da3849a283bb35f26fa1dc55e4bc11d;hpb=a62886091365824f04a967638dee269196c3899d;p=platal.git
diff --git a/include/notifs.inc.php b/include/notifs.inc.php
index d48f010..4d8013c 100644
--- a/include/notifs.inc.php
+++ b/include/notifs.inc.php
@@ -1,6 +1,6 @@
title);
+ } else {
+ return str_replace(array('$x', '$s'), array('x', 's'), $this->title);
}
- XDB::execute('UPDATE auth_user_md5 SET DATE=NOW() WHERE user_id={?}', $uid);
- } elseif($cid == WATCH_INSCR) {
- XDB::execute('REPLACE INTO contacts (uid,contact)
- SELECT uid,ni_id
- FROM watch_nonins
- WHERE ni_id={?}', $uid);
- XDB::execute('DELETE FROM watch_nonins WHERE ni_id={?}', $uid);
- }
- Platal::session()->updateNbNotifs();
-}
-
-// }}}
-// {{{ function _select_notifs_base
+ }
-function _select_notifs_base($table, $mail, $where)
-{
- $cases = Array(
- 'contacts' => Array('wfield' => 'contact', 'ufield' => 'user_id', 'need_contact' => false,
- 'freq_sql' => '',
- 'contact_sql' => '1'
- ),
- 'watch_promo' => Array('wfield' => 'promo', 'ufield' => 'promo', 'need_contact' => true,
- 'freq_sql' => ' AND ( wc.type = "basic" OR wc.type="near" AND (u.promo <= v.promo_sortie-2 AND u.promo_sortie >= v.promo+2) )',
- 'contact_sql' => 'IF(c.contact IS NULL, 0, 1)'
- ),
- 'watch_nonins' => Array('wfield' => 'ni_id', 'ufield' => 'user_id', 'need_contact' => true,
- 'freq_sql' => '',
- 'contact_sql' => 'IF(c.contact IS NULL, 0, 1)'
- )
- );
-
- $our = $cases[$table];
- $sql = "
- (
- SELECT u.promo, u.prenom, IF(u.nom_usage='',u.nom,u.nom_usage) AS nom,
- u.deces != 0 AS dcd, (u.flags = 'femme') AS sexe,
- a.alias AS bestalias,
- wo.*,
- {$our['contact_sql']} AS contact,
- (u.perms IN('admin','user')) AS inscrit";
- if ($mail) {
- $sql.=",
- w.uid AS aid, v.prenom AS aprenom, IF(v.nom_usage='',v.nom,v.nom_usage) AS anom,
- b.alias AS abestalias, (v.flags='femme') AS asexe, q.core_mail_fmt AS mail_fmt";
- }
-
- $sql .= "
- FROM $table AS w
- INNER JOIN auth_user_md5 AS u ON(u.{$our['ufield']} = w.{$our['wfield']})
- INNER JOIN auth_user_quick AS q ON(q.user_id = w.uid)
- INNER JOIN auth_user_md5 AS v ON(v.user_id = q.user_id)";
- if ($mail) {
- $sql .="
- INNER JOIN aliases AS b ON(b.id = q.user_id AND FIND_IN_SET('bestalias', b.flags))";
- }
- if ($our['need_contact']) {
- $sql .="
- LEFT JOIN contacts AS c ON(c.uid = w.uid AND c.contact = u.user_id)";
- }
-
- $sql .="
- INNER JOIN watch_ops AS wo ON(wo.uid = u.user_id AND ".($mail ? 'wo.known > q.watch_last' : '( wo.known > {?} OR wo.date=NOW() )').")
- INNER JOIN watch_sub AS ws ON(ws.cid = wo.cid AND ws.uid = w.uid)
- INNER JOIN watch_cat AS wc ON(wc.id = wo.cid{$our['freq_sql']})
- LEFT JOIN aliases AS a ON(a.id = u.user_id AND FIND_IN_SET('bestalias', a.flags))
- WHERE $where
- )";
-
- return $sql;
-}
+ public function getCondition(Watch $watch)
+ {
+ if (!$watch->user()->watchType($this->flag)) {
+ if (!self::$false) {
+ self::$false = new PFC_False();
+ }
+ return self::$false;
+ } else {
+ return $this->buildCondition($watch);
+ }
+ }
-// }}}
-// {{{ function select_notifs
+ abstract protected function buildCondition(Watch $watch);
+ abstract public function getOrder();
+ abstract public function getDate(PlUser $user);
-function select_notifs($mail, $uid=null, $last=null, $iterator=true)
-{
- $where = $mail ? 'q.watch_flags=3' : 'w.uid = {?}';
- $sql = _select_notifs_base('contacts', $mail, $where.($mail?'':' AND (q.watch_flags=1 OR q.watch_flags=3)')) . " UNION DISTINCT ";
- $sql .= _select_notifs_base('watch_promo', $mail, $where) . " UNION DISTINCT ";
- $sql .= _select_notifs_base('watch_nonins', $mail, $where);
+ public function publicationDate(PlUser $user)
+ {
+ return $this->getDate($user);
+ }
- if ($iterator) {
- return XDB::iterator($sql . ' ORDER BY cid, promo, date DESC, nom', $last, $uid, $last, $uid, $last, $uid);
- } else {
- return XDB::query($sql, $last, $uid, $last, $uid, $last, $uid);
+ public function seen(PlUser $user, $last)
+ {
+ return strtotime($this->getDate($user)) > $last;
}
-}
-// }}}
-// {{{
-
-global $prf_desc;
-$prf_desc = array('nom' => 'Son patronyme',
- 'freetext' => 'Le texte libre',
- 'mobile' => 'Son numéro de téléphone portable',
- 'nationalite' => 'Sa nationalité',
- 'nick' => 'Son surnom',
- 'web' => 'L\'adresse de son site web',
- 'appli1' => 'Son école d\'application',
- 'appli2' => 'Son école de post-application',
- 'addresses' => 'Ses adresses',
- 'section' => 'Sa section sportive',
- 'binets' => 'La liste de ses binets',
- 'medals' => 'Ses décorations',
- 'cv' => 'Son Curriculum Vitae',
- 'jobs' => 'Ses informations professionnelles',
- 'photo' => 'Sa photographie');
-
-function get_profile_change_details($event, $limit) {
- global $prf_desc;
- $res = XDB::iterRow("SELECT field
- FROM watch_profile
- WHERE uid = {?} AND ts > {?}
- ORDER BY ts DESC",
- $event['uid'], $limit);
- if ($res->total() > 0) {
- $data = array();
- while (list($field) = $res->next()) {
- $data[] .= $prf_desc[$field];
- }
- return '
- ' . implode('
- ', $data) . '
';
+ public function getData(PlUser $user)
+ {
+ return null;
}
- return null;
}
-// }}}
-// {{{ function register_profile_update
+class WatchProfileUpdate extends WatchOperation
+{
+ private static $order = null;
-function register_profile_update($uid, $field) {
- XDB::execute("REPLACE INTO watch_profile (uid, ts, field)
- VALUES ({?}, NOW(), {?})",
- $uid, $field);
-}
+ public $flag = 'profile';
+ public $title = 'Mise$s à jour de fiche';
+ private $date = null;
-// {{{ class AllNotifs
+ public static function register(Profile $profile, $field)
+ {
+ XDB::execute('INSERT INTO watch_profile (pid, ts, field)
+ VALUES ({?}, NOW(), {?})
+ ON DUPLICATE KEY UPDATE ts = NOW()',
+ $profile->id(), $field);
+ }
-class AllNotifs
-{
- public $_cats = Array();
- public $_data = Array();
+ protected function buildCondition(Watch $watch)
+ {
+ $this->date = $watch->date();
+ return new PFC_And(new UFC_ProfileUpdated('>', $watch->date()),
+ $watch->contactCondition());
+ }
- public function __construct()
+ public function getOrder()
{
- $res = XDB::iterator("SELECT * FROM watch_cat");
- while($tmp = $res->next()) {
- $this->_cats[$tmp['id']] = $tmp;
+ if (!self::$order) {
+ self::$order = new UFO_ProfileUpdate();
}
+ return self::$order;
+ }
- // recupère tous les watchers, avec détails des watchers, a partir du
- // watch_last de chacun, seulement ceux qui sont surveillés, ordonnés
- $res = select_notifs(true);
+ public function getDate(PlUser $user)
+ {
+ return $user->profile()->last_change;
+ }
- while($tmp = $res->next()) {
- $aid = $tmp['aid'];
- if (empty($this->_data[$aid])) {
- $this->_data[$aid] = Array("prenom" => $tmp['aprenom'], 'nom' => $tmp['anom'],
- 'bestalias'=>$tmp['abestalias'], 'sexe' => $tmp['asexe'], 'mail_fmt' => $tmp['mail_fmt'],
- 'dcd'=>$tmp['dcd']);
+ public function getData(PlUser $user)
+ {
+ $data = XDB::fetchColumn("SELECT field
+ FROM watch_profile
+ WHERE pid = {?} AND ts > FROM_UNIXTIME({?}) AND field != ''
+ ORDER BY ts",
+ $user->profile()->id(), $this->date);
+ if (count($data) == 0) {
+ return null;
+ } else {
+ $text = array();
+ foreach ($data as $f) {
+ $text[] = Profile::$descriptions[$f];
}
- unset($tmp['aprenom'], $tmp['anom'], $tmp['abestalias'], $tmp['aid'], $tmp['asexe'], $tmp['mail_fmt'], $tmp['dcd']);
- $this->_data[$aid]['data'][$tmp['cid']][] = $tmp;
+ return $text;
}
}
}
-// }}}
-// {{{ class Notifs
-
-class Notifs
+class WatchRegistration extends WatchOperation
{
- public $_uid;
- public $_cats = Array();
- public $_data = Array();
+ private static $order = null;
- function __construct($uid, $up=false)
- {
- $this->_uid = $uid;
+ public $flag = 'registration';
+ public $title = 'Inscription$s';
- $res = XDB::iterator("SELECT * FROM watch_cat");
- while($tmp = $res->next()) {
- $this->_cats[$tmp['id']] = $tmp;
- }
-
- $lastweek = date('YmdHis', time() - 7*24*60*60);
+ protected function buildCondition(Watch $watch)
+ {
+ return new PFC_And(new UFC_Registered(false, '>', $watch->date()),
+ new PFC_Or($watch->contactCondition(),
+ $watch->promoCondition()));
+ }
- // recupere les notifs du watcher $uid, sans detail sur le watcher,
- // depuis la semaine dernière, meme ceux sans surveillance, ordonnés
- $res = select_notifs(false, $uid, $lastweek);
- while($tmp = $res->next()) {
- if ($tmp['cid'] == WATCH_FICHE) {
- $tmp['data'] = get_profile_change_details($tmp, $lastweek);
- }
- $this->_data[$tmp['cid']][$tmp['promo']][] = $tmp;
+ public function getOrder()
+ {
+ if (!self::$order) {
+ self::$order = new UFO_Registration();
}
+ return self::$order;
+ }
- if($up) {
- XDB::execute('UPDATE auth_user_quick SET watch_last=NOW() WHERE user_id={?}', $uid);
- }
+ public function getDate(PlUser $user)
+ {
+ return $user->registration_date;
}
}
-// }}}
-// {{{ class Watch
-
-class Watch
+class WatchDeath extends WatchOperation
{
- public $_uid;
- public $_promos;
- public $_nonins;
- public $_cats = Array();
- public $_subs;
- public $watch_contacts;
- public $watch_mail;
-
- public function __construct($uid)
- {
- $this->_uid = $uid;
- $this->_promos = new PromoNotifs($uid);
- $this->_nonins = new NoninsNotifs($uid);
- $this->_subs = new WatchSub($uid);
- $res = XDB::query("SELECT FIND_IN_SET('contacts',watch_flags),FIND_IN_SET('mail',watch_flags)
- FROM auth_user_quick
- WHERE user_id={?}", $uid);
- list($this->watch_contacts,$this->watch_mail) = $res->fetchOneRow();
-
- $res = XDB::iterator("SELECT * FROM watch_cat");
- while($tmp = $res->next()) {
- $this->_cats[$tmp['id']] = $tmp;
- }
- }
+ private static $order = null;
+
+ public $flag = 'death';
+ public $title = 'Décès';
- public function saveFlags()
+ protected function buildCondition(Watch $watch)
{
- $flags = "";
- if ($this->watch_contacts)
- $flags = "contacts";
- if ($this->watch_mail)
- $flags .= ($flags ? ',' : '')."mail";
- XDB::execute('UPDATE auth_user_quick SET watch_flags={?} WHERE user_id={?}',
- $flags, $this->_uid);
+ return new PFC_And(new UFC_Dead('>', $watch->date(), true),
+ new PFC_Or($watch->contactCondition(),
+ $watch->promoCondition()));
}
- public function cats()
+ public function getOrder()
{
- return $this->_cats;
+ if (!self::$order) {
+ self::$order = new UFO_Death();
+ }
+ return self::$order;
}
- public function subs($i)
+ public function getDate(PlUser $user)
{
- return $this->_subs->_data[$i];
+ return $user->profile()->deathdate;
}
- public function promos()
+ public function publicationDate(PlUser $user)
{
- return $this->_promos->toRanges();
+ return $user->profile()->deathdate_rec;
}
- public function nonins()
+ public function seen(PlUser $user, $last)
{
- return $this->_nonins->_data;
+ return strtotime($user->profile()->deathdate_rec) > $last;
}
}
-// }}}
-// {{{ class WatchSub
-
-class WatchSub
+class WatchBirthday extends WatchOperation
{
- public $_uid;
- public $_data = Array();
+ const WATCH_LIMIT = 604800; // 1 week
+
+ private static $order = null;
+
+ public $flag = 'birthday';
+ public $title = 'Anniversaire$s';
- public function __construct($uid)
+ protected function buildCondition(Watch $watch)
{
- $this->_uid = $uid;
- $res = XDB::iterRow('SELECT cid FROM watch_sub WHERE uid={?}', $uid);
- while(list($c) = $res->next()) {
- $this->_data[$c] = $c;
+ $select_date = new PFC_OR(new UFC_Birthday('=', time()),
+ new PFC_And(new UFC_Birthday('<=', time() + self::WATCH_LIMIT),
+ new UFC_Birthday('>', $watch->date() + self::WATCH_LIMIT)));
+ $profile = $watch->profile();
+ $cond = $watch->contactCondition();
+ if ($profile) {
+ $cond = new PFC_Or($cond,
+ new PFC_And($watch->promoCondition(),
+ new UFC_Promo('>=', $profile->mainGrade(), $profile->yearpromo() - 1),
+ new UFC_Promo('<=', $profile->mainGrade(), $profile->yearpromo() + 1)));
}
+ return new PFC_And($select_date, $cond);
}
- public function update($ind)
+ public function getOrder()
{
- $this->_data = Array();
- XDB::execute('DELETE FROM watch_sub WHERE uid={?}', $this->_uid);
- foreach (Env::v($ind) as $key=>$val) {
- XDB::query('INSERT INTO watch_sub SELECT {?},id FROM watch_cat WHERE id={?}', $this->_uid, $key);
- if(XDB::affectedRows()) {
- $this->_data[$key] = $key;
- }
+ if (!self::$order) {
+ self::$order = new UFO_Birthday();
}
+ return self::$order;
+ }
+
+ public function getDate(PlUser $user)
+ {
+ return $user->profile()->next_birthday;
+ }
+
+ public function publicationDate(PlUser $user)
+ {
+ return date('Y-m-d', strtotime($user->profile()->next_birthday) - self::WATCH_LIMIT);
}
-}
-// }}}
-// {{{ class PromoNotifs
+ public function seen(PlUser $user, $last)
+ {
+ $birthday = strtotime($user->profile()->next_birthday);
+ return $birthday > $last + self::WATCH_LIMIT
+ || date('Ymd', $birthday) == date('Ymd');
+ }
+}
-class PromoNotifs
+class Watch
{
- public $_uid;
- public $_data = Array();
+ private static $classes = array('WatchRegistration',
+ 'WatchProfileUpdate',
+ 'WatchDeath',
+ 'WatchBirthday');
+ private static $events = array();
- public function __construct($uid)
+ private $user = null;
+ private $date = null;
+ private $contactCond = null;
+ private $promoCond = null;
+
+ private $filters = array();
+
+ public function __construct(PlUser $user, $date = null)
+ {
+ $this->user = $user;
+ $this->date = self::getDate($user, $date);
+ }
+
+ public function user()
{
- $this->_uid = $uid;
- $res = XDB::iterRow('SELECT promo FROM watch_promo WHERE uid={?} ORDER BY promo', $uid);
- while (list($p) = $res->next()) {
- $this->_data[intval($p)] = intval($p);
+ return $this->user;
+ }
+
+ public function profile()
+ {
+ return $this->user->profile();
+ }
+
+ public function date()
+ {
+ return $this->date;
+ }
+
+ public function contactCondition()
+ {
+ if (!$this->contactCond) {
+ $this->contactCond = new UFC_WatchContact($this->user);
}
+ return $this->contactCond;
}
- public function add($p)
+ public function promoCondition()
{
- $promo = intval($p);
- XDB::execute('REPLACE INTO watch_promo (uid,promo) VALUES({?},{?})', $this->_uid, $promo);
- $this->_data[$promo] = $promo;
- asort($this->_data);
+ if (!$this->promoCond) {
+ $this->promoCond = new UFC_WatchPromo($this->user);
+ }
+ return $this->promoCond;
}
- public function del($p)
+ private function fetchEventWatch($class)
{
- $promo = intval($p);
- XDB::execute('DELETE FROM watch_promo WHERE uid={?} AND promo={?}', $this->_uid, $promo);
- unset($this->_data[$promo]);
+ if (!isset(self::$events[$class])) {
+ self::$events[$class] = new $class();
+ }
+ return self::$events[$class];
}
- public function addRange($_p1,$_p2)
+ private function fetchFilter($class)
{
- $p1 = intval($_p1);
- $p2 = intval($_p2);
- $values = Array();
- for($i = min($p1,$p2); $i<=max($p1,$p2); $i++) {
- $values[] = "('{$this->_uid}',$i)";
- $this->_data[$i] = $i;
+
+ if (!isset($this->filters[$class])) {
+ $event = $this->fetchEventWatch($class);
+ $this->filters[$class] = new UserFilter($event->getCondition($this),
+ array($event->getOrder(), new UFO_Name(Profile::DN_SORT)));
}
- XDB::execute('REPLACE INTO watch_promo (uid,promo) VALUES '.join(',',$values));
- asort($this->_data);
+ return $this->filters[$class];
}
- public function delRange($_p1,$_p2)
+ public function count()
{
- $p1 = intval($_p1);
- $p2 = intval($_p2);
- $where = Array();
- for($i = min($p1,$p2); $i<=max($p1,$p2); $i++) {
- $where[] = "promo=$i";
- unset($this->_data[$i]);
+ $count = 0;
+ foreach (self::$classes as $class) {
+ $uf = $this->fetchFilter($class);
+ $count += $uf->getTotalCount();
}
- XDB::execute('DELETE FROM watch_promo WHERE uid={?} AND ('.join(' OR ',$where).')', $this->_uid);
+ return $count;
}
- public function toRanges()
+
+ private function fetchEvents($class)
{
- $ranges = Array();
- $I = Array();
- foreach($this->_data as $promo) {
- if(!isset($I[0])) {
- $I = Array($promo,$promo);
- }
- elseif($I[1]+1 == $promo) {
- $I[1] ++;
- }
- else {
- $ranges[] = $I;
- $I = Array($promo,$promo);
+ $obj = $this->fetchEventWatch($class);
+ $uf = $this->fetchFilter($class);
+ $users = $uf->getUsers();
+ if (count($users) == 0) {
+ return null;
+ } else {
+ return array('type' => $obj->flag,
+ 'operation' => $obj,
+ 'title' => $obj->getTitle(count($users)),
+ 'users' => $users);
+ }
+ }
+
+ public function events()
+ {
+ $events = array();
+ foreach (self::$classes as $class) {
+ $e = $this->fetchEvents($class);
+ if (!is_null($e)) {
+ $events[] = $e;
}
}
- if(isset($I[0])) $ranges[] = $I;
- return $ranges;
+ return $events;
}
-}
-// }}}
-// {{{ class NoninsNotifs
-class NoninsNotifs
-{
- public $_uid;
- public $_data = Array();
-
- public function __construct($uid)
- {
- $this->_uid = $uid;
- $res = XDB::iterator("SELECT u.prenom,IF(u.nom_usage='',u.nom,u.nom_usage) AS nom, u.promo, u.user_id
- FROM watch_nonins AS w
- INNER JOIN auth_user_md5 AS u ON (u.user_id = w.ni_id)
- WHERE w.uid = {?}
- ORDER BY promo,nom", $uid);
- while($tmp = $res->next()) {
- $this->_data[$tmp['user_id']] = $tmp;
+ private static function getDate(PlUser $user, $date)
+ {
+ if (is_null($date)) {
+ $date = $user->watchLast();
+ $limit = time() - (7 * 86400);
+ if ($date < $limit) {
+ $date = $limit;
+ }
}
+ return $date;
}
- public function del($p)
+ public static function getCount(PlUser $user, $date = null)
{
- unset($this->_data["$p"]);
- XDB::execute('DELETE FROM watch_nonins WHERE uid={?} AND ni_id={?}', $this->_uid, $p);
+ $watch = new Watch($user, $date);
+ return $watch->count();
}
- public function add($p)
+ public static function getEvents(PlUser $user, $date = null)
{
- XDB::execute('INSERT INTO watch_nonins (uid,ni_id) VALUES({?},{?})', $this->_uid, $p);
- $res = XDB::query('SELECT prenom,IF(nom_usage="",nom,nom_usage) AS nom,promo,user_id
- FROM auth_user_md5
- WHERE user_id={?}', $p);
- $this->_data["$p"] = $res->fetchOneAssoc();
+ $watch = new Watch($user, $date);
+ return $watch->events();
}
}
-// }}}
-
// vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
?>