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