Remove debugging echo in xnetevents module
[platal.git] / modules / xnetevents / xnetevents.inc.php
1 <?php
2 /***************************************************************************
3 * Copyright (C) 2003-2014 Polytechnique.org *
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_order()
23 /* get the order to paste the events
24 * @param $asso_id: group's id
25 */
26 function get_event_order($asso_id)
27 {
28 $order = XDB::fetchOneCell('SELECT g.event_order
29 FROM groups as g
30 WHERE id = {?}',
31 $asso_id);
32 return $order;
33 }
34 // }}}
35
36 // {{{ function get_events()
37 /* get the events of the given group ordered by the standard order for the group
38 * @param $asso_id: group's id
39 * @param $order: order to paste the events (asc or desc)
40 * @param $archive: whether to get the archived events (1) or the actuals (0)
41 */
42 function get_events($asso_id, $order, $archive)
43 {
44 if ($order != 'asc' && $order != 'desc') {
45 $order = 'desc';
46 }
47 $evts = XDB::fetchAllAssoc('eid', "SELECT ge.eid, ge.uid, ge.intitule, ge.debut, ge.fin, ge.show_participants, ge.deadline_inscription, ge.accept_nonmembre, ge.paiement_id
48 FROM group_events as ge
49 WHERE asso_id = {?} and archive = {?}
50 ORDER BY ge.debut $order",
51 $asso_id, $archive);
52 return $evts;
53 }
54 // }}}
55
56 // {{{ function get_event() (detail, for subs page only for now)
57 /* get event details
58 * @param $eid: event's id
59 */
60 function get_event($eid)
61 {
62 $evt = XDB::fetchOneAssoc('SELECT ge.uid, ge.intitule, ge.descriptif, ge.debut, ge.fin, ge.deadline_inscription, ge.accept_nonmembre, ge.paiement_id
63 FROM group_events as ge
64 WHERE eid = {?}',
65 $eid);
66 if (!is_null($evt['deadline_inscription']) && strtotime($evt['deadline_inscription']) < time()) {
67 $evt['inscr_open'] = false;
68 } else {
69 $evt['inscr_open'] = true;
70 }
71 $evt['organizer'] = User::getSilent($evt['uid'])->profile();
72 $evt['date'] = make_event_date($evt['debut'], $evt['fin']);
73
74 return $evt;
75 }
76 // }}}
77
78 // {{{ function get_event_items()
79 /** get items of the given event
80 *
81 * @param $eid : event's id
82 *
83 */
84 function get_event_items($eid)
85 {
86 $evt = XDB::fetchAllAssoc('item_id', 'SELECT gei.item_id, gei.titre, gei.details, gei.montant
87 FROM group_event_items as gei
88 WHERE eid = {?}',
89 $eid);
90 return $evt;
91 }
92 // }}}
93
94 // {{{ function get_event_subscription()
95 /* get all participations if uid is not specified, only the user's participation if uid specified
96 * @param $eid: event's id
97 * @param $uid: user's id
98 */
99 function get_event_subscription($eid, $uid = null)
100 {
101 if (!is_null($uid)) {
102 $where = ' and gep.uid = '.$uid;
103 }
104 else {
105 $where = '';
106 }
107 $sub = XDB::fetchAllAssoc('item_id','SELECT gep.item_id, gep.nb, gep.paid FROM group_event_participants as gep
108 WHERE gep.eid = {?}'.$where,
109 $eid);
110 return $sub;
111 }
112 // }}}
113
114 // {{{ function get_event_telepaid()
115 /* get the total payments made by a user for an event
116 * @param $eid: event's id
117 * @param $uid: user's id
118 */
119 function get_event_telepaid($eid, $uid)
120 {
121 $telepaid = XDB::fetchOneCell('SELECT SUM(pt.amount)
122 FROM payment_transactions AS pt
123 LEFT JOIN group_events as ge ON (ge.paiement_id = pt.ref)
124 WHERE ge.eid = {?} AND pt.uid = {?}',
125 $eid, $uid);
126 return $telepaid;
127 }
128 // }}}
129
130 // {{{ function get_event_detail()
131
132 function get_event_detail($eid, $item_id = false, $asso_id = null)
133 {
134 global $globals;
135 if (is_null($asso_id)) {
136 $asso_id = $globals->asso('id');
137 }
138 if (!$item_id) {
139 $where = '';
140 $group_by = 'e.eid';
141 } else {
142 $where = XDB::format(' AND ei.item_id = {?}', $item_id);
143 $group_by = 'ei.item_id';
144 }
145 $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,
146 IF(e.deadline_inscription,
147 e.deadline_inscription >= LEFT(NOW(), 10),
148 1) AS inscr_open,
149 LEFT(e.debut, 10) AS first_day, LEFT(e.fin, 10) AS last_day,
150 LEFT(NOW(), 10) AS now,
151 ei.titre, e.subscription_notification
152 FROM group_events AS e
153 INNER JOIN group_event_items AS ei ON (e.eid = ei.eid)
154 LEFT JOIN group_event_participants AS ep ON(e.eid = ep.eid AND ei.item_id = ep.item_id)
155 WHERE (e.eid = {?} OR e.short_name = {?}) AND e.asso_id = {?}' . $where . '
156 GROUP BY ' . $group_by,
157 $eid, $eid, $asso_id);
158
159 if (!$evt) {
160 return null;
161 }
162 if ($GLOBALS['IS_XNET_SITE'] && $evt['accept_nonmembre'] == 0 && !is_member() && !may_update()) {
163 return false;
164 }
165
166 if (!$item_id) {
167 /* Don't try to be to smart here, in case we're getting the global summary, we cannot have
168 * a general formula to estimate the total number of comers since 'moments' may (or may not be)
169 * disjuncted. As a consequence, we can only provides the number of user having fullfiled the
170 * registration procedure.
171 */
172 $evt['user_count'] = $evt['nb_tot'] = $evt['nb'];
173 $evt['titre'] = '';
174 $evt['item_id'] = 0;
175 $evt['csv_name'] = urlencode($evt['intitule']);
176 } else {
177 $evt['csv_name'] = urlencode($evt['intitule'] . '.' . $evt['titre']);
178 }
179
180 $evt['moments'] = XDB::fetchAllAssoc('SELECT titre, details, montant, ei.item_id, nb,
181 ep.paid, FIND_IN_SET(\'notify_payment\', ep.flags) AS notify_payment
182 FROM group_event_items AS ei
183 LEFT JOIN group_event_participants AS ep ON (ep.eid = ei.eid AND ep.item_id = ei.item_id
184 AND uid = {?})
185 WHERE ei.eid = {?}',
186 S::i('uid'), $evt['eid']);
187 $evt['topay'] = 0;
188 $evt['paid'] = 0;
189 $evt['notify_payment'] = false;
190 foreach ($evt['moments'] as $m) {
191 $evt['topay'] += $m['nb'] * $m['montant'];
192 if ($m['montant']) {
193 $evt['money'] = true;
194 }
195 $evt['paid'] += $m['paid'];
196 $evt['notify_payment'] = $evt['notify_payment'] || $m['notify_payment'];
197 }
198
199 $montant = XDB::fetchOneCell('SELECT SUM(amount) AS sum_amount
200 FROM payment_transactions AS t
201 WHERE ref = {?} AND uid = {?}',
202 $evt['paiement_id'], S::v('uid'));
203 $evt['telepaid'] = $montant;
204 $evt['paid'] += $montant;
205 $evt['organizer'] = User::getSilent($evt['uid']);
206
207 $evt['date'] = make_event_date($evt['debut'], $evt['fin']);
208
209 $evt['show_participants'] = ($evt['show_participants'] && $GLOBALS['IS_XNET_SITE'] && (is_member() || may_update()));
210
211 return $evt;
212 }
213
214 // }}}
215
216 // {{{ function get_event_participants()
217 function get_event_participants(&$evt, $item_id, array $tri = array(), $limit = null, $offset = 0)
218 {
219 global $globals;
220
221 $eid = $evt['eid'];
222 $money = $evt['money'] && (function_exists('may_update')) && may_update();
223 $pay_id = $evt['paiement_id'];
224
225 $append = $item_id ? XDB::format(' AND ep.item_id = {?}', $item_id) : '';
226 $query = XDB::fetchAllAssoc('uid', 'SELECT ep.uid, SUM(ep.paid) AS paid, SUM(ep.nb) AS nb,
227 FIND_IN_SET(\'notify_payment\', ep.flags) AS notify_payment
228 FROM group_event_participants AS ep
229 WHERE ep.eid = {?} AND nb > 0 ' . $append . '
230 GROUP BY ep.uid', $eid);
231 $uf = new UserFilter(new PFC_True(), $tri);
232 $users = User::getBulkUsersWithUIDs($uf->filter(array_keys($query), new PlLimit($limit, $offset)));
233 $tab = array();
234 foreach ($users as $user) {
235 $uid = $user->id();
236 $tab[$uid] = $query[$uid];
237 $tab[$uid]['user'] = $user;
238 }
239
240 if ($item_id) {
241 return $tab;
242 }
243
244 $evt['adminpaid'] = 0;
245 $evt['telepaid'] = 0;
246 $evt['topay'] = 0;
247 $evt['paid'] = 0;
248 foreach ($tab as $uid=>&$u) {
249 $u['adminpaid'] = (float)$u['paid'];
250 $u['montant'] = 0;
251 if ($money && $pay_id) {
252 $montant = XDB::fetchOneCell('SELECT SUM(amount)
253 FROM payment_transactions AS t
254 WHERE ref = {?} AND uid = {?}',
255 $pay_id, $uid);
256 $u['paid'] += $montant;
257 }
258 $u['telepayment'] = $u['paid'] - $u['adminpaid'];
259 $res_ = XDB::iterator('SELECT ep.nb, ep.item_id, ei.montant
260 FROM group_event_participants AS ep
261 INNER JOIN group_event_items AS ei ON (ei.eid = ep.eid AND ei.item_id = ep.item_id)
262 WHERE ep.eid = {?} AND ep.uid = {?}',
263 $eid, $uid);
264 while ($i = $res_->next()) {
265 $u[$i['item_id']] = $i['nb'];
266 $u['montant'] += $i['montant'] * $i['nb'];
267 }
268 $evt['telepaid'] += $u['telepayment'];
269 $evt['adminpaid'] += $u['adminpaid'];
270 $evt['paid'] += $u['paid'];
271 $evt['topay'] += $u['montant'];
272 }
273 return $tab;
274 }
275 // }}}
276
277 // {{{ function subscribe()
278 /** set or update the user's subscription
279 *
280 * @param $uid: user's id
281 * @param $eid: event's id
282 * @param $subs: user's new subscription
283 *
284 */
285 function subscribe($uid, $eid, $subs = array())
286 {
287 global $globals;
288 // get items
289 $items = get_event_items($eid);
290 // get previous subscription
291 $old_subs = get_event_subscription($eid, $uid);
292 $participate = false;
293 $updated = false;
294 // TODO : change the way to deal with manual payment
295 $paid = 0;
296 foreach ($old_subs as $item_id => $s) {
297 $paid += $s['paid'];
298 }
299 $paid_updated = false;
300 // for each item of the event
301 foreach ($items as $item_id => $details) {
302 // check if there is an old subscription
303 if (array_key_exists($item_id, $old_subs)) {
304 // compares new and old subscription
305 if ($old_subs[$item_id]['nb'] != $subs[$item_id]) {
306 if ($subs[$item_id] != 0) {
307 echo "je m'inscris ";
308 XDB::execute('INSERT INTO group_event_participants (eid, uid, item_id, nb, flags, paid)
309 VALUES ({?}, {?}, {?}, {?}, {?}, {?})
310 ON DUPLICATE KEY UPDATE nb = VALUES(nb), flags = VALUES(flags), paid = VALUES(paid)',
311 $eid, $uid, $item_id, $subs[$item_id],(Env::has('notify_payment') ? 'notify_payment' : 0), (!$paid_updated ? $paid : 0));
312 $participate = true;
313 $paid_updated = true;
314 } else { // we do not store non-subscription to event items
315 XDB::execute('DELETE FROM group_event_participants
316 WHERE eid = {?} AND uid = {?} AND item_id = {?}',
317 $eid, $uid, $item_id);
318 }
319 $updated = true;
320 }
321 } else { // if no old subscription
322 if ($subs[$item_id] != 0) {
323 XDB::execute('INSERT INTO group_event_participants (eid, uid, item_id, nb, flags, paid)
324 VALUES ({?}, {?}, {?}, {?}, {?}, {?})',
325 $eid, $uid, $item_id, $subs[$item_id], '', 0);
326 $participate = true;
327 $updated = true;
328 }
329 }
330 }
331 // item 0 stores whether the user participates globally or not, if he has to be notified when payment is created and his manual payment
332 /*
333 if (array_key_exists(0, $old_subs)) {
334 XDB::execute('UPDATE group_event_participants
335 SET nb = {?}
336 WHERE eid = {?}, uid = {?}, item_id = 0',
337 ($participate ? 1 : 0), $eid, $uid);
338 } else {
339 XDB::execute('INSERT INTO group_event_participants (eid, uid, item_id, nb, flags, paid)
340 VALUES ({?}, {?}, {?}, {?}, {?}, {?})',
341 $eid, $uid, 0, ($participate ? 1 : 0), (Env::has('notify_payment') ? 'notify_payment' : ''), 0);
342 }
343 */
344 // if subscription is updated, we have to update the event aliases
345 if ($updated) {
346 $short_name = get_event_detail($eid)['short_name'];
347 subscribe_lists_event($uid, $short_name, ($participate ? 1 : -1), 0);
348 }
349 return $updated;
350 }
351 // }}}
352
353 // TODO : correct this function to be compatible with subscribe() (use $eid, remove useless argument)
354 // TODO : correct other calls
355 // {{{ function subscribe_lists_event()
356 /** Subscribes user to various event related mailing lists.
357 *
358 * @param $uid: user's id.
359 * @param short_name: event's short_name, which corresponds to the beginning of the emails.
360 * @param participate: indicates if the user takes part at the event or not;
361 * -1 means he did not answer, 0 means no, and 1 means yes.
362 * @param paid: has the user already payed anything?
363 * 0 means no, a positive amount means yes.
364 * @param payment: is this function called from a payment page?
365 * If true, only payment related lists should be updated.
366 */
367 function subscribe_lists_event($uid, $short_name, $participate, $paid, $payment = false)
368 {
369 global $globals;
370 require_once 'emails.inc.php';
371
372 if (is_null($short_name)) {
373 return;
374 }
375
376 /** If $payment is not null, we do not retrieve the value of $participate,
377 * thus we do not alter participant and absent lists.
378 */
379 if ($payment === true) {
380 if ($paid > 0) {
381 delete_from_list_alias($uid, $short_name . $globals->xnet->unpayed_list, $globals->xnet->evts_domain, 'event');
382 add_to_list_alias($uid, $short_name . $globals->xnet->payed_list, $globals->xnet->evts_domain, 'event');
383 }
384 } else {
385 switch ($participate) {
386 case -1:
387 delete_from_list_alias($uid, $short_name . $globals->xnet->participant_list, $globals->xnet->evts_domain, 'event');
388 delete_from_list_alias($uid, $short_name . $globals->xnet->unpayed_list, $globals->xnet->evts_domain, 'event');
389 delete_from_list_alias($uid, $short_name . $globals->xnet->payed_list, $globals->xnet->evts_domain, 'event');
390 add_to_list_alias($uid, $short_name . $globals->xnet->absent_list, $globals->xnet->evts_domain, 'event');
391 break;
392 case 0:
393 delete_from_list_alias($uid, $short_name . $globals->xnet->participant_list, $globals->xnet->evts_domain, 'event');
394 delete_from_list_alias($uid, $short_name . $globals->xnet->absent_list, $globals->xnet->evts_domain, 'event');
395 delete_from_list_alias($uid, $short_name . $globals->xnet->unpayed_list, $globals->xnet->evts_domain, 'event');
396 delete_from_list_alias($uid, $short_name . $globals->xnet->payed_list, $globals->xnet->evts_domain, 'event');
397 break;
398 case 1:
399 add_to_list_alias($uid, $short_name . $globals->xnet->participant_list, $globals->xnet->evts_domain, 'event');
400 delete_from_list_alias($uid, $short_name . $globals->xnet->absent_list, $globals->xnet->evts_domain, 'event');
401 if ($paid > 0) {
402 delete_from_list_alias($uid, $short_name . $globals->xnet->unpayed_list, $globals->xnet->evts_domain, 'event');
403 add_to_list_alias($uid, $short_name . $globals->xnet->payed_list, $globals->xnet->evts_domain, 'event');
404 } else {
405 add_to_list_alias($uid, $short_name . $globals->xnet->unpayed_list, $globals->xnet->evts_domain, 'event');
406 delete_from_list_alias($uid, $short_name . $globals->xnet->payed_list, $globals->xnet->evts_domain, 'event');
407 }
408 break;
409 }
410 }
411 }
412 // }}}
413
414 // {{{ function event_change_shortname()
415 function event_change_shortname($page, $eid, $old, $new)
416 {
417 global $globals;
418 require_once 'emails.inc.php';
419
420 if (is_null($old)) {
421 $old = '';
422 }
423 // Quelques vérifications sur l'alias (caractères spéciaux)
424 if ($new && !preg_match( "/^[a-zA-Z0-9\-.]{3,20}$/", $new)) {
425 $page->trigError("Le raccourci demandé n'est pas valide.
426 Vérifie qu'il comporte entre 3 et 20 caractères
427 et qu'il ne contient que des lettres non accentuées,
428 des chiffres ou les caractères - et .");
429 return $old;
430 } elseif ($new && (is_int($new) || ctype_digit($new))) {
431 $page->trigError("Le raccourci demandé ne peut être accepté car il
432 ne contient que des chiffres. Rajoute-lui par exemple
433 une lettre.");
434 return $old;
435 }
436
437 //vérifier que l'alias n'est pas déja pris
438 if ($new && $old != $new) {
439 $res = XDB::query('SELECT COUNT(*)
440 FROM group_events
441 WHERE short_name = {?}',
442 $new);
443 if ($res->fetchOneCell() > 0) {
444 $page->trigError("Le raccourci demandé est déjà utilisé. Choisis en un autre.");
445 return $old;
446 }
447 }
448
449 if ($old == $new) {
450 return $new;
451 }
452
453 if ($old && $new) {
454 // if had a previous shortname change the old lists
455 foreach (explode(',', $globals->xnet->event_lists) as $suffix) {
456 XDB::execute('UPDATE email_virtual
457 SET email = {?}
458 WHERE type = \'event\' AND email = {?}',
459 $new . $suffix, $old . $suffix);
460 }
461
462 return $new;
463 }
464
465 if (!$old && $new) {
466 // if we have a first new short_name create the lists
467 $lastid = array();
468 $where = array(
469 $globals->xnet->participant_list => 'g.nb > 0',
470 $globals->xnet->payed_list => '(g.paid > 0 OR p.amount > 0)',
471 $globals->xnet->unpayed_list => 'g.nb > 0 AND g.paid = 0 AND p.amount IS NULL'
472 );
473
474 foreach (array($globals->xnet->participant_list, $globals->xnet->payed_list, $globals->xnet->unpayed_list) as $suffix) {
475 $uids = XDB::fetchColumn('SELECT g.uid
476 FROM group_event_participants AS g
477 INNER JOIN group_events AS e ON (g.eid = e.eid)
478 LEFT JOIN payment_transactions AS p ON (e.paiement_id = p.ref AND g.uid = p.uid)
479 WHERE g.eid = {?} AND ' . $where[$suffix],
480 $eid);
481 foreach ($uids as $uid) {
482 add_to_list_alias($uid, $new . $suffix, $globals->xnet->evts_domain, 'event');
483 }
484 }
485
486 $uids = XDB::fetchColumn('SELECT m.uid
487 FROM group_members AS m
488 LEFT JOIN group_event_participants AS e ON (e.uid = m.uid AND e.eid = {?})
489 WHERE m.asso_id = {?} AND e.uid IS NULL',
490 $eid, $globals->asso('id'));
491 foreach ($uids as $uid) {
492 add_to_list_alias($uid, $new . $globals->xnet->absent_list, $globals->xnet->evts_domain, 'event');
493 }
494
495 return $new;
496 }
497
498 if ($old && !$new) {
499 // if we delete the old short name, delete the lists
500 foreach (explode(',', $globals->xnet->event_lists) as $suffix) {
501 delete_list_alias($old . $suffix, $globals->xnet->evts_domain);
502 }
503
504 return $new;
505 }
506
507 // cannot happen
508 return $old;
509 }
510 // }}}
511
512 // {{{ function make_event_date()
513 function make_event_date($debut, $fin)
514 {
515 $start = strtotime($debut);
516 $end = strtotime($fin);
517 // $first_day = $e['first_day'];
518 // $last_day = $e['last_day'];
519 $first_day = strftime("%d %B %Y", $start);
520 $last_day = strftime("%d %B %Y", $end);
521 $date = "";
522 if ($start && $end != $start) {
523 if ($first_day == $last_day) {
524 $date .= "le " . strftime("%d %B %Y", $start) . " de "
525 . strftime("%H:%M", $start) . " à " . strftime("%H:%M", $end);
526 } else {
527 $date .= "du " . strftime("%d %B %Y à %H:%M", $start)
528 . "\nau " . strftime("%d %B %Y à %H:%M", $end);
529 }
530 } else {
531 $date .= "le " . strftime("%d %B %Y à %H:%M", $start);
532 }
533 return $date;
534 }
535 // }}}
536
537 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker fenc=utf-8:
538 ?>