From 1670558473dba47219d470ff21d6224db6ab42ba Mon Sep 17 00:00:00 2001
From: Yassine Doghri <yassine@doghri.fr>
Date: Thu, 6 Jan 2022 16:52:13 +0000
Subject: [PATCH] feat: add basic stats on podcast about page

number of seasons and episodes + publication date of the first episode
---
 app/Controllers/PodcastController.php |  3 +++
 app/Language/en/Podcast.php           | 15 +++++++++++---
 app/Language/fr/Podcast.php           | 17 +++++++++++++---
 app/Models/EpisodeModel.php           | 28 +++++++++++++++++++++++++++
 modules/Admin/Language/en/Podcast.php |  1 -
 modules/Admin/Language/fr/Podcast.php |  1 -
 themes/cp_app/podcast/about.php       | 10 ++++++++++
 7 files changed, 67 insertions(+), 8 deletions(-)

diff --git a/app/Controllers/PodcastController.php b/app/Controllers/PodcastController.php
index ccc786b3c3..d113648edb 100644
--- a/app/Controllers/PodcastController.php
+++ b/app/Controllers/PodcastController.php
@@ -125,9 +125,12 @@ class PodcastController extends BaseController
         );
 
         if (! ($cachedView = cache($cacheName))) {
+            $stats = (new EpisodeModel())->getPodcastStats($this->podcast->id);
+
             $data = [
                 'metatags' => get_podcast_metatags($this->podcast, 'about'),
                 'podcast' => $this->podcast,
+                'stats' => $stats,
             ];
 
             // // if user is logged in then send to the authenticated activity view
diff --git a/app/Language/en/Podcast.php b/app/Language/en/Podcast.php
index 7528a9a3e2..d86ce16518 100644
--- a/app/Language/en/Podcast.php
+++ b/app/Language/en/Podcast.php
@@ -28,12 +28,21 @@ return [
         other {<span class="font-semibold">#</span> posts}
     }',
     'activity' => 'Activity',
-    'activity_title' => '{podcastTitle} news & activity',
     'episodes' => 'Episodes',
     'episodes_title' => 'Episodes of {podcastTitle}',
     'about' => 'About',
-    'about_title' => 'About {podcastTitle}',
-    'sponsor_title' => 'Enjoying the show?',
+    'stats' => [
+        'title' => 'Stats',
+        'number_of_seasons' => '{0, plural,
+            one {<span class="font-semibold">#</span> season}
+            other {<span class="font-semibold">#</span> seasons}
+        }',
+        'number_of_episodes' => '{0, plural,
+            one {<span class="font-semibold">#</span> episode}
+            other {<span class="font-semibold">#</span> episodes}
+        }',
+        'first_published_at' => 'First episode published on <span class="font-semibold">{0, date, medium}</span>',
+    ],
     'sponsor' => 'Sponsor',
     'funding_links' => 'Funding links for {podcastTitle}',
     'find_on' => 'Find {podcastTitle} on',
diff --git a/app/Language/fr/Podcast.php b/app/Language/fr/Podcast.php
index f389e4978c..bc249c372c 100644
--- a/app/Language/fr/Podcast.php
+++ b/app/Language/fr/Podcast.php
@@ -9,7 +9,7 @@ declare(strict_types=1);
  */
 
 return [
-    'feed' => 'Podcast RSS feed',
+    'feed' => 'Flux RSS Podcast',
     'season' => 'Saison {seasonNumber}',
     'list_of_episodes_year' => 'Épisodes de {year} ({episodeCount})',
     'list_of_episodes_season' =>
@@ -29,8 +29,19 @@ return [
     }',
     'activity' => 'Activité',
     'episodes' => 'Épisodes',
-    'about' => 'About',
-    'sponsor_title' => 'Vous aimez le podcast ?',
+    'about' => 'À propos',
+    'stats' => [
+        'title' => 'Statistiques',
+        'number_of_seasons' => '{0, plural,
+            one {<span class="font-semibold">#</span> saison}
+            other {<span class="font-semibold">#</span> saisons}
+        }',
+        'number_of_episodes' => '{0, plural,
+            one {<span class="font-semibold">#</span> épisode}
+            other {<span class="font-semibold">#</span> épisodes}
+        }',
+        'first_published_at' => 'Premier épisode publié le <span class="font-semibold">{0, date, medium}</span>',
+    ],
     'sponsor' => 'Soutenez-nous',
     'funding_links' => 'Liens de financement pour {podcastTitle}',
     'find_on' => 'Trouvez {podcastTitle} sur',
diff --git a/app/Models/EpisodeModel.php b/app/Models/EpisodeModel.php
index 4edebdecd3..0e4fa9cf39 100644
--- a/app/Models/EpisodeModel.php
+++ b/app/Models/EpisodeModel.php
@@ -11,6 +11,7 @@ declare(strict_types=1);
 namespace App\Models;
 
 use App\Entities\Episode;
+use CodeIgniter\I18n\Time;
 use CodeIgniter\Model;
 
 class EpisodeModel extends Model
@@ -294,6 +295,33 @@ class EpisodeModel extends Model
         return (int) $result[0]['next_episode_number'] + 1;
     }
 
+    /**
+     * @return array<string, int|Time>
+     */
+    public function getPodcastStats(int $podcastId): array
+    {
+        $result = $this->select(
+            'COUNT(DISTINCT season_number) as number_of_seasons, COUNT(*) as number_of_episodes, MIN(published_at) as first_published_at'
+        )
+            ->where([
+                'podcast_id' => $podcastId,
+                'published_at IS NOT' => null,
+                $this->deletedField => null,
+            ])->get()
+            ->getResultArray();
+
+        $stats = [
+            'number_of_seasons' => (int) $result[0]['number_of_seasons'],
+            'number_of_episodes' => (int) $result[0]['number_of_episodes'],
+        ];
+
+        if ($result[0]['first_published_at'] !== null) {
+            $stats['first_published_at'] = new Time($result[0]['first_published_at']);
+        }
+
+        return $stats;
+    }
+
     /**
      * @param mixed[] $data
      *
diff --git a/modules/Admin/Language/en/Podcast.php b/modules/Admin/Language/en/Podcast.php
index 54a5e210f0..c1cd4cdcbc 100644
--- a/modules/Admin/Language/en/Podcast.php
+++ b/modules/Admin/Language/en/Podcast.php
@@ -236,7 +236,6 @@ return [
     }',
     'activity' => 'Activity',
     'episodes' => 'Episodes',
-    'sponsor_title' => 'Enjoying the show?',
     'sponsor' => 'Sponsor',
     'funding_links' => 'Funding links for {podcastTitle}',
     'find_on' => 'Find {podcastTitle} on',
diff --git a/modules/Admin/Language/fr/Podcast.php b/modules/Admin/Language/fr/Podcast.php
index 211e9459e1..3d207f04e6 100644
--- a/modules/Admin/Language/fr/Podcast.php
+++ b/modules/Admin/Language/fr/Podcast.php
@@ -238,7 +238,6 @@ return [
     }',
     'activity' => 'Activité',
     'episodes' => 'Épisodes',
-    'sponsor_title' => 'Vous aimez le podcast ?',
     'sponsor' => 'Soutenez-nous',
     'funding_links' => 'Liens de financement pour {podcastTitle}',
     'find_on' => 'Trouvez {podcastTitle} sur',
diff --git a/themes/cp_app/podcast/about.php b/themes/cp_app/podcast/about.php
index 629577d9fa..50a27c33b9 100644
--- a/themes/cp_app/podcast/about.php
+++ b/themes/cp_app/podcast/about.php
@@ -40,6 +40,16 @@
             <?= location_link($podcast->location, 'text-xs font-semibold p-2') ?>
         <?php endif; ?>
     </div>
+    <div class="mt-4">
+        <h2 class="text-xs font-bold tracking-wider text-gray-600 uppercase border-b-2 border-subtle font-display"><?= lang('Podcast.stats.title') ?></h2>
+        <div class="flex flex-col text-sm">
+            <?php foreach ($stats as $key => $value): ?>
+                <span class="py-2 border-b border-subtle">
+                    <?= lang('Podcast.about.stats.' . $key, [$value]) ?>
+                </span>
+            <?php endforeach; ?>
+        </div>
+    </div>
 </div>
 
 
-- 
GitLab