Adds content caching for static content (css, images, javascript).
[platal.git] / modules / stats.php
1 <?php
2 /***************************************************************************
3 * Copyright (C) 2003-2009 Polytechnique.org *
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
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');
27 $ret = Array();
28 foreach ($flags as $flag) {
29 $ret[] = $trad[$flag];
30 }
31 return implode(', ',$ret);
32 }
33
34 class StatsModule extends PLModule
35 {
36 function handlers()
37 {
38 return array(
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),
44
45 'stats/coupures' => $this->make_hook('coupures', AUTH_PUBLIC),
46 );
47 }
48
49 function handler_stats(&$page)
50 {
51 $page->changeTpl('stats/index.tpl');
52 }
53
54 function handler_evolution(&$page, $jours = 365)
55 {
56 $page->changeTpl('stats/evolution_inscrits.tpl');
57 $page->assign('jours', $jours);
58 }
59
60 function handler_graph_evo(&$page, $jours = 365)
61 {
62 define('DUREEJOUR',24*3600);
63
64 //recupere le nombre d'inscriptions par jour sur la plage concernée
65 $res = XDB::iterRow(
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
70 FROM auth_user_md5
71 WHERE perms IN ('admin','user') AND deces = 0
72 GROUP BY jour");
73
74 //genere des donnees compatibles avec GNUPLOT
75 $inscrits='';
76
77 // la première ligne contient le total des inscrits avant la date de départ (J - $jours)
78 list(,$init_nb) = $res->next();
79 $total = $init_nb;
80 $numjour = - $jours - 1;
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
93 //Genere le graphique à la volée avec GNUPLOT
94 header( "Content-type: image/png");
95
96 $delt = ($total - $init_nb)/10;
97 $delt = $delt ? $delt : 5;
98 $ymin = round($init_nb - $delt,0);
99 $ymax = round($total + $delt,0);
100
101 $gnuplot = <<<EOF2
102 gnuplot <<EOF
103
104 set term png small color
105 set size 640/480
106 set xdata time
107 set timefmt "%d/%m/%y"
108
109 set format x "%m/%y"
110 set yr [$ymin:$ymax]
111
112 set title "Nombre d'inscrits"
113
114 plot "-" using 1:2 title 'inscrits' with lines;
115 {$inscrits}
116 EOF
117 EOF2;
118
119 passthru($gnuplot);
120 exit;
121 }
122
123 function handler_graph(&$page, $promo = null)
124 {
125 if ($promo == 'all') {
126 // date de départ
127 $depart = 1930;
128
129 //recupere le nombre d'inscriptions par jour sur la plage concernée
130 $res = XDB::iterRow(
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
139 // la première ligne contient le total des inscrits avant la date de départ
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
153 //Genere le graphique à la volée avec GNUPLOT
154 $fin = $i+2;
155
156 $gnuplot = <<<EOF2
157 gnuplot <<EOF
158
159 set term png small color
160 set size 640/480
161 set timefmt "%d/%m/%y"
162
163 set xr [$depart:$fin]
164 set yr [0:100]
165
166 set title "Proportion d'inscrits par promotion depuis $depart, en %."
167
168 plot "-" using 1:2 title 'inscrits' with boxes;
169 {$inscrits}
170 EOF
171 EOF2;
172
173 } else {
174 //nombre de jours sur le graph
175 $jours = 365;
176 define('DUREEJOUR',24*3600);
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);
182 $jours = -$res->fetchOneCell();
183
184 //recupere le nombre d'inscriptions par jour sur la plage concernée
185 $res = XDB::iterRow(
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
190 FROM auth_user_md5
191 WHERE promo = {?} AND perms IN ('admin','user') AND deces = 0
192 GROUP BY jour", $promo);
193
194 //genere des donnees compatibles avec GNUPLOT
195 $inscrits='';
196
197 // la première ligne contient le total des inscrits avant la date de départ (J - $jours)
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
214 //Genere le graphique à la volée avec GNUPLOT
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
221 gnuplot <<EOF
222
223 set term png small color
224 set size 640/480
225 set xdata time
226 set timefmt "%d/%m/%y"
227
228 set format x "%m/%y"
229 set yr [$ymin:$ymax]
230
231 set title "Nombre d'inscrits de la promotion $promo."
232
233 plot "-" using 1:2 title 'inscrits' with lines;
234 {$inscrits}e
235 EOF
236 EOF2;
237 }
238
239 header('Content-type: image/png');
240 passthru($gnuplot);
241 exit;
242 }
243
244 function handler_promos(&$page, $promo = null)
245 {
246 $page->changeTpl('stats/nb_by_promo.tpl');
247
248 $res = XDB::iterRow(
249 "SELECT promo,COUNT(*)
250 FROM auth_user_md5
251 WHERE promo > 1900 AND perms IN ('admin','user') AND deces = 0
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);
268 }
269
270 function handler_coupures(&$page, $cp_id = null)
271 {
272 $page->changeTpl('stats/coupure.tpl');
273
274 if (!is_null($cp_id)) {
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);
280 $cp = $res->fetchOneAssoc();
281 }
282
283 if(@$cp) {
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";
288 $sql = "SELECT id, debut, resume, services
289 FROM coupures where debut > '$beginning_date' order by debut desc";
290 $page->assign('coupures', XDB::iterator($sql));
291 $res = XDB::iterator("SELECT host, text
292 FROM mx_watch
293 WHERE state != 'ok'");
294 $page->assign('mxs', $res);
295 }
296 }
297 }
298
299 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
300 ?>