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