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