Newer
Older
* Class AnalyticsPodcastByEpisodeModel
* Model for analytics_podcasts_by_episodes table in database
* @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;
class AnalyticsPodcastByEpisodeModel extends Model
{
protected $table = 'analytics_podcasts_by_episode';
protected $allowedFields = [];
protected $returnType = \App\Entities\AnalyticsPodcastsByEpisode::class;
protected $useSoftDeletes = false;
protected $useTimestamps = false;
/**
* @param int $podcastId, $episodeId
*
* @return array
*/
public function getDataByDay(int $podcastId, int $episodeId = null): array
{
if (!$episodeId) {
if (
!($found = cache(
"{$podcastId}_analytics_podcast_by_episode_by_day"
))
) {
$lastEpisodes = (new EpisodeModel())

Benjamin Bellamy
committed
->select('`id`, `season_number`, `number`, `title`')
->orderBy('`id`', 'DESC')
->where(['`podcast_id`' => $podcastId])

Benjamin Bellamy
committed
$found = $this->select('`age` AS `X`');
$letter = 97;
foreach ($lastEpisodes as $episode) {
$found = $found
->selectSum(
'(CASE WHEN `episode_id`=' .
$episode->id .
' THEN `hits` END)',

Benjamin Bellamy
committed
'`' . chr($letter) . 'Y`'
)
->select(
'"' .
(empty($episode->season_number)
? ''
: $episode->season_number) .
(empty($episode->number)
? ''
: '-' . $episode->number . '/ ') .
$episode->title .

Benjamin Bellamy
committed
'" AS `' .

Benjamin Bellamy
committed
'Value`'
);
$letter++;
}
$found = $found
->where([

Benjamin Bellamy
committed
'`podcast_id`' => $podcastId,
'`age` <' => 60,

Benjamin Bellamy
committed
->groupBy('`X`')
->orderBy('`X`', 'ASC')
->findAll();
cache()->save(
"{$podcastId}_analytics_podcast_by_episode_by_day",
$found,
);
}
return $found;
} else {
if (
!($found = cache(
"{$podcastId}_{$episodeId}_analytics_podcast_by_episode_by_day"
))
) {

Benjamin Bellamy
committed
$found = $this->select('`date as `labels`')
->selectSum('`hits`', '`values`')

Benjamin Bellamy
committed
'`episode_id`' => $episodeId,
'`podcast_id`' => $podcastId,
'`age` <' => 60,

Benjamin Bellamy
committed
->groupBy('`labels`')
->orderBy('`labels`', 'ASC')
->findAll();
cache()->save(
"{$podcastId}_{$episodeId}_analytics_podcast_by_episode_by_day",
$found,
);
}
return $found;
}
}

Benjamin Bellamy
committed
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/**
* @param int $podcastId, $episodeId
*
* @return array
*/
public function getDataByMonth(int $podcastId, int $episodeId = null): array
{
if (
!($found = cache(
"{$podcastId}_{$episodeId}_analytics_podcast_by_episode_by_month"
))
) {
$found = $this->select('DATE_FORMAT(`date`,"%Y-%m-01") as `labels`')
->selectSum('`hits`', '`values`')
->where([
'episode_id' => $episodeId,
'podcast_id' => $podcastId,
])
->groupBy('`labels`')
->orderBy('`labels`', 'ASC')
->findAll();
cache()->save(
"{$podcastId}_{$episodeId}_analytics_podcast_by_episode_by_month",
$found,
600
);
}
return $found;
}