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