Repository: openfun/richie

Issue:
Limit the number of visible archived course runs
## Feature Request

**Problem description**
When a course uses a short lived course runs, then after a couple of months (years) it starts to have a long archived course runs.
So I think that would be better to have an option to limit the number of visible archived course runs on the course page, by default Richie could show all the course runs.

Here on nau.edu.pt we have at least one of those courses. Specifically, in the past our partner has created the same course (slightly adapted) to specficic audience. But now, the course is till open, and we have a very long old (archived) course runs.

**Describe the solution you'd like**
Hide the oldest archived course runs, or at least don't show them by default. So the student don't view a not formatted course page.

**Describe alternatives you've considered**
Add a `visible` field to the course run that allow the user, and also via API, to show / hide it on the course run. Probably both solutions could be added to the Richie core.

**Discovery, Documentation, Adoption, Migration Strategy**
The site administrator could add a Django setting that would made the course page to only show by default the first N archived course runs.

**Do you want to work on it through a Pull Request?**
I would send a PR so we could discuss it further. If you think that it don't makes sense to add it to the Richie, I could added it only to the NAU Richie repo.

Screenshot of the "problem":
![image](https://user-images.githubusercontent.com/67018/139219837-a01cc787-520b-4863-99b5-8891d423fa29.png)



Discussion:

Resolving patch:
diff --git a/src/richie/apps/core/context_processors.py b/src/richie/apps/core/context_processors.py
--- a/src/richie/apps/core/context_processors.py
+++ b/src/richie/apps/core/context_processors.py
@@ -10,6 +10,7 @@
 from django.middleware.csrf import get_token
 from django.utils.translation import get_language_from_request
 
+from richie.apps.courses.defaults import RICHIE_MAX_ARCHIVED_COURSE_RUNS
 from richie.apps.courses.models import Organization
 
 from . import defaults
@@ -107,6 +108,11 @@ def site_metas(request: HttpRequest):
         context[
             "RICHIE_MINIMUM_COURSE_RUNS_ENROLLMENT_COUNT"
         ] = settings.RICHIE_MINIMUM_COURSE_RUNS_ENROLLMENT_COUNT
+
+    context["RICHIE_MAX_ARCHIVED_COURSE_RUNS"] = getattr(
+        settings, "RICHIE_MAX_ARCHIVED_COURSE_RUNS", RICHIE_MAX_ARCHIVED_COURSE_RUNS
+    )
+
     return context
 
 
diff --git a/src/richie/apps/courses/defaults.py b/src/richie/apps/courses/defaults.py
--- a/src/richie/apps/courses/defaults.py
+++ b/src/richie/apps/courses/defaults.py
@@ -343,3 +343,7 @@
     MINUTE: TIME_UNITS[MINUTE],
     HOUR: TIME_UNITS[HOUR],
 }
+
+# Maximum number of archived course runs displayed by default on course detail page.
+# The additional runs can be viewed by clicking on `View more` link.
+RICHIE_MAX_ARCHIVED_COURSE_RUNS = 10


Test patch:
diff --git a/tests/apps/courses/test_templates_course_detail_rendering.py b/tests/apps/courses/test_templates_course_detail_rendering.py
--- a/tests/apps/courses/test_templates_course_detail_rendering.py
+++ b/tests/apps/courses/test_templates_course_detail_rendering.py
@@ -1231,3 +1231,39 @@ def test_templates_course_detail_license_only_participation(self):
 
         # Without course license participation
         self.assertNotContains(response, "License for the course content")
+
+    @override_settings(RICHIE_MAX_ARCHIVED_COURSE_RUNS=3)
+    def test_templates_course_detail_view_richie_max_archived_course_runs(self):
+        """
+        Only the number of archived course runs defined by `RICHIE_MAX_ARCHIVED_COURSE_RUNS`
+        setting should be displayed.
+        """
+        course = CourseFactory()
+        for _ in range(5):
+            self.create_run_archived_closed(course).refresh_from_db()
+        course.extended_object.publish("en")
+        response = self.client.get(course.extended_object.get_absolute_url())
+
+        self.assertContains(
+            response,
+            '<li class="is-hidden">',
+            count=2,
+        )
+        self.assertContains(response, "course-detail__view-more-runs")
+
+    @override_settings(RICHIE_MAX_ARCHIVED_COURSE_RUNS=None)
+    def test_templates_course_detail_view_no_richie_max_archived_course_runs(self):
+        """
+        All course runs should be displayed when `RICHIE_MAX_ARCHIVED_COURSE_RUNS` setting is None
+        """
+        course = CourseFactory()
+        for _ in range(5):
+            self.create_run_archived_closed(course).refresh_from_db()
+        course.extended_object.publish("en")
+        response = self.client.get(course.extended_object.get_absolute_url())
+
+        self.assertNotContains(
+            response,
+            '<li class="is-hidden">',
+        )
+        self.assertNotContains(response, "course-detail__view-more-runs")