[SPIP] ~v3.0.20-->v3.0.25
[lhc/web/clavette_www.git] / www / plugins-dist / statistiques / inc / stats_visites_to_array.php
1 <?php
2
3 /***************************************************************************\
4 * SPIP, Systeme de publication pour l'internet *
5 * *
6 * Copyright (c) 2001-2016 *
7 * Arnaud Martin, Antoine Pitrou, Philippe Riviere, Emmanuel Saint-James *
8 * *
9 * Ce programme est un logiciel libre distribue sous licence GNU/GPL. *
10 * Pour plus de details voir le fichier COPYING.txt ou l'aide en ligne. *
11 \***************************************************************************/
12
13
14 if (!defined('_ECRIRE_INC_VERSION')) return;
15
16 include_spip('inc/statistiques');
17 // moyenne glissante sur 30 jours
18 define('MOYENNE_GLISSANTE_JOUR', 30);
19 // moyenne glissante sur 12 mois
20 define('MOYENNE_GLISSANTE_MOIS', 12);
21
22 function inc_stats_visites_to_array_dist($unite, $duree, $id_article, $options = array()) {
23 $now = time();
24
25 if (!in_array($unite,array('jour','mois')))
26 $unite = 'jour';
27 $serveur = '';
28
29 $table = "spip_visites";
30 $order = "date";
31 $where = array();
32 if ($duree)
33 $where[] = sql_date_proche($order,-$duree,'day',$serveur);
34
35 if ($id_article) {
36 $table = "spip_visites_articles";
37 $where[] = "id_article=".intval($id_article);
38 }
39
40 $where = implode(" AND ",$where);
41 $format = ($unite=='jour'?'%Y-%m-%d':'%Y-%m-01');
42
43 $res = sql_select("SUM(visites) AS v, DATE_FORMAT($order,'$format') AS d", $table, $where, "d", "d", "",'',$serveur);
44
45 $format = str_replace('%','',$format);
46 $periode = ($unite=='jour'?24*3600:365*24*3600/12);
47 $step = intval(round($periode*1.1,0));
48 $glisse = constant('MOYENNE_GLISSANTE_'.strtoupper($unite));
49 moyenne_glissante();
50 $data = array();
51 $r = sql_fetch($res,$serveur);
52 if (!$r){
53 $r = array('d'=>date($format,$now),'v'=>0);
54 }
55 do {
56 $data[$r['d']] = array('visites'=>$r['v'],'moyenne'=>moyenne_glissante($r['v'], $glisse));
57 $last = $r['d'];
58
59 // donnee suivante
60 $r = sql_fetch($res,$serveur);
61 // si la derniere n'est pas la date courante, l'ajouter
62 if (!$r AND $last!=date($format,$now))
63 $r = array('d'=>date($format,$now),'v'=>0);
64
65 // completer les trous manquants si besoin
66 if ($r){
67 $next = strtotime($last);
68 $current = strtotime($r['d']);
69 while (($next+=$step)<$current AND $d=date($format,$next)){
70 if (!isset($data[$d]))
71 $data[$d] = array('visites'=>0,'moyenne'=>moyenne_glissante(0, $glisse));
72 $last = $d;
73 $next = strtotime($last);
74 }
75 }
76 }
77 while ($r);
78
79 // projection pour la derniere barre :
80 // mesure courante
81 // + moyenne au pro rata du temps qui reste
82 $moyenne = end($data);
83 $moyenne = prev($data);
84 $moyenne = ($moyenne AND isset($moyenne['moyenne']))?$moyenne['moyenne']:0;
85 $data[$last]['moyenne'] = $moyenne;
86
87 // temps restant
88 $remaining = strtotime(date($format,strtotime(date($format,$now))+$step))-$now;
89
90 $prorata = $remaining/$periode;
91
92 // projection
93 $data[$last]['prevision'] = $data[$last]['visites'] + intval(round($moyenne*$prorata));
94
95 return $data;
96 }
97
98
99 ?>