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

backport: reset a Randomized Content Block via AJAX #452

Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,14 @@ function LibraryContentReset(runtime, element) {
e.preventDefault();
$.post({
url: runtime.handlerUrl(element, 'reset_selected_children'),
success() {
location.reload();
success(data) {
edx.HtmlUtils.setHtml(element, edx.HtmlUtils.HTML(data));
// Rebind the reset button for the block
XBlock.initializeBlock(element);
// Render the new set of problems (XBlocks)
$(".xblock", element).each(function(i, child) {
XBlock.initializeBlock(child);
});
},
});
});
Expand Down
2 changes: 1 addition & 1 deletion common/lib/xmodule/xmodule/library_content_module.py
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,7 @@ def reset_selected_children(self, _, __):
block.save()

self.selected = []
return Response()
return Response(json.dumps(self.student_view({}).content))

def _get_selected_child_blocks(self):
"""
Expand Down
8 changes: 7 additions & 1 deletion common/lib/xmodule/xmodule/tests/test_library_content.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
Higher-level tests are in `cms/djangoapps/contentstore/tests/test_libraries.py`.
"""
import ddt
from unittest.mock import Mock, patch
from unittest.mock import MagicMock, Mock, patch

from bson.objectid import ObjectId
from fs.memoryfs import MemoryFS
Expand Down Expand Up @@ -405,13 +405,19 @@ def test_reset_selected_children_capa_blocks(self, allow_resetting_children, max
self._create_capa_problems()
self.lc_block.refresh_children()
self.lc_block = self.store.get_item(self.lc_block.location)
# Mock the student view to return an empty dict to be returned as response
self.lc_block.student_view = MagicMock()
self.lc_block.student_view.return_value.content = {}

with patch.object(ProblemBlock, 'reset_problem', return_value={'success': True}) as reset_problem:
response = self.lc_block.reset_selected_children(None, None)

if allow_resetting_children:
self.lc_block.student_view.assert_called_once_with({})
assert reset_problem.call_count == len(self.problem_types)
assert response.status_code == status.HTTP_200_OK
assert response.content_type == "text/html"
assert response.body == b"{}"
else:
reset_problem.assert_not_called()
assert response.status_code == status.HTTP_400_BAD_REQUEST
Expand Down