Merge commit 'origin/fusionax' into account
[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 // FIXME: don't count dead peaple
68 $res = XDB::iterRow('SELECT IF(registration_date > DATE_SUB(NOW(), INTERVAL {?} DAY),
69 TO_DAYS(registration_date) - TO_DAYS(NOW()),
70 -{?}) AS jour,
71 COUNT(uid) AS nb
72 FROM accounts
73 WHERE state = \'active\'
74 GROUP BY jour', (int)$jours, 1 + (int)$jours);
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 // XXX: Manage dead peaple...
133 $res = XDB::iterRow("SELECT pe.entry_year AS promo, SUM(state = 'active') / COUNT(*) * 100
134 FROM accounts AS a
135 INNER JOIN account_profiles AS ap ON (ap.uid = a.uid AND FIND_IN_SET('owner', ap.perms))
136 INNER JOIN profile_education AS pe ON (pe.uid = ap.pid AND FIND_IN_SET('primary', pe.flags))
137 WHERE pe.entry_year >= {?}
138 GROUP BY promo", $depart);
139
140 //genere des donnees compatibles avec GNUPLOT
141 $inscrits='';
142
143 // la première ligne contient le total des inscrits avant la date de départ
144 list($annee, $nb) = $res->next();
145
146 for ($i = $depart; $i <= date("Y"); $i++) {
147 if ($annee < $i) {
148 if(!list($annee, $nb) = $res->next()) {
149 $annee = 0;
150 $nb = 0;
151 }
152 }
153 if ($nb > 0 || $i < date('Y'))
154 $inscrits .= $i.' '.$nb."\n";
155 }
156
157 //Genere le graphique à la volée avec GNUPLOT
158 $fin = $i+2;
159
160 $gnuplot = <<<EOF2
161 gnuplot <<EOF
162
163 set term png small color
164 set size 640/480
165 set timefmt "%d/%m/%y"
166
167 set xr [$depart:$fin]
168 set yr [0:100]
169
170 set title "Proportion d'inscrits par promotion depuis $depart, en %."
171
172 plot "-" using 1:2 title 'inscrits' with boxes;
173 {$inscrits}
174 EOF
175 EOF2;
176
177 } else {
178 //nombre de jours sur le graph
179 $jours = 365;
180 define('DUREEJOUR', 24 * 3600);
181
182 // XXX: And promo again \o/
183 $res = XDB::query("SELECT MIN(TO_DAYS(a.registration_date) - TO_DAYS(NOW()))
184 FROM accounts AS a
185 INNER JOIN account_profiles AS ap ON (ap.uid = a.uid AND FIND_IN_SET('owner', ap.perms))
186 INNER JOIN profile_education AS pe ON (pe.uid = ap.pid AND FIND_IN_SET('primary', pe.flags))
187 WHERE pe.entry_year = {?} AND a.state = 'active'", (int)$promo);
188 $jours = -$res->fetchOneCell();
189
190 //recupere le nombre d'inscriptions par jour sur la plage concernée
191 $res = XDB::iterRow("SELECT IF(a.registration_date > DATE_SUB(NOW(), INTERVAL {?} DAY),
192 TO_DAYS(a.registration_date) - TO_DAYS(NOW()),
193 -{?}) AS jour,
194 COUNT(a.uid) AS nb
195 FROM accounts AS a
196 INNER JOIN account_profiles AS ap ON (ap.uid = a.uid AND FIND_IN_SET('owner', ap.perms))
197 INNER JOIN profile_education AS pe ON (pe.uid = ap.pid AND FIND_IN_SET('primary', pe.flags))
198 WHERE pe.entry_year = {?} AND a.state = 'active'
199 GROUP BY jour", (int)$jours, 1 + (int)$jours, (int)$promo);
200
201 //genere des donnees compatibles avec GNUPLOT
202 $inscrits='';
203
204 // la première ligne contient le total des inscrits avant la date de départ (J - $jours)
205 list(,$init_nb) = $res->next();
206 $total = $init_nb;
207
208 list($numjour, $nb) = $res->next();
209
210 for ($i = -$jours;$i<=0;$i++) {
211 if ($numjour<$i) {
212 if(!list($numjour, $nb) = $res->next()) {
213 $numjour = 0;
214 $nb = 0;
215 }
216 }
217 if ($numjour==$i) $total+=$nb;
218 $inscrits .= date('d/m/y',$i*DUREEJOUR+time())." ".$total."\n";
219 }
220
221 //Genere le graphique à la volée avec GNUPLOT
222 $delt = ($total - $init_nb) / 10;
223 $delt += ($delt < 1);
224 $ymin = round($init_nb - $delt,0);
225 $ymax = round($total + $delt,0);
226
227 $gnuplot = <<<EOF2
228 gnuplot <<EOF
229
230 set term png small color
231 set size 640/480
232 set xdata time
233 set timefmt "%d/%m/%y"
234
235 set format x "%m/%y"
236 set yr [$ymin:$ymax]
237
238 set title "Nombre d'inscrits de la promotion $promo."
239
240 plot "-" using 1:2 title 'inscrits' with lines;
241 {$inscrits}e
242 EOF
243 EOF2;
244 }
245
246 header('Content-type: image/png');
247 passthru($gnuplot);
248 exit;
249 }
250
251 function handler_promos(&$page, $promo = null)
252 {
253 $page->changeTpl('stats/nb_by_promo.tpl');
254
255 $res = XDB::iterRow('SELECT pe.entry_year AS promo, COUNT(*)
256 FROM accounts AS a
257 INNER JOIN account_profiles AS ap ON (ap.uid = a.uid AND FIND_IN_SET(\'owner\', ap.perms))
258 INNER JOIN profile_education AS pe ON (pe.uid = ap.pid AND FIND_IN_SET(\'primary\', pe.flags))
259 WHERE pe.entry_year >= 1900 AND a.state = \'active\'
260 GROUP BY promo
261 ORDER BY promo');
262 $max=0; $min=3000;
263
264 while (list($p,$nb) = $res->next()) {
265 $p = intval($p);
266 if(!isset($nbpromo[$p/10])) {
267 $nbpromo[$p/10] = Array('','','','','','','','','',''); // tableau de 10 cases vides
268 }
269 $nbpromo[$p/10][$p%10]=Array('promo' => $p, 'nb' => $nb);
270 }
271
272 $page->assign_by_ref('nbs', $nbpromo);
273 $page->assign('min', $min-$min % 10);
274 $page->assign('max', $max+10-$max%10);
275 $page->assign('promo', $promo);
276 }
277
278 function handler_coupures(&$page, $cp_id = null)
279 {
280 $page->changeTpl('stats/coupure.tpl');
281
282 if (!is_null($cp_id)) {
283 $res = XDB::query("SELECT debut,
284 TIME_FORMAT(duree,'%kh%i') AS duree,
285 resume, description, services
286 FROM coupures
287 WHERE id = {?}", $cp_id);
288 $cp = $res->fetchOneAssoc();
289 }
290
291 if(@$cp) {
292 $cp['lg_services'] = serv_to_str($cp['services']);
293 $page->assign_by_ref('cp',$cp);
294 } else {
295 $beginning_date = date("Ymd", time() - 3600*24*21) . "000000";
296 $sql = "SELECT id, debut, resume, services
297 FROM coupures where debut > '$beginning_date' order by debut desc";
298 $page->assign('coupures', XDB::iterator($sql));
299 $res = XDB::iterator("SELECT host, text
300 FROM mx_watch
301 WHERE state != 'ok'");
302 $page->assign('mxs', $res);
303 }
304 }
305
306 function handler_profile(&$page, $period = 'overall')
307 {
308 $page->changeTpl('stats/profile.tpl');
309
310 $time = '';
311 switch ($period) {
312 case 'week': case 'month': case 'year':
313 $time = ' AND e.stamp > DATE_SUB(CURDATE(), INTERVAL 1 ' . strtoupper($period) . ')';
314 break;
315 }
316 // XXX: Need to be port to profile stuff
317 $rows = XDB::iterator("SELECT IF(u.nom_usage != '', u.nom_usage, u.nom) AS nom,
318 u.prenom, u.promo, e.data AS forlife, COUNT(*) AS count
319 FROM logger.events AS e
320 INNER JOIN logger.actions AS act ON (e.action = act.id)
321 INNER JOIN aliases AS a ON (a.alias = e.data)
322 INNER JOIN auth_user_md5 AS u ON (u.user_id = a.id)
323 WHERE act.text = 'view_profile' $time
324 GROUP BY e.data
325 ORDER BY count DESC
326 LIMIT 10");
327 $page->assign('profiles', $rows);
328 $page->assign('period', $period);
329 }
330 }
331
332 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
333 ?>