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