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

feat: handle optional blocks in verticals #279

Closed
Closed
Show file tree
Hide file tree
Changes from 3 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
4 changes: 4 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ Change Log
Unreleased
~~~~~~~~~~

[4.5.0] - 2024-02-26
~~~~~~~~~~~~~~~~~~~~
* Ignore optional blocks in non-optional verticals

[4.4.1] - 2023-10-27
~~~~~~~~~~~~~~~~~~~~
* Fix RemovedInDjango41Warning by removing `django_app_config`
Expand Down
2 changes: 1 addition & 1 deletion completion/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
"""


__version__ = '4.4.1'
__version__ = '4.5.0'
27 changes: 26 additions & 1 deletion completion/services.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,24 @@
user_children = [node]
return user_children

@staticmethod
def matches_vertical_optional_completion(optional_vertical, optional_child):
"""
Checks if child should count towards a vertical's completion.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we expand this docstring a bit to explain the concept of "optional" XBlocks?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done


There are only four combinations:
1. Optional Vertical and Optional Child -> Include Child
2. Optional Vertical and Required Child -> Include Child
3. Required Vertical and Required Child -> Include Child
4. Required Vertical and Optional Child -> Exclude Child
(case 2 shouldn't happen but this is how we can handle it gracefully)

This means that the only case in which we want to exclude the child
is when the parent is required and the child isn't.
"""
required_vertical = not optional_vertical
return not (required_vertical and optional_child) # exclude case 4 from docstring

def vertical_is_complete(self, item):
"""
Calculates and returns whether a particular vertical is complete.
Expand All @@ -117,8 +135,15 @@
if not self.completion_tracking_enabled():
return None

optional_vertical = getattr(item, "optional_completion", False)

Check warning on line 138 in completion/services.py

View check run for this annotation

Codecov / codecov/patch

completion/services.py#L138

Added line #L138 was not covered by tests

# this is temporary local logic and will be removed when the whole course tree is included in completion
child_locations = [child.scope_ids.usage_id for child in self.get_completable_children(item)]
child_locations = [
child.scope_ids.usage_id for child in self.get_completable_children(item)
if self.matches_vertical_optional_completion(
optional_vertical, getattr(child, "optional_completion", False)
)
]
completions = self.get_completions(child_locations)
for child_location in child_locations:
if completions[child_location] < 1.0:
Expand Down
14 changes: 14 additions & 0 deletions completion/tests/test_services.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,20 @@ def test_blocks_to_mark_complete_on_view(self):
[]
)

def test_matches_vertical_optional_completion(self):
for optional_vertical, optional_child, should_include_child in (
(True, True, True),
(True, False, True),
(False, False, True),
(False, True, False), # do not count optional children for non-optional verticals
):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: we should use @ddt.data for this.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

self.assertEqual(
self.completion_service.matches_vertical_optional_completion(
optional_vertical, optional_child
),
should_include_child,
)


@ddt.ddt
class CompletionDelayTestCase(CompletionSetUpMixin, TestCase):
Expand Down
Loading