From 9e2eab506fc6b657c9ff65ad855487d0875d6273 Mon Sep 17 00:00:00 2001
From: Dillon Dumesnil <ddumesnil@edx.org>
Date: Fri, 4 Dec 2020 13:24:26 +0000
Subject: [PATCH] AA-454 and AA-470: Update language and bug fix for highlights

Since Course Highlights aren't necessarily weekly (self-paced courses),
update the language to be more generic. And then includes a bug fix to
not send highlights to learners after they have unenrolled from a course.
---
 .../js/views/modals/course_outline_modals.js  |  2 +-
 .../js/course-highlights-enable.underscore    |  2 +-
 .../js/highlights-enable-editor.underscore    |  6 +++---
 .../schedules/content_highlights.py           |  2 +-
 .../core/djangoapps/schedules/resolvers.py    |  1 +
 openedx/core/djangoapps/schedules/signals.py  | 20 ++++++++++++++++++-
 6 files changed, 26 insertions(+), 7 deletions(-)

diff --git a/cms/static/js/views/modals/course_outline_modals.js b/cms/static/js/views/modals/course_outline_modals.js
index 7e5996b7c74..7419a0ae107 100644
--- a/cms/static/js/views/modals/course_outline_modals.js
+++ b/cms/static/js/views/modals/course_outline_modals.js
@@ -273,7 +273,7 @@ define(['jquery', 'backbone', 'underscore', 'gettext', 'js/views/baseview',
         },
 
         getTitle: function() {
-            return gettext('Enable Weekly Highlight Emails');
+            return gettext('Enable Course Highlight Emails');
         },
 
         getIntroductionMessage: function() {
diff --git a/cms/templates/js/course-highlights-enable.underscore b/cms/templates/js/course-highlights-enable.underscore
index 2b3ea55d5c8..cfc9c9ac1e1 100644
--- a/cms/templates/js/course-highlights-enable.underscore
+++ b/cms/templates/js/course-highlights-enable.underscore
@@ -1,6 +1,6 @@
 <div class="course-highlights-setting">
 <h2 id="highlights-enabled-label" class="status-highlights-enabled-label">
-  <%- gettext('Weekly Highlight Emails') %>
+  <%- gettext('Course Highlight Emails') %>
 </h2>
 <br>
 <% if (highlights_enabled_for_messaging) { %>
diff --git a/cms/templates/js/highlights-enable-editor.underscore b/cms/templates/js/highlights-enable-editor.underscore
index 95411130724..1159b20a452 100644
--- a/cms/templates/js/highlights-enable-editor.underscore
+++ b/cms/templates/js/highlights-enable-editor.underscore
@@ -1,7 +1,7 @@
 <p>
 <%- gettext(
-    'When you enable weekly highlight emails, learners ' +
-    'automatically receive weekly email messages for each section that ' +
+    'When you enable course highlight emails, learners ' +
+    'automatically receive email messages for each section that ' +
     'has highlights. You cannot disable highlights after you start ' +
     'sending them.'
 ) %>
@@ -10,7 +10,7 @@
 <% // xss-lint: disable=underscore-not-escaped %>
 <%= edx.HtmlUtils.interpolateHtml(
     gettext(
-        'Are you sure you want to enable weekly highlight emails? '
+        'Are you sure you want to enable course highlight emails? '
         + '{linkStart}Learn more.{linkEnd}'
     ),
     {
diff --git a/openedx/core/djangoapps/schedules/content_highlights.py b/openedx/core/djangoapps/schedules/content_highlights.py
index 670fe427c63..d329c3f38fe 100644
--- a/openedx/core/djangoapps/schedules/content_highlights.py
+++ b/openedx/core/djangoapps/schedules/content_highlights.py
@@ -1,5 +1,5 @@
 """
-Contains methods for accessing weekly course highlights. Weekly highlights is a
+Contains methods for accessing course highlights. Course highlights is a
 schedule experience built on the Schedules app.
 """
 
diff --git a/openedx/core/djangoapps/schedules/resolvers.py b/openedx/core/djangoapps/schedules/resolvers.py
index 79c80d9e6e0..839a5b7298d 100644
--- a/openedx/core/djangoapps/schedules/resolvers.py
+++ b/openedx/core/djangoapps/schedules/resolvers.py
@@ -471,6 +471,7 @@ class CourseNextSectionUpdate(PrefixedDebugLoggerMixin, RecipientResolver):
         schedules = Schedule.objects.select_related('enrollment').filter(
             self.experience_filter,
             active=True,
+            enrollment__is_active=True,
             enrollment__course_id=self.course_id,
             enrollment__user__is_active=True,
             start_date__gte=target_date - course_duration,
diff --git a/openedx/core/djangoapps/schedules/signals.py b/openedx/core/djangoapps/schedules/signals.py
index 09ef4cc9fd9..c5a7ca6af4e 100644
--- a/openedx/core/djangoapps/schedules/signals.py
+++ b/openedx/core/djangoapps/schedules/signals.py
@@ -23,7 +23,7 @@ from openedx.core.djangoapps.schedules.models import ScheduleExperience
 from openedx.core.djangoapps.schedules.utils import reset_self_paced_schedule
 from openedx.core.djangoapps.theming.helpers import get_current_site
 from common.djangoapps.student.models import CourseEnrollment
-from common.djangoapps.student.signals import ENROLLMENT_TRACK_UPDATED
+from common.djangoapps.student.signals import ENROLL_STATUS_CHANGE, ENROLLMENT_TRACK_UPDATED
 from common.djangoapps.track import segment
 
 from .config import CREATE_SCHEDULE_WAFFLE_FLAG
@@ -61,6 +61,24 @@ def create_schedule(sender, **kwargs):  # pylint: disable=unused-argument
         ))
 
 
+@receiver(ENROLL_STATUS_CHANGE)
+def update_schedule(sender, event, user, course_id, **kwargs):  # pylint: disable=unused-argument
+    """
+    When a CourseEnrollment's status is updated, update the Schedule's active status if configured.
+    """
+    try:
+        schedule = Schedule.objects.get(enrollment__user=user, enrollment__course=course_id)
+    except Schedule.DoesNotExist:
+        # Exit since it could just be an indication of Schedules are not enabled.
+        return
+
+    if event == 'enroll':
+        schedule.active = True
+    elif event == 'unenroll':
+        schedule.active = False
+    schedule.save()
+
+
 @receiver(COURSE_START_DATE_CHANGED)
 def update_schedules_on_course_start_changed(sender, updated_course_overview, previous_start_date, **kwargs):   # pylint: disable=unused-argument
     """
-- 
GitLab