<?php
function analitics_get_by_site_action($params) {
$site = $params["site"];
$before = date("Y-m-d H:i:s", time() - intval($params["period"]) * 24 * 3600);
$site = ($site == "*") ? "%%" : $site;
$execute_args = [
":site" => $site,
":before" => $before
];
$query = db() -> prepare("SELECT COUNT(*) FROM `adm_logs` WHERE `site` LIKE :site AND `create_at`>:before");
$query -> execute($execute_args);
$result = $query -> fetch(PDO::FETCH_ASSOC);
$total_dialogs = $result["COUNT(*)"];
$select_ratings = [-1, 1];
$ratings = ["1" => 0, "-1" => 0];
foreach($select_ratings as $rating) {
$query = db() -> prepare("SELECT COUNT(*) FROM `adm_logs` WHERE `site` LIKE :site AND `create_at`>:before AND `rating`=:rating");
$query -> execute(array_merge($execute_args, [":rating" => $rating]));
$result = $query -> fetch(PDO::FETCH_ASSOC);
$ratings[$rating] = $result["COUNT(*)"];
}
$final_rating = ($ratings["1"] + $ratings["-1"])
? round($ratings["1"] / ($ratings["1"] + $ratings["-1"]) * 100) / 10
: 0;
echo json_encode([
"status" => true,
"site" => $site == "%%" ? "Всі разом" : $site,
"total_dialogs" => $total_dialogs,
"total_likes" => $ratings["1"],
"total_dislikes" => $ratings["-1"],
"final_rating" => $final_rating
]);
}