2 /***************************************************************************
3 * Copyright (C) 2003-2013 Polytechnique.org *
4 * http://opensource.polytechnique.org/ *
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. *
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. *
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 *
19 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *
20 ***************************************************************************/
22 abstract class WatchOperation
24 private static $false = null
;
26 public function getTitle($count = 0)
29 return str_replace(array('$x', '$s'), '', $this->title
);
31 return str_replace(array('$x', '$s'), array('x', 's'), $this->title
);
35 public function getCondition(Watch
$watch)
37 if (!$watch->user()->watchType($this->flag
)) {
39 self
::$false = new PFC_False();
43 return $this->buildCondition($watch);
47 abstract protected function buildCondition(Watch
$watch);
48 abstract public function getOrder();
49 abstract public function getDate(PlUser
$user);
51 public function publicationDate(PlUser
$user)
53 return $this->getDate($user);
56 public function seen(PlUser
$user, $last)
58 return strtotime($this->getDate($user)) > $last;
61 public function getData(PlUser
$user)
67 class WatchProfileUpdate
extends WatchOperation
69 private static $order = null
;
71 public $flag = 'profile';
72 public $title = 'Mise$s à jour de fiche';
75 public static function register(Profile
$profile, $field)
77 XDB
::execute('INSERT INTO watch_profile (pid, ts, field)
78 VALUES ({?}, NOW(), {?})
79 ON DUPLICATE KEY UPDATE ts = NOW()',
80 $profile->id(), $field);
83 protected function buildCondition(Watch
$watch)
85 $this->date
= $watch->date();
86 return new PFC_And(new UFC_ProfileUpdated('>', $watch->date()),
87 $watch->contactCondition());
90 public function getOrder()
93 self
::$order = new UFO_ProfileUpdate();
98 public function getDate(PlUser
$user)
100 return $user->profile()->last_change
;
103 public function getData(PlUser
$user)
105 $data = XDB
::fetchColumn("SELECT field
107 WHERE pid = {?} AND ts > FROM_UNIXTIME({?}) AND field != ''
109 $user->profile()->id(), $this->date
);
110 if (count($data) == 0) {
114 foreach ($data as $f) {
115 $text[] = Profile
::$descriptions[$f];
122 class WatchRegistration
extends WatchOperation
124 private static $order = null
;
126 public $flag = 'registration';
127 public $title = 'Inscription$s';
129 protected function buildCondition(Watch
$watch)
131 return new PFC_And(new UFC_Registered(false
, '>', $watch->date()),
132 new PFC_Or($watch->contactCondition(),
133 $watch->promoCondition(),
134 $watch->groupCondition()));
137 public function getOrder()
140 self
::$order = new UFO_Registration();
145 public function getDate(PlUser
$user)
147 return $user->registration_date
;
151 class WatchDeath
extends WatchOperation
153 private static $order = null
;
155 public $flag = 'death';
156 public $title = 'Décès';
158 protected function buildCondition(Watch
$watch)
160 return new PFC_And(new UFC_Dead('>', $watch->date(), true
),
161 new PFC_Or($watch->contactCondition(),
162 $watch->promoCondition(),
163 $watch->groupCondition()));
166 public function getOrder()
169 self
::$order = new UFO_Death();
174 public function getDate(PlUser
$user)
176 return $user->profile()->deathdate
;
179 public function publicationDate(PlUser
$user)
181 return $user->profile()->deathdate_rec
;
184 public function seen(PlUser
$user, $last)
186 return strtotime($user->profile()->deathdate_rec
) > $last;
190 class WatchBirthday
extends WatchOperation
192 const WATCH_LIMIT
= 604800; // 1 week
194 private static $order = null
;
196 public $flag = 'birthday';
197 public $title = 'Anniversaire$s';
199 protected function buildCondition(Watch
$watch)
201 $not_dead = new PFC_Not(new UFC_Dead());
202 $select_date = new PFC_OR(new UFC_Birthday('=', time()),
203 new PFC_And(new UFC_Birthday('<=', time() + self
::WATCH_LIMIT
),
204 new UFC_Birthday('>', $watch->date() + self
::WATCH_LIMIT
)));
205 $profile = $watch->profile();
206 $cond = $watch->contactCondition();
208 $cond = new PFC_Or($cond,
209 new PFC_And($watch->promoCondition(),
210 new UFC_Promo('>=', $profile->mainGrade(), $profile->yearpromo() - 1),
211 new UFC_Promo('<=', $profile->mainGrade(), $profile->yearpromo() +
1)),
212 $watch->groupCondition());
214 return new PFC_And($not_dead, $select_date, $cond);
217 public function getOrder()
220 self
::$order = new UFO_Birthday();
225 public function getDate(PlUser
$user)
227 return $user->profile()->next_birthday
;
230 public function publicationDate(PlUser
$user)
232 return date('Y-m-d', strtotime($user->profile()->next_birthday
) - self
::WATCH_LIMIT
);
235 public function seen(PlUser
$user, $last)
237 $birthday = strtotime($user->profile()->next_birthday
);
238 return $birthday > $last + self
::WATCH_LIMIT
239 ||
date('Ymd', $birthday) == date('Ymd');
245 private static $classes = array('WatchRegistration',
246 'WatchProfileUpdate',
249 private static $events = array();
251 private $user = null
;
252 private $date = null
;
253 private $contactCond = null
;
254 private $promoCond = null
;
255 private $groupCond = null
;
257 private $filters = array();
259 public function __construct(PlUser
$user, $date = null
)
262 $this->date
= self
::getDate($user, $date);
265 public function user()
270 public function profile()
272 return $this->user
->profile();
275 public function date()
280 public function contactCondition()
282 if (!$this->contactCond
) {
283 $this->contactCond
= new UFC_WatchContact($this->user
);
285 return $this->contactCond
;
288 public function promoCondition()
290 if (!$this->promoCond
) {
291 $this->promoCond
= new UFC_WatchPromo($this->user
);
293 return $this->promoCond
;
296 public function groupCondition()
298 if (!$this->groupCond
) {
299 $this->groupCond
= new UFC_WatchGroup($this->user
);
301 return $this->groupCond
;
304 private function fetchEventWatch($class)
306 if (!isset(self
::$events[$class])) {
307 self
::$events[$class] = new $class();
309 return self
::$events[$class];
312 private function fetchFilter($class)
315 if (!isset($this->filters
[$class])) {
316 $event = $this->fetchEventWatch($class);
317 $this->filters
[$class] = new UserFilter($event->getCondition($this),
318 array($event->getOrder(), new UFO_Name()));
320 return $this->filters
[$class];
323 public function count()
326 foreach (self
::$classes as $class) {
327 $uf = $this->fetchFilter($class);
328 $count +
= $uf->getTotalCount();
334 private function fetchEvents($class)
336 $obj = $this->fetchEventWatch($class);
337 $uf = $this->fetchFilter($class);
338 $users = $uf->getUsers();
339 if (count($users) == 0) {
342 return array('type' => $obj->flag
,
344 'title' => $obj->getTitle(count($users)),
349 public function events()
352 foreach (self
::$classes as $class) {
353 $e = $this->fetchEvents($class);
362 private static function getDate(PlUser
$user, $date)
364 if (is_null($date)) {
365 $date = $user->watchLast();
366 $limit = time() - (7 * 86400);
367 if ($date < $limit) {
374 public static function getCount(PlUser
$user, $date = null
)
376 $watch = new Watch($user, $date);
377 return $watch->count();
380 public static function getEvents(PlUser
$user, $date = null
)
382 $watch = new Watch($user, $date);
383 return $watch->events();
387 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: