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