2 /***************************************************************************
3 * Copyright (C) 2003-2010 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';
74 public static function register(Profile
&$profile, $field)
76 XDB
::execute('REPLACE INTO watch_profile (pid, ts, field)
77 VALUES ({?}, NOW(), {?})',
78 $profile->id(), $field);
81 protected function buildCondition(Watch
$watch)
83 return new PFC_And(new UFC_ProfileUpdated('>', $watch->date()),
84 $watch->contactCondition());
87 public function getOrder()
90 self
::$order = new UFO_ProfileUpdate();
95 public function getDate(PlUser
&$user)
97 return $user->profile()->last_change
;
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)
119 $data = XDB
::fetchColumn("SELECT field
121 WHERE pid = {?} AND ts > FROM_UNIXTIME({?}) AND field != ''
123 $user->profile()->id(), $this->date
);
124 if (count($data) == 0) {
128 foreach ($data as $f) {
129 $text[] = self
::$descriptions[$f];
136 class WatchRegistration
extends WatchOperation
138 private static $order = null
;
140 public $flag = 'registration';
141 public $title = 'Inscription$s';
143 protected function buildCondition(Watch
$watch)
145 return new PFC_And(new UFC_Registered(false
, '>', $watch->date()),
146 new PFC_Or($watch->contactCondition(),
147 $watch->promoCondition()));
150 public function getOrder()
153 self
::$order = new UFO_Registration();
158 public function getDate(PlUser
&$user)
160 return $user->registration_date
;
164 class WatchDeath
extends WatchOperation
166 private static $order = null
;
168 public $flag = 'death';
169 public $title = 'Décès';
171 protected function buildCondition(Watch
$watch)
173 return new PFC_And(new UFC_Dead('>', $watch->date(), true
),
174 new PFC_Or($watch->contactCondition(),
175 $watch->promoCondition()));
178 public function getOrder()
181 self
::$order = new UFO_Death();
186 public function getDate(PlUser
&$user)
188 return $user->profile()->deathdate
;
191 public function publicationDate(PlUser
&$user)
193 return $user->profile()->deathdate_rec
;
196 public function seen(PlUser
&$user, $last)
198 return strtotime($user->profile()->deathdate_rec
) > $last;
202 class WatchBirthday
extends WatchOperation
204 const WATCH_LIMIT
= 604800; // 1 week
206 private static $order = null
;
208 public $flag = 'birthday';
209 public $title = 'Anniversaire$s';
211 protected function buildCondition(Watch
$watch)
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();
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)));
224 return new PFC_And($select_date, $cond);
227 public function getOrder()
230 self
::$order = new UFO_Birthday();
235 public function getDate(PlUser
&$user)
237 return $user->profile()->next_birthday
;
240 public function publicationDate(PlUser
&$user)
242 return date('Y-m-d', strtotime($user->profile()->next_birthday
) - self
::WATCH_LIMIT
);
245 public function seen(PlUser
&$user, $last)
247 $birthday = strtotime($user->profile()->next_birthday
);
248 return $birthday > $last + self
::WATCH_LIMIT
249 ||
date('Ymd', $birthday) == date('Ymd');
255 private static $classes = array('WatchRegistration',
256 'WatchProfileUpdate',
259 private static $events = array();
261 private $user = null
;
262 private $date = null
;
263 private $contactCond = null
;
264 private $promoCond = null
;
266 private $filters = array();
268 public function __construct(PlUser
$user, $date = null
)
271 $this->date
= self
::getDate($user, $date);
274 public function user()
279 public function profile()
281 return $this->user
->profile();
284 public function date()
289 public function contactCondition()
291 if (!$this->contactCond
) {
292 $this->contactCond
= new UFC_WatchContact($this->user
);
294 return $this->contactCond
;
297 public function promoCondition()
299 if (!$this->promoCond
) {
300 $this->promoCond
= new UFC_WatchPromo($this->user
);
302 return $this->promoCond
;
305 private function fetchEventWatch($class)
307 if (!isset(self
::$events[$class])) {
308 self
::$events[$class] = new $class();
310 return self
::$events[$class];
313 private function fetchFilter($class)
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
)));
321 return $this->filters
[$class];
324 public function count()
327 foreach (self
::$classes as $class) {
328 $uf = $this->fetchFilter($class);
329 $count +
= $uf->getTotalCount();
335 private function fetchEvents($class)
337 $obj = $this->fetchEventWatch($class);
338 $uf = $this->fetchFilter($class);
339 $users = $uf->getUsers();
340 if (count($users) == 0) {
343 return array('type' => $obj->flag
,
345 'title' => $obj->getTitle(count($users)),
350 public function events()
353 foreach (self
::$classes as $class) {
354 $e = $this->fetchEvents($class);
363 private static function getDate(PlUser
&$user, $date)
365 if (is_null($date)) {
366 $date = $user->watchLast();
367 $limit = time() - (7 * 86400);
368 if ($date < $limit) {
375 public static function getCount(PlUser
&$user, $date = null
)
377 $watch = new Watch($user, $date);
378 return $watch->count();
381 public static function getEvents(PlUser
&$user, $date = null
)
383 $watch = new Watch($user, $date);
384 return $watch->events();
388 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: