Revert "Rewrites xnetevents index page."
[platal.git] / modules / xnetevents / xnetevents.inc.php
index fe20a8e..594f0aa 100644 (file)
@@ -1,6 +1,6 @@
 <?php
 /***************************************************************************
- *  Copyright (C) 2003-2011 Polytechnique.org                              *
+ *  Copyright (C) 2003-2014 Polytechnique.org                              *
  *  http://opensource.polytechnique.org/                                   *
  *                                                                         *
  *  This program is free software; you can redistribute it and/or modify   *
@@ -27,19 +27,26 @@ function get_event_detail($eid, $item_id = false, $asso_id = null)
     if (is_null($asso_id)) {
         $asso_id = $globals->asso('id');
     }
+    if (!$item_id) {
+        $where = '';
+        $group_by = 'e.eid';
+    } else {
+        $where = XDB::format(' AND ei.item_id = {?}', $item_id);
+        $group_by = 'ei.item_id';
+    }
     $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,
                                        IF(e.deadline_inscription,
                                           e.deadline_inscription >= LEFT(NOW(), 10),
                                           1) AS inscr_open,
-                                       LEFT(10, e.debut) AS start_day, LEFT(10, e.fin) AS last_day,
+                                       LEFT(e.debut, 10) AS first_day, LEFT(e.fin, 10) AS last_day,
                                        LEFT(NOW(), 10) AS now,
                                        ei.titre, e.subscription_notification
                                  FROM  group_events             AS e
                            INNER JOIN  group_event_items        AS ei ON (e.eid = ei.eid)
                             LEFT JOIN  group_event_participants AS ep ON(e.eid = ep.eid AND ei.item_id = ep.item_id)
-                                WHERE  (e.eid = {?} OR e.short_name = {?}) AND ei.item_id = {?} AND e.asso_id = {?}
-                             GROUP BY  ei.item_id',
-                           $eid, $eid, $item_id ? $item_id : 1, $asso_id);
+                                WHERE  (e.eid = {?} OR e.short_name = {?}) AND e.asso_id = {?}' . $where . '
+                             GROUP BY  ' . $group_by,
+                              $eid, $eid, $asso_id);
 
     if (!$evt) {
         return null;
@@ -48,15 +55,13 @@ function get_event_detail($eid, $item_id = false, $asso_id = null)
         return false;
     }
 
-    // smart calculation of the total number
     if (!$item_id) {
-        $res = XDB::query('SELECT  MAX(nb)
-                             FROM  group_events              AS e
-                       INNER JOIN  group_event_items        AS ei ON (e.eid = ei.eid)
-                        LEFT JOIN  group_event_participants AS ep ON (e.eid = ep.eid AND ei.item_id = ep.item_id)
-                            WHERE  e.eid = {?}
-                         GROUP BY  ep.uid', $evt['eid']);
-        $evt['nb_tot'] = array_sum($res->fetchColumn());
+        /* Don't try to be to smart here, in case we're getting the global summary, we cannot have
+         * a general formula to estimate the total number of comers since 'moments' may (or may not be)
+         * disjuncted. As a consequence, we can only provides the number of user having fullfiled the
+         * registration procedure.
+         */
+        $evt['user_count'] = $evt['nb_tot'] = $evt['nb'];
         $evt['titre'] = '';
         $evt['item_id'] = 0;
         $evt['csv_name'] = urlencode($evt['intitule']);
@@ -133,7 +138,7 @@ function get_event_participants(&$evt, $item_id, array $tri = array(), $limit =
     $evt['topay']     = 0;
     $evt['paid']      = 0;
     foreach ($tab as $uid=>&$u) {
-        $u['adminpaid'] = $u['paid'];
+        $u['adminpaid'] = (float)$u['paid'];
         $u['montant'] = 0;
         if ($money && $pay_id) {
             $montant = XDB::fetchOneCell('SELECT  SUM(amount)
@@ -150,7 +155,7 @@ function get_event_participants(&$evt, $item_id, array $tri = array(), $limit =
                             $eid, $uid);
         while ($i = $res_->next()) {
             $u[$i['item_id']] = $i['nb'];
-            $u['montant'] += $i['montant']*$i['nb'];
+            $u['montant'] += $i['montant'] * $i['nb'];
         }
         $evt['telepaid']  += $u['telepayment'];
         $evt['adminpaid'] += $u['adminpaid'];
@@ -323,8 +328,8 @@ function make_event_date(&$e)
 {
     $start     = strtotime($e['debut']);
     $end       = strtotime($e['fin']);
-    $first_day = @strtotime($e['first_day']);
-    $last_day  = strtotime($e['last_day']);
+    $first_day = $e['first_day'];
+    $last_day  = $e['last_day'];
 
     $date = "";
     if ($start && $end != $start) {
@@ -342,5 +347,5 @@ function make_event_date(&$e)
 }
 // }}}
 
-// vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
+// vim:set et sw=4 sts=4 sws=4 foldmethod=marker fenc=utf-8:
 ?>