Cleans up watch_nonins when someone dies (Closes #975).
[platal.git] / include / notifs.inc.php
CommitLineData
0337d704 1<?php
2/***************************************************************************
8d84c630 3 * Copyright (C) 2003-2009 Polytechnique.org *
0337d704 4 * http://opensource.polytechnique.org/ *
5 * *
6 * This program is free software; you can redistribute it and/or modify *
7 * it under the terms of the GNU General Public License as published by *
8 * the Free Software Foundation; either version 2 of the License, or *
9 * (at your option) any later version. *
10 * *
11 * This program is distributed in the hope that it will be useful, *
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14 * GNU General Public License for more details. *
15 * *
16 * You should have received a copy of the GNU General Public License *
17 * along with this program; if not, write to the Free Software *
18 * Foundation, Inc., *
19 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *
20 ***************************************************************************/
21
22define('WATCH_FICHE', 1);
23define('WATCH_INSCR', 2);
24define('WATCH_DEATH', 3);
25define('WATCH_BIRTH', 4);
26
27// {{{ function inscription_notifs_base
28
29function inscription_notifs_base($uid)
30{
08cce2ff 31 XDB::execute('REPLACE INTO watch_sub (uid,cid) SELECT {?},id FROM watch_cat', $uid);
0337d704 32}
33
34// }}}
35// {{{ function register_watch_op
36
cfb05c41 37function register_watch_op($uid, $cid, $date = '', $info = '')
0337d704 38{
0337d704 39 if (empty($date)) {
40 $date = date('Y-m-d');
cfb05c41
SJ
41 }
42 XDB::execute('REPLACE INTO watch_ops (uid, cid, known, date, info)
a2a1c2f2
FB
43 VALUES ({?}, {?}, NOW(), {?}, {?})',
44 $uid, $cid, $date, $info);
cfb05c41 45 if ($cid == WATCH_FICHE) {
a2a1c2f2
FB
46 if ($info) {
47 register_profile_update($uid, $info);
48 }
cfb05c41
SJ
49 XDB::execute('UPDATE auth_user_md5
50 SET DATE = NOW()
51 WHERE user_id = {?}', $uid);
52 } elseif ($cid == WATCH_INSCR) {
53 XDB::execute('REPLACE INTO contacts (uid, contact)
54 SELECT uid, ni_id
612a2d8a 55 FROM watch_nonins
cfb05c41
SJ
56 WHERE ni_id = {?}', $uid);
57 XDB::execute('DELETE FROM watch_nonins
58 WHERE ni_id = {?}', $uid);
59 } elseif ($cid == WATCH_DEATH) {
60 // We delete nonins watches both for the dead (if non registered), and
61 // for people watched by the dead.
62 XDB::execute('DELETE FROM watch_nonins
63 WHERE ni_id = {?} OR uid = {?}', $uid);
0337d704 64 }
ebfdf077 65 Platal::session()->updateNbNotifs();
0337d704 66}
67
68// }}}
69// {{{ function _select_notifs_base
70
71function _select_notifs_base($table, $mail, $where)
72{
73 $cases = Array(
612a2d8a 74 'contacts' => Array('wfield' => 'contact', 'ufield' => 'user_id', 'need_contact' => false,
75 'freq_sql' => '',
76 'contact_sql' => '1'
77 ),
78 'watch_promo' => Array('wfield' => 'promo', 'ufield' => 'promo', 'need_contact' => true,
79 'freq_sql' => ' AND ( wc.type = "basic" OR wc.type="near" AND (u.promo <= v.promo_sortie-2 AND u.promo_sortie >= v.promo+2) )',
3ebe4a0a 80 'contact_sql' => 'IF(c.contact IS NULL, 0, 1)'
612a2d8a 81 ),
3ebe4a0a 82 'watch_nonins' => Array('wfield' => 'ni_id', 'ufield' => 'user_id', 'need_contact' => true,
612a2d8a 83 'freq_sql' => '',
3ebe4a0a 84 'contact_sql' => 'IF(c.contact IS NULL, 0, 1)'
612a2d8a 85 )
0337d704 86 );
87
88 $our = $cases[$table];
89 $sql = "
90 (
612a2d8a 91 SELECT u.promo, u.prenom, IF(u.nom_usage='',u.nom,u.nom_usage) AS nom,
1430b1f1 92 u.deces != 0 AS dcd, (u.flags = 'femme') AS sexe,
612a2d8a 93 a.alias AS bestalias,
94 wo.*,
95 {$our['contact_sql']} AS contact,
96 (u.perms IN('admin','user')) AS inscrit";
0337d704 97 if ($mail) {
98 $sql.=",
612a2d8a 99 w.uid AS aid, v.prenom AS aprenom, IF(v.nom_usage='',v.nom,v.nom_usage) AS anom,
1430b1f1 100 b.alias AS abestalias, (v.flags='femme') AS asexe, q.core_mail_fmt AS mail_fmt";
0337d704 101 }
102
103 $sql .= "
104 FROM $table AS w
105 INNER JOIN auth_user_md5 AS u ON(u.{$our['ufield']} = w.{$our['wfield']})
106 INNER JOIN auth_user_quick AS q ON(q.user_id = w.uid)
107 INNER JOIN auth_user_md5 AS v ON(v.user_id = q.user_id)";
108 if ($mail) {
109 $sql .="
110 INNER JOIN aliases AS b ON(b.id = q.user_id AND FIND_IN_SET('bestalias', b.flags))";
111 }
112 if ($our['need_contact']) {
113 $sql .="
114 LEFT JOIN contacts AS c ON(c.uid = w.uid AND c.contact = u.user_id)";
115 }
116
117 $sql .="
118 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() )').")
119 INNER JOIN watch_sub AS ws ON(ws.cid = wo.cid AND ws.uid = w.uid)
120 INNER JOIN watch_cat AS wc ON(wc.id = wo.cid{$our['freq_sql']})
121 LEFT JOIN aliases AS a ON(a.id = u.user_id AND FIND_IN_SET('bestalias', a.flags))
612a2d8a 122 WHERE $where
123 )";
0337d704 124
125 return $sql;
126}
127
128// }}}
129// {{{ function select_notifs
130
131function select_notifs($mail, $uid=null, $last=null, $iterator=true)
132{
0337d704 133 $where = $mail ? 'q.watch_flags=3' : 'w.uid = {?}';
134 $sql = _select_notifs_base('contacts', $mail, $where.($mail?'':' AND (q.watch_flags=1 OR q.watch_flags=3)')) . " UNION DISTINCT ";
135 $sql .= _select_notifs_base('watch_promo', $mail, $where) . " UNION DISTINCT ";
136 $sql .= _select_notifs_base('watch_nonins', $mail, $where);
137
138 if ($iterator) {
08cce2ff 139 return XDB::iterator($sql . ' ORDER BY cid, promo, date DESC, nom', $last, $uid, $last, $uid, $last, $uid);
0337d704 140 } else {
08cce2ff 141 return XDB::query($sql, $last, $uid, $last, $uid, $last, $uid);
0337d704 142 }
143}
144
145// }}}
a2a1c2f2
FB
146// {{{
147
148global $prf_desc;
149$prf_desc = array('nom' => 'Son patronyme',
150 'freetext' => 'Le texte libre',
151 'mobile' => 'Son numéro de téléphone portable',
152 'nationalite' => 'Sa nationalité',
153 'nick' => 'Son surnom',
154 'web' => 'L\'adresse de son site web',
155 'appli1' => 'Son école d\'application',
156 'appli2' => 'Son école de post-application',
157 'addresses' => 'Ses adresses',
158 'section' => 'Sa section sportive',
159 'binets' => 'La liste de ses binets',
160 'medals' => 'Ses décorations',
161 'cv' => 'Son Curriculum Vitae',
162 'jobs' => 'Ses informations professionnelles',
163 'photo' => 'Sa photographie');
164
165function get_profile_change_details($event, $limit) {
166 global $prf_desc;
167 $res = XDB::iterRow("SELECT field
168 FROM watch_profile
169 WHERE uid = {?} AND ts > {?}
170 ORDER BY ts DESC",
171 $event['uid'], $limit);
172 if ($res->total() > 0) {
173 $data = array();
174 while (list($field) = $res->next()) {
175 $data[] .= $prf_desc[$field];
176 }
177 return '<ul><li>' . implode('</li><li>', $data) . '</li></ul>';
178 }
179 return null;
180}
181
182// }}}
183// {{{ function register_profile_update
184
185function register_profile_update($uid, $field) {
186 XDB::execute("REPLACE INTO watch_profile (uid, ts, field)
187 VALUES ({?}, NOW(), {?})",
188 $uid, $field);
189}
190
0337d704 191// {{{ class AllNotifs
192
eaf30d86 193class AllNotifs
612a2d8a 194{
195 public $_cats = Array();
196 public $_data = Array();
0337d704 197
612a2d8a 198 public function __construct()
199 {
a3a049fc 200 $res = XDB::iterator("SELECT * FROM watch_cat");
201 while($tmp = $res->next()) {
0337d704 202 $this->_cats[$tmp['id']] = $tmp;
203 }
204
a7de4ef7 205 // recupère tous les watchers, avec détails des watchers, a partir du
206 // watch_last de chacun, seulement ceux qui sont surveillés, ordonnés
a3a049fc 207 $res = select_notifs(true);
0337d704 208
a3a049fc 209 while($tmp = $res->next()) {
210 $aid = $tmp['aid'];
0337d704 211 if (empty($this->_data[$aid])) {
212 $this->_data[$aid] = Array("prenom" => $tmp['aprenom'], 'nom' => $tmp['anom'],
1430b1f1 213 'bestalias'=>$tmp['abestalias'], 'sexe' => $tmp['asexe'], 'mail_fmt' => $tmp['mail_fmt'],
612a2d8a 214 'dcd'=>$tmp['dcd']);
0337d704 215 }
1430b1f1 216 unset($tmp['aprenom'], $tmp['anom'], $tmp['abestalias'], $tmp['aid'], $tmp['asexe'], $tmp['mail_fmt'], $tmp['dcd']);
a3a049fc 217 $this->_data[$aid]['data'][$tmp['cid']][] = $tmp;
218 }
0337d704 219 }
220}
221
222// }}}
223// {{{ class Notifs
224
eaf30d86 225class Notifs
612a2d8a 226{
227 public $_uid;
228 public $_cats = Array();
229 public $_data = Array();
a3a049fc 230
6720bd53 231 function __construct($uid, $up=false)
612a2d8a 232 {
a3a049fc 233 $this->_uid = $uid;
234
235 $res = XDB::iterator("SELECT * FROM watch_cat");
236 while($tmp = $res->next()) {
0337d704 237 $this->_cats[$tmp['id']] = $tmp;
238 }
239
b576bf60 240 $lastweek = date('YmdHis', time() - 7*24*60*60);
0337d704 241
a3a049fc 242 // recupere les notifs du watcher $uid, sans detail sur le watcher,
a7de4ef7 243 // depuis la semaine dernière, meme ceux sans surveillance, ordonnés
0337d704 244 $res = select_notifs(false, $uid, $lastweek);
a3a049fc 245 while($tmp = $res->next()) {
a2a1c2f2
FB
246 if ($tmp['cid'] == WATCH_FICHE) {
247 $tmp['data'] = get_profile_change_details($tmp, $lastweek);
248 }
a3a049fc 249 $this->_data[$tmp['cid']][$tmp['promo']][] = $tmp;
250 }
0337d704 251
a3a049fc 252 if($up) {
253 XDB::execute('UPDATE auth_user_quick SET watch_last=NOW() WHERE user_id={?}', $uid);
254 }
0337d704 255 }
256}
257
258// }}}
259// {{{ class Watch
260
eaf30d86 261class Watch
612a2d8a 262{
263 public $_uid;
264 public $_promos;
265 public $_nonins;
266 public $_cats = Array();
267 public $_subs;
268 public $watch_contacts;
269 public $watch_mail;
270
271 public function __construct($uid)
272 {
a3a049fc 273 $this->_uid = $uid;
274 $this->_promos = new PromoNotifs($uid);
275 $this->_nonins = new NoninsNotifs($uid);
276 $this->_subs = new WatchSub($uid);
277 $res = XDB::query("SELECT FIND_IN_SET('contacts',watch_flags),FIND_IN_SET('mail',watch_flags)
3ebe4a0a
FB
278 FROM auth_user_quick
279 WHERE user_id={?}", $uid);
a3a049fc 280 list($this->watch_contacts,$this->watch_mail) = $res->fetchOneRow();
281
282 $res = XDB::iterator("SELECT * FROM watch_cat");
283 while($tmp = $res->next()) {
0337d704 284 $this->_cats[$tmp['id']] = $tmp;
285 }
286 }
287
612a2d8a 288 public function saveFlags()
289 {
a3a049fc 290 $flags = "";
291 if ($this->watch_contacts)
292 $flags = "contacts";
293 if ($this->watch_mail)
294 $flags .= ($flags ? ',' : '')."mail";
295 XDB::execute('UPDATE auth_user_quick SET watch_flags={?} WHERE user_id={?}',
612a2d8a 296 $flags, $this->_uid);
0337d704 297 }
298
612a2d8a 299 public function cats()
300 {
a3a049fc 301 return $this->_cats;
0337d704 302 }
303
612a2d8a 304 public function subs($i)
305 {
a3a049fc 306 return $this->_subs->_data[$i];
0337d704 307 }
a3a049fc 308
612a2d8a 309 public function promos()
310 {
a3a049fc 311 return $this->_promos->toRanges();
0337d704 312 }
a3a049fc 313
612a2d8a 314 public function nonins()
315 {
a3a049fc 316 return $this->_nonins->_data;
0337d704 317 }
318}
319
320// }}}
321// {{{ class WatchSub
322
612a2d8a 323class WatchSub
324{
325 public $_uid;
326 public $_data = Array();
0337d704 327
612a2d8a 328 public function __construct($uid)
329 {
a3a049fc 330 $this->_uid = $uid;
331 $res = XDB::iterRow('SELECT cid FROM watch_sub WHERE uid={?}', $uid);
332 while(list($c) = $res->next()) {
0337d704 333 $this->_data[$c] = $c;
334 }
335 }
336
612a2d8a 337 public function update($ind)
338 {
a3a049fc 339 $this->_data = Array();
340 XDB::execute('DELETE FROM watch_sub WHERE uid={?}', $this->_uid);
5e2307dc 341 foreach (Env::v($ind) as $key=>$val) {
a3a049fc 342 XDB::query('INSERT INTO watch_sub SELECT {?},id FROM watch_cat WHERE id={?}', $this->_uid, $key);
0380bf85 343 if(XDB::affectedRows()) {
0337d704 344 $this->_data[$key] = $key;
345 }
a3a049fc 346 }
0337d704 347 }
348}
349
350// }}}
351// {{{ class PromoNotifs
352
612a2d8a 353class PromoNotifs
354{
355 public $_uid;
356 public $_data = Array();
0337d704 357
612a2d8a 358 public function __construct($uid)
359 {
a3a049fc 360 $this->_uid = $uid;
361 $res = XDB::iterRow('SELECT promo FROM watch_promo WHERE uid={?} ORDER BY promo', $uid);
362 while (list($p) = $res->next()) {
0337d704 363 $this->_data[intval($p)] = intval($p);
364 }
365 }
366
612a2d8a 367 public function add($p)
368 {
a3a049fc 369 $promo = intval($p);
370 XDB::execute('REPLACE INTO watch_promo (uid,promo) VALUES({?},{?})', $this->_uid, $promo);
371 $this->_data[$promo] = $promo;
372 asort($this->_data);
0337d704 373 }
a3a049fc 374
612a2d8a 375 public function del($p)
376 {
a3a049fc 377 $promo = intval($p);
378 XDB::execute('DELETE FROM watch_promo WHERE uid={?} AND promo={?}', $this->_uid, $promo);
379 unset($this->_data[$promo]);
0337d704 380 }
a3a049fc 381
612a2d8a 382 public function addRange($_p1,$_p2)
383 {
a3a049fc 384 $p1 = intval($_p1);
385 $p2 = intval($_p2);
386 $values = Array();
387 for($i = min($p1,$p2); $i<=max($p1,$p2); $i++) {
388 $values[] = "('{$this->_uid}',$i)";
389 $this->_data[$i] = $i;
390 }
391 XDB::execute('REPLACE INTO watch_promo (uid,promo) VALUES '.join(',',$values));
392 asort($this->_data);
0337d704 393 }
394
612a2d8a 395 public function delRange($_p1,$_p2)
396 {
a3a049fc 397 $p1 = intval($_p1);
398 $p2 = intval($_p2);
399 $where = Array();
400 for($i = min($p1,$p2); $i<=max($p1,$p2); $i++) {
401 $where[] = "promo=$i";
402 unset($this->_data[$i]);
403 }
404 XDB::execute('DELETE FROM watch_promo WHERE uid={?} AND ('.join(' OR ',$where).')', $this->_uid);
0337d704 405 }
406
612a2d8a 407 public function toRanges()
408 {
a3a049fc 409 $ranges = Array();
410 $I = Array();
411 foreach($this->_data as $promo) {
412 if(!isset($I[0])) {
413 $I = Array($promo,$promo);
414 }
415 elseif($I[1]+1 == $promo) {
416 $I[1] ++;
417 }
418 else {
419 $ranges[] = $I;
420 $I = Array($promo,$promo);
421 }
422 }
423 if(isset($I[0])) $ranges[] = $I;
424 return $ranges;
0337d704 425 }
426}
427
428// }}}
429// {{{ class NoninsNotifs
430
612a2d8a 431class NoninsNotifs
432{
433 public $_uid;
434 public $_data = Array();
0337d704 435
612a2d8a 436 public function __construct($uid)
437 {
a3a049fc 438 $this->_uid = $uid;
439 $res = XDB::iterator("SELECT u.prenom,IF(u.nom_usage='',u.nom,u.nom_usage) AS nom, u.promo, u.user_id
612a2d8a 440 FROM watch_nonins AS w
441 INNER JOIN auth_user_md5 AS u ON (u.user_id = w.ni_id)
442 WHERE w.uid = {?}
443 ORDER BY promo,nom", $uid);
a3a049fc 444 while($tmp = $res->next()) {
0337d704 445 $this->_data[$tmp['user_id']] = $tmp;
446 }
447 }
448
612a2d8a 449 public function del($p)
450 {
a3a049fc 451 unset($this->_data["$p"]);
3ebe4a0a 452 XDB::execute('DELETE FROM watch_nonins WHERE uid={?} AND ni_id={?}', $this->_uid, $p);
0337d704 453 }
454
612a2d8a 455 public function add($p)
456 {
88e31f35 457 XDB::execute('INSERT IGNORE INTO watch_nonins (uid,ni_id) VALUES({?},{?})', $this->_uid, $p);
a3a049fc 458 $res = XDB::query('SELECT prenom,IF(nom_usage="",nom,nom_usage) AS nom,promo,user_id
612a2d8a 459 FROM auth_user_md5
460 WHERE user_id={?}', $p);
a3a049fc 461 $this->_data["$p"] = $res->fetchOneAssoc();
0337d704 462 }
463}
a3a049fc 464
0337d704 465// }}}
466
a7de4ef7 467// vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
0337d704 468?>