From a09bd0bca530650fa661f8364da746c2f32fdccf Mon Sep 17 00:00:00 2001 From: Riccardo Di Dato Date: Tue, 30 Aug 2022 17:22:08 +0200 Subject: [PATCH] Avviato nuovo sistema statistiche --- app/os/000-guiapi.template.conf | 23 +- app/private/bin/convertStats.php | 4 +- app/private/lib/AnalyticsHelper.php | 252 ++++++------- app/private/lib/AnalyticsHelper_OLD.php | 352 ++++++++++++++++++ app/private/lib/PipelineHelper.php | 63 +++- app/private/lib/dao/GenericDao.php | 26 ++ app/private/lib/dao/lib.inclusion.php | 3 + .../dao/models/stats/ArticleStatsModel.php | 43 +++ .../lib/dao/models/stats/BannerStatsModel.php | 45 +++ .../dao/models/stats/PageViewStatsModel.php | 31 ++ app/private/lib/gui/GUIHandler.php | 2 +- app/public/admin/homeBanner.php | 8 +- app/public/admin/homeNotizie.php | 4 +- app/public/admin/index.php | 1 - app/public/mobile/stats/bannerClick.php | 2 +- app/public/mobile/stats/bannerImpression.php | 2 +- app/public/mobile/stats/notiziaImpression.php | 2 +- app/public/mobile/stats/pageImpression.php | 2 +- app/public/stats/bannerClick.php | 2 +- app/public/stats/bannerImpression.php | 2 +- app/public/stats/notiziaImpression.php | 2 +- app/public/stats/pageImpression.php | 2 +- 22 files changed, 704 insertions(+), 169 deletions(-) create mode 100644 app/private/lib/AnalyticsHelper_OLD.php create mode 100644 app/private/lib/dao/models/stats/ArticleStatsModel.php create mode 100644 app/private/lib/dao/models/stats/BannerStatsModel.php create mode 100644 app/private/lib/dao/models/stats/PageViewStatsModel.php diff --git a/app/os/000-guiapi.template.conf b/app/os/000-guiapi.template.conf index 59cd337..55c0742 100644 --- a/app/os/000-guiapi.template.conf +++ b/app/os/000-guiapi.template.conf @@ -9,17 +9,22 @@ RewriteEngine On - RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI}.php -f - RewriteRule "^(.*)$" "$1.php" [NC,L,QSA] - - RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -f [OR] - RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -d - RewriteRule ^ - [L] - - RewriteRule "^(api/.*)$" "api/index.php" [NC,L,QSA] - + RewriteRule "sitemap.xml" "sitemap.php" [L,QSA] + RewriteRule "^(.*)articolo_(.*)$" "$1articolo.php?id=$2" [QSA] + RewriteRule "^art_(.*).html$" "articolo.php?short=$1" [QSA] + RewriteRule "^mobile/art_(.*).html$" "mobile/articolo.php?short=$1" [QSA] + RewriteRule "^(.*)categorySummary_(.*)$" "$1categorySummary.php?category=$2" [QSA] + RewriteRule "^(.*)dynImg/(.*)/(.*)$" "$1getImage.php?id=$3&size=$2" [QSA] + RewriteRule "giornale/all/.*" "oldArticleRedirect.php" [L,QSA] + RewriteRule "Giornale/all/.*" "oldArticleRedirect.php" [L,QSA] + + + XSendFile on + XSendFilePath "/tmp/xsend" + + diff --git a/app/private/bin/convertStats.php b/app/private/bin/convertStats.php index 08007f0..430a809 100644 --- a/app/private/bin/convertStats.php +++ b/app/private/bin/convertStats.php @@ -8,8 +8,6 @@ require_once(dirname(__FILE__)."/../load.php"); - - function getModels() { static $iteration = 0; $size = 1000; @@ -32,7 +30,7 @@ exit; $models = getModels(); while (sizeof($models)>0) { foreach ($models as $model) { - + } $models = getModels(); } diff --git a/app/private/lib/AnalyticsHelper.php b/app/private/lib/AnalyticsHelper.php index 6d263e2..2254565 100644 --- a/app/private/lib/AnalyticsHelper.php +++ b/app/private/lib/AnalyticsHelper.php @@ -8,6 +8,26 @@ class AnalyticsHelper { private static $COOKIE_NAME = "FDN_STAT_NUM"; private static $COOKIE_TIME = "+1 year"; + + public static function pageImpression($content, $d, $m, $y, $isUnique) { + $dao = GlobalVariables::get("dao"); + if ($dao instanceof GenericDao) { + $upd = [ + '$inc' => [ + 'views' => 1 + ] + ]; + if ($isUnique) { + $upd['$inc']['unique'] = 1; + } + $dao->rawUpsert( + PageViewStatsModel::class, + PageViewStatsModel::getFilter($content, $d, $m, $y), + $upd + ); + } + } + /** * La funzione si occupa del salvataggio vero e proprio della impression della pagina. * UTILIZZARE QUESTA FUNZIONE SOLO NELLA PAGINA DI BACKEND (stats/pageImpression.php). Nelle pagine base utilizzare invece la funzione di log @@ -15,37 +35,21 @@ class AnalyticsHelper { * @param string $content * @param string $source */ - public static function savePageImpression($ip, $content, $source){ - $tmp = new stdClass(); - - $tmp->user = null; - if (isset($_COOKIE[self::$COOKIE_NAME])){ - $id = $_COOKIE[self::$COOKIE_NAME]; - - $tmp->user = new stdClass(); - $tmp->user->id = $id; - $tmp->user->isNew = false; - if (GlobalVariables::get("dao")->count("StatisticModel",array("user.id"=>$id))==0){ - $tmp->user->isNew = true; - } - } - - $tmp->content = $content; - $tmp->source = $source; - $tmp->ip = $ip; - $tmp->statType = StatisticModel::STAT_TYPE_PAGE; - $stat = new StatisticModel($tmp); - GlobalVariables::get("dao")->save($stat); + public static function savePageImpression($ip, $content, $source) { + self::pageImpression( + $content, + intval(date('d')), + intval(date('m')), + intval(date('Y')), + !isset($_COOKIE[self::$COOKIE_NAME]) + ); + self::prepareStatCookie(); } /** * La funzione si occupa di aggiungere una visualizzazione alla pagina corrente */ public static function logPageImpression(){ - if (!isset($_COOKIE[self::$COOKIE_NAME])){ - self::prepareStatCookie(); - } - $ip = $_SERVER['REMOTE_ADDR']; $content = $_SERVER['PHP_SELF']; $source = null; @@ -63,6 +67,21 @@ class AnalyticsHelper { } + public static function notiziaImpression($content, $d, $m, $y) { + $dao = GlobalVariables::get("dao"); + if ($dao instanceof GenericDao) { + $dao->rawUpsert( + ArticleStatsModel::class, + ArticleStatsModel::getFilter($content, $d, $m, $y), + [ + '$inc' => [ + 'views' => 1 + ] + ] + ); + } + } + /** * La funzione si occupa del salvataggio vero e proprio della impression della notizia. * UTILIZZARE QUESTA FUNZIONE SOLO NELLA PAGINA DI BACKEND (stats/notiziaImpression.php). Nelle pagine base utilizzare invece la funzione di log @@ -70,34 +89,13 @@ class AnalyticsHelper { * @param string $content * @param string $source */ - public static function saveNotiziaImpression($ip, $notiziaId, $source){ - $tmp = new stdClass(); - - $tmp->user = null; - if (isset($_COOKIE[self::$COOKIE_NAME])){ - $id = $_COOKIE[self::$COOKIE_NAME]; - - $tmp->user = new stdClass(); - $tmp->user->id = $id; - $tmp->user->isNew = false; - if (GlobalVariables::get("dao")->count("StatisticModel",array("user.id"=>$id))==0){ - $tmp->user->isNew = true; - } - } - - $tmp->content = new stdClass(); - $notizia = GlobalVariables::get("dao")->getFirst("NotiziaModel",array("id"=>$notiziaId)); - if (!is_null($notizia)){ - $tmp->content->notizia = $notiziaId; - $tmp->content->category = $notizia->category; - - $tmp->source = $source; - $tmp->ip = $ip; - $tmp->statType = StatisticModel::STAT_TYPE_NOTIZIA; - $stat = new StatisticModel($tmp); - GlobalVariables::get("dao")->save($stat); - } - + public static function saveNotiziaImpression($ip, $notiziaId, $source) { + self::notiziaImpression( + $notiziaId, + intval(date('d')), + intval(date('m')), + intval(date('Y')) + ); } /** @@ -125,21 +123,44 @@ class AnalyticsHelper { * @param string $id */ public static function countNotiziaImpressions($id, $start = null, $end = null){ - $searchArray = array("statType"=>StatisticModel::STAT_TYPE_NOTIZIA,"content.notizia"=>$id); - if (!is_null($start)){ -// $searchArray["date"]['$gte'] = new MongoDate($start); - $searchArray["date"]['$gte'] = new MongoDB\BSON\UTCDateTime( $start * 1000 ); - - } - if (!is_null($end)){ -// $searchArray["date"]['$lte'] = new MongoDate($end); - $searchArray["date"]['$lte'] = new MongoDB\BSON\UTCDateTime( $end * 1000 ); - } - return GlobalVariables::get("dao")->count("StatisticModel",$searchArray); + $ret = GlobalVariables::get("dao")->aggregate( + ArticleStatsModel::class, + PipelineHelper::getNotiziaImpressions($id, $start, $end) + ); + + return sizeof($ret) == 0 ? 0 : $ret[0]['tot']; } - + public static function bannerImpression($bannerId, $d, $m, $y) { + $dao = GlobalVariables::get("dao"); + if ($dao instanceof GenericDao) { + $dao->rawUpsert( + BannerStatsModel::class, + BannerStatsModel::getFilter($bannerId, $d, $m, $y), + [ + '$inc' => [ + 'views' => 1 + ] + ] + ); + } + } + public static function bannerClick($bannerId, $d, $m, $y) { + $dao = GlobalVariables::get("dao"); + if ($dao instanceof GenericDao) { + $dao->rawUpsert( + BannerStatsModel::class, + BannerStatsModel::getFilter($bannerId, $d, $m, $y), + [ + '$inc' => [ + 'clicks' => 1 + ] + ] + ); + } + } + /** * La funzione si occupa del salvataggio vero e proprio della impression del banner. * UTILIZZARE QUESTA FUNZIONE SOLO NELLA PAGINA DI BACKEND (stats/bannerImpression.php). Nelle pagine base utilizzare invece la funzione di log @@ -148,33 +169,12 @@ class AnalyticsHelper { * @param string $source */ public static function saveBannerImpression($ip, $bannerId, $source){ - $tmp = new stdClass(); - - $tmp->user = null; - if (isset($_COOKIE[self::$COOKIE_NAME])){ - $id = $_COOKIE[self::$COOKIE_NAME]; - - $tmp->user = new stdClass(); - $tmp->user->id = $id; - $tmp->user->isNew = false; - if (GlobalVariables::get("dao")->count("StatisticModel",array("user.id"=>$id))==0){ - $tmp->user->isNew = true; - } - } - - $tmp->content = new stdClass(); - $banner = GlobalVariables::get("dao")->getFirst("BannerModel",array("id"=>$bannerId)); - if (!is_null($banner)){ - $tmp->content->banner = $bannerId; - $tmp->content->position = $banner->position; - - $tmp->source = $source; - $tmp->ip = $ip; - $tmp->statType = StatisticModel::STAT_TYPE_BANNER_IMPRESSION; - $stat = new StatisticModel($tmp); - GlobalVariables::get("dao")->save($stat); - } - + self::bannerImpression( + $bannerId, + intval(date('d')), + intval(date('m')), + intval(date('Y')) + ); } /** @@ -201,20 +201,16 @@ class AnalyticsHelper { } /** - * Restitusce il numero di impressions per una notizia + * Restitusce il numero di impressions per un banner * @param string $id */ public static function countBannerImpressions($id, $start = null, $end = null){ - $searchArray = array("statType"=>StatisticModel::STAT_TYPE_BANNER_IMPRESSION,"content.banner"=>$id); - if (!is_null($start)){ -// $searchArray["date"]['$gte'] = new MongoDate($start); - $searchArray["date"]['$gte'] = new MongoDB\BSON\UTCDateTime($start * 1000); - } - if (!is_null($end)){ -// $searchArray["date"]['$lte'] = new MongoDate($end); - $searchArray["date"]['$lte'] = new MongoDB\BSON\UTCDateTime($end * 1000); - } - return GlobalVariables::get("dao")->count("StatisticModel",$searchArray); + $ret = GlobalVariables::get("dao")->aggregate( + BannerStatsModel::class, + PipelineHelper::getBannerImpressions($id, $start, $end) + ); + + return sizeof($ret) == 0 ? 0 : $ret[0]['tot']; } @@ -226,32 +222,12 @@ class AnalyticsHelper { * @param string $source */ public static function saveBannerClick($bannerId, $source){ - $tmp = new stdClass(); - - $tmp->user = null; - if (isset($_COOKIE[self::$COOKIE_NAME])){ - $id = $_COOKIE[self::$COOKIE_NAME]; - - $tmp->user = new stdClass(); - $tmp->user->id = $id; - $tmp->user->isNew = false; - if (GlobalVariables::get("dao")->count("StatisticModel",array("user.id"=>$id))==0){ - $tmp->user->isNew = true; - } - } - - $tmp->content = new stdClass(); - $banner = GlobalVariables::get("dao")->getFirst("BannerModel",array("id"=>$bannerId)); - if (!is_null($banner)){ - $tmp->content->banner = $bannerId; - $tmp->content->position = $banner->position; - - $tmp->source = $source; - $tmp->ip = $_SERVER['REMOTE_ADDR']; - $tmp->statType = StatisticModel::STAT_TYPE_BANNER_CLICK; - $stat = new StatisticModel($tmp); - GlobalVariables::get("dao")->save($stat); - } + self::bannerClick( + $bannerId, + intval(date('d')), + intval(date('m')), + intval(date('Y')) + ); } /** @@ -270,16 +246,12 @@ class AnalyticsHelper { * @param string $id */ public static function countBannerClicks($id, $start = null, $end = null){ - $searchArray = array("statType"=>StatisticModel::STAT_TYPE_BANNER_CLICK,"content.banner"=>$id); - if (!is_null($start)){ -// $searchArray["date"]['$gte'] = new MongoDate($start); - $searchArray["date"]['$gte'] = new MongoDB\BSON\UTCDateTime($start * 1000); - } - if (!is_null($end)){ -// $searchArray["date"]['$lte'] = new MongoDate($end); - $searchArray["date"]['$lte'] = new MongoDB\BSON\UTCDateTime($end * 1000); - } - return GlobalVariables::get("dao")->count("StatisticModel",$searchArray); + $ret = GlobalVariables::get("dao")->aggregate( + BannerStatsModel::class, + PipelineHelper::getBannerClicks($id, $start, $end) + ); + + return sizeof($ret) == 0 ? 0 : $ret[0]['tot']; } @@ -314,7 +286,7 @@ class AnalyticsHelper { } // $baseSearchArray = array("position"=>$position,"end"=>array('$gte'=>new MongoDate(strtotime("tomorrow"))),"categories"=>$category); - $baseSearchArray = array("position"=>$position,"end"=>array('$gte'=>new MongoDB\BSON\UTCDateTime( strtotime("tomorrow") * 1000 ) ),"categories"=>$category); + $baseSearchArray = array("position"=>$position,"end"=>array('$gte'=>new MongoDB\BSON\UTCDateTime( strtotime("tomorrow CET") * 1000 ) ),"categories"=>$category); // Prendi il primo dei "mai mostrati oggi" tra quelli non presenti nella pagina $todayPlusShown = array_merge($idList,$shown); if (sizeof($todayPlusShown)>0){ diff --git a/app/private/lib/AnalyticsHelper_OLD.php b/app/private/lib/AnalyticsHelper_OLD.php new file mode 100644 index 0000000..7e7607e --- /dev/null +++ b/app/private/lib/AnalyticsHelper_OLD.php @@ -0,0 +1,352 @@ +user = null; + if (isset($_COOKIE[self::$COOKIE_NAME])){ + $id = $_COOKIE[self::$COOKIE_NAME]; + + $tmp->user = new stdClass(); + $tmp->user->id = $id; + $tmp->user->isNew = false; + if (GlobalVariables::get("dao")->count("StatisticModel",array("user.id"=>$id))==0){ + $tmp->user->isNew = true; + } + } + + $tmp->content = $content; + $tmp->source = $source; + $tmp->ip = $ip; + $tmp->statType = StatisticModel::STAT_TYPE_PAGE; + $stat = new StatisticModel($tmp); + GlobalVariables::get("dao")->save($stat); + } + + /** + * La funzione si occupa di aggiungere una visualizzazione alla pagina corrente + */ + public static function logPageImpression(){ + if (!isset($_COOKIE[self::$COOKIE_NAME])){ + self::prepareStatCookie(); + } + + $ip = $_SERVER['REMOTE_ADDR']; + $content = $_SERVER['PHP_SELF']; + $source = null; + if (array_key_exists("HTTP_REFERER",$_SERVER)){ + $source = $_SERVER['HTTP_REFERER']; + } + + $function = '$.ajax({url: "stats/pageImpression.php", method: "POST", data: { "ip": "'.$ip.'", "content": "'.$content.'", "source": "'.$source.'" }, dataType: "html" });'; + GUIHandler::addOnLoadJsEvent($function); + } + + + private static function prepareStatCookie(){ + setcookie(self::$COOKIE_NAME, uniqid("",true), strtotime(self::$COOKIE_TIME) ); + } + + + /** + * La funzione si occupa del salvataggio vero e proprio della impression della notizia. + * UTILIZZARE QUESTA FUNZIONE SOLO NELLA PAGINA DI BACKEND (stats/notiziaImpression.php). Nelle pagine base utilizzare invece la funzione di log + * @param string $ip + * @param string $content + * @param string $source + */ + public static function saveNotiziaImpression($ip, $notiziaId, $source){ + $tmp = new stdClass(); + + $tmp->user = null; + if (isset($_COOKIE[self::$COOKIE_NAME])){ + $id = $_COOKIE[self::$COOKIE_NAME]; + + $tmp->user = new stdClass(); + $tmp->user->id = $id; + $tmp->user->isNew = false; + if (GlobalVariables::get("dao")->count("StatisticModel",array("user.id"=>$id))==0){ + $tmp->user->isNew = true; + } + } + + $tmp->content = new stdClass(); + $notizia = GlobalVariables::get("dao")->getFirst("NotiziaModel",array("id"=>$notiziaId)); + if (!is_null($notizia)){ + $tmp->content->notizia = $notiziaId; + $tmp->content->category = $notizia->category; + + $tmp->source = $source; + $tmp->ip = $ip; + $tmp->statType = StatisticModel::STAT_TYPE_NOTIZIA; + $stat = new StatisticModel($tmp); + GlobalVariables::get("dao")->save($stat); + } + + } + + /** + * La funzione si occupa di aggiungere una visualizzazione alla notizia correlata a $id + * @param string $id + */ + public static function logNotiziaImpression($id){ + if (!isset($_COOKIE[self::$COOKIE_NAME])){ + self::prepareStatCookie(); + } + + $ip = $_SERVER['REMOTE_ADDR']; + $notizia = $id; + $source = null; + if (array_key_exists("HTTP_REFERER",$_SERVER)){ + $source = $_SERVER['HTTP_REFERER']; + } + + $function = '$.ajax({url: "stats/notiziaImpression.php", method: "POST", data: { "ip": "'.$ip.'", "notizia": "'.$id.'", "source": "'.$source.'" }, dataType: "html" });'; + GUIHandler::addOnLoadJsEvent($function); + } + + /** + * Restitusce il numero di impressions per una notizia + * @param string $id + */ + public static function countNotiziaImpressions($id, $start = null, $end = null){ + $searchArray = array("statType"=>StatisticModel::STAT_TYPE_NOTIZIA,"content.notizia"=>$id); + if (!is_null($start)){ +// $searchArray["date"]['$gte'] = new MongoDate($start); + $searchArray["date"]['$gte'] = new MongoDB\BSON\UTCDateTime( $start * 1000 ); + + } + if (!is_null($end)){ +// $searchArray["date"]['$lte'] = new MongoDate($end); + $searchArray["date"]['$lte'] = new MongoDB\BSON\UTCDateTime( $end * 1000 ); + } + return GlobalVariables::get("dao")->count("StatisticModel",$searchArray); + } + + + + /** + * La funzione si occupa del salvataggio vero e proprio della impression del banner. + * UTILIZZARE QUESTA FUNZIONE SOLO NELLA PAGINA DI BACKEND (stats/bannerImpression.php). Nelle pagine base utilizzare invece la funzione di log + * @param string $ip + * @param string $content + * @param string $source + */ + public static function saveBannerImpression($ip, $bannerId, $source){ + $tmp = new stdClass(); + + $tmp->user = null; + if (isset($_COOKIE[self::$COOKIE_NAME])){ + $id = $_COOKIE[self::$COOKIE_NAME]; + + $tmp->user = new stdClass(); + $tmp->user->id = $id; + $tmp->user->isNew = false; + if (GlobalVariables::get("dao")->count("StatisticModel",array("user.id"=>$id))==0){ + $tmp->user->isNew = true; + } + } + + $tmp->content = new stdClass(); + $banner = GlobalVariables::get("dao")->getFirst("BannerModel",array("id"=>$bannerId)); + if (!is_null($banner)){ + $tmp->content->banner = $bannerId; + $tmp->content->position = $banner->position; + + $tmp->source = $source; + $tmp->ip = $ip; + $tmp->statType = StatisticModel::STAT_TYPE_BANNER_IMPRESSION; + $stat = new StatisticModel($tmp); + GlobalVariables::get("dao")->save($stat); + } + + } + + /** + * La funzione si occupa di aggiungere una visualizzazione all banner $id + * @param string $id + */ + public static function logBannerImpression($id){ + if (!isset($_COOKIE[self::$COOKIE_NAME])){ + self::prepareStatCookie(); + } + + $ip = $_SERVER['REMOTE_ADDR']; + $source = null; + if (array_key_exists("HTTP_REFERER",$_SERVER)){ + $source = $_SERVER['HTTP_REFERER']; + } + + $function = '$.ajax({url: "stats/bannerImpression.php", method: "POST", data: { "ip": "'.$ip.'", "banner": "'.$id.'", "source": "'.$source.'" }, dataType: "html" });'; + echo ''; + + // OLD VERSION +// $function = '$.ajax({url: "stats/bannerImpression.php", method: "POST", data: { "ip": "'.$ip.'", "banner": "'.$id.'", "source": "'.$source.'" }, dataType: "html" });'; +// GUIHandler::addOnLoadJsEvent($function); + } + + /** + * Restitusce il numero di impressions per una notizia + * @param string $id + */ + public static function countBannerImpressions($id, $start = null, $end = null){ + $searchArray = array("statType"=>StatisticModel::STAT_TYPE_BANNER_IMPRESSION,"content.banner"=>$id); + if (!is_null($start)){ +// $searchArray["date"]['$gte'] = new MongoDate($start); + $searchArray["date"]['$gte'] = new MongoDB\BSON\UTCDateTime($start * 1000); + } + if (!is_null($end)){ +// $searchArray["date"]['$lte'] = new MongoDate($end); + $searchArray["date"]['$lte'] = new MongoDB\BSON\UTCDateTime($end * 1000); + } + return GlobalVariables::get("dao")->count("StatisticModel",$searchArray); + } + + + /** + * La funzione si occupa del salvataggio vero e proprio della impression dell banner. + * UTILIZZARE QUESTA FUNZIONE SOLO NELLA PAGINA DI BACKEND (stats/bannerImpression.php). Nelle pagine base utilizzare invece la funzione di log + * @param string $ip + * @param string $content + * @param string $source + */ + public static function saveBannerClick($bannerId, $source){ + $tmp = new stdClass(); + + $tmp->user = null; + if (isset($_COOKIE[self::$COOKIE_NAME])){ + $id = $_COOKIE[self::$COOKIE_NAME]; + + $tmp->user = new stdClass(); + $tmp->user->id = $id; + $tmp->user->isNew = false; + if (GlobalVariables::get("dao")->count("StatisticModel",array("user.id"=>$id))==0){ + $tmp->user->isNew = true; + } + } + + $tmp->content = new stdClass(); + $banner = GlobalVariables::get("dao")->getFirst("BannerModel",array("id"=>$bannerId)); + if (!is_null($banner)){ + $tmp->content->banner = $bannerId; + $tmp->content->position = $banner->position; + + $tmp->source = $source; + $tmp->ip = $_SERVER['REMOTE_ADDR']; + $tmp->statType = StatisticModel::STAT_TYPE_BANNER_CLICK; + $stat = new StatisticModel($tmp); + GlobalVariables::get("dao")->save($stat); + } + } + + /** + * La funzione si occupa di aggiungere una visualizzazione all banner $id + * @param string $id + */ + public static function getBannerClickLink($banner){ + if (!isset($_COOKIE[self::$COOKIE_NAME])){ + self::prepareStatCookie(); + } + return "bannerClick('".$banner->id."','".$banner->link."');"; + } + + /** + * Restitusce il numero di impressions per una notizia + * @param string $id + */ + public static function countBannerClicks($id, $start = null, $end = null){ + $searchArray = array("statType"=>StatisticModel::STAT_TYPE_BANNER_CLICK,"content.banner"=>$id); + if (!is_null($start)){ +// $searchArray["date"]['$gte'] = new MongoDate($start); + $searchArray["date"]['$gte'] = new MongoDB\BSON\UTCDateTime($start * 1000); + } + if (!is_null($end)){ +// $searchArray["date"]['$lte'] = new MongoDate($end); + $searchArray["date"]['$lte'] = new MongoDB\BSON\UTCDateTime($end * 1000); + } + return GlobalVariables::get("dao")->count("StatisticModel",$searchArray); + } + + + /** + * Restituisce il banner da mostrare tenendo in considerazione quelli giĆ  visualizzati e le visualizzazioni totali + * @param int $position + * @param string $replaced id del banner rimpiazzato + */ + public static function getBannerToShow($position, $replaced = null, $category = BannerModel::GENERAL_CATEGORY){ + // Rimuovi il replaced dalla lista + $shown = array(); + $rval = null; + if (ASDSessionHandler::sessionValueExists("bannerActive")){ + $shown = ASDSessionHandler::getSessionValue("bannerActive"); + + if (!is_null($replaced) && in_array($replaced, $shown)){ + $key = array_search($replaced, $shown); + unset($shown[$key]); + ASDSessionHandler::setSessionValue("bannerActive",$shown); + } + } + + // idlist contiene gli id dei banner in ordine di visualizzazione crescente (esclusi quelli non visualizzati) + $pipeline = PipelineHelper::getTodaysBannerVisualizationPipeline($position); + $stats = GlobalVariables::get("dao")->aggregate("StatisticModel",$pipeline); +// var_dump($stats); + $idList = array(); + if (sizeof($stats)>0){ + foreach ($stats as $stat){ + $idList[] = $stat["_id"]["banner"]; + } + } + +// $baseSearchArray = array("position"=>$position,"end"=>array('$gte'=>new MongoDate(strtotime("tomorrow"))),"categories"=>$category); + $baseSearchArray = array("position"=>$position,"end"=>array('$gte'=>new MongoDB\BSON\UTCDateTime( strtotime("tomorrow") * 1000 ) ),"categories"=>$category); + // Prendi il primo dei "mai mostrati oggi" tra quelli non presenti nella pagina + $todayPlusShown = array_merge($idList,$shown); + if (sizeof($todayPlusShown)>0){ + $tmp = array_map(function ($id) {return new MongoDB\BSON\ObjectID($id);}, $todayPlusShown); + $tmp = array('_id'=>array('$nin'=>$tmp)); + $rval = GlobalVariables::get("dao")->getFirst("BannerModel", array_merge($baseSearchArray, $tmp) ); + } + else { + $rval = GlobalVariables::get("dao")->getFirst("BannerModel", $baseSearchArray); + } + + if (is_null($rval)){ + $todayLessShown = array_values(array_diff($idList, $shown)); + if (sizeof($todayLessShown)>0){ + $i = 0; + while (is_null($rval) && $i$todayLessShown[$i]) ); + $rval = GlobalVariables::get("dao")->getFirst("BannerModel", $condition ); + $i++; + } + } + + } + + if (!is_null($rval)){ + $shown[] = $rval->id; + ASDSessionHandler::setSessionValue("bannerActive",$shown); + } + return $rval; + } + + +} + +?> \ No newline at end of file diff --git a/app/private/lib/PipelineHelper.php b/app/private/lib/PipelineHelper.php index 09856ee..bc0c8ae 100644 --- a/app/private/lib/PipelineHelper.php +++ b/app/private/lib/PipelineHelper.php @@ -5,6 +5,67 @@ Creation Date: 05/feb/2016 */ class PipelineHelper { + + private static function sumFieldFilterDate($filter, $field, $from = null, $to = null) { + if (!is_null($from) || !is_null($to)) { + $filter['time'] = []; + if (!is_null($from)) { + $filter['time'] = [ + '$gte' => new MongoDB\BSON\UTCDateTime($from * 1000) + ]; + } + if (!is_null($to)) { + $filter['time'] = [ + '$lte' => new MongoDB\BSON\UTCDateTime($to * 1000) + ]; + } + } + $rval = [ + [ + '$match' => $filter + ], + [ + '$group' => [ + '_id' => null, + 'tot' => ['$sum' => '$'.$field] + ] + ] + ]; + return $rval; + } + + public static function getNotiziaImpressions($id, $from = null, $to = null){ + return self::sumFieldFilterDate( + [ + 'notizia' => $id + ], + 'views', + $from, + $to + ); + } + + public static function getBannerImpressions($id, $from = null, $to = null){ + return self::sumFieldFilterDate( + [ + 'banner' => $id + ], + 'views', + $from, + $to + ); + } + + public static function getBannerClicks($id, $from = null, $to = null){ + return self::sumFieldFilterDate( + [ + 'banner' => $id + ], + 'clicks', + $from, + $to + ); + } public static function getDailyVisualizationPipeline($startDate){ $rval = array( @@ -217,7 +278,7 @@ class PipelineHelper { 'statType'=>StatisticModel::STAT_TYPE_BANNER_IMPRESSION, 'content.position'=>$position, // 'date'=>array( '$gte'=>new MongoDate(strtotime("today")) ) - 'date'=>array( '$gte'=>new MongoDB\BSON\UTCDateTime(strtotime("today") * 1000) ) + 'date'=>array( '$gte'=>new MongoDB\BSON\UTCDateTime(strtotime("today CET") * 1000) ) ) ), array( diff --git a/app/private/lib/dao/GenericDao.php b/app/private/lib/dao/GenericDao.php index bbebc8b..85db506 100644 --- a/app/private/lib/dao/GenericDao.php +++ b/app/private/lib/dao/GenericDao.php @@ -462,6 +462,32 @@ class GenericDao{ return $cursor->count(true); } + + + public function rawUpsert($modelClass, $query, $update) { + if (!is_null($modelClass)){ + if (!is_subclass_of($modelClass, "DaoSaveable", true)){ + throw new CoreException("Invalid return class passed to GenericDao::query ($modelClass)"); + } + } + if (!array_key_exists('deleted', $query)) { + $query['deleted'] = false; + } + + $collectionName = $modelClass::getCollectionName(); + + $command = new \MongoDB\Driver\Command([ + 'findAndModify' => $collectionName, + 'query' => $query, + 'update' => $update, + 'upsert' => true, + 'new' => false + ]); + + $this->client->executeCommand( $this->dbName, $command ); + } + + } ?> \ No newline at end of file diff --git a/app/private/lib/dao/lib.inclusion.php b/app/private/lib/dao/lib.inclusion.php index 854cc9e..a37a644 100644 --- a/app/private/lib/dao/lib.inclusion.php +++ b/app/private/lib/dao/lib.inclusion.php @@ -41,6 +41,9 @@ require_once(dirname(__FILE__)."/models/CBSQueueModel.php"); require_once(dirname(__FILE__)."/models/PasswordRecoveryRequestModel.php"); +require_once(dirname(__FILE__)."/models/stats/ArticleStatsModel.php"); +require_once(dirname(__FILE__)."/models/stats/BannerStatsModel.php"); +require_once(dirname(__FILE__)."/models/stats/PageViewStatsModel.php"); $dao = new GenericDao($database->dbName,$database->connectionString); GlobalVariables::set("dao", $dao); diff --git a/app/private/lib/dao/models/stats/ArticleStatsModel.php b/app/private/lib/dao/models/stats/ArticleStatsModel.php new file mode 100644 index 0000000..8ccc745 --- /dev/null +++ b/app/private/lib/dao/models/stats/ArticleStatsModel.php @@ -0,0 +1,43 @@ + $articleId, + 'y' => intval($year), + 'm' => intval($month), + 'd' => intval($day), + 'views' => 0 + ] + ); + } + + public static function getFilter($articleId, $day, $month, $year) { + $time = strtotime("$year-$month-$day CET"); + return [ + 'notizia' => $articleId, + 'time' => new MongoDB\BSON\UTCDateTime( $time * 1000 ), + 'y' => intval($year), + 'm' => intval($month), + 'd' => intval($day) + ]; + } + +} + +?> \ No newline at end of file diff --git a/app/private/lib/dao/models/stats/BannerStatsModel.php b/app/private/lib/dao/models/stats/BannerStatsModel.php new file mode 100644 index 0000000..87fb8f6 --- /dev/null +++ b/app/private/lib/dao/models/stats/BannerStatsModel.php @@ -0,0 +1,45 @@ + $bannerId, + 'time' => new MongoDB\BSON\UTCDateTime( $time * 1000 ), + 'y' => intval($year), + 'm' => intval($month), + 'd' => intval($day), + 'views' => 0, + 'clicks' => 0 + ] + ); + } + public static function getFilter($bannerId, $day, $month, $year) { + $time = strtotime("$year-$month-$day CET"); + return [ + 'banner' => $bannerId, + 'time' => new MongoDB\BSON\UTCDateTime( $time * 1000 ), + 'y' => intval($year), + 'm' => intval($month), + 'd' => intval($day) + ]; + } + +} + +?> \ No newline at end of file diff --git a/app/private/lib/dao/models/stats/PageViewStatsModel.php b/app/private/lib/dao/models/stats/PageViewStatsModel.php new file mode 100644 index 0000000..34a9716 --- /dev/null +++ b/app/private/lib/dao/models/stats/PageViewStatsModel.php @@ -0,0 +1,31 @@ + $page, + 'time' => new MongoDB\BSON\UTCDateTime( $time * 1000 ), + 'y' => intval($year), + 'm' => intval($month), + 'd' => intval($day) + ]; + } + +} + +?> \ No newline at end of file diff --git a/app/private/lib/gui/GUIHandler.php b/app/private/lib/gui/GUIHandler.php index 82332d5..be0094a 100644 --- a/app/private/lib/gui/GUIHandler.php +++ b/app/private/lib/gui/GUIHandler.php @@ -393,7 +393,7 @@ class GUIHandler { // $lastsNews = array_merge($lastsNews,$add); // } - $stats = GlobalVariables::get("dao")->aggregate("StatisticModel",PipelineHelper::getMostViewedOfLastDays(strtotime("-7 days"),5)); + $stats = GlobalVariables::get("dao")->aggregate("StatisticModel",PipelineHelper::getMostViewedOfLastDays(strtotime("-7 days CET"),5)); $idArray = array_map(function ($ele){ return new MongoDB\BSON\ObjectID($ele["_id"]["notizia"]);}, $stats); // $lastsNews = GlobalVariables::get("dao")->query("NotiziaModel", diff --git a/app/public/admin/homeBanner.php b/app/public/admin/homeBanner.php index a4b787c..7c2fc6b 100644 --- a/app/public/admin/homeBanner.php +++ b/app/public/admin/homeBanner.php @@ -72,13 +72,13 @@ try{
Oggi
-
Visualizzazioni: id,strtotime("today"));?>
-
Click: id,strtotime("today"));?>
+
Visualizzazioni: id,strtotime("today CET"));?>
+
Click: id,strtotime("today CET"));?>
Ultima settimana
-
Visualizzazioni: id,strtotime("-7 days"));?>
-
Click: id,strtotime("-7 days"));?>
+
Visualizzazioni: id,strtotime("-7 days CET"));?>
+
Click: id,strtotime("-7 days CET"));?>
Totali
diff --git a/app/public/admin/homeNotizie.php b/app/public/admin/homeNotizie.php index f1b9b5a..8e76cff 100644 --- a/app/public/admin/homeNotizie.php +++ b/app/public/admin/homeNotizie.php @@ -144,11 +144,11 @@ try{
Oggi
-
id,strtotime("today"));?>
+
id,strtotime("today CET"));?>
Ultima settimana
-
id,strtotime("-7 days"));?>
+
id,strtotime("-7 days CET"));?>
Totali
diff --git a/app/public/admin/index.php b/app/public/admin/index.php index 5eddf6a..7f12e16 100644 --- a/app/public/admin/index.php +++ b/app/public/admin/index.php @@ -15,7 +15,6 @@ if (!AdminLoginManager::isLogged()){ MenuGenerator::generateMenu(); HTMLHelper::generateAdminHead(); - ?>
Benvenuto! diff --git a/app/public/mobile/stats/bannerClick.php b/app/public/mobile/stats/bannerClick.php index 8d6d619..5c3974d 100644 --- a/app/public/mobile/stats/bannerClick.php +++ b/app/public/mobile/stats/bannerClick.php @@ -10,7 +10,7 @@ try { if (array_key_exists("HTTP_REFERER",$_SERVER)){ $host = parse_url($_SERVER['HTTP_REFERER'], PHP_URL_HOST); - if (strcmp($host,$_SERVER["HTTP_HOST"])==0){ + if (strcmp($host,$_SERVER["SERVER_NAME"])==0){ $banner = PostHandler::get("banner"); $source = PostHandler::get("source"); if (!is_null($banner) && !is_null($source)){ diff --git a/app/public/mobile/stats/bannerImpression.php b/app/public/mobile/stats/bannerImpression.php index 8cb2bd5..b98bc0d 100644 --- a/app/public/mobile/stats/bannerImpression.php +++ b/app/public/mobile/stats/bannerImpression.php @@ -10,7 +10,7 @@ try { if (array_key_exists("HTTP_REFERER",$_SERVER)){ $host = parse_url($_SERVER['HTTP_REFERER'], PHP_URL_HOST); - if (strcmp($host,$_SERVER["HTTP_HOST"])==0){ + if (strcmp($host,$_SERVER["SERVER_NAME"])==0){ $ip = PostHandler::get("ip"); $banner = PostHandler::get("banner"); $source = PostHandler::get("source"); diff --git a/app/public/mobile/stats/notiziaImpression.php b/app/public/mobile/stats/notiziaImpression.php index 207a8ca..8e8568c 100644 --- a/app/public/mobile/stats/notiziaImpression.php +++ b/app/public/mobile/stats/notiziaImpression.php @@ -10,7 +10,7 @@ try { if (array_key_exists("HTTP_REFERER",$_SERVER)){ $host = parse_url($_SERVER['HTTP_REFERER'], PHP_URL_HOST); - if (strcmp($host,$_SERVER["HTTP_HOST"])==0){ + if (strcmp($host,$_SERVER["SERVER_NAME"])==0){ $ip = PostHandler::get("ip"); $notizia = PostHandler::get("notizia"); $source = PostHandler::get("source"); diff --git a/app/public/mobile/stats/pageImpression.php b/app/public/mobile/stats/pageImpression.php index e5c63ac..6abfbcb 100644 --- a/app/public/mobile/stats/pageImpression.php +++ b/app/public/mobile/stats/pageImpression.php @@ -10,7 +10,7 @@ try { if (array_key_exists("HTTP_REFERER",$_SERVER)){ $host = parse_url($_SERVER['HTTP_REFERER'], PHP_URL_HOST); - if (strcmp($host,$_SERVER["HTTP_HOST"])==0){ + if (strcmp($host,$_SERVER["SERVER_NAME"])==0){ $ip = PostHandler::get("ip"); $content = PostHandler::get("content"); $source = PostHandler::get("source"); diff --git a/app/public/stats/bannerClick.php b/app/public/stats/bannerClick.php index eb25522..8fcb781 100644 --- a/app/public/stats/bannerClick.php +++ b/app/public/stats/bannerClick.php @@ -10,7 +10,7 @@ try { if (array_key_exists("HTTP_REFERER",$_SERVER)){ $host = parse_url($_SERVER['HTTP_REFERER'], PHP_URL_HOST); - if (strcmp($host,$_SERVER["HTTP_HOST"])==0){ + if (strcmp($host,$_SERVER["SERVER_NAME"])==0){ $banner = PostHandler::get("banner"); $source = PostHandler::get("source"); if (!is_null($banner) && !is_null($source)){ diff --git a/app/public/stats/bannerImpression.php b/app/public/stats/bannerImpression.php index 3aad251..dc6df06 100644 --- a/app/public/stats/bannerImpression.php +++ b/app/public/stats/bannerImpression.php @@ -10,7 +10,7 @@ try { if (array_key_exists("HTTP_REFERER",$_SERVER)){ $host = parse_url($_SERVER['HTTP_REFERER'], PHP_URL_HOST); - if (strcmp($host,$_SERVER["HTTP_HOST"])==0){ + if (strcmp($host,$_SERVER["SERVER_NAME"])==0){ $ip = PostHandler::get("ip"); $banner = PostHandler::get("banner"); $source = PostHandler::get("source"); diff --git a/app/public/stats/notiziaImpression.php b/app/public/stats/notiziaImpression.php index 8860700..09eccc6 100644 --- a/app/public/stats/notiziaImpression.php +++ b/app/public/stats/notiziaImpression.php @@ -10,7 +10,7 @@ try { if (array_key_exists("HTTP_REFERER",$_SERVER)){ $host = parse_url($_SERVER['HTTP_REFERER'], PHP_URL_HOST); - if (strcmp($host,$_SERVER["HTTP_HOST"])==0){ + if (strcmp($host,$_SERVER["SERVER_NAME"])==0){ $ip = PostHandler::get("ip"); $notizia = PostHandler::get("notizia"); $source = PostHandler::get("source"); diff --git a/app/public/stats/pageImpression.php b/app/public/stats/pageImpression.php index 0aa3033..5e34471 100644 --- a/app/public/stats/pageImpression.php +++ b/app/public/stats/pageImpression.php @@ -10,7 +10,7 @@ try { if (array_key_exists("HTTP_REFERER",$_SERVER)){ $host = parse_url($_SERVER['HTTP_REFERER'], PHP_URL_HOST); - if (strcmp($host,$_SERVER["HTTP_HOST"])==0){ + if (strcmp($host,$_SERVER["SERVER_NAME"])==0){ $ip = PostHandler::get("ip"); $content = PostHandler::get("content"); $source = PostHandler::get("source");