Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Show enroll links on public courses only if self enrollment allowed #19837

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lms/djangoapps/courseware/views/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -551,7 +551,7 @@ def register_user_access_warning_messages(request, course):
else:
if not CourseEnrollment.is_enrolled(request.user, course.id) and not allow_anonymous:
# Only show enroll button if course is open for enrollment.
if course_open_for_self_enrollment(course.id):
if course_open_for_self_enrollment(course.id) and not course.invitation_only:
enroll_message = _(u'You must be enrolled in the course to see course content. \
{enroll_link_start}Enroll now{enroll_link_end}.')
PageLevelMessages.register_warning_message(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,14 @@ def setUpClass(cls):
number='test',
display_name='Test Course',
start=now() - timedelta(days=30),
metadata={"invitation_only": False}
)
cls.private_course = CourseFactory.create(
org='edX',
number='test',
display_name='Test Private Course',
start=now() - timedelta(days=30),
metadata={"invitation_only": True}
)
with cls.store.bulk_operations(cls.course.id):
chapter = ItemFactory.create(
Expand Down Expand Up @@ -292,6 +300,9 @@ def test_home_page(
url = course_home_url(self.course)
response = self.client.get(url)

private_url = course_home_url(self.private_course)
private_response = self.client.get(private_url)

# Verify that the course tools and dates are always shown
self.assertContains(response, TEST_COURSE_TOOLS)
self.assertContains(response, TEST_COURSE_TODAY)
Expand All @@ -312,7 +323,7 @@ def test_home_page(
# Verify the outline is shown to enrolled users, unenrolled_staff and anonymous users if allowed
self.assertContains(response, TEST_CHAPTER_NAME, count=(1 if expected_course_outline else 0))

# Verify that the expected message is shown to the user
# Verify the message shown to the user
if not enable_unenrolled_access or course_visibility != COURSE_VISIBILITY_PUBLIC:
self.assertContains(
response, 'To see course content', count=(1 if is_anonymous else 0)
Expand All @@ -321,6 +332,12 @@ def test_home_page(
if expected_enroll_message:
self.assertContains(response, 'You must be enrolled in the course to see course content.')

if enable_unenrolled_access and course_visibility == COURSE_VISIBILITY_PUBLIC:
if user_type == CourseUserType.UNENROLLED and self.private_course.invitation_only:
if expected_enroll_message:
self.assertContains(private_response,
'You must be enrolled in the course to see course content.')

@override_waffle_flag(UNIFIED_COURSE_TAB_FLAG, active=False)
@override_waffle_flag(SHOW_REVIEWS_TOOL_FLAG, active=True)
@ddt.data(
Expand Down
48 changes: 29 additions & 19 deletions openedx/features/course_experience/views/course_home_messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,12 @@
from datetime import datetime

from babel.dates import format_date, format_timedelta
from courseware.courses import get_course_date_blocks, get_course_with_access
from django.contrib import auth
from django.template.loader import render_to_string
from django.utils.http import urlquote_plus
from django.utils.translation import ugettext as _
from django.utils.translation import get_language, to_locale
from opaque_keys.edx.keys import CourseKey
from pytz import UTC
from web_fragments.fragment import Fragment

from courseware.courses import get_course_date_blocks, get_course_with_access
from django.utils.translation import ugettext as _
from lms.djangoapps.course_goals.api import (
get_course_goal,
get_course_goal_options,
Expand All @@ -23,10 +19,15 @@
valid_course_goals_ordered
)
from lms.djangoapps.course_goals.models import GOAL_KEY_CHOICES
from lms.djangoapps.courseware.courses import allow_public_access
from opaque_keys.edx.keys import CourseKey
from openedx.core.djangoapps.plugin_api.views import EdxFragmentView
from openedx.core.djangolib.markup import HTML, Text
from openedx.features.course_experience import CourseHomeMessages
from pytz import UTC
from student.models import CourseEnrollment
from web_fragments.fragment import Fragment
from xmodule.course_module import COURSE_VISIBILITY_PUBLIC


class CourseHomeMessageFragmentView(EdxFragmentView):
Expand Down Expand Up @@ -107,7 +108,9 @@ def _register_course_home_messages(request, course, user_access, course_start_da
"""
Register messages to be shown in the course home content page.
"""
if user_access['is_anonymous']:
allow_anonymous = allow_public_access(course, [COURSE_VISIBILITY_PUBLIC])

if user_access['is_anonymous'] and not allow_anonymous:
CourseHomeMessages.register_info_message(
request,
Text(_(
Expand All @@ -124,19 +127,26 @@ def _register_course_home_messages(request, course, user_access, course_start_da
),
title=Text(_('You must be enrolled in the course to see course content.'))
)
if not user_access['is_anonymous'] and not user_access['is_staff'] and not user_access['is_enrolled']:
CourseHomeMessages.register_info_message(
request,
Text(_(
u'{open_enroll_link}Enroll now{close_enroll_link} to access the full course.'
)).format(
open_enroll_link=HTML('<button class="enroll-btn btn-link">'),
close_enroll_link=HTML('</button>')
),
title=Text(_(u'Welcome to {course_display_name}')).format(
course_display_name=course.display_name
if not user_access['is_anonymous'] and not user_access['is_staff'] and \
not user_access['is_enrolled']:
if not course.invitation_only:
CourseHomeMessages.register_info_message(
request,
Text(_(
u'{open_enroll_link}Enroll now{close_enroll_link} to access the full course.'
)).format(
open_enroll_link=HTML('<button class="enroll-btn btn-link">'),
close_enroll_link=HTML('</button>')
),
title=Text(_(u'Welcome to {course_display_name}')).format(
course_display_name=course.display_name
)
)
else:
CourseHomeMessages.register_info_message(
request,
Text(_('You must be enrolled in the course to see course content.')),
)
)


def _register_course_goal_message(request, course):
Expand Down