Merge pull request #16096 from edx/HarryRein/LEARNER-2308-update-course-goal
Harry rein/learner 2308 update course goal
This commit is contained in:
@@ -17,6 +17,7 @@ from courseware.courses import (
|
||||
get_course_info_section,
|
||||
get_course_with_access,
|
||||
)
|
||||
from lms.djangoapps.course_goals.api import get_course_goal, has_course_goal_permission, get_course_goal_options, get_goal_api_url
|
||||
from lms.djangoapps.courseware.exceptions import CourseAccessRedirect
|
||||
from lms.djangoapps.courseware.views.views import CourseTabView
|
||||
from opaque_keys.edx.keys import CourseKey
|
||||
@@ -155,6 +156,16 @@ class CourseHomeFragmentView(EdxFragmentView):
|
||||
# Get the course tools enabled for this user and course
|
||||
course_tools = CourseToolsPluginManager.get_enabled_course_tools(request, course_key)
|
||||
|
||||
# Check if the user can access the course goal functionality
|
||||
has_goal_permission = has_course_goal_permission(request, course_id, user_access)
|
||||
|
||||
# Grab the current course goal and the acceptable course goal keys mapped to translated values
|
||||
current_goal = get_course_goal(request.user, course_key)
|
||||
goal_options = get_course_goal_options()
|
||||
|
||||
# Get the course goals api endpoint
|
||||
goal_api_url = get_goal_api_url(request)
|
||||
|
||||
# Grab the course home messages fragment to render any relevant django messages
|
||||
course_home_message_fragment = CourseHomeMessageFragmentView().render_to_fragment(
|
||||
request, course_id=course_id, user_access=user_access, **kwargs
|
||||
@@ -182,6 +193,11 @@ class CourseHomeFragmentView(EdxFragmentView):
|
||||
'resume_course_url': resume_course_url,
|
||||
'course_tools': course_tools,
|
||||
'dates_fragment': dates_fragment,
|
||||
'username': request.user.username,
|
||||
'goal_api_url': goal_api_url,
|
||||
'has_goal_permission': has_goal_permission,
|
||||
'goal_options': goal_options,
|
||||
'current_goal': current_goal,
|
||||
'update_message_fragment': update_message_fragment,
|
||||
'course_sock_fragment': course_sock_fragment,
|
||||
'disable_courseware_js': True,
|
||||
|
||||
@@ -5,7 +5,6 @@ import math
|
||||
from datetime import datetime
|
||||
|
||||
from babel.dates import format_date, format_timedelta
|
||||
from django.conf import settings
|
||||
from django.contrib import auth
|
||||
from django.template.loader import render_to_string
|
||||
from django.utils.http import urlquote_plus
|
||||
@@ -14,20 +13,16 @@ from django.utils.translation import get_language, to_locale
|
||||
from django.utils.translation import ugettext as _
|
||||
from django.utils.translation import get_language, to_locale
|
||||
from opaque_keys.edx.keys import CourseKey
|
||||
from rest_framework.reverse import reverse
|
||||
from web_fragments.fragment import Fragment
|
||||
|
||||
from course_modes.models import CourseMode
|
||||
from courseware.courses import get_course_date_blocks, get_course_with_access
|
||||
from lms.djangoapps.course_goals.api import get_course_goal
|
||||
from lms.djangoapps.course_goals.api import get_course_goal, get_course_goal_options, get_goal_api_url, has_course_goal_permission
|
||||
from lms.djangoapps.course_goals.models import GOAL_KEY_CHOICES
|
||||
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 student.models import CourseEnrollment
|
||||
|
||||
from .. import ENABLE_COURSE_GOALS
|
||||
|
||||
|
||||
class CourseHomeMessageFragmentView(EdxFragmentView):
|
||||
"""
|
||||
@@ -72,14 +67,19 @@ class CourseHomeMessageFragmentView(EdxFragmentView):
|
||||
course_date_block.register_alerts(request, course)
|
||||
|
||||
# Register a course goal message, if appropriate
|
||||
if _should_show_course_goal_message(request, course, user_access):
|
||||
# Only show the set course goal message for enrolled, unverified
|
||||
# users that have not yet set a goal in a course that allows for
|
||||
# verified statuses.
|
||||
user_goal = get_course_goal(auth.get_user(request), course_key)
|
||||
is_already_verified = CourseEnrollment.is_enrolled_as_verified(request.user, course_key)
|
||||
if has_course_goal_permission(request, course_id, user_access) and not is_already_verified and not user_goal:
|
||||
_register_course_goal_message(request, course)
|
||||
|
||||
# Grab the relevant messages
|
||||
course_home_messages = list(CourseHomeMessages.user_messages(request))
|
||||
|
||||
# Pass in the url used to set a course goal
|
||||
goal_api_url = reverse('course_goals_api:v0:course_goal-list', request=request)
|
||||
goal_api_url = get_goal_api_url(request)
|
||||
|
||||
# Grab the logo
|
||||
image_src = 'course_experience/images/home_message_author.png'
|
||||
@@ -132,39 +132,11 @@ def _register_course_home_messages(request, course, user_access, course_start_da
|
||||
)
|
||||
|
||||
|
||||
def _should_show_course_goal_message(request, course, user_access):
|
||||
"""
|
||||
Returns true if the current learner should be shown a course goal message.
|
||||
"""
|
||||
course_key = course.id
|
||||
|
||||
# Don't show a message if course goals has not been enabled
|
||||
if not ENABLE_COURSE_GOALS.is_enabled(course_key) or not settings.FEATURES.get('ENABLE_COURSE_GOALS'):
|
||||
return False
|
||||
|
||||
# Don't show a message if the user is not enrolled
|
||||
if not user_access['is_enrolled']:
|
||||
return False
|
||||
|
||||
# Don't show a message if the learner has already specified a goal
|
||||
if get_course_goal(auth.get_user(request), course_key):
|
||||
return False
|
||||
|
||||
# Don't show a message if the course does not have a verified mode
|
||||
if not CourseMode.has_verified_mode(CourseMode.modes_for_course_dict(unicode(course_key))):
|
||||
return False
|
||||
|
||||
# Don't show a message if the learner has already verified
|
||||
if CourseEnrollment.is_enrolled_as_verified(request.user, course_key):
|
||||
return False
|
||||
|
||||
return True
|
||||
|
||||
|
||||
def _register_course_goal_message(request, course):
|
||||
"""
|
||||
Register a message to let a learner specify a course goal.
|
||||
"""
|
||||
course_goal_options = get_course_goal_options()
|
||||
goal_choices_html = Text(_(
|
||||
'To start, set a course goal by selecting the option below that best describes '
|
||||
'your learning plan. {goal_options_container}'
|
||||
@@ -182,44 +154,44 @@ def _register_course_goal_message(request, course):
|
||||
).format(
|
||||
goal_key=GOAL_KEY_CHOICES.unsure,
|
||||
aria_label_choice=Text(_("Set goal to: {choice}")).format(
|
||||
choice=GOAL_KEY_CHOICES[GOAL_KEY_CHOICES.unsure]
|
||||
choice=course_goal_options[GOAL_KEY_CHOICES.unsure],
|
||||
),
|
||||
),
|
||||
choice=Text(_('{choice}')).format(
|
||||
choice=GOAL_KEY_CHOICES[GOAL_KEY_CHOICES.unsure],
|
||||
choice=course_goal_options[GOAL_KEY_CHOICES.unsure],
|
||||
),
|
||||
closing_tag=HTML('</div>'),
|
||||
)
|
||||
|
||||
# Add the option to set a goal to earn a certificate,
|
||||
# complete the course or explore the course
|
||||
goal_options = [
|
||||
GOAL_KEY_CHOICES.certify,
|
||||
GOAL_KEY_CHOICES.complete,
|
||||
GOAL_KEY_CHOICES.explore
|
||||
]
|
||||
for goal_key in goal_options:
|
||||
goal_text = GOAL_KEY_CHOICES[goal_key]
|
||||
course_goal_keys = course_goal_options.keys()
|
||||
course_goal_keys.remove(GOAL_KEY_CHOICES.unsure)
|
||||
for goal_key in course_goal_keys:
|
||||
goal_text = course_goal_options[goal_key]
|
||||
goal_choices_html += HTML(
|
||||
'{initial_tag}{goal_text}{closing_tag}'
|
||||
).format(
|
||||
initial_tag=HTML(
|
||||
'<div tabindex="0" aria-label="{aria_label_choice}" class="goal-option {col_sel} btn" '
|
||||
'<button tabindex="0" aria-label="{aria_label_choice}" class="goal-option {col_sel} btn" '
|
||||
'data-choice="{goal_key}">'
|
||||
).format(
|
||||
goal_key=goal_key,
|
||||
aria_label_choice=Text(_("Set goal to: {goal_text}")).format(
|
||||
goal_text=Text(_(goal_text))
|
||||
),
|
||||
col_sel='col-' + str(int(math.floor(12 / len(goal_options))))
|
||||
col_sel='col-' + str(int(math.floor(12 / len(course_goal_keys))))
|
||||
),
|
||||
goal_text=goal_text,
|
||||
closing_tag=HTML('</div>')
|
||||
closing_tag=HTML('</button>')
|
||||
)
|
||||
|
||||
CourseHomeMessages.register_info_message(
|
||||
request,
|
||||
goal_choices_html,
|
||||
HTML('{goal_choices_html}{closing_tag}').format(
|
||||
goal_choices_html=goal_choices_html,
|
||||
closing_tag=HTML('</div>')
|
||||
),
|
||||
title=Text(_('Welcome to {course_display_name}')).format(
|
||||
course_display_name=course.display_name
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user