b7a8eee428bf19f112b0a70011975491f6d741fb
3 /***************************************************************************\
4 * SPIP, Systeme de publication pour l'internet *
6 * Copyright (c) 2001-2014 *
7 * Arnaud Martin, Antoine Pitrou, Philippe Riviere, Emmanuel Saint-James *
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 \***************************************************************************/
14 if (!defined('_ECRIRE_INC_VERSION')) return;
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);
22 function inc_stats_visites_to_array_dist($unite, $duree, $id_article, $options = array()) {
25 if (!in_array($unite,array('jour','mois')))
29 $table = "spip_visites";
33 $where[] = sql_date_proche($order,-$duree,'day',$serveur);
36 $table = "spip_visites_articles";
37 $where[] = "id_article=".intval($id_article);
40 $where = implode(" AND ",$where);
41 $format = ($unite=='jour'?
'%Y-%m-%d':'%Y-%m-01');
43 $res = sql_select("SUM(visites) AS v, DATE_FORMAT($order,'$format') AS d", $table, $where, "d", "d", "",'',$serveur);
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));
51 $r = sql_fetch($res,$serveur);
53 $r = array('d'=>date($format,$now),'v'=>0);
56 $data[$r['d']] = array('visites'=>$r['v'],'moyenne'=>moyenne_glissante($r['v'], $glisse));
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);
65 // completer les trous manquants si besoin
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));
73 $next = strtotime($last);
79 // projection pour la derniere barre :
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;
88 $remaining = strtotime(date($format,strtotime(date($format,$now))+
$step))-$now;
90 $prorata = $remaining/$periode;
93 $data[$last]['prevision'] = $data[$last]['visites'] +
intval(round($moyenne*$prorata));