2020-10-08 14:45:46 +00:00
|
|
|
<?php
|
|
|
|
|
2021-06-08 09:52:11 +00:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
2020-10-08 14:45:46 +00:00
|
|
|
/**
|
2021-05-19 16:35:13 +00:00
|
|
|
* Class AnalyticsPodcastByRegionModel Model for analytics_podcasts_by_region table in database
|
|
|
|
*
|
2020-10-08 14:45:46 +00:00
|
|
|
* @copyright 2020 Podlibre
|
|
|
|
* @license https://www.gnu.org/licenses/agpl-3.0.en.html AGPL3
|
|
|
|
* @link https://castopod.org/
|
|
|
|
*/
|
|
|
|
|
2021-04-14 15:58:40 +00:00
|
|
|
namespace Analytics\Models;
|
2020-10-08 14:45:46 +00:00
|
|
|
|
2021-05-06 14:00:48 +00:00
|
|
|
use Analytics\Entities\AnalyticsPodcastsByRegion;
|
2020-10-08 14:45:46 +00:00
|
|
|
use CodeIgniter\Model;
|
|
|
|
|
|
|
|
class AnalyticsPodcastByRegionModel extends Model
|
|
|
|
{
|
2021-05-06 14:00:48 +00:00
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
2020-10-08 14:45:46 +00:00
|
|
|
protected $table = 'analytics_podcasts_by_region';
|
|
|
|
|
2021-05-06 14:00:48 +00:00
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $returnType = AnalyticsPodcastsByRegion::class;
|
2021-05-19 16:35:13 +00:00
|
|
|
|
2021-05-06 14:00:48 +00:00
|
|
|
/**
|
|
|
|
* @var bool
|
|
|
|
*/
|
2020-10-08 14:45:46 +00:00
|
|
|
protected $useSoftDeletes = false;
|
|
|
|
|
2021-05-06 14:00:48 +00:00
|
|
|
/**
|
|
|
|
* @var bool
|
|
|
|
*/
|
2020-10-08 14:45:46 +00:00
|
|
|
protected $useTimestamps = false;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets region data for a podcast
|
|
|
|
*
|
2021-05-06 14:00:48 +00:00
|
|
|
* @return AnalyticsPodcastsByRegion[]
|
2020-10-08 14:45:46 +00:00
|
|
|
*/
|
|
|
|
public function getData(int $podcastId): array
|
|
|
|
{
|
2021-05-19 16:35:13 +00:00
|
|
|
$locale = service('request')
|
|
|
|
->getLocale();
|
2020-10-14 10:38:48 +00:00
|
|
|
if (
|
2021-06-09 12:40:22 +00:00
|
|
|
! ($found = cache("{$podcastId}_analytics_podcast_by_region_{$locale}"))
|
2020-10-14 10:38:48 +00:00
|
|
|
) {
|
2021-04-02 17:20:02 +00:00
|
|
|
$found = $this->select('country_code, region_code')
|
|
|
|
->selectSum('hits', 'value')
|
|
|
|
->selectAvg('latitude')
|
|
|
|
->selectAvg('longitude')
|
|
|
|
->groupBy('country_code, region_code')
|
2020-10-08 14:45:46 +00:00
|
|
|
->where([
|
2021-04-02 17:20:02 +00:00
|
|
|
'podcast_id' => $podcastId,
|
|
|
|
'date >' => date('Y-m-d', strtotime('-1 week')),
|
2020-10-08 14:45:46 +00:00
|
|
|
])
|
2021-04-02 17:20:02 +00:00
|
|
|
->orderBy('value', 'DESC')
|
2020-10-08 14:45:46 +00:00
|
|
|
->findAll();
|
|
|
|
|
2021-05-19 16:35:13 +00:00
|
|
|
cache()
|
2021-06-08 09:52:11 +00:00
|
|
|
->save("{$podcastId}_analytics_podcast_by_region_{$locale}", $found, 600);
|
2020-10-08 14:45:46 +00:00
|
|
|
}
|
|
|
|
return $found;
|
|
|
|
}
|
|
|
|
}
|