Commit 05fc6738 by Braden MacDonald

Add in "previous_count" and "max_count"

parent 10fe9c01
......@@ -221,35 +221,39 @@ class LibraryContentModule(LibraryContentFields, XModule, StudioEditableModule):
# Already done:
return self._selected_set # pylint: disable=access-member-before-definition
selected = set(tuple(k) for k in self.selected) # set of (block_type, block_id) tuples assigned to this student
previous_count = len(selected)
lib_tools = self.runtime.service(self, 'library_tools')
format_block_keys = lambda keys: lib_tools.create_block_analytics_summary(self.location.course_key, keys)
def publish_event(event_name, **kwargs):
""" Publish an event for analytics purposes """
event_data = {
"location": unicode(self.location),
"result": format_block_keys(selected),
"previous_count": previous_count,
"max_count": self.max_count,
}
event_data.update(kwargs)
self.runtime.publish(self, "edx.librarycontentblock.content.{}".format(event_name), event_data)
# Determine which of our children we will show:
selected = set(tuple(k) for k in self.selected) # set of (block_type, block_id) tuples
valid_block_keys = set([(c.block_type, c.block_id) for c in self.children]) # pylint: disable=no-member
# Remove any selected blocks that are no longer valid:
invalid_block_keys = (selected - valid_block_keys)
if invalid_block_keys:
selected -= invalid_block_keys
# Publish an event for analytics purposes:
self.runtime.publish(self, "edx.librarycontentblock.content.removed", {
"location": unicode(self.location),
"removed": format_block_keys(invalid_block_keys),
"reason": "invalid", # Deleted from library or library being used has changed
"result": format_block_keys(selected),
})
# reason "invalid" means deleted from library or a different library is now being used.
publish_event("removed", removed=format_block_keys(invalid_block_keys), reason="invalid")
# If max_count has been decreased, we may have to drop some previously selected blocks:
overlimit_block_keys = set()
while len(selected) > self.max_count:
overlimit_block_keys.add(selected.pop())
if overlimit_block_keys:
# Publish an event for analytics purposes:
self.runtime.publish(self, "edx.librarycontentblock.content.removed", {
"location": unicode(self.location),
"removed": format_block_keys(overlimit_block_keys),
"reason": "overlimit",
"result": format_block_keys(selected),
})
publish_event("removed", removed=format_block_keys(overlimit_block_keys), reason="overlimit")
# Do we have enough blocks now?
num_to_add = self.max_count - len(selected)
if num_to_add > 0:
......@@ -265,11 +269,7 @@ class LibraryContentModule(LibraryContentFields, XModule, StudioEditableModule):
selected |= added_block_keys
if added_block_keys:
# Publish an event for analytics purposes:
self.runtime.publish(self, "edx.librarycontentblock.content.assigned", {
"location": unicode(self.location),
"added": format_block_keys(added_block_keys),
"result": format_block_keys(selected),
})
publish_event("assigned", added=format_block_keys(added_block_keys))
# Save our selections to the user state, to ensure consistency:
self.selected = list(selected) # TODO: this doesn't save from the LMS "Progress" page.
# Cache the results
......
......@@ -351,6 +351,8 @@ class TestLibraryContentAnalytics(LibraryContentTest):
"location": unicode(self.lc_block.location),
"added": [block_info],
"result": [block_info],
"previous_count": 0,
"max_count": 1,
})
self.publisher.reset_mock()
......@@ -364,6 +366,8 @@ class TestLibraryContentAnalytics(LibraryContentTest):
event_data = self._assert_event_was_published("assigned")
self.assertEqual(event_data["added"][0]["usage_key"], unicode(new_child.location))
self.assertEqual(len(event_data["result"]), 2)
self.assertEqual(event_data["previous_count"], 1)
self.assertEqual(event_data["max_count"], 2)
def test_assigned_descendants(self):
"""
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment