2020-10-06 15:39:27 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
2020-10-08 14:45:46 +00:00
|
|
|
* Class AnalyticsPodcastByCountryModel
|
|
|
|
* Model for analytics_podcasts_by_country table in database
|
2020-10-06 15:39:27 +00:00
|
|
|
* @copyright 2020 Podlibre
|
|
|
|
* @license https://www.gnu.org/licenses/agpl-3.0.en.html AGPL3
|
|
|
|
* @link https://castopod.org/
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace App\Models;
|
|
|
|
|
|
|
|
use CodeIgniter\Model;
|
|
|
|
|
2020-10-08 14:45:46 +00:00
|
|
|
class AnalyticsPodcastByCountryModel extends Model
|
2020-10-06 15:39:27 +00:00
|
|
|
{
|
2020-10-08 14:45:46 +00:00
|
|
|
protected $table = 'analytics_podcasts_by_country';
|
2020-10-06 15:39:27 +00:00
|
|
|
|
|
|
|
protected $allowedFields = [];
|
|
|
|
|
2020-10-08 14:45:46 +00:00
|
|
|
protected $returnType = \App\Entities\AnalyticsPodcastsByCountry::class;
|
2020-10-06 15:39:27 +00:00
|
|
|
protected $useSoftDeletes = false;
|
|
|
|
|
|
|
|
protected $useTimestamps = false;
|
|
|
|
|
|
|
|
/**
|
2020-10-08 14:45:46 +00:00
|
|
|
* Gets country data for a podcast
|
2020-10-06 15:39:27 +00:00
|
|
|
*
|
|
|
|
* @param int $podcastId
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
2020-10-14 10:38:48 +00:00
|
|
|
public function getDataWeekly(int $podcastId): array
|
2020-10-06 15:39:27 +00:00
|
|
|
{
|
2020-10-14 10:38:48 +00:00
|
|
|
$locale = service('request')->getLocale();
|
|
|
|
if (
|
|
|
|
!($found = cache(
|
|
|
|
"{$podcastId}_analytics_podcast_by_country_weekly_{$locale}"
|
|
|
|
))
|
|
|
|
) {
|
2020-10-08 14:45:46 +00:00
|
|
|
$found = $this->select('`country_code` as `labels`')
|
2020-10-06 15:39:27 +00:00
|
|
|
->selectSum('`hits`', '`values`')
|
|
|
|
->where([
|
|
|
|
'`podcast_id`' => $podcastId,
|
2020-10-08 14:45:46 +00:00
|
|
|
'`date` >' => date('Y-m-d', strtotime('-1 week')),
|
2020-10-06 15:39:27 +00:00
|
|
|
])
|
2020-10-08 19:42:57 +02:00
|
|
|
->groupBy('`labels`')
|
2020-10-08 18:42:00 +02:00
|
|
|
->orderBy('`values`', 'DESC')
|
2020-10-08 19:42:57 +02:00
|
|
|
->findAll(10);
|
2020-10-06 15:39:27 +00:00
|
|
|
|
|
|
|
cache()->save(
|
2020-10-14 10:38:48 +00:00
|
|
|
"{$podcastId}_analytics_podcast_by_country_weekly_{$locale}",
|
|
|
|
$found,
|
|
|
|
600
|
|
|
|
);
|
|
|
|
}
|
|
|
|
return $found;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets country data for a podcast
|
|
|
|
*
|
|
|
|
* @param int $podcastId
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function getDataYearly(int $podcastId): array
|
|
|
|
{
|
|
|
|
$locale = service('request')->getLocale();
|
|
|
|
if (
|
|
|
|
!($found = cache(
|
|
|
|
"{$podcastId}_analytics_podcast_by_country_yearly_{$locale}"
|
|
|
|
))
|
|
|
|
) {
|
|
|
|
$found = $this->select('`country_code` as `labels`')
|
|
|
|
->selectSum('`hits`', '`values`')
|
|
|
|
->where([
|
|
|
|
'`podcast_id`' => $podcastId,
|
|
|
|
'`date` >' => date('Y-m-d', strtotime('-1 year')),
|
|
|
|
])
|
|
|
|
->groupBy('`labels`')
|
|
|
|
->orderBy('`values`', 'DESC')
|
|
|
|
->findAll(10);
|
|
|
|
|
|
|
|
cache()->save(
|
|
|
|
"{$podcastId}_analytics_podcast_by_country_yearly_{$locale}",
|
2020-10-06 15:39:27 +00:00
|
|
|
$found,
|
2020-10-08 14:45:46 +00:00
|
|
|
600
|
2020-10-06 15:39:27 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
return $found;
|
|
|
|
}
|
|
|
|
}
|