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('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 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)
122 $data = XDB
::fetchColumn("SELECT field
124 WHERE pid = {?} AND ts > FROM_UNIXTIME({?}) AND field != ''
126 $user->profile()->id(), $this->date
);
127 if (count($data) == 0) {
131 foreach ($data as $f) {
132 $text[] = self
::$descriptions[$f];
139 class WatchRegistration
extends WatchOperation
141 private static $order = null
;
143 public $flag = 'registration';
144 public $title = 'Inscription$s';
146 protected function buildCondition(Watch
$watch)
148 return new PFC_And(new UFC_Registered(false
, '>', $watch->date()),
149 new PFC_Or($watch->contactCondition(),
150 $watch->promoCondition()));
153 public function getOrder()
156 self
::$order = new UFO_Registration();
161 public function getDate(PlUser
&$user)
163 return $user->registration_date
;
167 class WatchDeath
extends WatchOperation
169 private static $order = null
;
171 public $flag = 'death';
172 public $title = 'Décès';
174 protected function buildCondition(Watch
$watch)
176 return new PFC_And(new UFC_Dead('>', $watch->date(), true
),
177 new PFC_Or($watch->contactCondition(),
178 $watch->promoCondition()));
181 public function getOrder()
184 self
::$order = new UFO_Death();
189 public function getDate(PlUser
&$user)
191 return $user->profile()->deathdate
;
194 public function publicationDate(PlUser
&$user)
196 return $user->profile()->deathdate_rec
;
199 public function seen(PlUser
&$user, $last)
201 return strtotime($user->profile()->deathdate_rec
) > $last;
205 class WatchBirthday
extends WatchOperation
207 const WATCH_LIMIT
= 604800; // 1 week
209 private static $order = null
;
211 public $flag = 'birthday';
212 public $title = 'Anniversaire$s';
214 protected function buildCondition(Watch
$watch)
216 $select_date = new PFC_OR(new UFC_Birthday('=', time()),
217 new PFC_And(new UFC_Birthday('<=', time() + self
::WATCH_LIMIT
),
218 new UFC_Birthday('>', $watch->date() + self
::WATCH_LIMIT
)));
219 $profile = $watch->profile();
220 $cond = $watch->contactCondition();
222 $cond = new PFC_Or($cond,
223 new PFC_And($watch->promoCondition(),
224 new UFC_Promo('>=', $profile->mainGrade(), $profile->yearpromo() - 1),
225 new UFC_Promo('<=', $profile->mainGrade(), $profile->yearpromo() +
1)));
227 return new PFC_And($select_date, $cond);
230 public function getOrder()
233 self
::$order = new UFO_Birthday();
238 public function getDate(PlUser
&$user)
240 return $user->profile()->next_birthday
;
243 public function publicationDate(PlUser
&$user)
245 return date('Y-m-d', strtotime($user->profile()->next_birthday
) - self
::WATCH_LIMIT
);
248 public function seen(PlUser
&$user, $last)
250 $birthday = strtotime($user->profile()->next_birthday
);
251 return $birthday > $last + self
::WATCH_LIMIT
252 ||
date('Ymd', $birthday) == date('Ymd');
258 private static $classes = array('WatchRegistration',
259 'WatchProfileUpdate',
262 private static $events = array();
264 private $user = null
;
265 private $date = null
;
266 private $contactCond = null
;
267 private $promoCond = null
;
269 private $filters = array();
271 public function __construct(PlUser
$user, $date = null
)
274 $this->date
= self
::getDate($user, $date);
277 public function user()
282 public function profile()
284 return $this->user
->profile();
287 public function date()
292 public function contactCondition()
294 if (!$this->contactCond
) {
295 $this->contactCond
= new UFC_WatchContact($this->user
);
297 return $this->contactCond
;
300 public function promoCondition()
302 if (!$this->promoCond
) {
303 $this->promoCond
= new UFC_WatchPromo($this->user
);
305 return $this->promoCond
;
308 private function fetchEventWatch($class)
310 if (!isset(self
::$events[$class])) {
311 self
::$events[$class] = new $class();
313 return self
::$events[$class];
316 private function fetchFilter($class)
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
)));
324 return $this->filters
[$class];
327 public function count()
330 foreach (self
::$classes as $class) {
331 $uf = $this->fetchFilter($class);
332 $count +
= $uf->getTotalCount();
338 private function fetchEvents($class)
340 $obj = $this->fetchEventWatch($class);
341 $uf = $this->fetchFilter($class);
342 $users = $uf->getUsers();
343 if (count($users) == 0) {
346 return array('type' => $obj->flag
,
348 'title' => $obj->getTitle(count($users)),
353 public function events()
356 foreach (self
::$classes as $class) {
357 $e = $this->fetchEvents($class);
366 private static function getDate(PlUser
&$user, $date)
368 if (is_null($date)) {
369 $date = $user->watchLast();
370 $limit = time() - (7 * 86400);
371 if ($date < $limit) {
378 public static function getCount(PlUser
&$user, $date = null
)
380 $watch = new Watch($user, $date);
381 return $watch->count();
384 public static function getEvents(PlUser
&$user, $date = null
)
386 $watch = new Watch($user, $date);
387 return $watch->events();
391 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: