Merge remote branch 'origin/platal-1.0.1'
[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 = 'Mise$s à jour de fiche';
73 private $date = null;
74
75 public static function register(Profile &$profile, $field)
76 {
77 XDB::execute('REPLACE INTO watch_profile (pid, ts, field)
78 VALUES ({?}, NOW(), {?})',
79 $profile->id(), $field);
80 }
81
82 protected function buildCondition(Watch $watch)
83 {
84 $this->date = $watch->date();
85 return new PFC_And(new UFC_ProfileUpdated('>', $watch->date()),
86 $watch->contactCondition());
87 }
88
89 public function getOrder()
90 {
91 if (!self::$order) {
92 self::$order = new UFO_ProfileUpdate();
93 }
94 return self::$order;
95 }
96
97 public function getDate(PlUser &$user)
98 {
99 return $user->profile()->last_change;
100 }
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 {
121 $data = XDB::fetchColumn("SELECT field
122 FROM watch_profile
123 WHERE pid = {?} AND ts > FROM_UNIXTIME({?}) AND field != ''
124 ORDER BY ts",
125 $user->profile()->id(), $this->date);
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 }
136 }
137
138 class WatchRegistration extends WatchOperation
139 {
140 private static $order = null;
141
142 public $flag = 'registration';
143 public $title = 'Inscription$s';
144
145 protected function buildCondition(Watch $watch)
146 {
147 return new PFC_And(new UFC_Registered(false, '>', $watch->date()),
148 new PFC_Or($watch->contactCondition(),
149 $watch->promoCondition()));
150 }
151
152 public function getOrder()
153 {
154 if (!self::$order) {
155 self::$order = new UFO_Registration();
156 }
157 return self::$order;
158 }
159
160 public function getDate(PlUser &$user)
161 {
162 return $user->registration_date;
163 }
164 }
165
166 class WatchDeath extends WatchOperation
167 {
168 private static $order = null;
169
170 public $flag = 'death';
171 public $title = 'Décès';
172
173 protected function buildCondition(Watch $watch)
174 {
175 return new PFC_And(new UFC_Dead('>', $watch->date(), true),
176 new PFC_Or($watch->contactCondition(),
177 $watch->promoCondition()));
178 }
179
180 public function getOrder()
181 {
182 if (!self::$order) {
183 self::$order = new UFO_Death();
184 }
185 return self::$order;
186 }
187
188 public function getDate(PlUser &$user)
189 {
190 return $user->profile()->deathdate;
191 }
192
193 public function publicationDate(PlUser &$user)
194 {
195 return $user->profile()->deathdate_rec;
196 }
197
198 public function seen(PlUser &$user, $last)
199 {
200 return strtotime($user->profile()->deathdate_rec) > $last;
201 }
202 }
203
204 class WatchBirthday extends WatchOperation
205 {
206 const WATCH_LIMIT = 604800; // 1 week
207
208 private static $order = null;
209
210 public $flag = 'birthday';
211 public $title = 'Anniversaire$s';
212
213 protected function buildCondition(Watch $watch)
214 {
215 $select_date = new PFC_OR(new UFC_Birthday('=', time()),
216 new PFC_And(new UFC_Birthday('<=', time() + self::WATCH_LIMIT),
217 new UFC_Birthday('>', $watch->date() + self::WATCH_LIMIT)));
218 $profile = $watch->profile();
219 $cond = $watch->contactCondition();
220 if ($profile) {
221 $cond = new PFC_Or($cond,
222 new PFC_And($watch->promoCondition(),
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);
227 }
228
229 public function getOrder()
230 {
231 if (!self::$order) {
232 self::$order = new UFO_Birthday();
233 }
234 return self::$order;
235 }
236
237 public function getDate(PlUser &$user)
238 {
239 return $user->profile()->next_birthday;
240 }
241
242 public function publicationDate(PlUser &$user)
243 {
244 return date('Y-m-d', strtotime($user->profile()->next_birthday) - self::WATCH_LIMIT);
245 }
246
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');
252 }
253 }
254
255 class Watch
256 {
257 private static $classes = array('WatchRegistration',
258 'WatchProfileUpdate',
259 'WatchDeath',
260 'WatchBirthday');
261 private static $events = array();
262
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)
271 {
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);
295 }
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;
305 }
306
307 private function fetchEventWatch($class)
308 {
309 if (!isset(self::$events[$class])) {
310 self::$events[$class] = new $class();
311 }
312 return self::$events[$class];
313 }
314
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()
327 {
328 $count = 0;
329 foreach (self::$classes as $class) {
330 $uf = $this->fetchFilter($class);
331 $count += $uf->getTotalCount();
332 }
333 return $count;
334 }
335
336
337 private function fetchEvents($class)
338 {
339 $obj = $this->fetchEventWatch($class);
340 $uf = $this->fetchFilter($class);
341 $users = $uf->getUsers();
342 if (count($users) == 0) {
343 return null;
344 } else {
345 return array('type' => $obj->flag,
346 'operation' => $obj,
347 'title' => $obj->getTitle(count($users)),
348 'users' => $users);
349 }
350 }
351
352 public function events()
353 {
354 $events = array();
355 foreach (self::$classes as $class) {
356 $e = $this->fetchEvents($class);
357 if (!is_null($e)) {
358 $events[] = $e;
359 }
360 }
361 return $events;
362 }
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 }
388 }
389
390 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
391 ?>