594f0aa09a7d75e2e6ce795d07eda3b6b550c273
[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_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 if (!$item_id) {
31 $where = '';
32 $group_by = 'e.eid';
33 } else {
34 $where = XDB::format(' AND ei.item_id = {?}', $item_id);
35 $group_by = 'ei.item_id';
36 }
37 $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,
38 IF(e.deadline_inscription,
39 e.deadline_inscription >= LEFT(NOW(), 10),
40 1) AS inscr_open,
41 LEFT(e.debut, 10) AS first_day, LEFT(e.fin, 10) AS last_day,
42 LEFT(NOW(), 10) AS now,
43 ei.titre, e.subscription_notification
44 FROM group_events AS e
45 INNER JOIN group_event_items AS ei ON (e.eid = ei.eid)
46 LEFT JOIN group_event_participants AS ep ON(e.eid = ep.eid AND ei.item_id = ep.item_id)
47 WHERE (e.eid = {?} OR e.short_name = {?}) AND e.asso_id = {?}' . $where . '
48 GROUP BY ' . $group_by,
49 $eid, $eid, $asso_id);
50
51 if (!$evt) {
52 return null;
53 }
54 if ($GLOBALS['IS_XNET_SITE'] && $evt['accept_nonmembre'] == 0 && !is_member() && !may_update()) {
55 return false;
56 }
57
58 if (!$item_id) {
59 /* Don't try to be to smart here, in case we're getting the global summary, we cannot have
60 * a general formula to estimate the total number of comers since 'moments' may (or may not be)
61 * disjuncted. As a consequence, we can only provides the number of user having fullfiled the
62 * registration procedure.
63 */
64 $evt['user_count'] = $evt['nb_tot'] = $evt['nb'];
65 $evt['titre'] = '';
66 $evt['item_id'] = 0;
67 $evt['csv_name'] = urlencode($evt['intitule']);
68 } else {
69 $evt['csv_name'] = urlencode($evt['intitule'] . '.' . $evt['titre']);
70 }
71
72 $evt['moments'] = XDB::fetchAllAssoc('SELECT titre, details, montant, ei.item_id, nb,
73 ep.paid, FIND_IN_SET(\'notify_payment\', ep.flags) AS notify_payment
74 FROM group_event_items AS ei
75 LEFT JOIN group_event_participants AS ep ON (ep.eid = ei.eid AND ep.item_id = ei.item_id
76 AND uid = {?})
77 WHERE ei.eid = {?}',
78 S::i('uid'), $evt['eid']);
79 $evt['topay'] = 0;
80 $evt['paid'] = 0;
81 $evt['notify_payment'] = false;
82 foreach ($evt['moments'] as $m) {
83 $evt['topay'] += $m['nb'] * $m['montant'];
84 if ($m['montant']) {
85 $evt['money'] = true;
86 }
87 $evt['paid'] += $m['paid'];
88 $evt['notify_payment'] = $evt['notify_payment'] || $m['notify_payment'];
89 }
90
91 $montant = XDB::fetchOneCell('SELECT SUM(amount) AS sum_amount
92 FROM payment_transactions AS t
93 WHERE ref = {?} AND uid = {?}',
94 $evt['paiement_id'], S::v('uid'));
95 $evt['telepaid'] = $montant;
96 $evt['paid'] += $montant;
97 $evt['organizer'] = User::getSilent($evt['uid']);
98
99 make_event_date($evt);
100
101 $evt['show_participants'] = ($evt['show_participants'] && $GLOBALS['IS_XNET_SITE'] && (is_member() || may_update()));
102
103 return $evt;
104 }
105
106 // }}}
107
108 // {{{ function get_event_participants()
109 function get_event_participants(&$evt, $item_id, array $tri = array(), $limit = null, $offset = 0)
110 {
111 global $globals;
112
113 $eid = $evt['eid'];
114 $money = $evt['money'] && (function_exists('may_update')) && may_update();
115 $pay_id = $evt['paiement_id'];
116
117 $append = $item_id ? XDB::format(' AND ep.item_id = {?}', $item_id) : '';
118 $query = XDB::fetchAllAssoc('uid', 'SELECT ep.uid, SUM(ep.paid) AS paid, SUM(ep.nb) AS nb,
119 FIND_IN_SET(\'notify_payment\', ep.flags) AS notify_payment
120 FROM group_event_participants AS ep
121 WHERE ep.eid = {?} AND nb > 0 ' . $append . '
122 GROUP BY ep.uid', $eid);
123 $uf = new UserFilter(new PFC_True(), $tri);
124 $users = User::getBulkUsersWithUIDs($uf->filter(array_keys($query), new PlLimit($limit, $offset)));
125 $tab = array();
126 foreach ($users as $user) {
127 $uid = $user->id();
128 $tab[$uid] = $query[$uid];
129 $tab[$uid]['user'] = $user;
130 }
131
132 if ($item_id) {
133 return $tab;
134 }
135
136 $evt['adminpaid'] = 0;
137 $evt['telepaid'] = 0;
138 $evt['topay'] = 0;
139 $evt['paid'] = 0;
140 foreach ($tab as $uid=>&$u) {
141 $u['adminpaid'] = (float)$u['paid'];
142 $u['montant'] = 0;
143 if ($money && $pay_id) {
144 $montant = XDB::fetchOneCell('SELECT SUM(amount)
145 FROM payment_transactions AS t
146 WHERE ref = {?} AND uid = {?}',
147 $pay_id, $uid);
148 $u['paid'] += $montant;
149 }
150 $u['telepayment'] = $u['paid'] - $u['adminpaid'];
151 $res_ = XDB::iterator('SELECT ep.nb, ep.item_id, ei.montant
152 FROM group_event_participants AS ep
153 INNER JOIN group_event_items AS ei ON (ei.eid = ep.eid AND ei.item_id = ep.item_id)
154 WHERE ep.eid = {?} AND ep.uid = {?}',
155 $eid, $uid);
156 while ($i = $res_->next()) {
157 $u[$i['item_id']] = $i['nb'];
158 $u['montant'] += $i['montant'] * $i['nb'];
159 }
160 $evt['telepaid'] += $u['telepayment'];
161 $evt['adminpaid'] += $u['adminpaid'];
162 $evt['paid'] += $u['paid'];
163 $evt['topay'] += $u['montant'];
164 }
165 return $tab;
166 }
167 // }}}
168
169 // {{{ function subscribe_lists_event()
170 /** Subscribes user to various event related mailing lists.
171 *
172 * @param $uid: user's id.
173 * @param short_name: event's short_name, which corresponds to the beginning of the emails.
174 * @param participate: indicates if the user takes part at the event or not;
175 * -1 means he did not answer, 0 means no, and 1 means yes.
176 * @param paid: has the user already payed anything?
177 * 0 means no, a positive amount means yes.
178 * @param payment: is this function called from a payment page?
179 * If true, only payment related lists should be updated.
180 */
181 function subscribe_lists_event($uid, $short_name, $participate, $paid, $payment = false)
182 {
183 global $globals;
184 require_once 'emails.inc.php';
185
186 if (is_null($short_name)) {
187 return;
188 }
189
190 /** If $payment is not null, we do not retrieve the value of $participate,
191 * thus we do not alter participant and absent lists.
192 */
193 if ($payment === true) {
194 if ($paid > 0) {
195 delete_from_list_alias($uid, $short_name . $globals->xnet->unpayed_list, $globals->xnet->evts_domain, 'event');
196 add_to_list_alias($uid, $short_name . $globals->xnet->payed_list, $globals->xnet->evts_domain, 'event');
197 }
198 } else {
199 switch ($participate) {
200 case -1:
201 delete_from_list_alias($uid, $short_name . $globals->xnet->participant_list, $globals->xnet->evts_domain, 'event');
202 delete_from_list_alias($uid, $short_name . $globals->xnet->unpayed_list, $globals->xnet->evts_domain, 'event');
203 delete_from_list_alias($uid, $short_name . $globals->xnet->payed_list, $globals->xnet->evts_domain, 'event');
204 add_to_list_alias($uid, $short_name . $globals->xnet->absent_list, $globals->xnet->evts_domain, 'event');
205 break;
206 case 0:
207 delete_from_list_alias($uid, $short_name . $globals->xnet->participant_list, $globals->xnet->evts_domain, 'event');
208 delete_from_list_alias($uid, $short_name . $globals->xnet->absent_list, $globals->xnet->evts_domain, 'event');
209 delete_from_list_alias($uid, $short_name . $globals->xnet->unpayed_list, $globals->xnet->evts_domain, 'event');
210 delete_from_list_alias($uid, $short_name . $globals->xnet->payed_list, $globals->xnet->evts_domain, 'event');
211 break;
212 case 1:
213 add_to_list_alias($uid, $short_name . $globals->xnet->participant_list, $globals->xnet->evts_domain, 'event');
214 delete_from_list_alias($uid, $short_name . $globals->xnet->absent_list, $globals->xnet->evts_domain, 'event');
215 if ($paid > 0) {
216 delete_from_list_alias($uid, $short_name . $globals->xnet->unpayed_list, $globals->xnet->evts_domain, 'event');
217 add_to_list_alias($uid, $short_name . $globals->xnet->payed_list, $globals->xnet->evts_domain, 'event');
218 } else {
219 add_to_list_alias($uid, $short_name . $globals->xnet->unpayed_list, $globals->xnet->evts_domain, 'event');
220 delete_from_list_alias($uid, $short_name . $globals->xnet->payed_list, $globals->xnet->evts_domain, 'event');
221 }
222 break;
223 }
224 }
225 }
226 // }}}
227
228 // {{{ function event_change_shortname()
229 function event_change_shortname($page, $eid, $old, $new)
230 {
231 global $globals;
232 require_once 'emails.inc.php';
233
234 if (is_null($old)) {
235 $old = '';
236 }
237 // Quelques vérifications sur l'alias (caractères spéciaux)
238 if ($new && !preg_match( "/^[a-zA-Z0-9\-.]{3,20}$/", $new)) {
239 $page->trigError("Le raccourci demandé n'est pas valide.
240 Vérifie qu'il comporte entre 3 et 20 caractères
241 et qu'il ne contient que des lettres non accentuées,
242 des chiffres ou les caractères - et .");
243 return $old;
244 } elseif ($new && (is_int($new) || ctype_digit($new))) {
245 $page->trigError("Le raccourci demandé ne peut être accepté car il
246 ne contient que des chiffres. Rajoute-lui par exemple
247 une lettre.");
248 return $old;
249 }
250
251 //vérifier que l'alias n'est pas déja pris
252 if ($new && $old != $new) {
253 $res = XDB::query('SELECT COUNT(*)
254 FROM group_events
255 WHERE short_name = {?}',
256 $new);
257 if ($res->fetchOneCell() > 0) {
258 $page->trigError("Le raccourci demandé est déjà utilisé. Choisis en un autre.");
259 return $old;
260 }
261 }
262
263 if ($old == $new) {
264 return $new;
265 }
266
267 if ($old && $new) {
268 // if had a previous shortname change the old lists
269 foreach (explode(',', $globals->xnet->event_lists) as $suffix) {
270 XDB::execute('UPDATE email_virtual
271 SET email = {?}
272 WHERE type = \'event\' AND email = {?}',
273 $new . $suffix, $old . $suffix);
274 }
275
276 return $new;
277 }
278
279 if (!$old && $new) {
280 // if we have a first new short_name create the lists
281 $lastid = array();
282 $where = array(
283 $globals->xnet->participant_list => 'g.nb > 0',
284 $globals->xnet->payed_list => '(g.paid > 0 OR p.amount > 0)',
285 $globals->xnet->unpayed_list => 'g.nb > 0 AND g.paid = 0 AND p.amount IS NULL'
286 );
287
288 foreach (array($globals->xnet->participant_list, $globals->xnet->payed_list, $globals->xnet->unpayed_list) as $suffix) {
289 $uids = XDB::fetchColumn('SELECT g.uid
290 FROM group_event_participants AS g
291 INNER JOIN group_events AS e ON (g.eid = e.eid)
292 LEFT JOIN payment_transactions AS p ON (e.paiement_id = p.ref AND g.uid = p.uid)
293 WHERE g.eid = {?} AND ' . $where[$suffix],
294 $eid);
295 foreach ($uids as $uid) {
296 add_to_list_alias($uid, $new . $suffix, $globals->xnet->evts_domain, 'event');
297 }
298 }
299
300 $uids = XDB::fetchColumn('SELECT m.uid
301 FROM group_members AS m
302 LEFT JOIN group_event_participants AS e ON (e.uid = m.uid AND e.eid = {?})
303 WHERE m.asso_id = {?} AND e.uid IS NULL',
304 $eid, $globals->asso('id'));
305 foreach ($uids as $uid) {
306 add_to_list_alias($uid, $new . $globals->xnet->absent_list, $globals->xnet->evts_domain, 'event');
307 }
308
309 return $new;
310 }
311
312 if ($old && !$new) {
313 // if we delete the old short name, delete the lists
314 foreach (explode(',', $globals->xnet->event_lists) as $suffix) {
315 delete_list_alias($old . $suffix, $globals->xnet->evts_domain);
316 }
317
318 return $new;
319 }
320
321 // cannot happen
322 return $old;
323 }
324 // }}}
325
326 // {{{ function make_event_date()
327 function make_event_date(&$e)
328 {
329 $start = strtotime($e['debut']);
330 $end = strtotime($e['fin']);
331 $first_day = $e['first_day'];
332 $last_day = $e['last_day'];
333
334 $date = "";
335 if ($start && $end != $start) {
336 if ($first_day == $last_day) {
337 $date .= "le " . strftime("%d %B %Y", $start) . " de "
338 . strftime("%H:%M", $start) . " à " . strftime("%H:%M", $end);
339 } else {
340 $date .= "du " . strftime("%d %B %Y à %H:%M", $start)
341 . "\nau " . strftime("%d %B %Y à %H:%M", $end);
342 }
343 } else {
344 $date .= "le " . strftime("%d %B %Y à %H:%M", $start);
345 }
346 $e['date'] = $date;
347 }
348 // }}}
349
350 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker fenc=utf-8:
351 ?>