Fix SUID
[platal.git] / modules / stats.php
1 <?php
2 /***************************************************************************
3 * Copyright (C) 2003-2008 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'
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),
46
47 'stats/coupures' => $this->make_hook('coupures', AUTH_PUBLIC),
48 );
49 }
50
51 function handler_stats(&$page)
52 {
53 $page->changeTpl('stats/index.tpl');
54 }
55
56 function handler_evolution(&$page, $jours = 365)
57 {
58 $page->changeTpl('stats/evolution_inscrits.tpl');
59 $page->assign('jours', $jours);
60 }
61
62 function handler_graph_evo(&$page, $jours = 365)
63 {
64 define('DUREEJOUR',24*3600);
65
66 //recupere le nombre d'inscriptions par jour sur la plage concernée
67 $res = XDB::iterRow(
68 "SELECT IF( date_ins>DATE_SUB(NOW(),INTERVAL $jours DAY),
69 TO_DAYS(date_ins)-TO_DAYS(NOW()),
70 ".(-($jours+1)).") AS jour,
71 COUNT(user_id) AS nb
72 FROM auth_user_md5
73 WHERE perms IN ('admin','user') AND deces = 0
74 GROUP BY jour");
75
76 //genere des donnees compatibles avec GNUPLOT
77 $inscrits='';
78
79 // la première ligne contient le total des inscrits avant la date de départ (J - $jours)
80 list(,$init_nb) = $res->next();
81 $total = $init_nb;
82 $numjour = - $jours - 1;
83
84 for ($i = -$jours; $i<=0; $i++) {
85 if ($numjour<$i) {
86 if(!list($numjour, $nb) = $res->next()) {
87 $numjour = 0;
88 $nb = 0;
89 }
90 }
91 if ($numjour==$i) $total+=$nb;
92 $inscrits .= date('d/m/y',$i*DUREEJOUR+time())." ".$total."\n";
93 }
94
95 //Genere le graphique à la volée avec GNUPLOT
96 header( "Content-type: image/png");
97
98 $delt = ($total - $init_nb)/10;
99 $delt = $delt ? $delt : 5;
100 $ymin = round($init_nb - $delt,0);
101 $ymax = round($total + $delt,0);
102
103 $gnuplot = <<<EOF2
104 gnuplot <<EOF
105
106 set term png small color
107 set size 640/480
108 set xdata time
109 set timefmt "%d/%m/%y"
110
111 set format x "%m/%y"
112 set yr [$ymin:$ymax]
113
114 set title "Nombre d'inscrits"
115
116 plot "-" using 1:2 title 'inscrits' with lines;
117 {$inscrits}
118 EOF
119 EOF2;
120
121 passthru($gnuplot);
122 exit;
123 }
124
125 function handler_graph(&$page, $promo = null)
126 {
127 if ($promo == 'all') {
128 // date de départ
129 $depart = 1930;
130
131 //recupere le nombre d'inscriptions par jour sur la plage concernée
132 $res = XDB::iterRow(
133 "SELECT promo, SUM(perms IN ('admin', 'user')) / COUNT(*) * 100
134 FROM auth_user_md5
135 WHERE promo >= $depart AND deces = 0
136 GROUP BY promo");
137
138 //genere des donnees compatibles avec GNUPLOT
139 $inscrits='';
140
141 // la première ligne contient le total des inscrits avant la date de départ
142 list($annee, $nb) = $res->next();
143
144 for ($i = $depart; $i <= date("Y"); $i++) {
145 if ($annee < $i) {
146 if(!list($annee, $nb) = $res->next()) {
147 $annee = 0;
148 $nb = 0;
149 }
150 }
151 if ($nb > 0 || $i < date('Y'))
152 $inscrits .= $i.' '.$nb."\n";
153 }
154
155 //Genere le graphique à la volée avec GNUPLOT
156 $fin = $i+2;
157
158 $gnuplot = <<<EOF2
159 gnuplot <<EOF
160
161 set term png small color
162 set size 640/480
163 set timefmt "%d/%m/%y"
164
165 set xr [$depart:$fin]
166 set yr [0:100]
167
168 set title "Proportion d'inscrits par promotion depuis $depart, en %."
169
170 plot "-" using 1:2 title 'inscrits' with boxes;
171 {$inscrits}
172 EOF
173 EOF2;
174
175 } else {
176 //nombre de jours sur le graph
177 $jours = 365;
178 define('DUREEJOUR',24*3600);
179 $res = XDB::query(
180 "SELECT min(TO_DAYS(date_ins)-TO_DAYS(now()))
181 FROM auth_user_md5
182 WHERE promo = {?} AND perms IN ('admin', 'user') AND deces = 0",
183 $promo);
184 $jours = -$res->fetchOneCell();
185
186 //recupere le nombre d'inscriptions par jour sur la plage concernée
187 $res = XDB::iterRow(
188 "SELECT IF( date_ins>DATE_SUB(NOW(),INTERVAL $jours DAY),
189 TO_DAYS(date_ins)-TO_DAYS(NOW()),
190 ".(-($jours+1)).") AS jour,
191 COUNT(user_id) AS nb
192 FROM auth_user_md5
193 WHERE promo = {?} AND perms IN ('admin','user') AND deces = 0
194 GROUP BY jour", $promo);
195
196 //genere des donnees compatibles avec GNUPLOT
197 $inscrits='';
198
199 // la première ligne contient le total des inscrits avant la date de départ (J - $jours)
200 list(,$init_nb) = $res->next();
201 $total = $init_nb;
202
203 list($numjour, $nb) = $res->next();
204
205 for ($i = -$jours;$i<=0;$i++) {
206 if ($numjour<$i) {
207 if(!list($numjour, $nb) = $res->next()) {
208 $numjour = 0;
209 $nb = 0;
210 }
211 }
212 if ($numjour==$i) $total+=$nb;
213 $inscrits .= date('d/m/y',$i*DUREEJOUR+time())." ".$total."\n";
214 }
215
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);
221
222 $gnuplot = <<<EOF2
223 gnuplot <<EOF
224
225 set term png small color
226 set size 640/480
227 set xdata time
228 set timefmt "%d/%m/%y"
229
230 set format x "%m/%y"
231 set yr [$ymin:$ymax]
232
233 set title "Nombre d'inscrits de la promotion $promo."
234
235 plot "-" using 1:2 title 'inscrits' with lines;
236 {$inscrits}e
237 EOF
238 EOF2;
239 }
240
241 header('Content-type: image/png');
242 passthru($gnuplot);
243 exit;
244 }
245
246 function handler_promos(&$page, $promo = null)
247 {
248 $page->changeTpl('stats/nb_by_promo.tpl');
249
250 $res = XDB::iterRow(
251 "SELECT promo,COUNT(*)
252 FROM auth_user_md5
253 WHERE promo > 1900 AND perms IN ('admin','user') AND deces = 0
254 GROUP BY promo
255 ORDER BY promo");
256 $max=0; $min=3000;
257
258 while (list($p,$nb) = $res->next()) {
259 $p = intval($p);
260 if(!isset($nbpromo[$p/10])) {
261 $nbpromo[$p/10] = Array('','','','','','','','','',''); // tableau de 10 cases vides
262 }
263 $nbpromo[$p/10][$p%10]=Array('promo' => $p, 'nb' => $nb);
264 }
265
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);
270 }
271
272 function handler_coupures(&$page, $cp_id = null)
273 {
274 $page->changeTpl('stats/coupure.tpl');
275
276 if (!is_null($cp_id)) {
277 $res = XDB::query("SELECT debut,
278 TIME_FORMAT(duree,'%kh%i') AS duree,
279 resume, description, services
280 FROM coupures
281 WHERE id = {?}", $cp_id);
282 $cp = $res->fetchOneAssoc();
283 }
284
285 if(@$cp) {
286 $cp['lg_services'] = serv_to_str($cp['services']);
287 $page->assign_by_ref('cp',$cp);
288 } else {
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
294 FROM mx_watch
295 WHERE state != 'ok'");
296 $page->assign('mxs', $res);
297 }
298 }
299
300 function handler_profile(&$page, $period = 'overall')
301 {
302 $page->changeTpl('stats/profile.tpl');
303
304 $time = '';
305 switch ($period) {
306 case 'week': case 'month': case 'year':
307 $time = ' AND e.stamp > DATE_SUB(CURDATE(), INTERVAL 1 ' . strtoupper($period) . ')';
308 break;
309 }
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
317 GROUP BY e.data
318 ORDER BY count DESC
319 LIMIT 10");
320 $page->assign('profiles', $rows);
321 $page->assign('period', $period);
322 }
323 }
324
325 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
326 ?>