2020-10-06 15:39:27 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
2021-04-14 15:58:40 +00:00
|
|
|
* Class AnalyticsPodcastByEpisodeModel
|
|
|
|
* Model for analytics_podcasts_by_episodes 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/
|
|
|
|
*/
|
|
|
|
|
2021-04-14 15:58:40 +00:00
|
|
|
namespace Analytics\Models;
|
2020-10-06 15:39:27 +00:00
|
|
|
|
|
|
|
use CodeIgniter\Model;
|
|
|
|
|
2021-04-14 15:58:40 +00:00
|
|
|
class AnalyticsPodcastByEpisodeModel extends Model
|
2020-10-06 15:39:27 +00:00
|
|
|
{
|
2021-04-14 15:58:40 +00:00
|
|
|
protected $table = 'analytics_podcasts_by_episode';
|
2020-10-06 15:39:27 +00:00
|
|
|
|
|
|
|
protected $allowedFields = [];
|
|
|
|
|
2021-04-14 15:58:40 +00:00
|
|
|
protected $returnType = \Analytics\Entities\AnalyticsPodcastsByEpisode::class;
|
2020-10-06 15:39:27 +00:00
|
|
|
protected $useSoftDeletes = false;
|
|
|
|
|
|
|
|
protected $useTimestamps = false;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param int $podcastId
|
2021-04-14 15:58:40 +00:00
|
|
|
* @param int $episodeId
|
2020-10-06 15:39:27 +00:00
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
2021-04-14 15:58:40 +00:00
|
|
|
public function getDataByDay(int $podcastId, int $episodeId): array
|
2020-10-06 15:39:27 +00:00
|
|
|
{
|
2020-10-14 10:38:48 +00:00
|
|
|
if (
|
|
|
|
!($found = cache(
|
2021-04-14 15:58:40 +00:00
|
|
|
"{$podcastId}_{$episodeId}_analytics_podcast_by_episode_by_day",
|
2020-10-14 10:38:48 +00:00
|
|
|
))
|
|
|
|
) {
|
2021-04-14 15:58:40 +00:00
|
|
|
$found = $this->select('date as labels')
|
2021-04-02 17:20:02 +00:00
|
|
|
->selectSum('hits', 'values')
|
2020-10-06 15:39:27 +00:00
|
|
|
->where([
|
2021-04-14 15:58:40 +00:00
|
|
|
'episode_id' => $episodeId,
|
2021-04-02 17:20:02 +00:00
|
|
|
'podcast_id' => $podcastId,
|
2021-04-14 15:58:40 +00:00
|
|
|
'age <' => 60,
|
2020-10-06 15:39:27 +00:00
|
|
|
])
|
2021-04-02 17:20:02 +00:00
|
|
|
->groupBy('labels')
|
2021-04-14 15:58:40 +00:00
|
|
|
->orderBy('labels', 'ASC')
|
2020-11-03 14:14:30 +00:00
|
|
|
->findAll();
|
2020-10-06 15:39:27 +00:00
|
|
|
|
|
|
|
cache()->save(
|
2021-04-14 15:58:40 +00:00
|
|
|
"{$podcastId}_{$episodeId}_analytics_podcast_by_episode_by_day",
|
2020-10-14 10:38:48 +00:00
|
|
|
$found,
|
2021-04-02 17:20:02 +00:00
|
|
|
600,
|
2020-10-14 10:38:48 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
return $found;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param int $podcastId
|
2021-04-14 15:58:40 +00:00
|
|
|
* @param int $episodeId
|
2020-10-14 10:38:48 +00:00
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
2021-04-14 15:58:40 +00:00
|
|
|
public function getDataByMonth(int $podcastId, int $episodeId = null): array
|
2020-10-14 10:38:48 +00:00
|
|
|
{
|
|
|
|
if (
|
|
|
|
!($found = cache(
|
2021-04-14 15:58:40 +00:00
|
|
|
"{$podcastId}_{$episodeId}_analytics_podcast_by_episode_by_month",
|
2020-10-14 10:38:48 +00:00
|
|
|
))
|
|
|
|
) {
|
2021-04-14 15:58:40 +00:00
|
|
|
$found = $this->select('DATE_FORMAT(date,"%Y-%m-01") as labels')
|
2021-04-02 17:20:02 +00:00
|
|
|
->selectSum('hits', 'values')
|
2020-10-14 10:38:48 +00:00
|
|
|
->where([
|
2021-04-14 15:58:40 +00:00
|
|
|
'episode_id' => $episodeId,
|
2021-04-02 17:20:02 +00:00
|
|
|
'podcast_id' => $podcastId,
|
2020-10-14 10:38:48 +00:00
|
|
|
])
|
2021-04-02 17:20:02 +00:00
|
|
|
->groupBy('labels')
|
2021-04-14 15:58:40 +00:00
|
|
|
->orderBy('labels', 'ASC')
|
2020-11-03 14:14:30 +00:00
|
|
|
->findAll();
|
2020-11-02 18:15:19 +00:00
|
|
|
|
2020-10-14 10:38:48 +00:00
|
|
|
cache()->save(
|
2021-04-14 15:58:40 +00:00
|
|
|
"{$podcastId}_{$episodeId}_analytics_podcast_by_episode_by_month",
|
2020-10-06 15:39:27 +00:00
|
|
|
$found,
|
2021-04-02 17:20:02 +00:00
|
|
|
600,
|
2020-10-06 15:39:27 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
return $found;
|
|
|
|
}
|
|
|
|
}
|