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