0f313fc5010dfeb79f991b62631dde0cfc8504ee
[platal.git] / include / notifs.inc.php
1 <?php
2 /***************************************************************************
3 * Copyright (C) 2003-2009 Polytechnique.org *
4 * http://opensource.polytechnique.org/ *
5 * *
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. *
10 * *
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. *
15 * *
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 *
18 * Foundation, Inc., *
19 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *
20 ***************************************************************************/
21
22 abstract class WatchOperation
23 {
24 public function getTitle($count = 0)
25 {
26 if ($count == 1) {
27 return str_replace(array('$x', '$s'), '', $this->title);
28 } else {
29 return str_replace(array('$x', '$s'), array('x', 's'), $this->title);
30 }
31 }
32
33 public function getCondition(PlUser &$user, $date)
34 {
35 if (!$user->watch($this->flag)) {
36 return new UFC_False();
37 } else {
38 return $this->buildCondition($user, $date);
39 }
40 }
41
42 abstract protected function buildCondition(PlUser &$user, $date);
43 abstract public function getOrder();
44 abstract public function getDate(PlUser &$user);
45
46 public function seen(PlUser &$user, $last)
47 {
48 return strtotime($this->getDate($user)) > $last;
49 }
50 }
51
52 class WatchProfileUpdate extends WatchOperation
53 {
54 public $flag = 'profile';
55 public $title = 'Mise$s à jour de fiche';
56
57 public static function register(Profile &$profile, $field)
58 {
59 XDB::execute('REPLACE INTO watch_profile (uid, ts, field)
60 VALUES ({?}, NOW(), {?})',
61 $profile->id(), $field);
62 }
63
64 protected function buildCondition(PlUser &$user, $date)
65 {
66 return new UFC_And(new UFC_ProfileUpdated('>', $date),
67 new UFC_WatchContact($user));
68 }
69
70 public function getOrder()
71 {
72 return new UFO_ProfileUpdate();
73 }
74
75 public function getDate(PlUser &$user)
76 {
77 return $user->profile()->last_change;
78 }
79 }
80
81 class WatchRegistration extends WatchOperation
82 {
83 public $flag = 'registration';
84 public $title = 'Inscription$s';
85
86 protected function buildCondition(PlUser &$user, $date)
87 {
88 return new UFC_And(new UFC_Registered(false, '>', $date),
89 new UFC_Or(new UFC_WatchContact($user),
90 new UFC_WatchPromo($user)));
91 }
92
93 public function getOrder()
94 {
95 return new UFO_Registration();
96 }
97
98 public function getDate(PlUser &$user)
99 {
100 return $user->registration_date;
101 }
102 }
103
104 class WatchDeath extends WatchOperation
105 {
106 public $flag = 'death';
107 public $title = 'Décès';
108
109 protected function buildCondition(PlUser &$user, $date)
110 {
111 return new UFC_And(new UFC_Dead('>', $date, true),
112 new UFC_Or(new UFC_WatchPromo($user),
113 new UFC_WatchContact($user)));
114 }
115
116 public function getOrder()
117 {
118 return new UFO_Death();
119 }
120
121 public function getDate(PlUser &$user)
122 {
123 return $user->profile()->deathdate;
124 }
125
126 public function seen(PlUser &$user, $last)
127 {
128 return strtotime($user->profile()->deathdate_rec) > $last;
129 }
130 }
131
132 class WatchBirthday extends WatchOperation
133 {
134 const WATCH_LIMIT = 604800; // 1 week
135
136 public $flag = 'birthday';
137 public $title = 'Anniversaire$s';
138
139 protected function buildCondition(PlUser &$user, $date)
140 {
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)));
146 }
147
148 public function getOrder()
149 {
150 return new UFO_Birthday();
151 }
152
153 public function getDate(PlUser &$user)
154 {
155 return $user->profile()->next_birthday;
156 }
157
158 public function seen(PlUser &$user, $last)
159 {
160 $birthday = strtotime($user->profile()->next_birthday);
161 return $birthday > $last + self::WATCH_LIMIT
162 || date('Ymd', $birthday) == date('Ymd');
163 }
164 }
165
166 class Watch
167 {
168 private static $classes = array('WatchRegistration',
169 'WatchProfileUpdate',
170 'WatchDeath',
171 'WatchBirthday');
172
173 private static function fetchCount(PlUser &$user, $date, $class)
174 {
175 $obj = new $class();
176 $uf = new UserFilter($obj->getCondition($user, $date));
177 return $uf->getTotalCount();
178 }
179
180 public static function getCount(PlUser &$user, $date = null)
181 {
182 $count = 0;
183 if (is_null($date)) {
184 $date = $user->watchLast();
185 }
186 foreach (self::$classes as $class) {
187 $count += self::fetchCount($user, $date, $class);
188 }
189 return $count;
190 }
191
192
193 private static function fetchEvents(PlUser &$user, $date, $class)
194 {
195 $obj = new $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) {
200 return null;
201 } else {
202 return array('operation' => $obj,
203 'title' => $obj->getTitle(count($users)),
204 'users' => $users);
205 }
206 }
207
208 public static function getEvents(PlUser &$user, $date = null)
209 {
210 if (is_null($date)) {
211 $date = $user->watchLast();
212 }
213 $events = array();
214 foreach (self::$classes as $class) {
215 $e = self::fetchEvents($user, $date, $class);
216 if (!is_null($e)) {
217 $events[] = $e;
218 }
219 }
220 return $events;
221 }
222 }
223
224 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
225 ?>