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-08 14:45:46 +00:00
|
|
|
public function getData(int $podcastId): array
|
2020-10-06 15:39:27 +00:00
|
|
|
{
|
2020-10-08 14:45:46 +00:00
|
|
|
if (!($found = cache("{$podcastId}_analytics_podcast_by_country"))) {
|
|
|
|
$found = $this->select('`country_code` as `labels`')
|
2020-10-06 15:39:27 +00:00
|
|
|
->selectSum('`hits`', '`values`')
|
2020-10-08 14:45:46 +00:00
|
|
|
->groupBy('`country_code`')
|
2020-10-06 15:39:27 +00:00
|
|
|
->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 14:45:46 +00:00
|
|
|
->orderBy('`labels`', 'ASC')
|
2020-10-06 15:39:27 +00:00
|
|
|
->findAll();
|
|
|
|
|
|
|
|
cache()->save(
|
2020-10-08 14:45:46 +00:00
|
|
|
"{$podcastId}_analytics_podcast_by_country",
|
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;
|
|
|
|
}
|
|
|
|
}
|