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