Adds content caching for static content (css, images, javascript).
[platal.git] / modules / stats.php
CommitLineData
bf2692e3 1<?php
2/***************************************************************************
8d84c630 3 * Copyright (C) 2003-2009 Polytechnique.org *
bf2692e3 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
22function serv_to_str($params) {
23 $flags = explode(',',$params);
24 $trad = Array('web' => 'site web', 'mail'=> 'redirection mail',
a7de4ef7 25 'smtp' => 'serveur sécurisé d\'envoi de mails',
bf2692e3 26 'nntp' => 'serveur des forums de discussion');
27 $ret = Array();
28 foreach ($flags as $flag) {
29 $ret[] = $trad[$flag];
30 }
31 return implode(', ',$ret);
32}
33
34class StatsModule extends PLModule
35{
36 function handlers()
37 {
38 return array(
eb5a266d
SJ
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),
bf2692e3 44
eb5a266d 45 'stats/coupures' => $this->make_hook('coupures', AUTH_PUBLIC),
bf2692e3 46 );
47 }
48
49 function handler_stats(&$page)
50 {
51 $page->changeTpl('stats/index.tpl');
bf2692e3 52 }
53
54 function handler_evolution(&$page, $jours = 365)
55 {
56 $page->changeTpl('stats/evolution_inscrits.tpl');
57 $page->assign('jours', $jours);
bf2692e3 58 }
59
60 function handler_graph_evo(&$page, $jours = 365)
61 {
bf2692e3 62 define('DUREEJOUR',24*3600);
63
a7de4ef7 64 //recupere le nombre d'inscriptions par jour sur la plage concernée
08cce2ff 65 $res = XDB::iterRow(
bf2692e3 66 "SELECT IF( date_ins>DATE_SUB(NOW(),INTERVAL $jours DAY),
67 TO_DAYS(date_ins)-TO_DAYS(NOW()),
68 ".(-($jours+1)).") AS jour,
69 COUNT(user_id) AS nb
eaf30d86 70 FROM auth_user_md5
99592c92 71 WHERE perms IN ('admin','user') AND deces = 0
bf2692e3 72 GROUP BY jour");
73
74 //genere des donnees compatibles avec GNUPLOT
75 $inscrits='';
76
a7de4ef7 77 // la première ligne contient le total des inscrits avant la date de départ (J - $jours)
bf2692e3 78 list(,$init_nb) = $res->next();
0521940c 79 $total = $init_nb;
80 $numjour = - $jours - 1;
bf2692e3 81
82 for ($i = -$jours; $i<=0; $i++) {
83 if ($numjour<$i) {
84 if(!list($numjour, $nb) = $res->next()) {
85 $numjour = 0;
86 $nb = 0;
87 }
88 }
89 if ($numjour==$i) $total+=$nb;
90 $inscrits .= date('d/m/y',$i*DUREEJOUR+time())." ".$total."\n";
91 }
92
a7de4ef7 93 //Genere le graphique à la volée avec GNUPLOT
bf2692e3 94 header( "Content-type: image/png");
95
96 $delt = ($total - $init_nb)/10;
0521940c 97 $delt = $delt ? $delt : 5;
bf2692e3 98 $ymin = round($init_nb - $delt,0);
99 $ymax = round($total + $delt,0);
100
101 $gnuplot = <<<EOF2
102gnuplot <<EOF
103
104set term png small color
105set size 640/480
106set xdata time
107set timefmt "%d/%m/%y"
108
109set format x "%m/%y"
110set yr [$ymin:$ymax]
111
112set title "Nombre d'inscrits"
113
114plot "-" using 1:2 title 'inscrits' with lines;
115{$inscrits}
116EOF
117EOF2;
118
119 passthru($gnuplot);
120 exit;
121 }
122
123 function handler_graph(&$page, $promo = null)
124 {
bf2692e3 125 if ($promo == 'all') {
a7de4ef7 126 // date de départ
203bc766 127 $depart = 1930;
bf2692e3 128
a7de4ef7 129 //recupere le nombre d'inscriptions par jour sur la plage concernée
08cce2ff 130 $res = XDB::iterRow(
bf2692e3 131 "SELECT promo, SUM(perms IN ('admin', 'user')) / COUNT(*) * 100
132 FROM auth_user_md5
133 WHERE promo >= $depart AND deces = 0
134 GROUP BY promo");
135
136 //genere des donnees compatibles avec GNUPLOT
137 $inscrits='';
138
a7de4ef7 139 // la première ligne contient le total des inscrits avant la date de départ
bf2692e3 140 list($annee, $nb) = $res->next();
141
142 for ($i = $depart; $i <= date("Y"); $i++) {
143 if ($annee < $i) {
144 if(!list($annee, $nb) = $res->next()) {
145 $annee = 0;
146 $nb = 0;
147 }
148 }
149 if ($nb > 0 || $i < date('Y'))
150 $inscrits .= $i.' '.$nb."\n";
151 }
152
a7de4ef7 153 //Genere le graphique à la volée avec GNUPLOT
bf2692e3 154 $fin = $i+2;
155
156 $gnuplot = <<<EOF2
157gnuplot <<EOF
158
159set term png small color
160set size 640/480
161set timefmt "%d/%m/%y"
162
163set xr [$depart:$fin]
164set yr [0:100]
165
203bc766 166set title "Proportion d'inscrits par promotion depuis $depart, en %."
bf2692e3 167
168plot "-" using 1:2 title 'inscrits' with boxes;
169{$inscrits}
170EOF
171EOF2;
172
173 } else {
174 //nombre de jours sur le graph
175 $jours = 365;
176 define('DUREEJOUR',24*3600);
99592c92
VZ
177 $res = XDB::query(
178 "SELECT min(TO_DAYS(date_ins)-TO_DAYS(now()))
179 FROM auth_user_md5
180 WHERE promo = {?} AND perms IN ('admin', 'user') AND deces = 0",
181 $promo);
bf2692e3 182 $jours = -$res->fetchOneCell();
183
a7de4ef7 184 //recupere le nombre d'inscriptions par jour sur la plage concernée
08cce2ff 185 $res = XDB::iterRow(
bf2692e3 186 "SELECT IF( date_ins>DATE_SUB(NOW(),INTERVAL $jours DAY),
187 TO_DAYS(date_ins)-TO_DAYS(NOW()),
188 ".(-($jours+1)).") AS jour,
189 COUNT(user_id) AS nb
eaf30d86 190 FROM auth_user_md5
99592c92 191 WHERE promo = {?} AND perms IN ('admin','user') AND deces = 0
bf2692e3 192 GROUP BY jour", $promo);
193
194 //genere des donnees compatibles avec GNUPLOT
195 $inscrits='';
196
a7de4ef7 197 // la première ligne contient le total des inscrits avant la date de départ (J - $jours)
bf2692e3 198 list(,$init_nb) = $res->next();
199 $total = $init_nb;
200
201 list($numjour, $nb) = $res->next();
202
203 for ($i = -$jours;$i<=0;$i++) {
204 if ($numjour<$i) {
205 if(!list($numjour, $nb) = $res->next()) {
206 $numjour = 0;
207 $nb = 0;
208 }
209 }
210 if ($numjour==$i) $total+=$nb;
211 $inscrits .= date('d/m/y',$i*DUREEJOUR+time())." ".$total."\n";
212 }
213
a7de4ef7 214 //Genere le graphique à la volée avec GNUPLOT
bf2692e3 215 $delt = ($total - $init_nb) / 10;
216 $delt += ($delt < 1);
217 $ymin = round($init_nb - $delt,0);
218 $ymax = round($total + $delt,0);
219
220 $gnuplot = <<<EOF2
221gnuplot <<EOF
222
223set term png small color
224set size 640/480
225set xdata time
226set timefmt "%d/%m/%y"
227
228set format x "%m/%y"
229set yr [$ymin:$ymax]
230
231set title "Nombre d'inscrits de la promotion $promo."
232
233plot "-" using 1:2 title 'inscrits' with lines;
234{$inscrits}e
235EOF
236EOF2;
237 }
238
239 header('Content-type: image/png');
240 passthru($gnuplot);
241 exit;
242 }
243
244 function handler_promos(&$page, $promo = null)
245 {
bf2692e3 246 $page->changeTpl('stats/nb_by_promo.tpl');
247
08cce2ff 248 $res = XDB::iterRow(
bf2692e3 249 "SELECT promo,COUNT(*)
250 FROM auth_user_md5
99592c92 251 WHERE promo > 1900 AND perms IN ('admin','user') AND deces = 0
bf2692e3 252 GROUP BY promo
253 ORDER BY promo");
254 $max=0; $min=3000;
255
256 while (list($p,$nb) = $res->next()) {
257 $p = intval($p);
258 if(!isset($nbpromo[$p/10])) {
259 $nbpromo[$p/10] = Array('','','','','','','','','',''); // tableau de 10 cases vides
260 }
261 $nbpromo[$p/10][$p%10]=Array('promo' => $p, 'nb' => $nb);
262 }
263
264 $page->assign_by_ref('nbs', $nbpromo);
265 $page->assign('min', $min-$min % 10);
266 $page->assign('max', $max+10-$max%10);
267 $page->assign('promo', $promo);
bf2692e3 268 }
269
270 function handler_coupures(&$page, $cp_id = null)
271 {
bf2692e3 272 $page->changeTpl('stats/coupure.tpl');
273
274 if (!is_null($cp_id)) {
da398501 275 $res = XDB::query("SELECT debut,
276 TIME_FORMAT(duree,'%kh%i') AS duree,
277 resume, description, services
278 FROM coupures
279 WHERE id = {?}", $cp_id);
bf2692e3 280 $cp = $res->fetchOneAssoc();
281 }
282
ccdbc270 283 if(@$cp) {
bf2692e3 284 $cp['lg_services'] = serv_to_str($cp['services']);
285 $page->assign_by_ref('cp',$cp);
286 } else {
287 $beginning_date = date("Ymd", time() - 3600*24*21) . "000000";
da398501 288 $sql = "SELECT id, debut, resume, services
bf2692e3 289 FROM coupures where debut > '$beginning_date' order by debut desc";
08cce2ff 290 $page->assign('coupures', XDB::iterator($sql));
ccdbc270 291 $res = XDB::iterator("SELECT host, text
292 FROM mx_watch
293 WHERE state != 'ok'");
294 $page->assign('mxs', $res);
bf2692e3 295 }
bf2692e3 296 }
297}
298
a7de4ef7 299// vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
bf2692e3 300?>