Happy New Year!
[platal.git] / modules / xnetevents / xnetevents.inc.php
CommitLineData
0337d704 1<?php
2/***************************************************************************
5e1513f6 3 * Copyright (C) 2003-2011 Polytechnique.org *
0337d704 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()
d6d580ec 23
2ac0bcee 24function get_event_detail($eid, $item_id = false, $asso_id = null)
d6d580ec 25{
0337d704 26 global $globals;
2ac0bcee
FB
27 if (is_null($asso_id)) {
28 $asso_id = $globals->asso('id');
29 }
07eb5b0e
FB
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,
cc69b311 36 ei.titre, al.vid AS absent_list, pl.vid AS participant_list,
96074354
SJ
37 pyl.vid AS payed_list, bl.vid AS booked_unpayed_list,
38 e.subscription_notification
eb41eda9
FB
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)
c8d75c58
FB
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, {?}))
cc69b311
PC
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, {?}))
07eb5b0e
FB
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,
e46cf8c4
SJ
50 '-paye@' . $globals->xnet->evts_domain,
51 '-participants-non-paye@' . $globals->xnet->evts_domain,
07eb5b0e 52 $eid, $eid, $item_id ? $item_id : 1, $asso_id);
0337d704 53 $evt = $res->fetchOneAssoc();
d6d580ec 54
df1cf596 55 if (!$evt) {
d6d580ec 56 return null;
57 }
df1cf596
FB
58 if ($GLOBALS['IS_XNET_SITE'] && $evt['accept_nonmembre'] == 0 && !is_member() && !may_update()) {
59 return false;
60 }
0337d704 61
62 // smart calculation of the total number
63 if (!$item_id) {
07eb5b0e 64 $res = XDB::query('SELECT MAX(nb)
eb41eda9
FB
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)
07eb5b0e
FB
68 WHERE e.eid = {?}
69 GROUP BY ep.uid', $evt['eid']);
0337d704 70 $evt['nb_tot'] = array_sum($res->fetchColumn());
71 $evt['titre'] = '';
72 $evt['item_id'] = 0;
73 }
d6d580ec 74
07eb5b0e
FB
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
eb41eda9
FB
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
07eb5b0e
FB
79 AND uid = {?})
80 WHERE ei.eid = {?}',
81 S::i('uid'), $evt['eid']);
d6d580ec 82 $evt['topay'] = 0;
98a7e9dc 83 $evt['paid'] = 0;
2ac0bcee 84 $evt['notify_payment'] = false;
d6d580ec 85 foreach ($evt['moments'] as $m) {
86 $evt['topay'] += $m['nb'] * $m['montant'];
98a7e9dc 87 if ($m['montant']) {
54ac7230 88 $evt['money'] = true;
98a7e9dc 89 }
90 $evt['paid'] = $m['paid'];
2ac0bcee 91 $evt['notify_payment'] = $evt['notify_payment'] || $m['notify_payment'];
d6d580ec 92 }
93
b3cd1320
DB
94 $montants = XDB::fetchColumn('SELECT amount
95 FROM payment_transactions AS t
07eb5b0e
FB
96 WHERE ref = {?} AND uid = {?}',
97 $evt['paiement_id'], S::v('uid'));
8bac35d8 98 $evt['telepaid'] = 0;
d6d580ec 99 foreach ($montants as $m) {
100 $p = strtr(substr($m, 0, strpos($m, 'EUR')), ',', '.');
101 $evt['paid'] += trim($p);
8bac35d8 102 $evt['telepaid'] += trim($p);
0337d704 103 }
ed996b5a 104 $evt['organizer'] = User::getSilent($evt['uid']);
d6d580ec 105
20c5c7e6
SJ
106 make_event_date($evt);
107
0337d704 108 return $evt;
109}
d6d580ec 110
0337d704 111// }}}
112
113// {{{ function get_event_participants()
d1965ef6 114function get_event_participants(&$evt, $item_id, array $tri = array(), $limit = null)
07eb5b0e 115{
0337d704 116 global $globals;
ed21e24a 117
ed21e24a 118 $eid = $evt['eid'];
2ac0bcee 119 $money = $evt['money'] && (function_exists('may_update')) && may_update();
ed21e24a 120 $pay_id = $evt['paiement_id'];
121
c81e03c0 122 $append = $item_id ? XDB::format(' AND ep.item_id = {?}', $item_id) : '';
07eb5b0e
FB
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
eb41eda9 125 FROM group_event_participants AS ep
07eb5b0e
FB
126 WHERE ep.eid = {?} AND nb > 0 ' . $append . '
127 GROUP BY ep.uid', $eid);
c8763ca3 128 $uf = new UserFilter(new PFC_True(), $tri);
4935bab0 129 $users = User::getBulkUsersWithUIDs($uf->filter(array_keys($query), new PlLimit($count, $offset)));
07eb5b0e
FB
130 $tab = array();
131 foreach ($users as $user) {
132 $uid = $user->id();
133 $tab[$uid] = $query[$uid];
134 $tab[$uid]['user'] = $user;
135 }
ed21e24a 136
0337d704 137 if ($item_id) {
07eb5b0e 138 return $tab;
0337d704 139 }
ed21e24a 140
e01ebe65
FB
141 $evt['adminpaid'] = 0;
142 $evt['telepaid'] = 0;
143 $evt['topay'] = 0;
144 $evt['paid'] = 0;
07eb5b0e 145 foreach ($tab as $uid=>&$u) {
58591700 146 $u['adminpaid'] = $u['paid'];
0337d704 147 $u['montant'] = 0;
2ac0bcee 148 if ($money && $pay_id) {
b3cd1320
DB
149 $montants = XDB::fetchColumn('SELECT amount
150 FROM payment_transactions AS t
07eb5b0e
FB
151 WHERE ref = {?} AND uid = {?}',
152 $pay_id, $uid);
0337d704 153 foreach ($montants as $m) {
e01ebe65
FB
154 $p = strtr(substr($m, 0, strpos($m, "EUR")), ",", ".");
155 $u['paid'] += trim($p);
0337d704 156 }
2ac0bcee 157 }
58591700 158 $u['telepayment'] = $u['paid'] - $u['adminpaid'];
07eb5b0e 159 $res_ = XDB::iterator('SELECT ep.nb, ep.item_id, ei.montant
eb41eda9
FB
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)
07eb5b0e
FB
162 WHERE ep.eid = {?} AND ep.uid = {?}',
163 $eid, $uid);
0337d704 164 while ($i = $res_->next()) {
165 $u[$i['item_id']] = $i['nb'];
166 $u['montant'] += $i['montant']*$i['nb'];
167 }
e01ebe65
FB
168 $evt['telepaid'] += $u['telepayment'];
169 $evt['adminpaid'] += $u['adminpaid'];
170 $evt['paid'] += $u['paid'];
171 $evt['topay'] += $u['montant'];
0337d704 172 }
173 return $tab;
174}
175// }}}
176
177// {{{ function subscribe_lists_event()
50208d22
SJ
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 */
189function subscribe_lists_event($uid, $evt, $participate, $paid, $payment = false)
9193e8f7 190{
9ff5b337
SJ
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'];
ed21e24a 195
4514bea3 196 $user = User::getSilent($uid);
507208e6 197 $email = $user->forlifeEmail();
0337d704 198
9193e8f7 199 function subscribe($list, $email)
200 {
201 if ($list && $email) {
00ba8a74
SJ
202 XDB::execute('INSERT IGNORE INTO virtual_redirect
203 VALUES ({?}, {?})',
9193e8f7 204 $list, $email);
205 }
0337d704 206 }
207
9193e8f7 208 function unsubscribe($list, $email)
209 {
210 if ($list && $email) {
50208d22
SJ
211 XDB::execute('DELETE FROM virtual_redirect
212 WHERE vid = {?} AND redirect = {?}',
9193e8f7 213 $list, $email);
214 }
0337d704 215 }
216
50208d22
SJ
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:
9ff5b337 228 unsubscribe($participant_list, $email);
50208d22
SJ
229 unsubscribe($unpayed_list, $email);
230 unsubscribe($payed_list, $email);
9ff5b337 231 subscribe($absent_list, $email);
50208d22
SJ
232 break;
233 case 0:
9ff5b337
SJ
234 unsubscribe($participant_list, $email);
235 unsubscribe($absent_list, $email);
50208d22
SJ
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;
1cf7d38b 250 }
9193e8f7 251 }
0337d704 252}
253// }}}
254
20c5c7e6 255// {{{ function event_change_shortname()
2847640f 256function event_change_shortname(&$page, $eid, $old, $new)
5070a22d 257{
258 global $globals;
259
f56e5e53 260 if (is_null($old)) {
261 $old = '';
262 }
a7de4ef7 263 // Quelques vérifications sur l'alias (caractères spéciaux)
2179ffa2 264 if ($new && !preg_match( "/^[a-zA-Z0-9\-.]{3,20}$/", $new)) {
a7d35093 265 $page->trigError("Le raccourci demandé n'est pas valide.
a7de4ef7 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 .");
5070a22d 269 return $old;
4aae4d2c 270 } elseif ($new && (is_int($new) || ctype_digit($new))) {
940ae3a0
SJ
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;
5070a22d 275 }
276
a7de4ef7 277 //vérifier que l'alias n'est pas déja pris
5070a22d 278 if ($new && $old != $new) {
9193e8f7 279 $res = XDB::query('SELECT COUNT(*)
eb41eda9 280 FROM group_events
9193e8f7 281 WHERE short_name = {?}',
282 $new);
5070a22d 283 if ($res->fetchOneCell() > 0) {
a7d35093 284 $page->trigError("Le raccourci demandé est déjà utilisé. Choisis en un autre.");
5070a22d 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
9ff5b337 295 foreach (array('-absents@', '-participants@', '-paye@', '-participants-non-paye@') as $v) {
5070a22d 296 $v .= $globals->xnet->evts_domain;
9ff5b337
SJ
297 XDB::execute("UPDATE virtual
298 SET alias = {?}
299 WHERE type = 'evt' AND alias = {?}",
300 $new . $v, $old . $v);
5070a22d 301 }
5070a22d 302 return $new;
303 }
304
305 if (!$old && $new) {
306 // if we have a first new short_name create the lists
9ff5b337
SJ
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 }
5070a22d 321
9ff5b337 322 foreach (array('-participants@', '-paye@', '-participants-non-paye@') as $v) {
e46cf8c4
SJ
323 XDB::execute("INSERT IGNORE INTO virtual_redirect (
324 SELECT {?} AS vid, IF(al.alias IS NULL, a.email, CONCAT(al.alias, {?})) AS redirect
eb41eda9 325 FROM group_event_participants AS ep
fe13bc1d
FB
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')
e46cf8c4
SJ
328 WHERE ep.eid = {?} AND " . $where[$v] . "
329 GROUP BY ep.uid)",
330 $lastid[$v], '@' . $globals->mail->domain, $eid);
9ff5b337 331 }
07eb5b0e
FB
332 XDB::execute("INSERT IGNORE INTO virtual_redirect (
333 SELECT {?} AS vid, IF(al.alias IS NULL, a.email, CONCAT(al.alias, {?})) AS redirect
eb41eda9 334 FROM group_members AS m
fe13bc1d
FB
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')
eb41eda9 337 LEFT JOIN group_event_participants AS ep ON (ep.uid = m.uid AND ep.eid = {?})
07eb5b0e
FB
338 WHERE m.asso_id = {?} AND ep.uid IS NULL
339 GROUP BY m.uid)",
e46cf8c4 340 $lastid['-absents@'], '@' . $globals->mail->domain, $eid, $globals->asso('id'));
5070a22d 341
342 return $new;
343 }
344
345 if ($old && !$new) {
346 // if we delete the old short name, delete the lists
9ff5b337 347 foreach (array('-absents@', '-participants@', '-paye@', '-participants-non-paye@') as $v) {
5070a22d 348 $v .= $globals->xnet->evts_domain;
9ff5b337
SJ
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);
5070a22d 354 }
355 return $new;
356 }
357
358 // cannot happen
359 return $old;
360}
20c5c7e6
SJ
361// }}}
362
363// {{{ function make_event_date()
364function make_event_date(&$e)
365{
366 $start = strtotime($e['debut']);
367 $end = strtotime($e['fin']);
07eb5b0e 368 $first_day = @strtotime($e['first_day']);
20c5c7e6
SJ
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// }}}
5070a22d 387
a7de4ef7 388// vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
0337d704 389?>