2 /***************************************************************************
3 * Copyright (C) 2003-2014 Polytechnique.org *
4 * http://opensource.polytechnique.org/ *
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. *
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. *
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 *
19 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *
20 ***************************************************************************/
22 // {{{ function get_event_order()
23 /* get the order to paste the events
24 * @param $asso_id: group's id
26 function get_event_order($asso_id)
28 $order = XDB
::fetchOneCell('SELECT g.event_order
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)
42 function get_events($asso_id, $order, $archive)
44 if ($order != 'asc' && $order != 'desc') {
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",
56 // {{{ function get_event() (detail, for subs page only for now)
58 * @param $eid: event's id
60 function get_event(&$eid)
62 if (!is_numeric($eid)) {
63 $id = XDB
::fetchOneCell("SELECT eid
65 WHERE short_name = {?}",
69 $evt = XDB
::fetchOneAssoc('SELECT ge.uid, ge.intitule, ge.descriptif, ge.debut, ge.fin, ge.deadline_inscription, ge.accept_nonmembre, ge.noinvite, ge.paiement_id
70 FROM group_events as ge
73 if (!is_null($evt['deadline_inscription']) && strtotime($evt['deadline_inscription']) <= time()) {
74 $evt['inscr_open'] = false
;
76 $evt['inscr_open'] = true
;
78 $evt['organizer'] = User
::getSilent($evt['uid']);
79 $evt['date'] = make_event_date($evt['debut'], $evt['fin']);
85 // {{{ function get_event_items()
86 /** get items of the given event
88 * @param $eid : event's id
91 function get_event_items($eid)
93 $evt = XDB
::fetchAllAssoc('item_id', 'SELECT gei.item_id, gei.titre, gei.details, gei.montant
94 FROM group_event_items as gei
101 // {{{ function get_event_subscription()
102 /* get all participations if uid is not specified, only the user's participation if uid specified
103 * @param $eid: event's id
104 * @param $uid: user's id
106 function get_event_subscription($eid, $uid = null
)
108 if (!is_null($uid)) {
109 $where = ' and gep.uid = '.$uid;
114 $sub = XDB
::fetchAllAssoc('item_id','SELECT gep.item_id, gep.nb, gep.paid FROM group_event_participants as gep
115 WHERE gep.eid = {?}'.$where,
121 // {{{ function get_event_telepaid()
122 /* get the total payments made by a user for an event
123 * @param $eid: event's id
124 * @param $uid: user's id
126 function get_event_telepaid($eid, $uid)
128 $telepaid = XDB
::fetchOneCell('SELECT SUM(pt.amount)
129 FROM payment_transactions AS pt
130 LEFT JOIN group_events as ge ON (ge.paiement_id = pt.ref)
131 WHERE pt.status = "confirmed" AND ge.eid = {?} AND pt.uid = {?}',
137 // {{{ function get_event_detail()
139 function get_event_detail($eid, $item_id = false
, $asso_id = null
)
142 if (is_null($asso_id)) {
143 $asso_id = $globals->asso('id');
149 $where = XDB
::format(' AND ei.item_id = {?}', $item_id);
150 $group_by = 'ei.item_id';
152 $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,
153 IF(e.deadline_inscription,
154 e.deadline_inscription >= LEFT(NOW(), 10),
156 LEFT(e.debut, 10) AS first_day, LEFT(e.fin, 10) AS last_day,
157 LEFT(NOW(), 10) AS now,
158 ei.titre, e.subscription_notification
159 FROM group_events AS e
160 INNER JOIN group_event_items AS ei ON (e.eid = ei.eid)
161 LEFT JOIN group_event_participants AS ep ON(e.eid = ep.eid AND ei.item_id = ep.item_id)
162 WHERE (e.eid = {?} OR e.short_name = {?}) AND e.asso_id = {?}' . $where . '
163 GROUP BY ' . $group_by,
164 $eid, $eid, $asso_id);
169 if ($GLOBALS['IS_XNET_SITE'] && $evt['accept_nonmembre'] == 0 && !is_member() && !may_update()) {
174 /* Don't try to be to smart here, in case we're getting the global summary, we cannot have
175 * a general formula to estimate the total number of comers since 'moments' may (or may not be)
176 * disjuncted. As a consequence, we can only provides the number of user having fullfiled the
177 * registration procedure.
179 $evt['user_count'] = $evt['nb_tot'] = $evt['nb'];
182 $evt['csv_name'] = urlencode($evt['intitule']);
184 $evt['csv_name'] = urlencode($evt['intitule'] . '.' . $evt['titre']);
187 $evt['moments'] = XDB
::fetchAllAssoc('SELECT titre, details, montant, ei.item_id, nb,
188 ep.paid, FIND_IN_SET(\'notify_payment\', ep.flags) AS notify_payment
189 FROM group_event_items AS ei
190 LEFT JOIN group_event_participants AS ep ON (ep.eid = ei.eid AND ep.item_id = ei.item_id
193 S
::i('uid'), $evt['eid']);
196 $evt['notify_payment'] = false
;
197 foreach ($evt['moments'] as $m) {
198 $evt['topay'] +
= $m['nb'] * $m['montant'];
200 $evt['money'] = true
;
202 $evt['paid'] +
= $m['paid'];
203 $evt['notify_payment'] = $evt['notify_payment'] ||
$m['notify_payment'];
206 $montant = XDB
::fetchOneCell('SELECT SUM(amount) AS sum_amount
207 FROM payment_transactions AS t
208 WHERE status = "confirmed" AND ref = {?} AND uid = {?}',
209 $evt['paiement_id'], S
::v('uid'));
210 $evt['telepaid'] = $montant;
211 $evt['paid'] +
= $montant;
212 $evt['organizer'] = User
::getSilent($evt['uid']);
214 $evt['date'] = make_event_date($evt['debut'], $evt['fin']);
216 $evt['show_participants'] = ($evt['show_participants'] && $GLOBALS['IS_XNET_SITE'] && (is_member() ||
may_update()));
223 // {{{ function get_event_participants()
224 function get_event_participants(&$evt, $item_id, array $tri = array(), $limit = null
, $offset = 0)
229 $money = $evt['money'] && (function_exists('may_update')) && may_update();
230 $pay_id = $evt['paiement_id'];
232 $append = $item_id ? XDB
::format(' AND ep.item_id = {?}', $item_id) : '';
233 $query = XDB
::fetchAllAssoc('uid', 'SELECT ep.uid, SUM(ep.paid) AS paid, SUM(ep.nb) AS nb,
234 FIND_IN_SET(\'notify_payment\', ep.flags) AS notify_payment
235 FROM group_event_participants AS ep
236 WHERE ep.eid = {?} AND nb > 0 ' . $append . '
237 GROUP BY ep.uid', $eid);
238 $uf = new UserFilter(new PFC_True(), $tri);
239 $users = User
::getBulkUsersWithUIDs($uf->filter(array_keys($query), new PlLimit($limit, $offset)));
241 foreach ($users as $user) {
243 $tab[$uid] = $query[$uid];
244 $tab[$uid]['user'] = $user;
251 $evt['adminpaid'] = 0;
252 $evt['telepaid'] = 0;
255 foreach ($tab as $uid=>&$u) {
256 $u['adminpaid'] = (float)$u['paid'];
258 if ($money && $pay_id) {
259 $montant = XDB
::fetchOneCell('SELECT SUM(amount)
260 FROM payment_transactions AS t
261 WHERE status = "confirmed" AND ref = {?} AND uid = {?}',
263 $u['paid'] +
= $montant;
265 $u['telepayment'] = $u['paid'] - $u['adminpaid'];
266 $res_ = XDB
::iterator('SELECT ep.nb, ep.item_id, ei.montant
267 FROM group_event_participants AS ep
268 INNER JOIN group_event_items AS ei ON (ei.eid = ep.eid AND ei.item_id = ep.item_id)
269 WHERE ep.eid = {?} AND ep.uid = {?}',
271 while ($i = $res_->next()) {
272 $u[$i['item_id']] = $i['nb'];
273 $u['montant'] +
= $i['montant'] * $i['nb'];
275 $evt['telepaid'] +
= $u['telepayment'];
276 $evt['adminpaid'] +
= $u['adminpaid'];
277 $evt['paid'] +
= $u['paid'];
278 $evt['topay'] +
= $u['montant'];
284 // {{{ function subscribe()
285 /** set or update the user's subscription
287 * @param $uid: user's id
288 * @param $eid: event's id
289 * @param $subs: user's new subscription
292 function subscribe($uid, $eid, $subs = array())
296 $items = get_event_items($eid);
297 // get previous subscription
298 $old_subs = get_event_subscription($eid, $uid);
299 $participate = false
;
301 // TODO : change the way to deal with manual payment
303 foreach ($old_subs as $item_id => $s) {
306 $paid_updated = false
;
307 // for each item of the event
308 foreach ($items as $item_id => $details) {
309 // check if there is an old subscription
310 if (array_key_exists($item_id, $old_subs)) {
311 // compares new and old subscription
312 if ($old_subs[$item_id]['nb'] != $subs[$item_id]) {
313 if ($subs[$item_id] != 0) {
314 XDB
::execute('INSERT INTO group_event_participants (eid, uid, item_id, nb, flags, paid)
315 VALUES ({?}, {?}, {?}, {?}, {?}, {?})
316 ON DUPLICATE KEY UPDATE nb = VALUES(nb), flags = VALUES(flags), paid = VALUES(paid)',
317 $eid, $uid, $item_id, $subs[$item_id],(Env
::has('notify_payment') ?
'notify_payment' : 0), (!$paid_updated ?
$paid : 0));
319 $paid_updated = true
;
320 } else { // we do not store non-subscription to event items
321 XDB
::execute('DELETE FROM group_event_participants
322 WHERE eid = {?} AND uid = {?} AND item_id = {?}',
323 $eid, $uid, $item_id);
327 } else { // if no old subscription
328 if ($subs[$item_id] != 0) {
329 XDB
::execute('INSERT INTO group_event_participants (eid, uid, item_id, nb, flags, paid)
330 VALUES ({?}, {?}, {?}, {?}, {?}, {?})',
331 $eid, $uid, $item_id, $subs[$item_id], '', 0);
337 // item 0 stores whether the user participates globally or not, if he has to be notified when payment is created and his manual payment
339 if (array_key_exists(0, $old_subs)) {
340 XDB::execute('UPDATE group_event_participants
342 WHERE eid = {?}, uid = {?}, item_id = 0',
343 ($participate ? 1 : 0), $eid, $uid);
345 XDB::execute('INSERT INTO group_event_participants (eid, uid, item_id, nb, flags, paid)
346 VALUES ({?}, {?}, {?}, {?}, {?}, {?})',
347 $eid, $uid, 0, ($participate ? 1 : 0), (Env::has('notify_payment') ? 'notify_payment' : ''), 0);
350 // if subscription is updated, we have to update the event aliases
352 $short_name = get_event_detail($eid)['short_name'];
353 subscribe_lists_event($uid, $short_name, ($participate ?
1 : -1), 0);
359 // TODO : correct this function to be compatible with subscribe() (use $eid, remove useless argument)
360 // TODO : correct other calls
361 // {{{ function subscribe_lists_event()
362 /** Subscribes user to various event related mailing lists.
364 * @param $uid: user's id.
365 * @param short_name: event's short_name, which corresponds to the beginning of the emails.
366 * @param participate: indicates if the user takes part at the event or not;
367 * -1 means he did not answer, 0 means no, and 1 means yes.
368 * @param paid: has the user already payed anything?
369 * 0 means no, a positive amount means yes.
370 * @param payment: is this function called from a payment page?
371 * If true, only payment related lists should be updated.
373 function subscribe_lists_event($uid, $short_name, $participate, $paid, $payment = false
)
376 require_once 'emails.inc.php';
378 if (is_null($short_name)) {
382 /** If $payment is not null, we do not retrieve the value of $participate,
383 * thus we do not alter participant and absent lists.
385 if ($payment === true
) {
387 delete_from_list_alias($uid, $short_name . $globals->xnet
->unpayed_list
, $globals->xnet
->evts_domain
, 'event');
388 add_to_list_alias($uid, $short_name . $globals->xnet
->payed_list
, $globals->xnet
->evts_domain
, 'event');
391 switch ($participate) {
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
->unpayed_list
, $globals->xnet
->evts_domain
, 'event');
395 delete_from_list_alias($uid, $short_name . $globals->xnet
->payed_list
, $globals->xnet
->evts_domain
, 'event');
396 add_to_list_alias($uid, $short_name . $globals->xnet
->absent_list
, $globals->xnet
->evts_domain
, 'event');
399 delete_from_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 delete_from_list_alias($uid, $short_name . $globals->xnet
->unpayed_list
, $globals->xnet
->evts_domain
, 'event');
402 delete_from_list_alias($uid, $short_name . $globals->xnet
->payed_list
, $globals->xnet
->evts_domain
, 'event');
405 add_to_list_alias($uid, $short_name . $globals->xnet
->participant_list
, $globals->xnet
->evts_domain
, 'event');
406 delete_from_list_alias($uid, $short_name . $globals->xnet
->absent_list
, $globals->xnet
->evts_domain
, 'event');
408 delete_from_list_alias($uid, $short_name . $globals->xnet
->unpayed_list
, $globals->xnet
->evts_domain
, 'event');
409 add_to_list_alias($uid, $short_name . $globals->xnet
->payed_list
, $globals->xnet
->evts_domain
, 'event');
411 add_to_list_alias($uid, $short_name . $globals->xnet
->unpayed_list
, $globals->xnet
->evts_domain
, 'event');
412 delete_from_list_alias($uid, $short_name . $globals->xnet
->payed_list
, $globals->xnet
->evts_domain
, 'event');
420 // {{{ function event_change_shortname()
421 function event_change_shortname($page, $eid, $old, $new)
424 require_once 'emails.inc.php';
429 // Quelques vérifications sur l'alias (caractères spéciaux)
430 if ($new && !preg_match( "/^[a-zA-Z0-9\-.]{3,20}$/", $new)) {
431 $page->trigError("Le raccourci demandé n'est pas valide.
432 Vérifie qu'il comporte entre 3 et 20 caractères
433 et qu'il ne contient que des lettres non accentuées,
434 des chiffres ou les caractères - et .");
436 } elseif ($new && (is_int($new) ||
ctype_digit($new))) {
437 $page->trigError("Le raccourci demandé ne peut être accepté car il
438 ne contient que des chiffres. Rajoute-lui par exemple
443 //vérifier que l'alias n'est pas déja pris
444 if ($new && $old != $new) {
445 $res = XDB
::query('SELECT COUNT(*)
447 WHERE short_name = {?}',
449 if ($res->fetchOneCell() > 0) {
450 $page->trigError("Le raccourci demandé est déjà utilisé. Choisis en un autre.");
460 // if had a previous shortname change the old lists
461 foreach (explode(',', $globals->xnet
->event_lists
) as $suffix) {
462 XDB
::execute('UPDATE email_virtual
464 WHERE type = \'event\' AND email = {?}',
465 $new . $suffix, $old . $suffix);
472 // if we have a first new short_name create the lists
475 $globals->xnet
->participant_list
=> 'g.nb > 0',
476 $globals->xnet
->payed_list
=> '(g.paid > 0 OR p.amount > 0)',
477 $globals->xnet
->unpayed_list
=> 'g.nb > 0 AND g.paid = 0 AND p.amount IS NULL'
480 foreach (array($globals->xnet
->participant_list
, $globals->xnet
->payed_list
, $globals->xnet
->unpayed_list
) as $suffix) {
481 $uids = XDB
::fetchColumn('SELECT g.uid
482 FROM group_event_participants AS g
483 INNER JOIN group_events AS e ON (g.eid = e.eid)
484 LEFT JOIN payment_transactions AS p ON (e.paiement_id = p.ref AND g.uid = p.uid)
485 WHERE g.eid = {?} AND ' . $where[$suffix],
487 foreach ($uids as $uid) {
488 add_to_list_alias($uid, $new . $suffix, $globals->xnet
->evts_domain
, 'event');
492 $uids = XDB
::fetchColumn('SELECT m.uid
493 FROM group_members AS m
494 LEFT JOIN group_event_participants AS e ON (e.uid = m.uid AND e.eid = {?})
495 WHERE m.asso_id = {?} AND e.uid IS NULL',
496 $eid, $globals->asso('id'));
497 foreach ($uids as $uid) {
498 add_to_list_alias($uid, $new . $globals->xnet
->absent_list
, $globals->xnet
->evts_domain
, 'event');
505 // if we delete the old short name, delete the lists
506 foreach (explode(',', $globals->xnet
->event_lists
) as $suffix) {
507 delete_list_alias($old . $suffix, $globals->xnet
->evts_domain
);
518 // {{{ function make_event_date()
519 function make_event_date($debut, $fin)
521 $start = strtotime($debut);
522 $end = strtotime($fin);
523 // $first_day = $e['first_day'];
524 // $last_day = $e['last_day'];
525 $first_day = strftime("%d %B %Y", $start);
526 $last_day = strftime("%d %B %Y", $end);
528 if ($start && $end != $start) {
529 if ($first_day == $last_day) {
530 $date .= "le " . strftime("%d %B %Y", $start) . " de "
531 . strftime("%H:%M", $start) . " à " . strftime("%H:%M", $end);
533 $date .= "du " . strftime("%d %B %Y à %H:%M", $start)
534 . "\nau " . strftime("%d %B %Y à %H:%M", $end);
537 $date .= "le " . strftime("%d %B %Y à %H:%M", $start);
543 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker fenc=utf-8: