045adee37fc7aafdfad7e5f484e8f691a089bdba
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';
75 public static function register(Profile
&$profile, $field)
77 XDB
::execute('REPLACE INTO watch_profile (pid, ts, field)
78 VALUES ({?}, NOW(), {?})',
79 $profile->id(), $field);
82 protected function buildCondition(Watch
$watch)
84 $this->date
= $watch->date();
85 return new PFC_And(new UFC_ProfileUpdated('>', $watch->date()),
86 $watch->contactCondition());
89 public function getOrder()
92 self
::$order = new UFO_ProfileUpdate();
97 public function getDate(PlUser
&$user)
99 return $user->profile()->last_change
;
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)
121 $data = XDB
::fetchColumn("SELECT field
123 WHERE pid = {?} AND ts > FROM_UNIXTIME({?}) AND field != ''
125 $user->profile()->id(), $this->date
);
126 if (count($data) == 0) {
130 foreach ($data as $f) {
131 $text[] = self
::$descriptions[$f];
138 class WatchRegistration
extends WatchOperation
140 private static $order = null
;
142 public $flag = 'registration';
143 public $title = 'Inscription$s';
145 protected function buildCondition(Watch
$watch)
147 return new PFC_And(new UFC_Registered(false
, '>', $watch->date()),
148 new PFC_Or($watch->contactCondition(),
149 $watch->promoCondition()));
152 public function getOrder()
155 self
::$order = new UFO_Registration();
160 public function getDate(PlUser
&$user)
162 return $user->registration_date
;
166 class WatchDeath
extends WatchOperation
168 private static $order = null
;
170 public $flag = 'death';
171 public $title = 'Décès';
173 protected function buildCondition(Watch
$watch)
175 return new PFC_And(new UFC_Dead('>', $watch->date(), true
),
176 new PFC_Or($watch->contactCondition(),
177 $watch->promoCondition()));
180 public function getOrder()
183 self
::$order = new UFO_Death();
188 public function getDate(PlUser
&$user)
190 return $user->profile()->deathdate
;
193 public function publicationDate(PlUser
&$user)
195 return $user->profile()->deathdate_rec
;
198 public function seen(PlUser
&$user, $last)
200 return strtotime($user->profile()->deathdate_rec
) > $last;
204 class WatchBirthday
extends WatchOperation
206 const WATCH_LIMIT
= 604800; // 1 week
208 private static $order = null
;
210 public $flag = 'birthday';
211 public $title = 'Anniversaire$s';
213 protected function buildCondition(Watch
$watch)
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();
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)));
226 return new PFC_And($select_date, $cond);
229 public function getOrder()
232 self
::$order = new UFO_Birthday();
237 public function getDate(PlUser
&$user)
239 return $user->profile()->next_birthday
;
242 public function publicationDate(PlUser
&$user)
244 return date('Y-m-d', strtotime($user->profile()->next_birthday
) - self
::WATCH_LIMIT
);
247 public function seen(PlUser
&$user, $last)
249 $birthday = strtotime($user->profile()->next_birthday
);
250 return $birthday > $last + self
::WATCH_LIMIT
251 ||
date('Ymd', $birthday) == date('Ymd');
257 private static $classes = array('WatchRegistration',
258 'WatchProfileUpdate',
261 private static $events = array();
263 private $user = null
;
264 private $date = null
;
265 private $contactCond = null
;
266 private $promoCond = null
;
268 private $filters = array();
270 public function __construct(PlUser
$user, $date = null
)
273 $this->date
= self
::getDate($user, $date);
276 public function user()
281 public function profile()
283 return $this->user
->profile();
286 public function date()
291 public function contactCondition()
293 if (!$this->contactCond
) {
294 $this->contactCond
= new UFC_WatchContact($this->user
);
296 return $this->contactCond
;
299 public function promoCondition()
301 if (!$this->promoCond
) {
302 $this->promoCond
= new UFC_WatchPromo($this->user
);
304 return $this->promoCond
;
307 private function fetchEventWatch($class)
309 if (!isset(self
::$events[$class])) {
310 self
::$events[$class] = new $class();
312 return self
::$events[$class];
315 private function fetchFilter($class)
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
)));
323 return $this->filters
[$class];
326 public function count()
329 foreach (self
::$classes as $class) {
330 $uf = $this->fetchFilter($class);
331 $count +
= $uf->getTotalCount();
337 private function fetchEvents($class)
339 $obj = $this->fetchEventWatch($class);
340 $uf = $this->fetchFilter($class);
341 $users = $uf->getUsers();
342 if (count($users) == 0) {
345 return array('type' => $obj->flag
,
347 'title' => $obj->getTitle(count($users)),
352 public function events()
355 foreach (self
::$classes as $class) {
356 $e = $this->fetchEvents($class);
365 private static function getDate(PlUser
&$user, $date)
367 if (is_null($date)) {
368 $date = $user->watchLast();
369 $limit = time() - (7 * 86400);
370 if ($date < $limit) {
377 public static function getCount(PlUser
&$user, $date = null
)
379 $watch = new Watch($user, $date);
380 return $watch->count();
383 public static function getEvents(PlUser
&$user, $date = null
)
385 $watch = new Watch($user, $date);
386 return $watch->events();
390 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: