Skip to content

Commit

Permalink
feat: [AXIMST-819] Section progress calculates by units progress
Browse files Browse the repository at this point in the history
  • Loading branch information
NiedielnitsevIvan committed May 14, 2024
1 parent 3d7e7ed commit 8e16c31
Showing 1 changed file with 22 additions and 18 deletions.
40 changes: 22 additions & 18 deletions lms/djangoapps/course_home_api/outline/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -463,9 +463,7 @@ def get(self, request, *args, **kwargs):
)
if navigation_sidebar_caching_is_disabled := courseware_disable_navigation_sidebar_blocks_caching():
course_blocks = cache.get(cache_key)
cached = course_blocks is not None
else:
cached = False
course_blocks = None

if not course_blocks:
Expand All @@ -478,12 +476,7 @@ def get(self, request, *args, **kwargs):
cache.set(cache_key, course_blocks, self.COURSE_BLOCKS_CACHE_TIMEOUT)

course_blocks = self.filter_inaccessible_blocks(course_blocks, course_key)

if cached:
# Note: The course_blocks received from get_course_outline_block_tree already has completion data,
# but since the course_blocks can be cached, and this status can change quite often,
# we need to update it every time if the data has not been cached.
course_blocks = self.mark_complete_recursive(course_blocks)
course_blocks = self.mark_complete_recursive(course_blocks)

context = self.get_serializer_context()
context.update({
Expand Down Expand Up @@ -523,9 +516,7 @@ def mark_complete_recursive(self, block):
if 'children' in block:
block['children'] = [self.mark_complete_recursive(child) for child in block['children']]
completable_children = self.get_completable_children(block)
block['complete'] = all(
child['complete'] for child in block['children'] if child['type'] in self.completable_block_types
)
block['complete'] = all(child['complete'] for child in completable_children)
block['completion_stat'] = self.get_block_completion_stat(block, completable_children)
else:
# If the block is a course, chapter, sequential, or vertical, without children,
Expand All @@ -541,25 +532,32 @@ def get_block_completion_stat(self, block, completable_children):
Get the completion status of a block.
"""
block_type = block['type']
completable_children_num = len(completable_children)

if block_type in ['course', 'chapter', 'sequential']:
if block_type in ['course', 'sequential']:
completion = sum(child['complete'] for child in completable_children)
elif block_type == 'chapter':
# For sections, we have to count the status on the number of completed units
# and, accordingly, the number of units in it.
completion = sum(child['completion_stat']['completion'] for child in completable_children)
completable_children_num = sum(
child['completion_stat']['completable_children'] for child in completable_children
)
elif block_type == 'vertical':
completion = sum(child['completion_stat']['completion'] for child in completable_children)
else:
completion = self.completions_dict.get(block['id'], 0)

return {
'completion': completion,
'completable_children': len(completable_children),
'completable_children': completable_children_num,
}

@staticmethod
def get_completable_children(block):
def get_completable_children(self, block):
"""
Get the completable children of a block.
"""
return [child for child in block.get('children', []) if child['type'] != 'discussion']
return [child for child in block.get('children', []) if child['type'] in self.completable_block_types]

@staticmethod
def get_accessible_sections(user_course_outline, course_sections):
Expand Down Expand Up @@ -605,11 +603,17 @@ def completions_dict(self):
@cached_property
def completable_block_types(self):
"""
Return a set of block types that are completable.
Returns a set of block types that can be marked as completed.
In addition to the lower-level x-blocks, it also includes blocks
that belong to XBlockCompletionMode.AGGREGATOR, because they can also be marked as complete.
"""
return {
block_type for (block_type, block_cls) in XBlock.load_classes()
if XBlockCompletionMode.get_mode(block_cls) == XBlockCompletionMode.COMPLETABLE
if XBlockCompletionMode.get_mode(block_cls) in (
XBlockCompletionMode.COMPLETABLE,
XBlockCompletionMode.AGGREGATOR
)
}


Expand Down

0 comments on commit 8e16c31

Please sign in to comment.