Repository navigation
feat: migrate the games block to xblocks-core #316
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
Open
samuelallan72
wants to merge
6
commits into
openedx:main
Choose a base branch
from
open-craft:samuel/games-xblock
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
7361832
feat: migrate the games block to xblocks-core
samuelallan72 64d915a
remove unused test files
samuelallan72 33c3187
revert out of scope changes to testing
samuelallan72 0aa46ab
add validation for matching completion time
samuelallan72 fb3c1f1
fix lint issues
samuelallan72 852ec76
reorder entry-points for clarity on status
samuelallan72 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| """An XBlock for creating Flashcards, main entry point.""" | ||
|
|
||
| from .flashcards import FlashcardsXBlock as FlashcardsXBlock |
Empty file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| # Configuration for i18n workflow. | ||
|
|
||
| locales: | ||
|
farhan marked this conversation as resolved.
Resolved
|
||
| - en # English - Source Language | ||
|
|
||
| # Directories we don't search for strings. | ||
| ignore_dirs: | ||
| - '*/css' | ||
| - 'public/js/translations' | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,104 @@ | ||
| """An XBlock for creating Flashcards.""" | ||
|
|
||
| import json | ||
| import random | ||
| import string | ||
|
|
||
| from django.template import Context, Template | ||
| from django.utils.translation import gettext_noop as _ | ||
| from web_fragments.fragment import Fragment | ||
| from xblock.core import Response, XBlock | ||
| from xblock.fields import Boolean, List, Scope, String | ||
|
|
||
| from .types import Settings | ||
| from .utils import read_resource | ||
|
|
||
|
|
||
| @XBlock.needs("i18n") | ||
| class FlashcardsXBlock(XBlock): | ||
| """ | ||
| An XBlock for creating flashcards. | ||
|
|
||
| The Student view will display the game content and allow the student to interact | ||
| accordingly. | ||
| """ | ||
|
|
||
| display_name = String( | ||
| default=_("Flashcards"), | ||
| scope=Scope.settings, | ||
| help=_("Display name for this XBlock"), | ||
| ) | ||
|
|
||
| cards = List( | ||
| default=[], | ||
| scope=Scope.content, | ||
| help=_("The list of double sided flashcards."), | ||
| ) | ||
|
|
||
| is_shuffled = Boolean( | ||
| default=True, | ||
| scope=Scope.settings, | ||
| help=_("Whether the cards should be shuffled"), | ||
| ) | ||
|
|
||
| # NOTE: the student view will be rewritten soon in react | ||
| def student_view(self, context=None): # pylint: disable=unused-argument | ||
| """ | ||
| The student view for flashcards game. | ||
| """ | ||
| cards = random.sample(self.cards, k=len(self.cards)) if self.is_shuffled else self.cards | ||
|
|
||
| # to avoid conflicts with other instances of the flashcards block on the same page | ||
| # NOTE: these unique names and the init function shim won't be necessary when the frontend is ported to React | ||
| unique_prefix = "".join(random.choices(string.ascii_lowercase, k=16)) | ||
| init_function_name = f"{unique_prefix}_OpenedXFlashcardsInit" | ||
| data_element_id = f"{unique_prefix}_flashcards_data" | ||
|
|
||
| init_function = ( | ||
| f"function {init_function_name}(runtime, element){{" | ||
| f" var cards = JSON.parse($('#{data_element_id}', element).text());" | ||
| " OpenedXFlashcardsInit(runtime, element, cards);" | ||
| "}" | ||
| ) | ||
|
|
||
| template_context = { | ||
| "title": self.display_name, | ||
| "cards": json.dumps(cards), | ||
| "n_cards": len(cards), | ||
| "data_element_id": data_element_id, | ||
| } | ||
|
|
||
| template_str = read_resource("static/html/flashcards.html") | ||
| template = Template(template_str) | ||
|
farhan marked this conversation as resolved.
Resolved
|
||
| html = template.render(Context(template_context)) | ||
|
|
||
| frag = Fragment(html) | ||
| frag.add_css(read_resource("static/css/flashcards.css")) | ||
| frag.add_javascript(read_resource("static/js/src/flashcards.js")) | ||
| frag.add_javascript(init_function) | ||
| frag.initialize_js(init_function_name) | ||
| return frag | ||
|
|
||
| # this cannot be json_handler, because json_handler only supports POST requests | ||
| @XBlock.handler | ||
| def load_data(self, *_args, **_kwargs): | ||
| """ | ||
| Respond with a json representation of the block. | ||
| """ | ||
| settings = Settings( | ||
| cards=self.cards, | ||
| is_shuffled=self.is_shuffled, | ||
| display_name=self.display_name, | ||
| ) | ||
| return Response(json.dumps(settings.model_dump()), content_type="application/json", charset="utf8") | ||
|
|
||
| @staticmethod | ||
| def workbench_scenarios(): | ||
| """A canned scenario for display in the workbench.""" | ||
| return [ | ||
| ( | ||
| "Flashcards", | ||
| """<openedx_flashcards/> | ||
| """, | ||
| ), | ||
| ] | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.