0f313fc5010dfeb79f991b62631dde0cfc8504ee
2 /***************************************************************************
3 * Copyright (C) 2003-2009 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 public function getTitle($count = 0)
27 return str_replace(array('$x', '$s'), '', $this->title
);
29 return str_replace(array('$x', '$s'), array('x', 's'), $this->title
);
33 public function getCondition(PlUser
&$user, $date)
35 if (!$user->watch($this->flag
)) {
36 return new UFC_False();
38 return $this->buildCondition($user, $date);
42 abstract protected function buildCondition(PlUser
&$user, $date);
43 abstract public function getOrder();
44 abstract public function getDate(PlUser
&$user);
46 public function seen(PlUser
&$user, $last)
48 return strtotime($this->getDate($user)) > $last;
52 class WatchProfileUpdate
extends WatchOperation
54 public $flag = 'profile';
55 public $title = 'Mise$s à jour de fiche';
57 public static function register(Profile
&$profile, $field)
59 XDB
::execute('REPLACE INTO watch_profile (uid, ts, field)
60 VALUES ({?}, NOW(), {?})',
61 $profile->id(), $field);
64 protected function buildCondition(PlUser
&$user, $date)
66 return new UFC_And(new UFC_ProfileUpdated('>', $date),
67 new UFC_WatchContact($user));
70 public function getOrder()
72 return new UFO_ProfileUpdate();
75 public function getDate(PlUser
&$user)
77 return $user->profile()->last_change
;
81 class WatchRegistration
extends WatchOperation
83 public $flag = 'registration';
84 public $title = 'Inscription$s';
86 protected function buildCondition(PlUser
&$user, $date)
88 return new UFC_And(new UFC_Registered(false
, '>', $date),
89 new UFC_Or(new UFC_WatchContact($user),
90 new UFC_WatchPromo($user)));
93 public function getOrder()
95 return new UFO_Registration();
98 public function getDate(PlUser
&$user)
100 return $user->registration_date
;
104 class WatchDeath
extends WatchOperation
106 public $flag = 'death';
107 public $title = 'Décès';
109 protected function buildCondition(PlUser
&$user, $date)
111 return new UFC_And(new UFC_Dead('>', $date, true
),
112 new UFC_Or(new UFC_WatchPromo($user),
113 new UFC_WatchContact($user)));
116 public function getOrder()
118 return new UFO_Death();
121 public function getDate(PlUser
&$user)
123 return $user->profile()->deathdate
;
126 public function seen(PlUser
&$user, $last)
128 return strtotime($user->profile()->deathdate_rec
) > $last;
132 class WatchBirthday
extends WatchOperation
134 const WATCH_LIMIT
= 604800; // 1 week
136 public $flag = 'birthday';
137 public $title = 'Anniversaire$s';
139 protected function buildCondition(PlUser
&$user, $date)
141 return new UFC_And(new UFC_OR(new UFC_Birthday('=', time()),
142 new UFC_And(new UFC_Birthday('<=', time() + self
::WATCH_LIMIT
),
143 new UFC_Birthday('>', $date + self
::WATCH_LIMIT
))),
144 new UFC_Or(new UFC_WatchPromo($user),
145 new UFC_WatchContact($user)));
148 public function getOrder()
150 return new UFO_Birthday();
153 public function getDate(PlUser
&$user)
155 return $user->profile()->next_birthday
;
158 public function seen(PlUser
&$user, $last)
160 $birthday = strtotime($user->profile()->next_birthday
);
161 return $birthday > $last + self
::WATCH_LIMIT
162 ||
date('Ymd', $birthday) == date('Ymd');
168 private static $classes = array('WatchRegistration',
169 'WatchProfileUpdate',
173 private static function fetchCount(PlUser
&$user, $date, $class)
176 $uf = new UserFilter($obj->getCondition($user, $date));
177 return $uf->getTotalCount();
180 public static function getCount(PlUser
&$user, $date = null
)
183 if (is_null($date)) {
184 $date = $user->watchLast();
186 foreach (self
::$classes as $class) {
187 $count +
= self
::fetchCount($user, $date, $class);
193 private static function fetchEvents(PlUser
&$user, $date, $class)
196 $uf = new UserFilter($obj->getCondition($user, $date),
197 array($obj->getOrder(), new UFO_Name(UserFilter
::DN_SORT
)));
198 $users = $uf->getUsers();
199 if (count($users) == 0) {
202 return array('operation' => $obj,
203 'title' => $obj->getTitle(count($users)),
208 public static function getEvents(PlUser
&$user, $date = null
)
210 if (is_null($date)) {
211 $date = $user->watchLast();
214 foreach (self
::$classes as $class) {
215 $e = self
::fetchEvents($user, $date, $class);
224 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: