2 /***************************************************************************
3 * Copyright (C) 2003-2008 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'
43 => $this->make_hook('graph_evo', AUTH_COOKIE
),
44 'stats/promos' => $this->make_hook('promos', AUTH_COOKIE
),
45 'stats/profile' => $this->make_hook('profile', AUTH_COOKIE
),
47 'stats/coupures' => $this->make_hook('coupures', AUTH_PUBLIC
),
51 function handler_stats(&$page)
53 $page->changeTpl('stats/index.tpl');
56 function handler_evolution(&$page, $jours = 365)
58 $page->changeTpl('stats/evolution_inscrits.tpl');
59 $page->assign('jours', $jours);
62 function handler_graph_evo(&$page, $jours = 365)
64 define('DUREEJOUR',24*3600);
66 //recupere le nombre d'inscriptions par jour sur la plage concernée
68 "SELECT IF( date_ins>DATE_SUB(NOW(),INTERVAL $jours DAY),
69 TO_DAYS(date_ins)-TO_DAYS(NOW()),
70 ".(-($jours+
1)).") AS jour,
73 WHERE perms IN ('admin','user') AND deces = 0
76 //genere des donnees compatibles avec GNUPLOT
79 // la première ligne contient le total des inscrits avant la date de départ (J - $jours)
80 list(,$init_nb) = $res->next();
82 $numjour = - $jours - 1;
84 for ($i = -$jours; $i<=0; $i++
) {
86 if(!list($numjour, $nb) = $res->next()) {
91 if ($numjour==$i) $total+
=$nb;
92 $inscrits .= date('d/m/y',$i*DUREEJOUR+
time())." ".$total."\n";
95 //Genere le graphique à la volée avec GNUPLOT
96 header( "Content-type: 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 ($promo == 'all') {
131 //recupere le nombre d'inscriptions par jour sur la plage concernée
133 "SELECT promo, SUM(perms IN ('admin', 'user')) / COUNT(*) * 100
135 WHERE promo >= $depart AND deces = 0
138 //genere des donnees compatibles avec GNUPLOT
141 // la première ligne contient le total des inscrits avant la date de départ
142 list($annee, $nb) = $res->next();
144 for ($i = $depart; $i <= date("Y"); $i++
) {
146 if(!list($annee, $nb) = $res->next()) {
151 if ($nb > 0 ||
$i < date('Y'))
152 $inscrits .= $i.' '.$nb."\n";
155 //Genere le graphique à la volée avec GNUPLOT
161 set term png small color
163 set timefmt
"%d/%m/%y"
165 set xr
[$depart:$fin]
168 set title
"Proportion d'inscrits par promotion depuis $depart, en %."
170 plot
"-" using
1:2 title
'inscrits' with boxes
;
176 //nombre de jours sur le graph
178 define('DUREEJOUR',24*3600);
180 "SELECT min(TO_DAYS(date_ins)-TO_DAYS(now()))
182 WHERE promo = {?} AND perms IN ('admin', 'user') AND deces = 0",
184 $jours = -$res->fetchOneCell();
186 //recupere le nombre d'inscriptions par jour sur la plage concernée
188 "SELECT IF( date_ins>DATE_SUB(NOW(),INTERVAL $jours DAY),
189 TO_DAYS(date_ins)-TO_DAYS(NOW()),
190 ".(-($jours+
1)).") AS jour,
193 WHERE promo = {?} AND perms IN ('admin','user') AND deces = 0
194 GROUP BY jour", $promo);
196 //genere des donnees compatibles avec GNUPLOT
199 // la première ligne contient le total des inscrits avant la date de départ (J - $jours)
200 list(,$init_nb) = $res->next();
203 list($numjour, $nb) = $res->next();
205 for ($i = -$jours;$i<=0;$i++
) {
207 if(!list($numjour, $nb) = $res->next()) {
212 if ($numjour==$i) $total+
=$nb;
213 $inscrits .= date('d/m/y',$i*DUREEJOUR+
time())." ".$total."\n";
216 //Genere le graphique à la volée avec GNUPLOT
217 $delt = ($total - $init_nb) / 10;
218 $delt +
= ($delt < 1);
219 $ymin = round($init_nb - $delt,0);
220 $ymax = round($total +
$delt,0);
225 set term png small color
228 set timefmt
"%d/%m/%y"
233 set title
"Nombre d'inscrits de la promotion $promo."
235 plot
"-" using
1:2 title
'inscrits' with lines
;
241 header('Content-type: image/png');
246 function handler_promos(&$page, $promo = null
)
248 $page->changeTpl('stats/nb_by_promo.tpl');
251 "SELECT promo,COUNT(*)
253 WHERE promo > 1900 AND perms IN ('admin','user') AND deces = 0
258 while (list($p,$nb) = $res->next()) {
260 if(!isset($nbpromo[$p/10])) {
261 $nbpromo[$p/10] = Array('','','','','','','','','',''); // tableau de 10 cases vides
263 $nbpromo[$p/10][$p%10
]=Array('promo' => $p, 'nb' => $nb);
266 $page->assign_by_ref('nbs', $nbpromo);
267 $page->assign('min', $min-$min %
10);
268 $page->assign('max', $max+
10-$max%10
);
269 $page->assign('promo', $promo);
272 function handler_coupures(&$page, $cp_id = null
)
274 $page->changeTpl('stats/coupure.tpl');
276 if (!is_null($cp_id)) {
277 $res = XDB
::query("SELECT debut,
278 TIME_FORMAT(duree,'%kh%i') AS duree,
279 resume, description, services
281 WHERE id = {?}", $cp_id);
282 $cp = $res->fetchOneAssoc();
286 $cp['lg_services'] = serv_to_str($cp['services']);
287 $page->assign_by_ref('cp',$cp);
289 $beginning_date = date("Ymd", time() - 3600*24*21) . "000000";
290 $sql = "SELECT id, debut, resume, services
291 FROM coupures where debut > '$beginning_date' order by debut desc";
292 $page->assign('coupures', XDB
::iterator($sql));
293 $res = XDB
::iterator("SELECT host, text
295 WHERE state != 'ok'");
296 $page->assign('mxs', $res);
300 function handler_profile(&$page, $period = 'overall')
302 $page->changeTpl('stats/profile.tpl');
306 case 'week': case 'month': case 'year':
307 $time = ' AND e.stamp > DATE_SUB(CURDATE(), INTERVAL 1 ' . strtoupper($period) . ')';
310 $rows = XDB
::iterator("SELECT IF(u.nom_usage != '', u.nom_usage, u.nom) AS nom,
311 u.prenom, u.promo, e.data AS forlife, COUNT(*) AS count
312 FROM logger.events AS e
313 INNER JOIN logger.actions AS act ON (e.action = act.id)
314 INNER JOIN aliases AS a ON (a.alias = e.data)
315 INNER JOIN auth_user_md5 AS u ON (u.user_id = a.id)
316 WHERE act.text = 'view_profile' $time
320 $page->assign('profiles', $rows);
321 $page->assign('period', $period);
325 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: