Fix ids_from_emails (errors when invalid @x.org emails were given).
[platal.git] / include / notifs.inc.php
CommitLineData
0337d704 1<?php
2/***************************************************************************
9f5bd98e 3 * Copyright (C) 2003-2010 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
009b8ab7 22abstract class WatchOperation
612a2d8a 23{
e947c421 24 private static $false = null;
ee682285 25
009b8ab7
FB
26 public function getTitle($count = 0)
27 {
28 if ($count == 1) {
29 return str_replace(array('$x', '$s'), '', $this->title);
30 } else {
31 return str_replace(array('$x', '$s'), array('x', 's'), $this->title);
32 }
33 }
34
e947c421 35 public function getCondition(Watch $watch)
009b8ab7 36 {
e947c421
FB
37 if (!$watch->user()->watchType($this->flag)) {
38 if (!self::$false) {
39 self::$false = new PFC_False();
40 }
41 return self::$false;
009b8ab7 42 } else {
e947c421 43 return $this->buildCondition($watch);
009b8ab7
FB
44 }
45 }
46
e947c421 47 abstract protected function buildCondition(Watch $watch);
009b8ab7
FB
48 abstract public function getOrder();
49 abstract public function getDate(PlUser &$user);
50
c350577b
FB
51 public function publicationDate(PlUser &$user)
52 {
53 return $this->getDate($user);
54 }
55
009b8ab7
FB
56 public function seen(PlUser &$user, $last)
57 {
58 return strtotime($this->getDate($user)) > $last;
59 }
c350577b
FB
60
61 public function getData(PlUser &$user)
62 {
63 return null;
64 }
009b8ab7
FB
65}
66
67class WatchProfileUpdate extends WatchOperation
68{
e947c421
FB
69 private static $order = null;
70
009b8ab7 71 public $flag = 'profile';
e6957daf 72 public $title = 'Mise$s à jour de fiche';
92d3e726 73 private $date = null;
a3a049fc 74
4e7bf1e0 75 public static function register(Profile &$profile, $field)
612a2d8a 76 {
bf0e2a48 77 XDB::execute('REPLACE INTO watch_profile (pid, ts, field)
4e7bf1e0
FB
78 VALUES ({?}, NOW(), {?})',
79 $profile->id(), $field);
0337d704 80 }
81
e947c421 82 protected function buildCondition(Watch $watch)
612a2d8a 83 {
92d3e726 84 $this->date = $watch->date();
e947c421
FB
85 return new PFC_And(new UFC_ProfileUpdated('>', $watch->date()),
86 $watch->contactCondition());
009b8ab7
FB
87 }
88
89 public function getOrder()
90 {
e947c421
FB
91 if (!self::$order) {
92 self::$order = new UFO_ProfileUpdate();
93 }
94 return self::$order;
009b8ab7
FB
95 }
96
97 public function getDate(PlUser &$user)
98 {
99 return $user->profile()->last_change;
0337d704 100 }
ee682285
FB
101
102 static private $descriptions = array('search_names' => 'L\'un de ses noms',
103 'freetext' => 'Le texte libre',
104 'mobile' => 'Son numéro de téléphone portable',
105 'nationalite' => 'Sa nationalité',
106 'nationalite2' => 'Sa seconde nationalité',
107 'nationalite3' => 'Sa troisième nationalité',
108 'nick' => 'Son surnom',
109 'networking' => 'La liste de ses adresses de networking',
110 'edus' => 'Ses formations',
111 'addresses' => 'Ses adresses',
112 'section' => 'Sa section sportive',
113 'binets' => 'La liste de ses binets',
114 'medals' => 'Ses décorations',
115 'cv' => 'Son Curriculum Vitae',
116 'corps' => 'Son Corps d\'État',
117 'jobs' => 'Ses informations professionnelles',
118 'photo' => 'Sa photographie');
119 public function getData(PlUser &$user)
120 {
bf0e2a48 121 $data = XDB::fetchColumn("SELECT field
ee682285 122 FROM watch_profile
bf0e2a48
SJ
123 WHERE pid = {?} AND ts > FROM_UNIXTIME({?}) AND field != ''
124 ORDER BY ts",
125 $user->profile()->id(), $this->date);
ee682285
FB
126 if (count($data) == 0) {
127 return null;
128 } else {
129 $text = array();
130 foreach ($data as $f) {
131 $text[] = self::$descriptions[$f];
132 }
133 return $text;
134 }
135 }
0337d704 136}
137
009b8ab7 138class WatchRegistration extends WatchOperation
612a2d8a 139{
e947c421
FB
140 private static $order = null;
141
009b8ab7
FB
142 public $flag = 'registration';
143 public $title = 'Inscription$s';
144
e947c421 145 protected function buildCondition(Watch $watch)
009b8ab7 146 {
e947c421
FB
147 return new PFC_And(new UFC_Registered(false, '>', $watch->date()),
148 new PFC_Or($watch->contactCondition(),
149 $watch->promoCondition()));
009b8ab7
FB
150 }
151
152 public function getOrder()
153 {
e947c421
FB
154 if (!self::$order) {
155 self::$order = new UFO_Registration();
156 }
157 return self::$order;
009b8ab7 158 }
0337d704 159
009b8ab7 160 public function getDate(PlUser &$user)
612a2d8a 161 {
009b8ab7 162 return $user->registration_date;
0337d704 163 }
164}
165
009b8ab7 166class WatchDeath extends WatchOperation
612a2d8a 167{
e947c421
FB
168 private static $order = null;
169
009b8ab7
FB
170 public $flag = 'death';
171 public $title = 'Décès';
172
e947c421 173 protected function buildCondition(Watch $watch)
009b8ab7 174 {
e947c421
FB
175 return new PFC_And(new UFC_Dead('>', $watch->date(), true),
176 new PFC_Or($watch->contactCondition(),
177 $watch->promoCondition()));
009b8ab7 178 }
0337d704 179
009b8ab7 180 public function getOrder()
612a2d8a 181 {
e947c421
FB
182 if (!self::$order) {
183 self::$order = new UFO_Death();
184 }
185 return self::$order;
009b8ab7
FB
186 }
187
188 public function getDate(PlUser &$user)
189 {
190 return $user->profile()->deathdate;
191 }
192
c350577b
FB
193 public function publicationDate(PlUser &$user)
194 {
195 return $user->profile()->deathdate_rec;
196 }
197
009b8ab7
FB
198 public function seen(PlUser &$user, $last)
199 {
200 return strtotime($user->profile()->deathdate_rec) > $last;
0337d704 201 }
202}
203
009b8ab7 204class WatchBirthday extends WatchOperation
612a2d8a 205{
009b8ab7 206 const WATCH_LIMIT = 604800; // 1 week
0337d704 207
e947c421
FB
208 private static $order = null;
209
009b8ab7
FB
210 public $flag = 'birthday';
211 public $title = 'Anniversaire$s';
212
e947c421 213 protected function buildCondition(Watch $watch)
612a2d8a 214 {
e7ffcfd4
FB
215 $select_date = new PFC_OR(new UFC_Birthday('=', time()),
216 new PFC_And(new UFC_Birthday('<=', time() + self::WATCH_LIMIT),
e947c421
FB
217 new UFC_Birthday('>', $watch->date() + self::WATCH_LIMIT)));
218 $profile = $watch->profile();
219 $cond = $watch->contactCondition();
e7ffcfd4 220 if ($profile) {
e947c421
FB
221 $cond = new PFC_Or($cond,
222 new PFC_And($watch->promoCondition(),
e7ffcfd4
FB
223 new UFC_Promo('>=', $profile->mainGrade(), $profile->yearpromo() - 1),
224 new UFC_Promo('<=', $profile->mainGrade(), $profile->yearpromo() + 1)));
225 }
226 return new PFC_And($select_date, $cond);
009b8ab7
FB
227 }
228
229 public function getOrder()
230 {
e947c421
FB
231 if (!self::$order) {
232 self::$order = new UFO_Birthday();
233 }
234 return self::$order;
009b8ab7
FB
235 }
236
237 public function getDate(PlUser &$user)
238 {
239 return $user->profile()->next_birthday;
240 }
241
c350577b
FB
242 public function publicationDate(PlUser &$user)
243 {
244 return date('Y-m-d', strtotime($user->profile()->next_birthday) - self::WATCH_LIMIT);
245 }
246
009b8ab7
FB
247 public function seen(PlUser &$user, $last)
248 {
249 $birthday = strtotime($user->profile()->next_birthday);
250 return $birthday > $last + self::WATCH_LIMIT
251 || date('Ymd', $birthday) == date('Ymd');
0337d704 252 }
253}
a3a049fc 254
7e735012
FB
255class Watch
256{
257 private static $classes = array('WatchRegistration',
258 'WatchProfileUpdate',
259 'WatchDeath',
260 'WatchBirthday');
e947c421 261 private static $events = array();
7e735012 262
e947c421
FB
263 private $user = null;
264 private $date = null;
265 private $contactCond = null;
266 private $promoCond = null;
267
268 private $filters = array();
269
270 public function __construct(PlUser $user, $date = null)
e7ffcfd4 271 {
e947c421
FB
272 $this->user = $user;
273 $this->date = self::getDate($user, $date);
274 }
275
276 public function user()
277 {
278 return $this->user;
279 }
280
281 public function profile()
282 {
283 return $this->user->profile();
284 }
285
286 public function date()
287 {
288 return $this->date;
289 }
290
291 public function contactCondition()
292 {
293 if (!$this->contactCond) {
294 $this->contactCond = new UFC_WatchContact($this->user);
e7ffcfd4 295 }
e947c421
FB
296 return $this->contactCond;
297 }
298
299 public function promoCondition()
300 {
301 if (!$this->promoCond) {
302 $this->promoCond = new UFC_WatchPromo($this->user);
303 }
304 return $this->promoCond;
e7ffcfd4
FB
305 }
306
e947c421 307 private function fetchEventWatch($class)
7e735012 308 {
e947c421
FB
309 if (!isset(self::$events[$class])) {
310 self::$events[$class] = new $class();
311 }
312 return self::$events[$class];
7e735012
FB
313 }
314
e947c421
FB
315 private function fetchFilter($class)
316 {
317
318 if (!isset($this->filters[$class])) {
319 $event = $this->fetchEventWatch($class);
320 $this->filters[$class] = new UserFilter($event->getCondition($this),
321 array($event->getOrder(), new UFO_Name(Profile::DN_SORT)));
322 }
323 return $this->filters[$class];
324 }
325
326 public function count()
7e735012
FB
327 {
328 $count = 0;
329 foreach (self::$classes as $class) {
e947c421
FB
330 $uf = $this->fetchFilter($class);
331 $count += $uf->getTotalCount();
7e735012
FB
332 }
333 return $count;
334 }
009b8ab7
FB
335
336
e947c421 337 private function fetchEvents($class)
009b8ab7 338 {
e947c421
FB
339 $obj = $this->fetchEventWatch($class);
340 $uf = $this->fetchFilter($class);
009b8ab7
FB
341 $users = $uf->getUsers();
342 if (count($users) == 0) {
343 return null;
344 } else {
e73ae835
FB
345 return array('type' => $obj->flag,
346 'operation' => $obj,
009b8ab7
FB
347 'title' => $obj->getTitle(count($users)),
348 'users' => $users);
349 }
350 }
351
e947c421 352 public function events()
009b8ab7 353 {
009b8ab7
FB
354 $events = array();
355 foreach (self::$classes as $class) {
e947c421 356 $e = $this->fetchEvents($class);
009b8ab7
FB
357 if (!is_null($e)) {
358 $events[] = $e;
359 }
360 }
361 return $events;
362 }
e947c421
FB
363
364
365 private static function getDate(PlUser &$user, $date)
366 {
367 if (is_null($date)) {
368 $date = $user->watchLast();
369 $limit = time() - (7 * 86400);
370 if ($date < $limit) {
371 $date = $limit;
372 }
373 }
374 return $date;
375 }
376
377 public static function getCount(PlUser &$user, $date = null)
378 {
379 $watch = new Watch($user, $date);
380 return $watch->count();
381 }
382
383 public static function getEvents(PlUser &$user, $date = null)
384 {
385 $watch = new Watch($user, $date);
386 return $watch->events();
387 }
7e735012
FB
388}
389
390// vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
0337d704 391?>