Merge commit 'origin/fusionax' into account
[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 'stats/profile' => $this->make_hook('profile', AUTH_COOKIE),
45
46 'stats/coupures' => $this->make_hook('coupures', AUTH_PUBLIC),
47 );
48 }
49
50 function handler_stats(&$page)
51 {
52 $page->changeTpl('stats/index.tpl');
53 }
54
55 function handler_evolution(&$page, $jours = 365)
56 {
57 $page->changeTpl('stats/evolution_inscrits.tpl');
58 $page->assign('jours', $jours);
59 }
60
61 function handler_graph_evo(&$page, $jours = 365)
62 {
63 define('DUREEJOUR', 24 * 3600);
64
65 //recupere le nombre d'inscriptions par jour sur la plage concernée
66 $res = XDB::iterRow('SELECT IF(registration_date > DATE_SUB(NOW(), INTERVAL {?} DAY),
67 TO_DAYS(registration_date) - TO_DAYS(NOW()),
68 -{?}) AS jour,
69 COUNT(uid) AS nb
70 FROM accounts AS a
71 LEFT JOIN account_profiles AS ap ON(ap.uid = a.uid AND FIND_IN_SET(\'owner\', ap.flags))
72 LEFT JOIN profiles AS p ON (ap.pid = p.pid)
73 WHERE state = \'active\' AND p.deathdate IS NULL
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 $res = XDB::iterRow("SELECT pe.entry_year AS promo, SUM(state = 'active') / COUNT(*) * 100
133 FROM accounts AS a
134 INNER JOIN account_profiles AS ap ON (ap.uid = a.uid AND FIND_IN_SET('owner', ap.perms))
135 INNER JOIN profiles AS p ON (p.pid = ap.pid)
136 INNER JOIN profile_education AS pe ON (pe.uid = ap.pid AND FIND_IN_SET('primary', pe.flags))
137 WHERE pe.entry_year >= {?} AND p.deathdate IS NULL
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 $res = XDB::query("SELECT MIN(TO_DAYS(a.registration_date) - TO_DAYS(NOW()))
183 FROM accounts AS a
184 INNER JOIN account_profiles AS ap ON (ap.uid = a.uid AND FIND_IN_SET('owner', ap.perms))
185 INNER JOIN profile_education AS pe ON (pe.uid = ap.pid AND FIND_IN_SET('primary', pe.flags))
186 WHERE pe.entry_year = {?} AND a.state = 'active'", (int)$promo);
187 $jours = -$res->fetchOneCell();
188
189 //recupere le nombre d'inscriptions par jour sur la plage concernée
190 $res = XDB::iterRow("SELECT IF(a.registration_date > DATE_SUB(NOW(), INTERVAL {?} DAY),
191 TO_DAYS(a.registration_date) - TO_DAYS(NOW()),
192 -{?}) AS jour,
193 COUNT(a.uid) AS nb
194 FROM accounts AS a
195 INNER JOIN account_profiles AS ap ON (ap.uid = a.uid AND FIND_IN_SET('owner', ap.perms))
196 INNER JOIN profile_education AS pe ON (pe.uid = ap.pid AND FIND_IN_SET('primary', pe.flags))
197 WHERE pe.entry_year = {?} AND a.state = 'active'
198 GROUP BY jour", (int)$jours, 1 + (int)$jours, (int)$promo);
199
200 //genere des donnees compatibles avec GNUPLOT
201 $inscrits='';
202
203 // la première ligne contient le total des inscrits avant la date de départ (J - $jours)
204 list(,$init_nb) = $res->next();
205 $total = $init_nb;
206
207 list($numjour, $nb) = $res->next();
208
209 for ($i = -$jours;$i<=0;$i++) {
210 if ($numjour<$i) {
211 if(!list($numjour, $nb) = $res->next()) {
212 $numjour = 0;
213 $nb = 0;
214 }
215 }
216 if ($numjour==$i) $total+=$nb;
217 $inscrits .= date('d/m/y',$i*DUREEJOUR+time())." ".$total."\n";
218 }
219
220 //Genere le graphique à la volée avec GNUPLOT
221 $delt = ($total - $init_nb) / 10;
222 $delt += ($delt < 1);
223 $ymin = round($init_nb - $delt,0);
224 $ymax = round($total + $delt,0);
225
226 $gnuplot = <<<EOF2
227 gnuplot <<EOF
228
229 set term png small color
230 set size 640/480
231 set xdata time
232 set timefmt "%d/%m/%y"
233
234 set format x "%m/%y"
235 set yr [$ymin:$ymax]
236
237 set title "Nombre d'inscrits de la promotion $promo."
238
239 plot "-" using 1:2 title 'inscrits' with lines;
240 {$inscrits}e
241 EOF
242 EOF2;
243 }
244
245 header('Content-type: image/png');
246 passthru($gnuplot);
247 exit;
248 }
249
250 function handler_promos(&$page, $promo = null)
251 {
252 $page->changeTpl('stats/nb_by_promo.tpl');
253
254 $res = XDB::iterRow('SELECT pe.entry_year AS promo, COUNT(*)
255 FROM accounts AS a
256 INNER JOIN account_profiles AS ap ON (ap.uid = a.uid AND FIND_IN_SET(\'owner\', ap.perms))
257 INNER JOIN profile_education AS pe ON (pe.uid = ap.pid AND FIND_IN_SET(\'primary\', pe.flags))
258 WHERE pe.entry_year >= 1900 AND a.state = \'active\'
259 GROUP BY promo
260 ORDER BY promo');
261 $max=0; $min=3000;
262
263 while (list($p,$nb) = $res->next()) {
264 $p = intval($p);
265 if(!isset($nbpromo[$p/10])) {
266 $nbpromo[$p/10] = Array('','','','','','','','','',''); // tableau de 10 cases vides
267 }
268 $nbpromo[$p/10][$p%10]=Array('promo' => $p, 'nb' => $nb);
269 }
270
271 $page->assign_by_ref('nbs', $nbpromo);
272 $page->assign('min', $min-$min % 10);
273 $page->assign('max', $max+10-$max%10);
274 $page->assign('promo', $promo);
275 }
276
277 function handler_coupures(&$page, $cp_id = null)
278 {
279 $page->changeTpl('stats/coupure.tpl');
280
281 if (!is_null($cp_id)) {
282 $res = XDB::query("SELECT debut,
283 TIME_FORMAT(duree,'%kh%i') AS duree,
284 resume, description, services
285 FROM coupures
286 WHERE id = {?}", $cp_id);
287 $cp = $res->fetchOneAssoc();
288 }
289
290 if(@$cp) {
291 $cp['lg_services'] = serv_to_str($cp['services']);
292 $page->assign_by_ref('cp',$cp);
293 } else {
294 $beginning_date = date("Ymd", time() - 3600*24*21) . "000000";
295 $sql = "SELECT id, debut, resume, services
296 FROM coupures where debut > '$beginning_date' order by debut desc";
297 $page->assign('coupures', XDB::iterator($sql));
298 $res = XDB::iterator("SELECT host, text
299 FROM mx_watch
300 WHERE state != 'ok'");
301 $page->assign('mxs', $res);
302 }
303 }
304
305 function handler_profile(&$page, $period = 'overall')
306 {
307 $page->changeTpl('stats/profile.tpl');
308
309 $time = '';
310 switch ($period) {
311 case 'week': case 'month': case 'year':
312 $time = ' AND e.stamp > DATE_SUB(CURDATE(), INTERVAL 1 ' . strtoupper($period) . ')';
313 break;
314 }
315 $rows = XDB::fetchAllAssoc("SELECT p.pid AS profile, COUNT(*) AS count
316 FROM logger.events AS e
317 INNER JOIN logger.actions AS act ON (e.action = act.id)
318 INNER JOIN profiles AS p ON (p.hrpid = e.data)
319 WHERE act.text = 'view_profile' $time
320 GROUP BY e.data
321 ORDER BY count DESC
322 LIMIT 10");
323 foreach ($rows as $key=>$row) {
324 $rows[$key]['profile'] = Profile::get($rows[$key]['profile']);
325 }
326 $page->assign('profiles', $rows);
327 $page->assign('period', $period);
328 }
329 }
330
331 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
332 ?>