Happy New Year!
[platal.git] / modules / xnetevents / xnetevents.inc.php
1 <?php
2 /***************************************************************************
3 * Copyright (C) 2003-2011 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 // {{{ function get_event_detail()
23
24 function get_event_detail($eid, $item_id = false, $asso_id = null)
25 {
26 global $globals;
27 if (is_null($asso_id)) {
28 $asso_id = $globals->asso('id');
29 }
30 $res = XDB::query('SELECT SUM(nb) AS nb_tot, COUNT(DISTINCT ep.uid) AS nb, e.*,
31 IF(e.deadline_inscription,
32 e.deadline_inscription >= LEFT(NOW(), 10),
33 1) AS inscr_open,
34 LEFT(10, e.debut) AS start_day, LEFT(10, e.fin) AS last_day,
35 LEFT(NOW(), 10) AS now,
36 ei.titre, al.vid AS absent_list, pl.vid AS participant_list,
37 pyl.vid AS payed_list, bl.vid AS booked_unpayed_list,
38 e.subscription_notification
39 FROM group_events AS e
40 INNER JOIN group_event_items AS ei ON (e.eid = ei.eid)
41 LEFT JOIN group_event_participants AS ep ON(e.eid = ep.eid AND ei.item_id = ep.item_id)
42 LEFT JOIN virtual AS al ON(al.type = \'evt\' AND al.alias = CONCAT(short_name, {?}))
43 LEFT JOIN virtual AS pl ON(pl.type = \'evt\' AND pl.alias = CONCAT(short_name, {?}))
44 LEFT JOIN virtual AS pyl ON(pyl.type = \'evt\' AND pyl.alias = CONCAT(short_name, {?}))
45 LEFT JOIN virtual AS bl ON(bl.type = \'evt\' AND bl.alias = CONCAT(short_name, {?}))
46 WHERE (e.eid = {?} OR e.short_name = {?}) AND ei.item_id = {?} AND e.asso_id = {?}
47 GROUP BY ei.item_id',
48 '-absents@'.$globals->xnet->evts_domain,
49 '-participants@'.$globals->xnet->evts_domain,
50 '-paye@' . $globals->xnet->evts_domain,
51 '-participants-non-paye@' . $globals->xnet->evts_domain,
52 $eid, $eid, $item_id ? $item_id : 1, $asso_id);
53 $evt = $res->fetchOneAssoc();
54
55 if (!$evt) {
56 return null;
57 }
58 if ($GLOBALS['IS_XNET_SITE'] && $evt['accept_nonmembre'] == 0 && !is_member() && !may_update()) {
59 return false;
60 }
61
62 // smart calculation of the total number
63 if (!$item_id) {
64 $res = XDB::query('SELECT MAX(nb)
65 FROM group_events AS e
66 INNER JOIN group_event_items AS ei ON (e.eid = ei.eid)
67 LEFT JOIN group_event_participants AS ep ON (e.eid = ep.eid AND ei.item_id = ep.item_id)
68 WHERE e.eid = {?}
69 GROUP BY ep.uid', $evt['eid']);
70 $evt['nb_tot'] = array_sum($res->fetchColumn());
71 $evt['titre'] = '';
72 $evt['item_id'] = 0;
73 }
74
75 $evt['moments'] = XDB::fetchAllAssoc('SELECT titre, details, montant, ei.item_id, nb,
76 ep.paid, FIND_IN_SET(\'notify_payment\', ep.flags) AS notify_payment
77 FROM group_event_items AS ei
78 LEFT JOIN group_event_participants AS ep ON (ep.eid = ei.eid AND ep.item_id = ei.item_id
79 AND uid = {?})
80 WHERE ei.eid = {?}',
81 S::i('uid'), $evt['eid']);
82 $evt['topay'] = 0;
83 $evt['paid'] = 0;
84 $evt['notify_payment'] = false;
85 foreach ($evt['moments'] as $m) {
86 $evt['topay'] += $m['nb'] * $m['montant'];
87 if ($m['montant']) {
88 $evt['money'] = true;
89 }
90 $evt['paid'] = $m['paid'];
91 $evt['notify_payment'] = $evt['notify_payment'] || $m['notify_payment'];
92 }
93
94 $montants = XDB::fetchColumn('SELECT amount
95 FROM payment_transactions AS t
96 WHERE ref = {?} AND uid = {?}',
97 $evt['paiement_id'], S::v('uid'));
98 $evt['telepaid'] = 0;
99 foreach ($montants as $m) {
100 $p = strtr(substr($m, 0, strpos($m, 'EUR')), ',', '.');
101 $evt['paid'] += trim($p);
102 $evt['telepaid'] += trim($p);
103 }
104 $evt['organizer'] = User::getSilent($evt['uid']);
105
106 make_event_date($evt);
107
108 return $evt;
109 }
110
111 // }}}
112
113 // {{{ function get_event_participants()
114 function get_event_participants(&$evt, $item_id, array $tri = array(), $limit = null)
115 {
116 global $globals;
117
118 $eid = $evt['eid'];
119 $money = $evt['money'] && (function_exists('may_update')) && may_update();
120 $pay_id = $evt['paiement_id'];
121
122 $append = $item_id ? XDB::format(' AND ep.item_id = {?}', $item_id) : '';
123 $query = XDB::fetchAllAssoc('uid', 'SELECT ep.uid, SUM(ep.paid) AS paid, SUM(ep.nb) AS nb,
124 FIND_IN_SET(\'notify_payment\', ep.flags) AS notify_payment
125 FROM group_event_participants AS ep
126 WHERE ep.eid = {?} AND nb > 0 ' . $append . '
127 GROUP BY ep.uid', $eid);
128 $uf = new UserFilter(new PFC_True(), $tri);
129 $users = User::getBulkUsersWithUIDs($uf->filter(array_keys($query), new PlLimit($count, $offset)));
130 $tab = array();
131 foreach ($users as $user) {
132 $uid = $user->id();
133 $tab[$uid] = $query[$uid];
134 $tab[$uid]['user'] = $user;
135 }
136
137 if ($item_id) {
138 return $tab;
139 }
140
141 $evt['adminpaid'] = 0;
142 $evt['telepaid'] = 0;
143 $evt['topay'] = 0;
144 $evt['paid'] = 0;
145 foreach ($tab as $uid=>&$u) {
146 $u['adminpaid'] = $u['paid'];
147 $u['montant'] = 0;
148 if ($money && $pay_id) {
149 $montants = XDB::fetchColumn('SELECT amount
150 FROM payment_transactions AS t
151 WHERE ref = {?} AND uid = {?}',
152 $pay_id, $uid);
153 foreach ($montants as $m) {
154 $p = strtr(substr($m, 0, strpos($m, "EUR")), ",", ".");
155 $u['paid'] += trim($p);
156 }
157 }
158 $u['telepayment'] = $u['paid'] - $u['adminpaid'];
159 $res_ = XDB::iterator('SELECT ep.nb, ep.item_id, ei.montant
160 FROM group_event_participants AS ep
161 INNER JOIN group_event_items AS ei ON (ei.eid = ep.eid AND ei.item_id = ep.item_id)
162 WHERE ep.eid = {?} AND ep.uid = {?}',
163 $eid, $uid);
164 while ($i = $res_->next()) {
165 $u[$i['item_id']] = $i['nb'];
166 $u['montant'] += $i['montant']*$i['nb'];
167 }
168 $evt['telepaid'] += $u['telepayment'];
169 $evt['adminpaid'] += $u['adminpaid'];
170 $evt['paid'] += $u['paid'];
171 $evt['topay'] += $u['montant'];
172 }
173 return $tab;
174 }
175 // }}}
176
177 // {{{ function subscribe_lists_event()
178 /** Subscribes user to various event related mailing lists.
179 *
180 * @param $uid: user's id.
181 * @param evt: events data, in particular ids of the lists at stake.
182 * @param participate: indicates if the user takes part at the event or not;
183 * -1 means he did not answer, 0 means no, and 1 means yes.
184 * @param paid: has the user already payed anything?
185 * 0 means no, a positive amount means yes.
186 * @param payment: is this function called from a payment page?
187 * If true, only payment related lists should be updated.
188 */
189 function subscribe_lists_event($uid, $evt, $participate, $paid, $payment = false)
190 {
191 $participant_list = $evt['participant_list'];
192 $absent_list = $evt['absent_list'];
193 $unpayed_list = $evt['booked_unpayed_list'];
194 $payed_list = $evt['payed_list'];
195
196 $user = User::getSilent($uid);
197 $email = $user->forlifeEmail();
198
199 function subscribe($list, $email)
200 {
201 if ($list && $email) {
202 XDB::execute('INSERT IGNORE INTO virtual_redirect
203 VALUES ({?}, {?})',
204 $list, $email);
205 }
206 }
207
208 function unsubscribe($list, $email)
209 {
210 if ($list && $email) {
211 XDB::execute('DELETE FROM virtual_redirect
212 WHERE vid = {?} AND redirect = {?}',
213 $list, $email);
214 }
215 }
216
217 /** If $payment is not null, we do not retrieve the value of $participate,
218 * thus we do not alter participant and absent lists.
219 */
220 if ($payment === true) {
221 if ($paid > 0) {
222 unsubscribe($unpayed_list, $email);
223 subscribe($payed_list, $email);
224 }
225 } else {
226 switch ($participate) {
227 case -1:
228 unsubscribe($participant_list, $email);
229 unsubscribe($unpayed_list, $email);
230 unsubscribe($payed_list, $email);
231 subscribe($absent_list, $email);
232 break;
233 case 0:
234 unsubscribe($participant_list, $email);
235 unsubscribe($absent_list, $email);
236 unsubscribe($unpayed_list, $email);
237 unsubscribe($payed_list, $email);
238 break;
239 case 1:
240 subscribe($participant_list, $email);
241 unsubscribe($absent_list, $email);
242 if ($paid > 0) {
243 unsubscribe($unpayed_list, $email);
244 subscribe($payed_list, $email);
245 } else {
246 subscribe($unpayed_list, $email);
247 unsubscribe($payed_list, $email);
248 }
249 break;
250 }
251 }
252 }
253 // }}}
254
255 // {{{ function event_change_shortname()
256 function event_change_shortname(&$page, $eid, $old, $new)
257 {
258 global $globals;
259
260 if (is_null($old)) {
261 $old = '';
262 }
263 // Quelques vérifications sur l'alias (caractères spéciaux)
264 if ($new && !preg_match( "/^[a-zA-Z0-9\-.]{3,20}$/", $new)) {
265 $page->trigError("Le raccourci demandé n'est pas valide.
266 Vérifie qu'il comporte entre 3 et 20 caractères
267 et qu'il ne contient que des lettres non accentuées,
268 des chiffres ou les caractères - et .");
269 return $old;
270 } elseif ($new && (is_int($new) || ctype_digit($new))) {
271 $page->trigError("Le raccourci demandé ne peut être accepté car il
272 ne contient que des chiffres. Rajoute-lui par exemple
273 une lettre.");
274 return $old;
275 }
276
277 //vérifier que l'alias n'est pas déja pris
278 if ($new && $old != $new) {
279 $res = XDB::query('SELECT COUNT(*)
280 FROM group_events
281 WHERE short_name = {?}',
282 $new);
283 if ($res->fetchOneCell() > 0) {
284 $page->trigError("Le raccourci demandé est déjà utilisé. Choisis en un autre.");
285 return $old;
286 }
287 }
288
289 if ($old == $new) {
290 return $new;
291 }
292
293 if ($old && $new) {
294 // if had a previous shortname change the old lists
295 foreach (array('-absents@', '-participants@', '-paye@', '-participants-non-paye@') as $v) {
296 $v .= $globals->xnet->evts_domain;
297 XDB::execute("UPDATE virtual
298 SET alias = {?}
299 WHERE type = 'evt' AND alias = {?}",
300 $new . $v, $old . $v);
301 }
302 return $new;
303 }
304
305 if (!$old && $new) {
306 // if we have a first new short_name create the lists
307 $lastid = array();
308 $where = array(
309 '-participants@' => 'ep.nb > 0',
310 '-paye@' => 'ep.paid > 0',
311 '-participants-non-paye@' => 'ep.nb > 0 AND ep.paid = 0'
312 );
313
314 foreach (array('-absents@', '-participants@', '-paye@', '-participants-non-paye@') as $v) {
315 XDB::execute("INSERT INTO virtual
316 SET type = 'evt', alias = {?}",
317 $new . $v . $globals->xnet->evts_domain);
318
319 $lastid[$v] = XDB::insertId();
320 }
321
322 foreach (array('-participants@', '-paye@', '-participants-non-paye@') as $v) {
323 XDB::execute("INSERT IGNORE INTO virtual_redirect (
324 SELECT {?} AS vid, IF(al.alias IS NULL, a.email, CONCAT(al.alias, {?})) AS redirect
325 FROM group_event_participants AS ep
326 LEFT JOIN accounts AS a ON (ep.uid = a.uid)
327 LEFT JOIN aliases AS al ON (al.uid = a.uid AND al.type = 'a_vie')
328 WHERE ep.eid = {?} AND " . $where[$v] . "
329 GROUP BY ep.uid)",
330 $lastid[$v], '@' . $globals->mail->domain, $eid);
331 }
332 XDB::execute("INSERT IGNORE INTO virtual_redirect (
333 SELECT {?} AS vid, IF(al.alias IS NULL, a.email, CONCAT(al.alias, {?})) AS redirect
334 FROM group_members AS m
335 LEFT JOIN accounts AS a ON (a.uid = m.uid)
336 LEFT JOIN aliases AS al ON (al.uid = a.uid AND al.type = 'a_vie')
337 LEFT JOIN group_event_participants AS ep ON (ep.uid = m.uid AND ep.eid = {?})
338 WHERE m.asso_id = {?} AND ep.uid IS NULL
339 GROUP BY m.uid)",
340 $lastid['-absents@'], '@' . $globals->mail->domain, $eid, $globals->asso('id'));
341
342 return $new;
343 }
344
345 if ($old && !$new) {
346 // if we delete the old short name, delete the lists
347 foreach (array('-absents@', '-participants@', '-paye@', '-participants-non-paye@') as $v) {
348 $v .= $globals->xnet->evts_domain;
349 XDB::execute("DELETE virtual, virtual_redirect
350 FROM virtual
351 LEFT JOIN virtual_redirect USING(vid)
352 WHERE virtual.alias = {?}",
353 $infos['short_name'] . $v);
354 }
355 return $new;
356 }
357
358 // cannot happen
359 return $old;
360 }
361 // }}}
362
363 // {{{ function make_event_date()
364 function make_event_date(&$e)
365 {
366 $start = strtotime($e['debut']);
367 $end = strtotime($e['fin']);
368 $first_day = @strtotime($e['first_day']);
369 $last_day = strtotime($e['last_day']);
370 unset($e['debut'], $e['fin'], $e['first_day'], $e['last_day']);
371
372 $date = "";
373 if ($start && $end != $start) {
374 if ($first_day == $last_day) {
375 $date .= "le " . strftime("%d %B %Y", $start) . " de "
376 . strftime("%H:%M", $start) . " à " . strftime("%H:%M", $end);
377 } else {
378 $date .= "du " . strftime("%d %B %Y à %H:%M", $start)
379 . "\nau " . strftime("%d %B %Y à %H:%M", $end);
380 }
381 } else {
382 $date .= "le " . strftime("%d %B %Y à %H:%M", $start);
383 }
384 $e['date'] = $date;
385 }
386 // }}}
387
388 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
389 ?>