2020-10-06 15:39:27 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
2020-10-08 14:45:46 +00:00
|
|
|
* Class AnalyticsPodcastByEpisodeModel
|
2020-10-06 15:39:27 +00:00
|
|
|
* 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;
|
|
|
|
|
2020-10-08 14:45:46 +00:00
|
|
|
class AnalyticsPodcastByEpisodeModel extends Model
|
2020-10-06 15:39:27 +00:00
|
|
|
{
|
|
|
|
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())
|
2020-10-14 10:38:48 +00:00
|
|
|
->select('`id`, `season_number`, `number`, `title`')
|
|
|
|
->orderBy('`id`', 'DESC')
|
|
|
|
->where(['`podcast_id`' => $podcastId])
|
2020-10-06 15:39:27 +00:00
|
|
|
->findAll(5);
|
|
|
|
|
2020-10-14 10:38:48 +00:00
|
|
|
$found = $this->select('`age` AS `X`');
|
2020-10-06 15:39:27 +00:00
|
|
|
|
|
|
|
$letter = 97;
|
|
|
|
foreach ($lastEpisodes as $episode) {
|
|
|
|
$found = $found
|
|
|
|
->selectSum(
|
|
|
|
'(CASE WHEN `episode_id`=' .
|
|
|
|
$episode->id .
|
|
|
|
' THEN `hits` END)',
|
2020-10-14 10:38:48 +00:00
|
|
|
'`' . chr($letter) . 'Y`'
|
2020-10-06 15:39:27 +00:00
|
|
|
)
|
|
|
|
->select(
|
|
|
|
'"' .
|
|
|
|
(empty($episode->season_number)
|
|
|
|
? ''
|
|
|
|
: $episode->season_number) .
|
|
|
|
(empty($episode->number)
|
|
|
|
? ''
|
|
|
|
: '-' . $episode->number . '/ ') .
|
|
|
|
$episode->title .
|
2020-10-14 10:38:48 +00:00
|
|
|
'" AS `' .
|
2020-10-06 15:39:27 +00:00
|
|
|
chr($letter) .
|
2020-10-14 10:38:48 +00:00
|
|
|
'Value`'
|
2020-10-06 15:39:27 +00:00
|
|
|
);
|
|
|
|
$letter++;
|
|
|
|
}
|
|
|
|
|
|
|
|
$found = $found
|
|
|
|
->where([
|
2020-10-14 10:38:48 +00:00
|
|
|
'`podcast_id`' => $podcastId,
|
|
|
|
'`age` <' => 60,
|
2020-10-06 15:39:27 +00:00
|
|
|
])
|
2020-10-14 10:38:48 +00:00
|
|
|
->groupBy('`X`')
|
|
|
|
->orderBy('`X`', 'ASC')
|
2020-10-06 15:39:27 +00:00
|
|
|
->findAll();
|
|
|
|
|
|
|
|
cache()->save(
|
|
|
|
"{$podcastId}_analytics_podcast_by_episode_by_day",
|
|
|
|
$found,
|
2020-10-08 14:45:46 +00:00
|
|
|
600
|
2020-10-06 15:39:27 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
return $found;
|
|
|
|
} else {
|
|
|
|
if (
|
|
|
|
!($found = cache(
|
|
|
|
"{$podcastId}_{$episodeId}_analytics_podcast_by_episode_by_day"
|
|
|
|
))
|
|
|
|
) {
|
2020-10-14 10:38:48 +00:00
|
|
|
$found = $this->select('`date as `labels`')
|
|
|
|
->selectSum('`hits`', '`values`')
|
2020-10-06 15:39:27 +00:00
|
|
|
->where([
|
2020-10-14 10:38:48 +00:00
|
|
|
'`episode_id`' => $episodeId,
|
|
|
|
'`podcast_id`' => $podcastId,
|
|
|
|
'`age` <' => 60,
|
2020-10-06 15:39:27 +00:00
|
|
|
])
|
2020-10-14 10:38:48 +00:00
|
|
|
->groupBy('`labels`')
|
|
|
|
->orderBy('`labels`', 'ASC')
|
2020-10-06 15:39:27 +00:00
|
|
|
->findAll();
|
|
|
|
|
|
|
|
cache()->save(
|
|
|
|
"{$podcastId}_{$episodeId}_analytics_podcast_by_episode_by_day",
|
|
|
|
$found,
|
2020-10-08 14:45:46 +00:00
|
|
|
600
|
2020-10-06 15:39:27 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
return $found;
|
|
|
|
}
|
|
|
|
}
|
2020-10-14 10:38:48 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @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;
|
|
|
|
}
|
2020-10-06 15:39:27 +00:00
|
|
|
}
|