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