2 /***************************************************************************
3 * Copyright (C) 2003-2006 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 class XnetEventsModule
extends PLModule
27 'grp/events' => $this->make_hook('events', AUTH_MDP
),
28 'grp/events/csv' => $this->make_hook('csv', AUTH_MDP
),
29 'grp/events/edit' => $this->make_hook('edit', AUTH_MDP
),
30 'grp/events/admin' => $this->make_hook('admin', AUTH_MDP
),
34 function handler_events(&$page)
38 new_group_page('xnet/groupe/evenements.tpl');
40 /**** manage inscriptions ****/
41 // inscription to events
42 if (Env
::has('ins')) {
43 for ($i=1; Env
::has('evt_'.$i); $i++
) {
44 $eid = Env
::get('evt_'.$i);
45 $res = $globals->xdb
->query("
46 SELECT deadline_inscription,
47 LEFT(NOW(), 10) AS now,
50 FROM groupex.evenements
51 WHERE eid = {?}", $eid);
52 $e = $res->fetchOneAssoc();
53 // impossible to change inscription: either inscription closed or members only
54 if ($e['deadline_inscription'] && $e['deadline_inscription'] < $e['now'])
56 $page->trig("Les inscriptions sont closes");
60 if ($e['membres_only'] && !is_member())
62 $page->trig("Les inscriptions à cet événement ne sont pas publiques");
66 // impossible to unsubscribe if you already paid sthing
68 $inscriptions = array();
69 for ($j=1; Env
::has('moment'.$eid.'_'.$j); $j++
)
71 $inscriptions[$j] = Env
::get('moment'.$eid.'_'.$j);
72 // retreive ohter field when more than one person
73 if ($inscriptions[$j] == 2)
74 $inscriptions[$j] = 1 + Env
::get('personnes'.$eid.'_'.$j,0);
75 // avoid negative count if other field incorrect
76 if ($inscriptions[$j] < 0)
77 $inscriptions[$j] = 0;
78 // avoid floating count if other field incorrect
79 $inscriptions[$j] = floor($inscriptions[$j]);
80 // avoid invite if no invite allowed
81 if ($inscriptions[$j] > 1 && $e['noinvite'])
82 $inscriptions[$j] = 1;
83 $total_inscr +
= $inscriptions[$j];
85 $unsubscribing = ($total_inscr == 0);
87 // retreive the amount already paid for this event in cash
88 $res = $globals->xdb
->query("
90 FROM groupex.evenements_participants
91 WHERE eid = {?} AND uid = {?}
93 $eid, Session
::get("uid"));
94 $paid = $res->fetchOneCell();
95 if (!$paid) $paid = 0;
97 if ($unsubscribing && $paid != 0) {
98 $page->trig("Impossible de te désinscrire complètement ".
99 "parce que tu as fait un paiement par ".
100 "chèque ou par liquide. Contacte un ".
101 "administrateur du groupe si tu es sûr de ".
106 // update actual inscriptions
107 foreach ($inscriptions as $j=>$nb) {
109 $globals->xdb
->execute(
110 "REPLACE INTO groupex.evenements_participants
111 VALUES ({?}, {?}, {?}, {?}, {?})",
112 $eid, Session
::get("uid"), $j, $nb, $paid);
114 $globals->xdb
->execute(
115 "DELETE FROM groupex.evenements_participants
116 WHERE eid = {?} AND uid = {?} AND item_id = {?}",
117 $eid, Session
::get("uid"), $j);
123 /**** retreive all infos about all events ****/
124 $page->assign('logged', logged());
125 $page->assign('admin', may_update());
127 $evenements = $globals->xdb
->iterator(
128 "SELECT e.eid, IF(e.intitule = '', ' ', e.intitule) AS intitule,
129 IF(e.descriptif = '', ' ', e.descriptif) AS descriptif,
130 e.debut, e.fin, LEFT(10,e.debut) AS debut_day,
131 LEFT(10,e.fin) AS fin_day, e.paiement_id, e.membres_only,
132 e.noinvite, e.show_participants, u.nom, u.prenom,
133 u.promo, a.alias, MAX(ep.nb) AS inscrit,
134 MAX(ep.paid) AS paid, e.short_name,
135 IF(e.deadline_inscription, e.deadline_inscription >= LEFT(NOW(), 10),
136 1) AS inscr_open, e.deadline_inscription
137 FROM groupex.evenements AS e
138 INNER JOIN x4dat.auth_user_md5 AS u ON u.user_id = e.organisateur_uid
139 LEFT JOIN x4dat.aliases AS a ON (a.type = 'a_vie' AND a.id = u.user_id)
140 LEFT JOIN groupex.evenements_participants AS ep ON (ep.eid = e.eid AND ep.uid = {?})
143 ORDER BY debut", Session
::get('uid'), $globals->asso('id'));
146 while ($e = $evenements->next()) {
147 $e['moments'] = $globals->xdb
->iterator(
148 "SELECT titre, details, montant, ei.item_id, nb
149 FROM groupex.evenements_items AS ei
150 LEFT JOIN groupex.evenements_participants AS ep
151 ON (ep.eid = ei.eid AND ep.item_id = ei.item_id AND uid = {?})
153 Session
::get('uid'), $e['eid']);
154 $query = $globals->xdb
->query(
156 FROM {$globals->money->mpay_tprefix}transactions AS t
157 WHERE ref = {?} AND uid = {?}", $e['paiement_id'], Session
::get('uid'));
158 $montants = $query->fetchColumn();
159 foreach ($montants as $m) {
160 $p = strtr(substr($m, 0, strpos($m, "EUR")), ",", ".");
161 $e['paid'] +
= trim($p);
166 $page->assign('evenements', $evts);
167 $page->assign('is_member', is_member());
170 function handler_csv(&$page, $eid = null
, $item_id = null
)
172 require_once('xnet/evenements.php');
174 if (!is_numeric($item_id)) {
178 $evt = get_event_detail($eid, $item_id);
183 header('Content-type: text/x-csv');
185 header('Cache-Control: ');
187 new_nonhtml_page('xnet/groupe/evt-csv.tpl');
189 $admin = may_update();
191 $tri = (Env
::get('order') == 'alpha' ?
'promo, nom, prenom' : 'nom, prenom, promo');
193 if (Env
::has('initiale')) {
194 $ini = 'AND IF(u.nom IS NULL, m.nom,
195 IF(u.nom_usage<>"", u.nom_usage, u.nom))
196 LIKE "'.addslashes(Env
::get('initiale')).'%"';
201 $participants = get_event_participants($eid, $item_id, $ini, $tri, "",
202 $evt['money'] && $admin,
203 $evt['paiement_id']);
205 $page->assign('participants', $participants);
206 $page->assign('admin', $admin);
207 $page->assign('moments', $evt['moments']);
208 $page->assign('money', $evt['money']);
209 $page->assign('tout', !Env
::get('item_id', false
));
212 function handler_edit(&$page, $eid = null
)
216 new_groupadmin_page('xnet/groupe/evt-modif.tpl');
218 $page->assign('logged', logged());
219 $page->assign('admin', may_update());
221 $moments = range(1, 4);
222 $page->assign('moments', $moments);
224 if (!is_null($eid)) {
225 $res = $globals->xdb
->query("SELECT short_name, asso_id
226 FROM groupex.evenements
227 WHERE eid = {?}", $eid);
228 $infos = $res->fetchOneAssoc();
229 if ($infos['asso_id'] != $globals->asso('id')) {
230 return PL_NOT_ALLOWED
;
236 if (Post
::get('intitule')) {
238 $short_name = Env
::get('short_name');
240 // Quelques vérifications sur l'alias (caractères spéciaux)
241 if ($short_name && !preg_match( "/^[a-zA-Z0-9\-.]{3,20}$/", $short_name)) {
242 $page->trig("Le raccourci demandé n'est pas valide.
243 Vérifie qu'il comporte entre 3 et 20 caractères
244 et qu'il ne contient que des lettres non accentuées,
245 des chiffres ou les caractères - et .");
246 $short_name = $infos['short_name'];
250 //vérifier que l'alias n'est pas déja pris
251 if ($short_name && $short_name != $infos['short_name']) {
252 $res = $globals->xdb
->query('SELECT COUNT(*) FROM virtual WHERE alias LIKE {?}', $short_name."-%");
253 if ($res->fetchOneCell() > 0) {
254 $page->trig("Le raccourci demandé est déjà utilisé. Choisis en un autre.");
255 $short_name = $infos['short_name'];
260 // if had a previous shortname change the old lists
261 if ($short_name && $infos['short_name'] && $short_name != $infos['short_name']) {
262 $globals->xdb
->execute("UPDATE virtual
263 SET alias = REPLACE(alias, {?}, {?})
264 WHERE type = 'evt' AND alias LIKE {?}",
265 $infos['short_name'], $short_name,
266 $infos['short_name']."-%");
268 elseif ($short_name && !$infos['short_name']) {
269 // if we have a first new short_name create the lists
271 $globals->xdb
->execute("INSERT INTO virtual SET type = 'evt', alias = {?}",
272 $short_name."-participants@".$globals->xnet
->evts_domain
);
274 $res = $globals->xdb
->query("SELECT LAST_INSERT_ID()");
275 $globals->xdb
->execute("INSERT INTO virtual_redirect (
276 SELECT {?} AS vid, IF(u.nom IS NULL, m.email, CONCAT(a.alias, {?})) AS redirect
277 FROM groupex.evenements_participants AS ep
278 LEFT JOIN groupex.membres AS m ON (ep.uid = m.uid)
279 LEFT JOIN auth_user_md5 AS u ON (u.user_id = ep.uid)
280 LEFT JOIN aliases AS a ON (a.id = ep.uid AND a.type = 'a_vie')
283 $res->fetchOneCell(), "@".$globals->mail
->domain
, $eid);
285 $globals->xdb
->execute("INSERT INTO virtual SET type = 'evt', alias = {?}",
286 $short_name."-absents@".$globals->xnet
->evts_domain
);
288 $res = $globals->xdb
->query("SELECT LAST_INSERT_ID()");
289 $globals->xdb
->execute("INSERT INTO virtual_redirect (
290 SELECT {?} AS vid, IF(u.nom IS NULL, m.email, CONCAT(a.alias, {?})) AS redirect
291 FROM groupex.membres AS m
292 LEFT JOIN groupex.evenements_participants AS ep ON (ep.uid = m.uid)
293 LEFT JOIN auth_user_md5 AS u ON (u.user_id = m.uid)
294 LEFT JOIN aliases AS a ON (a.id = m.uid AND a.type = 'a_vie')
295 WHERE m.asso_id = {?} AND ep.uid IS NULL
297 $res->fetchOneCell(), "@".$globals->mail
->domain
, $globals->asso('id'));
299 elseif (!$short_name && $infos['short_name']) {
300 // if we delete the old short name, delete the lists
301 $globals->xdb
->execute("DELETE virtual, virtual_redirect FROM virtual
302 LEFT JOIN virtual_redirect USING(vid)
303 WHERE virtual.alias LIKE {?}",
304 $infos['short_name']."-%");
309 $evt['asso_id'] = $globals->asso('id');
310 $evt['organisateur_uid'] = Session
::get('uid');
311 $evt['intitule'] = Post
::get('intitule');
312 $evt['paiement_id'] = (Post
::get('paiement_id')>0) ? Post
::get('paiement_id') : null
;
313 $evt['descriptif'] = Post
::get('descriptif');
314 $evt['debut'] = Post
::get('deb_Year')."-".Post
::get('deb_Month')
315 . "-".Post
::get('deb_Day')." ".Post
::get('deb_Hour')
316 . ":".Post
::get('deb_Minute').":00";
317 $evt['fin'] = Post
::get('fin_Year')."-".Post
::get('fin_Month')
318 . "-".Post
::get('fin_Day')." ".Post
::get('fin_Hour')
319 . ":".Post
::get('fin_Minute').":00";
320 $evt['membres_only'] = Post
::get('membres_only');
321 $evt['advertise'] = Post
::get('advertise');
322 $evt['show_participants'] = Post
::get('show_participants');
323 $evt['noinvite'] = Post
::get('noinvite');
327 $evt['short_name'] = $short_name;
328 $evt['deadline_inscription'] = Post
::get('deadline', 'off') == 'on' ? null
329 : (Post
::get('inscr_Year')."-".Post
::get('inscr_Month')
330 ."-".Post
::get('inscr_Day'));
332 // Store the modifications in the database
333 $globals->xdb
->execute("REPLACE INTO groupex.evenements
334 SET eid={?}, asso_id={?}, organisateur_uid={?}, intitule={?},
335 paiement_id = {?}, descriptif = {?},
336 debut = {?}, fin = {?},
337 membres_only = {?}, advertise = {?}, show_participants = {?},
338 short_name = {?}, deadline_inscription = {?}, noinvite = {?}",
339 $evt['eid'], $evt['asso_id'], $evt['organisateur_uid'], $evt['intitule']
340 , $evt['paiement_id'], $evt['descriptif'],
341 $evt['debut'], $evt['fin'],
342 $evt['membres_only'], $evt['advertise'], $evt['show_participants'],
343 $evt['short_name'], $evt['deadline_inscription'], $evt['noinvite']);
345 // if new event, get its id
347 $res = $globals->xdb
->query("SELECT LAST_INSERT_ID()");
348 $eid = $res->fetchOneCell();
355 foreach ($moments as $i) {
356 if (Post
::get('titre'.$i)) {
358 if (!($money_defaut > 0))
359 $money_defaut = strtr(Post
::get('montant'.$i), ',', '.');
360 $globals->xdb
->execute("
361 REPLACE INTO groupex.evenements_items
362 VALUES ({?}, {?}, {?}, {?}, {?})",
363 $eid, $i, Post
::get('titre'.$i),
364 Post
::get('details'.$i),
365 strtr(Post
::get('montant'.$i), ',', '.'));
367 $globals->xdb
->execute("DELETE FROM groupex.evenements_items
368 WHERE eid = {?} AND item_id = {?}", $eid, $i);
372 // request for a new payment
373 if (Post
::get('paiement_id') == -1 && $money_defaut >= 0) {
374 require_once 'validations.inc.php';
375 $p = new PayReq(Session
::get('uid'),
376 Post
::get('intitule')." - ".$globals->asso('nom'),
377 Post
::get('site'), $money_defaut,
378 Post
::get('confirmation'), 0, 999,
379 $globals->asso('id'), $eid);
383 // events with no sub-event: add a sub-event with no name
384 if ($nb_moments == 0) {
385 $globals->xdb
->execute("INSERT INTO groupex.evenements_items
386 VALUES ({?}, {?}, '', '', 0)", $eid, 1);
390 if (Env
::has('sup') && $eid) {
392 $globals->xdb
->execute("DELETE FROM groupex.evenements
393 WHERE eid = {?} AND asso_id = {?}",
394 $eid, $globals->asso('id'));
396 // deletes the event items
397 $globals->xdb
->execute("DELETE FROM groupex.evenements_items WHERE eid = {?}", $eid);
399 // deletes the event participants
400 $globals->xdb
->execute("DELETE FROM groupex.evenements_participants
401 WHERE eid = {?}", $eid);
403 // deletes the event mailing aliases
404 if ($infos['short_name']) {
405 $globals->xdb
->execute("DELETE FROM virtual
406 WHERE type = 'evt' AND alias LIKE {?}",
407 $infos['short_name']."-%");
410 // delete the requests for payments
411 require_once 'validations.inc.php';
412 $globals->xdb
->execute("DELETE FROM requests
413 WHERE type = 'paiements' AND data LIKE {?}",
414 PayReq
::same_event($eid, $globals->asso('id')));
415 redirect("evenements.php");
419 redirect("evenements.php");
422 // get a list of all the payment for this asso
423 $res = $globals->xdb
->iterator("SELECT id, text
424 FROM {$globals->money->mpay_tprefix}paiements
425 WHERE asso_id = {?}", $globals->asso('id'));
426 $paiements = array();
427 while ($a = $res->next()) $paiements[$a['id']] = $a['text']; {
428 $page->assign('paiements', $paiements);
431 // when modifying an old event retreive the old datas
433 $res = $globals->xdb
->query(
434 "SELECT eid, intitule, descriptif, debut, fin,
435 membres_only, advertise, show_participants,
436 paiement_id, short_name, deadline_inscription,
438 FROM groupex.evenements
439 WHERE eid = {?}", $eid);
440 $evt = $res->fetchOneAssoc();
441 // find out if there is already a request for a payment for this event
442 require_once 'validations.inc.php';
443 $res = $globals->xdb
->query("SELECT stamp FROM requests
444 WHERE type = 'paiements' AND data LIKE {?}",
445 PayReq
::same_event($eid, $globals->asso('id')));
446 $stamp = $res->fetchOneCell();
448 $evt['paiement_id'] = -2;
449 $evt['paiement_req'] = $stamp;
451 $page->assign('evt', $evt);
452 // get all the different moments infos
453 $res = $globals->xdb
->iterator(
454 "SELECT item_id, titre, details, montant
455 FROM groupex.evenements_items AS ei
456 INNER JOIN groupex.evenements AS e ON(e.eid = ei.eid)
458 ORDER BY item_id", $eid);
460 while ($item = $res->next()) {
461 $items[$item['item_id']] = $item;
463 $page->assign('items', $items);
467 function handler_admin(&$page, $eid = null
, $item_id = null
)
471 define('NB_PER_PAGE', 25);
473 require_once('xnet/evenements.php');
475 $evt = get_event_detail($eid, $item_id);
477 // the event doesn't exist or doesn't belong to this assoif (!$evt)
482 if ($evt['show_participants']) {
483 new_group_page('xnet/groupe/evt-admin.tpl');
485 new_groupadmin_page('xnet/groupe/evt-admin.tpl');
488 $admin = may_update();
490 // select a member from his mail
491 if ($admin && Env
::get('adm') && Env
::get('mail')) {
492 if (strpos(Env
::get('mail'), '@') === false
) {
493 $res = $globals->xdb
->query(
495 FROM groupex.membres AS m
496 INNER JOIN aliases AS a ON (a.id = m.uid)
497 WHERE a.alias = {?} AND m.asso_id = {?}",
498 Env
::get('mail'), $globals->asso('id'));
500 $res = $globals->xdb
->query(
502 FROM groupex.membres AS m
503 WHERE m.email = {?} AND m.asso_id = {?}",
504 Env
::get('mail'), $globals->asso('id'));
506 $member = $res->fetchOneCell();
507 if (!$member) $page->trig("Membre introuvable");
510 // change the price paid by a participant
511 if ($admin && Env
::get('adm') == 'prix' && $member) {
512 $globals->xdb
->execute("UPDATE groupex.evenements_participants SET paid = IF(paid + {?} > 0, paid + {?}, 0) WHERE uid = {?} AND eid = {?}",
513 strtr(Env
::get('montant'), ',', '.'),
514 strtr(Env
::get('montant'), ',', '.'),
515 $member, Env
::get('eid'));
518 // change the number of personns coming with a participant
519 if ($admin && Env
::get('adm') == 'nbs' && $member) {
520 $res = $globals->xdb
->query("SELECT paid FROM groupex.evenements_participants WHERE uid = {?} AND eid = {?}", $member, Env
::get('eid'));
521 $paid = $res->fetchOneCell();
522 $participate = false
;
523 foreach ($evt['moments'] as $m) if (Env
::has('nb'.$m['item_id'])) {
524 $nb = Env
::getInt('nb'.$m['item_id'], 0);
525 if ($nb < 0) $nb = 0;
528 if (!$paid) $paid = 0;
529 $globals->xdb
->execute("REPLACE INTO groupex.evenements_participants VALUES ({?}, {?}, {?}, {?}, {?})",
530 Env
::get('eid'), $member, $m['item_id'], $nb, $paid);
532 $globals->xdb
->execute("DELETE FROM groupex.evenements_participants WHERE uid = {?} AND eid = {?} AND item_id = {?}", $member, Env
::get('eid'), $m['item_id']);
536 subscribe_lists_event(true
, $member, $evt['participant_list'], $evt['absent_list']);
538 $res = $globals->xdb
->query(
539 "SELECT uid FROM groupex.evenements_participants
540 WHERE uid = {?} AND eid = {?}", $member, $eid);
541 $u = $res->fetchOneCell();
542 subscribe_lists_event($u, $member, $evt['participant_list'], $evt['absent_list']);
544 $evt = get_event_detail($eid, $item_id);
547 $page->assign('admin', $admin);
548 $page->assign('evt', $evt);
549 $page->assign('url_page', Env
::get('PHP_SELF')."?eid=".Env
::get('eid').(Env
::has('item_id')?
("&item_id=".Env
::getInt('item_id')):''));
550 $page->assign('tout', !Env
::has('item_id'));
552 if (count($evt['moments'])) $page->assign('moments', $evt['moments']);
553 $page->assign('money', $evt['money']);
555 $tri = (Env
::get('order') == 'alpha' ?
'promo, nom, prenom' : 'nom, prenom, promo');
556 $whereitemid = Env
::has('item_id')?
('AND ep.item_id = '.Env
::getInt('item_id', 1)):'';
557 $res = $globals->xdb
->iterRow(
558 'SELECT UPPER(SUBSTRING(IF(u.nom IS NULL,m.nom,IF(u.nom_usage<>"", u.nom_usage, u.nom)), 1, 1)), COUNT(DISTINCT ep.uid)
559 FROM groupex.evenements_participants AS ep
560 INNER JOIN groupex.evenements AS e ON (ep.eid = e.eid)
561 LEFT JOIN groupex.membres AS m ON ( ep.uid = m.uid AND e.asso_id = m.asso_id)
562 LEFT JOIN auth_user_md5 AS u ON ( u.user_id = ep.uid )
563 WHERE ep.eid = {?} '.$whereitemid.'
564 GROUP BY UPPER(SUBSTRING(IF(u.nom IS NULL,m.nom,u.nom), 1, 1))', Env
::get('eid'));
568 while (list($char, $nb) = $res->next()) {
569 $alphabet[ord($char)] = $char;
571 if (Env
::has('initiale') && $char == strtoupper(Env
::get('initiale'))) {
576 $page->assign('alphabet', $alphabet);
578 $ofs = Env
::getInt('offset');
579 $tot = Env
::get('initiale') ?
$tot : $nb_tot;
580 $nbp = intval(($tot-1)/NB_PER_PAGE
);
583 $links['précédent'] = $ofs-1;
585 for ($i = 0; $i <= $nbp; $i++
) {
586 $links[(string)($i+
1)] = $i;
589 $links['suivant'] = $ofs+
1;
591 if (count($links)>1) {
592 $page->assign('links', $links);
595 $ini = Env
::has('initiale') ?
'AND IF(u.nom IS NULL,m.nom,IF(u.nom_usage<>"", u.nom_usage, u.nom)) LIKE "'.addslashes(Env
::get('initiale')).'%"' : '';
597 $participants = get_event_participants(Env
::get('eid'), Env
::get('item_id'), $ini, $tri, "LIMIT ".($ofs*NB_PER_PAGE
).", ".NB_PER_PAGE
, $evt['money'] && $admin, $evt['paiement_id']);
599 if ($evt['paiement_id']) {
600 $res = $globals->xdb
->iterator(
601 "SELECT IF(u.nom_usage<>'', u.nom_usage, u.nom) AS nom, u.prenom,
602 u.promo, a.alias AS email, t.montant
603 FROM {$globals->money->mpay_tprefix}transactions AS t
604 INNER JOIN auth_user_md5 AS u ON(t.uid = u.user_id)
605 INNER JOIN aliases AS a ON (a.id = t.uid AND a.type='a_vie' )
606 LEFT JOIN groupex.evenements_participants AS ep ON(ep.uid = t.uid AND ep.eid = {?})
607 WHERE t.ref = {?} AND ep.uid IS NULL",
608 $evt['eid'], $evt['paiement_id']);
609 $page->assign('oublis', $res->total());
610 $page->assign('oubliinscription', $res);
613 $page->assign('participants', $participants);