Displays redirections in succesful registration summary (Closes #1501).
[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 }
7852229b
SJ
30 $evt = XDB::fetchOneAssoc('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, e.subscription_notification
37 FROM group_events AS e
38 INNER JOIN group_event_items AS ei ON (e.eid = ei.eid)
39 LEFT JOIN group_event_participants AS ep ON(e.eid = ep.eid AND ei.item_id = ep.item_id)
40 WHERE (e.eid = {?} OR e.short_name = {?}) AND ei.item_id = {?} AND e.asso_id = {?}
41 GROUP BY ei.item_id',
42 $eid, $eid, $item_id ? $item_id : 1, $asso_id);
d6d580ec 43
df1cf596 44 if (!$evt) {
d6d580ec 45 return null;
46 }
df1cf596
FB
47 if ($GLOBALS['IS_XNET_SITE'] && $evt['accept_nonmembre'] == 0 && !is_member() && !may_update()) {
48 return false;
49 }
0337d704 50
51 // smart calculation of the total number
52 if (!$item_id) {
07eb5b0e 53 $res = XDB::query('SELECT MAX(nb)
eb41eda9
FB
54 FROM group_events AS e
55 INNER JOIN group_event_items AS ei ON (e.eid = ei.eid)
56 LEFT JOIN group_event_participants AS ep ON (e.eid = ep.eid AND ei.item_id = ep.item_id)
07eb5b0e
FB
57 WHERE e.eid = {?}
58 GROUP BY ep.uid', $evt['eid']);
0337d704 59 $evt['nb_tot'] = array_sum($res->fetchColumn());
60 $evt['titre'] = '';
61 $evt['item_id'] = 0;
62 }
d6d580ec 63
07eb5b0e
FB
64 $evt['moments'] = XDB::fetchAllAssoc('SELECT titre, details, montant, ei.item_id, nb,
65 ep.paid, FIND_IN_SET(\'notify_payment\', ep.flags) AS notify_payment
eb41eda9
FB
66 FROM group_event_items AS ei
67 LEFT JOIN group_event_participants AS ep ON (ep.eid = ei.eid AND ep.item_id = ei.item_id
07eb5b0e
FB
68 AND uid = {?})
69 WHERE ei.eid = {?}',
70 S::i('uid'), $evt['eid']);
d6d580ec 71 $evt['topay'] = 0;
98a7e9dc 72 $evt['paid'] = 0;
2ac0bcee 73 $evt['notify_payment'] = false;
d6d580ec 74 foreach ($evt['moments'] as $m) {
75 $evt['topay'] += $m['nb'] * $m['montant'];
98a7e9dc 76 if ($m['montant']) {
54ac7230 77 $evt['money'] = true;
98a7e9dc 78 }
79 $evt['paid'] = $m['paid'];
2ac0bcee 80 $evt['notify_payment'] = $evt['notify_payment'] || $m['notify_payment'];
d6d580ec 81 }
82
a7c0d514 83 $montant = XDB::fetchOneCell('SELECT SUM(amount) AS sum_amount
b3cd1320 84 FROM payment_transactions AS t
07eb5b0e
FB
85 WHERE ref = {?} AND uid = {?}',
86 $evt['paiement_id'], S::v('uid'));
a7c0d514
DB
87 $evt['telepaid'] = $montant;
88 $evt['paid'] += $montant;
ed996b5a 89 $evt['organizer'] = User::getSilent($evt['uid']);
d6d580ec 90
20c5c7e6
SJ
91 make_event_date($evt);
92
5a6bcfb7
SJ
93 $evt['show_participants'] = ($evt['show_participants'] && (is_member() || may_update()));
94
0337d704 95 return $evt;
96}
d6d580ec 97
0337d704 98// }}}
99
100// {{{ function get_event_participants()
d1965ef6 101function get_event_participants(&$evt, $item_id, array $tri = array(), $limit = null)
07eb5b0e 102{
0337d704 103 global $globals;
ed21e24a 104
ed21e24a 105 $eid = $evt['eid'];
2ac0bcee 106 $money = $evt['money'] && (function_exists('may_update')) && may_update();
ed21e24a 107 $pay_id = $evt['paiement_id'];
108
c81e03c0 109 $append = $item_id ? XDB::format(' AND ep.item_id = {?}', $item_id) : '';
07eb5b0e
FB
110 $query = XDB::fetchAllAssoc('uid', 'SELECT ep.uid, SUM(ep.paid) AS paid, SUM(ep.nb) AS nb,
111 FIND_IN_SET(\'notify_payment\', ep.flags) AS notify_payment
eb41eda9 112 FROM group_event_participants AS ep
07eb5b0e
FB
113 WHERE ep.eid = {?} AND nb > 0 ' . $append . '
114 GROUP BY ep.uid', $eid);
c8763ca3 115 $uf = new UserFilter(new PFC_True(), $tri);
4935bab0 116 $users = User::getBulkUsersWithUIDs($uf->filter(array_keys($query), new PlLimit($count, $offset)));
07eb5b0e
FB
117 $tab = array();
118 foreach ($users as $user) {
119 $uid = $user->id();
120 $tab[$uid] = $query[$uid];
121 $tab[$uid]['user'] = $user;
122 }
ed21e24a 123
0337d704 124 if ($item_id) {
07eb5b0e 125 return $tab;
0337d704 126 }
ed21e24a 127
e01ebe65
FB
128 $evt['adminpaid'] = 0;
129 $evt['telepaid'] = 0;
130 $evt['topay'] = 0;
131 $evt['paid'] = 0;
07eb5b0e 132 foreach ($tab as $uid=>&$u) {
58591700 133 $u['adminpaid'] = $u['paid'];
0337d704 134 $u['montant'] = 0;
2ac0bcee 135 if ($money && $pay_id) {
a7c0d514 136 $montant = XDB::fetchOneCell('SELECT SUM(amount)
b3cd1320 137 FROM payment_transactions AS t
07eb5b0e
FB
138 WHERE ref = {?} AND uid = {?}',
139 $pay_id, $uid);
a7c0d514 140 $u['paid'] += $montant;
2ac0bcee 141 }
58591700 142 $u['telepayment'] = $u['paid'] - $u['adminpaid'];
07eb5b0e 143 $res_ = XDB::iterator('SELECT ep.nb, ep.item_id, ei.montant
eb41eda9
FB
144 FROM group_event_participants AS ep
145 INNER JOIN group_event_items AS ei ON (ei.eid = ep.eid AND ei.item_id = ep.item_id)
07eb5b0e
FB
146 WHERE ep.eid = {?} AND ep.uid = {?}',
147 $eid, $uid);
0337d704 148 while ($i = $res_->next()) {
149 $u[$i['item_id']] = $i['nb'];
150 $u['montant'] += $i['montant']*$i['nb'];
151 }
e01ebe65
FB
152 $evt['telepaid'] += $u['telepayment'];
153 $evt['adminpaid'] += $u['adminpaid'];
154 $evt['paid'] += $u['paid'];
155 $evt['topay'] += $u['montant'];
0337d704 156 }
157 return $tab;
158}
159// }}}
160
161// {{{ function subscribe_lists_event()
50208d22
SJ
162/** Subscribes user to various event related mailing lists.
163 *
164 * @param $uid: user's id.
7852229b 165 * @param short_name: event's short_name, which corresponds to the beginning of the emails.
50208d22
SJ
166 * @param participate: indicates if the user takes part at the event or not;
167 * -1 means he did not answer, 0 means no, and 1 means yes.
168 * @param paid: has the user already payed anything?
169 * 0 means no, a positive amount means yes.
170 * @param payment: is this function called from a payment page?
171 * If true, only payment related lists should be updated.
172 */
7852229b 173function subscribe_lists_event($uid, $short_name, $participate, $paid, $payment = false)
9193e8f7 174{
7852229b
SJ
175 global $globals;
176 require_once 'emails.inc.php';
0337d704 177
7852229b
SJ
178 if (is_null($short_name)) {
179 return;
0337d704 180 }
181
50208d22
SJ
182 /** If $payment is not null, we do not retrieve the value of $participate,
183 * thus we do not alter participant and absent lists.
184 */
185 if ($payment === true) {
186 if ($paid > 0) {
7e82b545
SJ
187 delete_from_list_alias($uid, $short_name . $globals->xnet->unpayed_list, $globals->xnet->evts_domain, 'event');
188 add_to_list_alias($uid, $short_name . $globals->xnet->payed_list, $globals->xnet->evts_domain, 'event');
50208d22
SJ
189 }
190 } else {
191 switch ($participate) {
192 case -1:
7e82b545
SJ
193 delete_from_list_alias($uid, $short_name . $globals->xnet->participant_list, $globals->xnet->evts_domain, 'event');
194 delete_from_list_alias($uid, $short_name . $globals->xnet->unpayed_list, $globals->xnet->evts_domain, 'event');
195 delete_from_list_alias($uid, $short_name . $globals->xnet->payed_list, $globals->xnet->evts_domain, 'event');
196 add_to_list_alias($uid, $short_name . $globals->xnet->absent_list, $globals->xnet->evts_domain, 'event');
50208d22
SJ
197 break;
198 case 0:
7e82b545
SJ
199 delete_from_list_alias($uid, $short_name . $globals->xnet->participant_list, $globals->xnet->evts_domain, 'event');
200 delete_from_list_alias($uid, $short_name . $globals->xnet->absent_list, $globals->xnet->evts_domain, 'event');
201 delete_from_list_alias($uid, $short_name . $globals->xnet->unpayed_list, $globals->xnet->evts_domain, 'event');
202 delete_from_list_alias($uid, $short_name . $globals->xnet->payed_list, $globals->xnet->evts_domain, 'event');
50208d22
SJ
203 break;
204 case 1:
7e82b545
SJ
205 add_to_list_alias($uid, $short_name . $globals->xnet->participant_list, $globals->xnet->evts_domain, 'event');
206 delete_from_list_alias($uid, $short_name . $globals->xnet->absent_list, $globals->xnet->evts_domain, 'event');
50208d22 207 if ($paid > 0) {
7e82b545
SJ
208 delete_from_list_alias($uid, $short_name . $globals->xnet->unpayed_list, $globals->xnet->evts_domain, 'event');
209 add_to_list_alias($uid, $short_name . $globals->xnet->payed_list, $globals->xnet->evts_domain, 'event');
50208d22 210 } else {
7e82b545
SJ
211 add_to_list_alias($uid, $short_name . $globals->xnet->unpayed_list, $globals->xnet->evts_domain, 'event');
212 delete_from_list_alias($uid, $short_name . $globals->xnet->payed_list, $globals->xnet->evts_domain, 'event');
50208d22
SJ
213 }
214 break;
1cf7d38b 215 }
9193e8f7 216 }
0337d704 217}
218// }}}
219
20c5c7e6 220// {{{ function event_change_shortname()
26ba053e 221function event_change_shortname($page, $eid, $old, $new)
5070a22d 222{
223 global $globals;
7852229b 224 require_once 'emails.inc.php';
5070a22d 225
f56e5e53 226 if (is_null($old)) {
227 $old = '';
228 }
a7de4ef7 229 // Quelques vérifications sur l'alias (caractères spéciaux)
2179ffa2 230 if ($new && !preg_match( "/^[a-zA-Z0-9\-.]{3,20}$/", $new)) {
a7d35093 231 $page->trigError("Le raccourci demandé n'est pas valide.
a7de4ef7 232 Vérifie qu'il comporte entre 3 et 20 caractères
233 et qu'il ne contient que des lettres non accentuées,
234 des chiffres ou les caractères - et .");
5070a22d 235 return $old;
4aae4d2c 236 } elseif ($new && (is_int($new) || ctype_digit($new))) {
940ae3a0
SJ
237 $page->trigError("Le raccourci demandé ne peut être accepté car il
238 ne contient que des chiffres. Rajoute-lui par exemple
239 une lettre.");
240 return $old;
5070a22d 241 }
242
a7de4ef7 243 //vérifier que l'alias n'est pas déja pris
5070a22d 244 if ($new && $old != $new) {
9193e8f7 245 $res = XDB::query('SELECT COUNT(*)
eb41eda9 246 FROM group_events
9193e8f7 247 WHERE short_name = {?}',
248 $new);
5070a22d 249 if ($res->fetchOneCell() > 0) {
a7d35093 250 $page->trigError("Le raccourci demandé est déjà utilisé. Choisis en un autre.");
5070a22d 251 return $old;
252 }
253 }
254
255 if ($old == $new) {
256 return $new;
257 }
258
259 if ($old && $new) {
260 // if had a previous shortname change the old lists
7852229b
SJ
261 foreach (explode(',', $globals->xnet->event_lists) as $suffix) {
262 XDB::execute('UPDATE email_virtual
263 SET email = {?}
264 WHERE type = \'event\' AND email = {?}',
265 $new . $suffix, $old . $suffix);
5070a22d 266 }
7852229b 267
5070a22d 268 return $new;
269 }
270
271 if (!$old && $new) {
272 // if we have a first new short_name create the lists
9ff5b337
SJ
273 $lastid = array();
274 $where = array(
7852229b
SJ
275 $globals->xnet->participant_list => 'nb > 0',
276 $globals->xnet->payed_list => 'paid > 0',
277 $globals->xnet->unpayed_list => 'nb > 0 AND paid = 0'
9ff5b337
SJ
278 );
279
7852229b
SJ
280 foreach (array($globals->xnet->participant_list, $globals->xnet->payed_list, $globals->xnet->unpayed_list) as $suffix) {
281 $uids = XDB::fetchColumn('SELECT uid
282 FROM group_event_participants
a2e601ac 283 WHERE eid = {?} AND ' . $where[$suffix],
7852229b 284 $eid);
7e82b545
SJ
285 foreach ($uids as $uid) {
286 add_to_list_alias($uid, $new . $suffix, $globals->xnet->evts_domain, 'event');
7852229b 287 }
9ff5b337 288 }
7852229b
SJ
289 $uids = XDB::fetchColumn('SELECT m.uid
290 FROM group_members AS m
291 LEFT JOIN group_event_participants AS e ON (e.uid = m.uid AND e.eid = {?})
292 WHERE m.asso_id = {?} AND e.uid IS NULL',
293 $eid, $globals->asso('id'));
7e82b545
SJ
294 foreach ($uids as $uid) {
295 add_to_list_alias($uid, $new . $globals->xnet->absent_list, $globals->xnet->evts_domain, 'event');
9ff5b337 296 }
5070a22d 297
298 return $new;
299 }
300
301 if ($old && !$new) {
302 // if we delete the old short name, delete the lists
7852229b
SJ
303 foreach (explode(',', $globals->xnet->event_lists) as $suffix) {
304 delete_list_alias($old . $suffix, $globals->xnet->evts_domain);
5070a22d 305 }
7852229b 306
5070a22d 307 return $new;
308 }
309
310 // cannot happen
311 return $old;
312}
20c5c7e6
SJ
313// }}}
314
315// {{{ function make_event_date()
316function make_event_date(&$e)
317{
318 $start = strtotime($e['debut']);
319 $end = strtotime($e['fin']);
07eb5b0e 320 $first_day = @strtotime($e['first_day']);
20c5c7e6
SJ
321 $last_day = strtotime($e['last_day']);
322 unset($e['debut'], $e['fin'], $e['first_day'], $e['last_day']);
323
324 $date = "";
325 if ($start && $end != $start) {
326 if ($first_day == $last_day) {
327 $date .= "le " . strftime("%d %B %Y", $start) . " de "
328 . strftime("%H:%M", $start) . " à " . strftime("%H:%M", $end);
329 } else {
330 $date .= "du " . strftime("%d %B %Y à %H:%M", $start)
331 . "\nau " . strftime("%d %B %Y à %H:%M", $end);
332 }
333 } else {
334 $date .= "le " . strftime("%d %B %Y à %H:%M", $start);
335 }
336 $e['date'] = $date;
337}
338// }}}
5070a22d 339
a7de4ef7 340// vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
0337d704 341?>