Typo.
[platal.git] / include / notifs.inc.php
1 <?php
2 /***************************************************************************
3 * Copyright (C) 2003-2010 Polytechnique.org *
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
22 abstract class WatchOperation
23 {
24 private static $false = null;
25
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
35 public function getCondition(Watch $watch)
36 {
37 if (!$watch->user()->watchType($this->flag)) {
38 if (!self::$false) {
39 self::$false = new PFC_False();
40 }
41 return self::$false;
42 } else {
43 return $this->buildCondition($watch);
44 }
45 }
46
47 abstract protected function buildCondition(Watch $watch);
48 abstract public function getOrder();
49 abstract public function getDate(PlUser &$user);
50
51 public function publicationDate(PlUser &$user)
52 {
53 return $this->getDate($user);
54 }
55
56 public function seen(PlUser &$user, $last)
57 {
58 return strtotime($this->getDate($user)) > $last;
59 }
60
61 public function getData(PlUser &$user)
62 {
63 return null;
64 }
65 }
66
67 class WatchProfileUpdate extends WatchOperation
68 {
69 private static $order = null;
70
71 public $flag = 'profile';
72 public $title = 'Mises à jour de fiche';
73
74 public static function register(Profile &$profile, $field)
75 {
76 XDB::execute('REPLACE INTO watch_profile (pid, ts, field)
77 VALUES ({?}, NOW(), {?})',
78 $profile->id(), $field);
79 }
80
81 protected function buildCondition(Watch $watch)
82 {
83 return new PFC_And(new UFC_ProfileUpdated('>', $watch->date()),
84 $watch->contactCondition());
85 }
86
87 public function getOrder()
88 {
89 if (!self::$order) {
90 self::$order = new UFO_ProfileUpdate();
91 }
92 return self::$order;
93 }
94
95 public function getDate(PlUser &$user)
96 {
97 return $user->profile()->last_change;
98 }
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 {
119 $data = XDB::fetchColumn("SELECT field
120 FROM watch_profile
121 WHERE pid = {?} AND ts > FROM_UNIXTIME({?}) AND field != ''
122 ORDER BY ts",
123 $user->profile()->id(), $this->date);
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 }
134 }
135
136 class WatchRegistration extends WatchOperation
137 {
138 private static $order = null;
139
140 public $flag = 'registration';
141 public $title = 'Inscription$s';
142
143 protected function buildCondition(Watch $watch)
144 {
145 return new PFC_And(new UFC_Registered(false, '>', $watch->date()),
146 new PFC_Or($watch->contactCondition(),
147 $watch->promoCondition()));
148 }
149
150 public function getOrder()
151 {
152 if (!self::$order) {
153 self::$order = new UFO_Registration();
154 }
155 return self::$order;
156 }
157
158 public function getDate(PlUser &$user)
159 {
160 return $user->registration_date;
161 }
162 }
163
164 class WatchDeath extends WatchOperation
165 {
166 private static $order = null;
167
168 public $flag = 'death';
169 public $title = 'Décès';
170
171 protected function buildCondition(Watch $watch)
172 {
173 return new PFC_And(new UFC_Dead('>', $watch->date(), true),
174 new PFC_Or($watch->contactCondition(),
175 $watch->promoCondition()));
176 }
177
178 public function getOrder()
179 {
180 if (!self::$order) {
181 self::$order = new UFO_Death();
182 }
183 return self::$order;
184 }
185
186 public function getDate(PlUser &$user)
187 {
188 return $user->profile()->deathdate;
189 }
190
191 public function publicationDate(PlUser &$user)
192 {
193 return $user->profile()->deathdate_rec;
194 }
195
196 public function seen(PlUser &$user, $last)
197 {
198 return strtotime($user->profile()->deathdate_rec) > $last;
199 }
200 }
201
202 class WatchBirthday extends WatchOperation
203 {
204 const WATCH_LIMIT = 604800; // 1 week
205
206 private static $order = null;
207
208 public $flag = 'birthday';
209 public $title = 'Anniversaire$s';
210
211 protected function buildCondition(Watch $watch)
212 {
213 $select_date = new PFC_OR(new UFC_Birthday('=', time()),
214 new PFC_And(new UFC_Birthday('<=', time() + self::WATCH_LIMIT),
215 new UFC_Birthday('>', $watch->date() + self::WATCH_LIMIT)));
216 $profile = $watch->profile();
217 $cond = $watch->contactCondition();
218 if ($profile) {
219 $cond = new PFC_Or($cond,
220 new PFC_And($watch->promoCondition(),
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);
225 }
226
227 public function getOrder()
228 {
229 if (!self::$order) {
230 self::$order = new UFO_Birthday();
231 }
232 return self::$order;
233 }
234
235 public function getDate(PlUser &$user)
236 {
237 return $user->profile()->next_birthday;
238 }
239
240 public function publicationDate(PlUser &$user)
241 {
242 return date('Y-m-d', strtotime($user->profile()->next_birthday) - self::WATCH_LIMIT);
243 }
244
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');
250 }
251 }
252
253 class Watch
254 {
255 private static $classes = array('WatchRegistration',
256 'WatchProfileUpdate',
257 'WatchDeath',
258 'WatchBirthday');
259 private static $events = array();
260
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)
269 {
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);
293 }
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;
303 }
304
305 private function fetchEventWatch($class)
306 {
307 if (!isset(self::$events[$class])) {
308 self::$events[$class] = new $class();
309 }
310 return self::$events[$class];
311 }
312
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()
325 {
326 $count = 0;
327 foreach (self::$classes as $class) {
328 $uf = $this->fetchFilter($class);
329 $count += $uf->getTotalCount();
330 }
331 return $count;
332 }
333
334
335 private function fetchEvents($class)
336 {
337 $obj = $this->fetchEventWatch($class);
338 $uf = $this->fetchFilter($class);
339 $users = $uf->getUsers();
340 if (count($users) == 0) {
341 return null;
342 } else {
343 return array('type' => $obj->flag,
344 'operation' => $obj,
345 'title' => $obj->getTitle(count($users)),
346 'users' => $users);
347 }
348 }
349
350 public function events()
351 {
352 $events = array();
353 foreach (self::$classes as $class) {
354 $e = $this->fetchEvents($class);
355 if (!is_null($e)) {
356 $events[] = $e;
357 }
358 }
359 return $events;
360 }
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 }
386 }
387
388 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
389 ?>