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