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
),
40 'stats/evolution' => $this->make_hook('evolution', AUTH_COOKIE
),
41 'stats/graph' => $this->make_hook('graph', AUTH_COOKIE
),
42 'stats/graph/evolution' => $this->make_hook('graph_evo', AUTH_COOKIE
),
43 'stats/promos' => $this->make_hook('promos', AUTH_COOKIE
),
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"
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 %."
161 plot
"-" using
1:2 title
'inscrits' with boxes
;
167 $day_length = 24 * 3600;
170 $res = XDB
::query("SELECT MIN(TO_DAYS(a.registration_date) - TO_DAYS(NOW()))
172 INNER JOIN account_profiles AS ap ON (ap.uid = a.uid AND FIND_IN_SET('owner', ap.perms))
173 INNER JOIN profile_display AS pd ON (ap.pid = pd.pid)
174 WHERE pd.promo = {?} AND a.state = 'active'",
176 $days = -$res->fetchOneCell();
178 // Retrieve the registration count per days during the given date range.
179 $res = XDB
::iterRow("SELECT IF(a.registration_date > DATE_SUB(NOW(), INTERVAL {?} DAY),
180 TO_DAYS(a.registration_date) - TO_DAYS(NOW()),
184 INNER JOIN account_profiles AS ap ON (ap.uid = a.uid AND FIND_IN_SET('owner', ap.perms))
185 INNER JOIN profile_display AS pd ON (ap.pid = pd.pid)
186 WHERE pd.promo = {?} AND a.state = 'active'
188 (int)$days, 1 +
(int)$days, $promo);
190 // The first line contains the registration count before starting date (D - $days).
191 list(, $init_nb) = $res->next();
195 list($num_day, $nb) = $res->next();
197 for ($i = -$days; $i <= 0; ++
$i) {
199 if(!list($num_day, $nb) = $res->next()) {
204 if ($num_day == $i) {
207 $registered .= date('d/m/y', $i * $day_length +
time()) . ' ' . $total . "\n";
210 // Generate drawing thanks to Gnuplot.
211 $delt = ($total - $init_nb) / 10;
212 $delt +
= ($delt < 1);
213 $ymin = round($init_nb - $delt, 0);
214 $ymax = round($total +
$delt, 0);
219 set term png small color
222 set timefmt
"%d/%m/%y"
227 set title
"Nombre d'inscrits de la promotion $promo."
229 plot
"-" using
1:2 title
'inscrits' with lines
;
235 pl_cached_dynamic_content_headers("image/png");
240 function handler_promos($page, $required_promo = null
)
242 $page->changeTpl('stats/nb_by_promo.tpl');
244 $res = XDB
::iterRow('SELECT pd.promo, COUNT(*)
246 INNER JOIN account_profiles AS ap ON (ap.uid = a.uid AND FIND_IN_SET(\'owner\', ap.perms))
247 INNER JOIN profiles AS p ON (p.pid = ap.pid)
248 INNER JOIN profile_display AS pd ON (pd.pid = ap.pid)
249 WHERE a.state = \'active\' AND p.deathdate IS NULL AND pd.promo != \'D (en cours)\'
251 ORDER BY pd.promo LIKE \'D%\', pd.promo LIKE \'M%\', pd.promo LIKE \'X%\', pd.promo');
254 while (list($promo, $count) = $res->next()) {
255 $prefix = substr($promo, 0, 4) . '-';
256 $unit = substr($promo, -1);
257 if(!isset($nbpromo[$prefix])) {
258 $nbpromo[$prefix] = array('', '', '', '', '', '', '', '', '', ''); // Empty array containing 10 cells.
260 $nbpromo[$prefix][$unit] = array('promo' => $promo, 'nb' => $count);
263 $count = XDB
::fetchOneCell('SELECT COUNT(*)
265 INNER JOIN account_profiles AS ap ON (ap.uid = a.uid AND FIND_IN_SET(\'owner\', ap.perms))
266 INNER JOIN profiles AS p ON (p.pid = ap.pid)
267 INNER JOIN profile_display AS pd ON (pd.pid = ap.pid)
268 WHERE a.state = \'active\' AND p.deathdate IS NULL AND pd.promo = \'D (en cours)\'');
269 $nbpromo['D (en cours)'][0] = array('promo' => 'D (en cours)', 'nb' => $count);
271 $page->assign_by_ref('nbs', $nbpromo);
272 $page->assign('promo', $required_promo);
275 function handler_coupures($page, $cp_id = null
)
277 $page->changeTpl('stats/coupure.tpl');
279 if (!is_null($cp_id)) {
280 $res = XDB
::query("SELECT debut,
281 TIME_FORMAT(duree,'%kh%i') AS duree,
282 resume, description, services
284 WHERE id = {?}", $cp_id);
285 $cp = $res->fetchOneAssoc();
289 $cp['lg_services'] = serv_to_str($cp['services']);
290 $page->assign_by_ref('cp',$cp);
292 $beginning_date = date("Ymd", time() - 3600*24*21) . "000000";
293 $sql = "SELECT id, debut, resume, services
294 FROM downtimes where debut > '$beginning_date' order by debut desc";
295 $page->assign('coupures', XDB
::iterator($sql));
296 $res = XDB
::iterator("SELECT host, text
298 WHERE state != 'ok'");
299 $page->assign('mxs', $res);
304 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: