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