2 /***************************************************************************
3 * Copyright (C) 2003-2011 Polytechnique.org *
4 * http://opensource.polytechnique.org/ *
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. *
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. *
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 *
19 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *
20 ***************************************************************************/
22 function serv_to_str($params) {
23 $flags = explode(',',$params);
24 $trad = Array('web' => 'site web', 'mail'=> 'redirection mail',
25 'smtp' => 'serveur sécurisé d\'envoi de mails',
26 'nntp' => 'serveur des forums de discussion');
28 foreach ($flags as $flag) {
29 $ret[] = $trad[$flag];
31 return implode(', ',$ret);
34 class StatsModule
extends PLModule
39 'stats' => $this->make_hook('stats', AUTH_COOKIE
, 'user'),
40 'stats/evolution' => $this->make_hook('evolution', AUTH_COOKIE
, 'user'),
41 'stats/graph' => $this->make_hook('graph', AUTH_COOKIE
, 'user'),
42 'stats/graph/evolution' => $this->make_hook('graph_evo', AUTH_COOKIE
, 'user'),
43 'stats/promos' => $this->make_hook('promos', AUTH_COOKIE
, 'user'),
45 'stats/coupures' => $this->make_hook('coupures', AUTH_PUBLIC
),
49 function handler_stats($page)
51 $page->changeTpl('stats/index.tpl');
54 function handler_evolution($page, $days = 365)
56 $page->changeTpl('stats/evolution_inscrits.tpl');
57 $page->assign('days', $days);
60 function handler_graph_evo($page, $days = 365)
62 $day_length = 24 * 3600;
64 // Retrieve the registration count per days during the given date range.
65 $res = XDB
::iterRow('SELECT IF(registration_date > DATE_SUB(NOW(), INTERVAL {?} DAY),
66 TO_DAYS(registration_date) - TO_DAYS(NOW()),
70 INNER JOIN account_profiles AS ap ON (ap.uid = a.uid AND FIND_IN_SET(\'owner\', ap.perms))
71 INNER JOIN profiles AS p ON (ap.pid = p.pid)
72 WHERE a.state = \'active\' AND p.deathdate IS NULL
74 (int)$days, 1 +
(int)$days);
76 // The first contains the registration count before the starting date (J - $days)
77 list(, $init_nb) = $res->next();
79 $num_day = - $days - 1;
82 for ($i = -$days; $i <= 0; ++
$i) {
84 if(!list($num_day, $nb) = $res->next()) {
92 $registered .= date('d/m/y', $i * $day_length +
time()) . ' ' . $total . "\n";
95 //Genere le graphique à la volée avec GNUPLOT
96 pl_cached_dynamic_content_headers("image/png");
98 $delt = ($total - $init_nb) / 10;
99 $delt = $delt ?
$delt : 5;
100 $ymin = round($init_nb - $delt, 0);
101 $ymax = round($total +
$delt, 0);
106 set term png small color
109 set timefmt
"%d/%m/%y"
111 set format x
"%d/%m\\n%Y"
114 set title
"Nombre d'inscrits"
116 plot
"-" using
1:2 title
'inscrits' with lines
;
125 function handler_graph($page, $promo = null
)
127 if (in_array($promo, array(Profile
::DEGREE_X
, Profile
::DEGREE_M
, Profile
::DEGREE_D
))) {
128 $cycle = Profile
::$cycles[$promo] . 's';
129 $res = XDB
::iterRow("SELECT pe.promo_year, SUM(a.state = 'active') / COUNT(*) * 100
131 INNER JOIN account_profiles AS ap ON (ap.uid = a.uid AND FIND_IN_SET('owner', ap.perms))
132 INNER JOIN profiles AS p ON (p.pid = ap.pid)
133 INNER JOIN profile_education AS pe ON (pe.pid = ap.pid AND FIND_IN_SET('primary', pe.flags))
134 INNER JOIN profile_education_degree_enum AS ped ON (pe.degreeid = ped.id)
135 WHERE p.deathdate IS NULL AND ped.degree = {?}
136 GROUP BY pe.promo_year",
139 list($promo, $count) = $res->next();
141 $registered = $promo . ' ' . $count . "\n";
142 while ($next = $res->next()) {
143 list($promo, $count) = $next;
144 $registered .= $promo . ' ' . $count . "\n";
148 // Generate drawing thanks to Gnuplot.
152 set term png small color
154 set timefmt
"%d/%m/%y"
156 set xr
[$first:$last]
159 set title
"Proportion de $cycle inscrits par promotion, en %."
162 plot
"-" using
1:2 title
'inscrits' with boxes
;
168 $day_length = 24 * 3600;
171 $res = XDB
::query("SELECT MIN(TO_DAYS(a.registration_date) - TO_DAYS(NOW()))
173 INNER JOIN account_profiles AS ap ON (ap.uid = a.uid AND FIND_IN_SET('owner', ap.perms))
174 INNER JOIN profile_display AS pd ON (ap.pid = pd.pid)
175 WHERE pd.promo = {?} AND a.state = 'active'",
177 $days = -$res->fetchOneCell();
179 // Retrieve the registration count per days during the given date range.
180 $res = XDB
::iterRow("SELECT IF(a.registration_date > DATE_SUB(NOW(), INTERVAL {?} DAY),
181 TO_DAYS(a.registration_date) - TO_DAYS(NOW()),
185 INNER JOIN account_profiles AS ap ON (ap.uid = a.uid AND FIND_IN_SET('owner', ap.perms))
186 INNER JOIN profile_display AS pd ON (ap.pid = pd.pid)
187 WHERE pd.promo = {?} AND a.state = 'active'
189 (int)$days, 1 +
(int)$days, $promo);
191 // The first line contains the registration count before starting date (D - $days).
192 list(, $init_nb) = $res->next();
196 list($num_day, $nb) = $res->next();
198 for ($i = -$days; $i <= 0; ++
$i) {
200 if(!list($num_day, $nb) = $res->next()) {
205 if ($num_day == $i) {
208 $registered .= date('d/m/y', $i * $day_length +
time()) . ' ' . $total . "\n";
211 // Generate drawing thanks to Gnuplot.
212 $delt = ($total - $init_nb) / 10;
213 $delt +
= ($delt < 1);
214 $ymin = round($init_nb - $delt, 0);
215 $ymax = round($total +
$delt, 0);
220 set term png small color
223 set timefmt
"%d/%m/%y"
228 set title
"Nombre d'inscrits de la promotion $promo."
230 plot
"-" using
1:2 title
'inscrits' with lines
;
236 pl_cached_dynamic_content_headers("image/png");
241 function handler_promos($page, $required_promo = null
)
243 $page->changeTpl('stats/nb_by_promo.tpl');
244 $cycles = array('X' => 'Polytechniciens', 'M' => 'Masters', 'D' => 'Docteurs');
246 $res = XDB
::iterRow('SELECT pd.promo, COUNT(*)
248 INNER JOIN account_profiles AS ap ON (ap.uid = a.uid AND FIND_IN_SET(\'owner\', ap.perms))
249 INNER JOIN profiles AS p ON (p.pid = ap.pid)
250 INNER JOIN profile_display AS pd ON (pd.pid = ap.pid)
251 WHERE a.state = \'active\' AND p.deathdate IS NULL AND pd.promo != \'D (en cours)\'
253 ORDER BY pd.promo LIKE \'D%\', pd.promo LIKE \'M%\', pd.promo LIKE \'X%\', pd.promo');
256 while (list($promo, $count) = $res->next()) {
257 $prefix = substr($promo, 0, 4) . '-';
258 $unit = substr($promo, -1);
259 if(!isset($nbpromo[$cycles[$promo{0}]][$prefix])) {
260 $nbpromo[$cycles[$promo{0}]][$prefix] = array('', '', '', '', '', '', '', '', '', ''); // Empty array containing 10 cells.
262 $nbpromo[$cycles[$promo{0}]][$prefix][$unit] = array('promo' => $promo, 'nb' => $count);
265 $count = XDB
::fetchOneCell('SELECT COUNT(*)
267 INNER JOIN account_profiles AS ap ON (ap.uid = a.uid AND FIND_IN_SET(\'owner\', ap.perms))
268 INNER JOIN profiles AS p ON (p.pid = ap.pid)
269 INNER JOIN profile_display AS pd ON (pd.pid = ap.pid)
270 WHERE a.state = \'active\' AND p.deathdate IS NULL AND pd.promo = \'D (en cours)\'');
271 $nbpromo[$cycles['D']]['D (en cours)'][0] = array('promo' => 'D (en cours)', 'nb' => $count);
273 $page->assign_by_ref('nbs', $nbpromo);
274 $page->assign('promo', $required_promo);
277 function handler_coupures($page, $cp_id = null
)
279 $page->changeTpl('stats/coupure.tpl');
281 if (!is_null($cp_id)) {
282 $res = XDB
::query("SELECT debut,
283 TIME_FORMAT(duree,'%kh%i') AS duree,
284 resume, description, services
286 WHERE id = {?}", $cp_id);
287 $cp = $res->fetchOneAssoc();
291 $cp['lg_services'] = serv_to_str($cp['services']);
292 $page->assign_by_ref('cp',$cp);
294 $beginning_date = date("Ymd", time() - 3600*24*21) . "000000";
295 $sql = "SELECT id, debut, resume, services
296 FROM downtimes where debut > '$beginning_date' order by debut desc";
297 $page->assign('coupures', XDB
::iterator($sql));
298 $res = XDB
::iterator("SELECT host, text
300 WHERE state != 'ok'");
301 $page->assign('mxs', $res);
306 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: