Fix Start Course vs Resume Course using Course Blocks.

This commit is contained in:
Robert Raposa
2017-04-25 14:23:38 -04:00
parent 4a4f135f47
commit 3b31270e07
11 changed files with 277 additions and 126 deletions

View File

@@ -12,6 +12,8 @@ from xmodule.modulestore.django import modulestore
from eventtracking import tracker
from track import contexts
from ..utils import get_student_module_as_dict
class ContentLibraryTransformer(FilteringTransformerMixin, BlockStructureTransformer):
"""
@@ -78,12 +80,7 @@ class ContentLibraryTransformer(FilteringTransformerMixin, BlockStructureTransfo
max_count = block_structure.get_xblock_field(block_key, 'max_count')
# Retrieve "selected" json from LMS MySQL database.
module = self._get_student_module(usage_info.user, usage_info.course_key, block_key)
if module:
state_dict = json.loads(module.state)
else:
state_dict = {}
state_dict = get_student_module_as_dict(usage_info.user, usage_info.course_key, block_key)
for selected_block in state_dict.get('selected', []):
# Add all selected entries for this user for this
# library module to the selected list.
@@ -135,28 +132,6 @@ class ContentLibraryTransformer(FilteringTransformerMixin, BlockStructureTransfo
return [block_structure.create_removal_filter(check_child_removal)]
@classmethod
def _get_student_module(cls, user, course_key, block_key):
"""
Get the student module for the given user for the given block.
Arguments:
user (User)
course_key (CourseLocator)
block_key (BlockUsageLocator)
Returns:
StudentModule if exists, or None.
"""
try:
return StudentModule.objects.get(
student=user,
course_id=course_key,
module_state_key=block_key,
)
except StudentModule.DoesNotExist:
return None
def _publish_events(self, block_structure, location, previous_count, max_count, block_keys, user_id):
"""
Helper method to publish events for analytics purposes

View File

@@ -0,0 +1,32 @@
"""
Common utilities for use along with the course blocks.
"""
import json
from courseware.models import StudentModule
def get_student_module_as_dict(user, course_key, block_key):
"""
Get the student module as a dict for the given user for the given block.
Arguments:
user (User)
course_key (CourseLocator)
block_key (BlockUsageLocator)
Returns:
StudentModule as a (possibly empty) dict.
"""
try:
student_module = StudentModule.objects.get(
student=user,
course_id=course_key,
module_state_key=block_key,
)
except StudentModule.DoesNotExist:
student_module = None
if student_module:
return json.loads(student_module.state)
else:
return {}

View File

@@ -518,28 +518,3 @@ def get_current_child(xmodule, min_depth=None, requested_child=None):
child = _get_default_child_module(children)
return child
def get_last_accessed_courseware(course, request, user):
"""
Returns a tuple containing the courseware module (URL, id) that the user last accessed,
or (None, None) if it cannot be found.
"""
# TODO: convert this method to use the Course Blocks API
field_data_cache = FieldDataCache.cache_for_descriptor_descendents(
course.id, request.user, course, depth=2
)
course_module = get_module_for_descriptor(
user, request, course, field_data_cache, course.id, course=course
)
chapter_module = get_current_child(course_module)
if chapter_module is not None:
section_module = get_current_child(chapter_module)
if section_module is not None:
url = reverse('courseware_section', kwargs={
'course_id': unicode(course.id),
'chapter': chapter_module.url_name,
'section': section_module.url_name
})
return (url, section_module.url_name)
return (None, None)

View File

@@ -66,7 +66,7 @@ from courseware.courses import (
get_course_by_id,
get_course_overview_with_access,
get_course_with_access,
get_last_accessed_courseware,
get_current_child,
get_permission_for_course_about,
get_studio_url,
sort_by_announcement,
@@ -97,7 +97,6 @@ from openedx.features.course_experience import (
UNIFIED_COURSE_VIEW_FLAG,
)
from openedx.features.enterprise_support.api import data_sharing_consent_required
from shoppingcart.models import CourseRegistrationCode
from shoppingcart.utils import is_shopping_cart_enabled
from student.models import UserTestGroup, CourseEnrollment
from survey.utils import must_answer_survey
@@ -249,6 +248,28 @@ def course_info(request, course_id):
Assumes the course_id is in a valid format.
"""
def get_last_accessed_courseware(course, request, user):
"""
Returns the courseware module URL that the user last accessed, or None if it cannot be found.
"""
field_data_cache = FieldDataCache.cache_for_descriptor_descendents(
course.id, request.user, course, depth=2
)
course_module = get_module_for_descriptor(
user, request, course, field_data_cache, course.id, course=course
)
chapter_module = get_current_child(course_module)
if chapter_module is not None:
section_module = get_current_child(chapter_module)
if section_module is not None:
url = reverse('courseware_section', kwargs={
'course_id': unicode(course.id),
'chapter': chapter_module.url_name,
'section': section_module.url_name
})
return url
return None
# If the unified course experience is enabled, redirect to the "Course" tab
if waffle.flag_is_active(request, UNIFIED_COURSE_EXPERIENCE_FLAG):
return redirect(reverse(course_home_url_name(request), args=[course_id]))
@@ -343,7 +364,7 @@ def course_info(request, course_id):
# Get the URL of the user's last position in order to display the 'where you were last' message
context['last_accessed_courseware_url'] = None
if SelfPacedConfiguration.current().enable_course_home_improvements:
context['last_accessed_courseware_url'], _ = get_last_accessed_courseware(course, request, user)
context['last_accessed_courseware_url'] = get_last_accessed_courseware(course, request, user)
now = datetime.now(UTC())
effective_start = _adjust_start_date_for_beta_testers(user, course, course_key)