Skip to content
Open
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
2 changes: 1 addition & 1 deletion MANIFEST.in
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,4 @@ prune src/*/spec
# these deep directories to prevent them from being swept up by `recursive-include`.
prune src/xblock_problem/capa/static/js/capa/spec
prune src/xblock_problem/capa/static/js/capa/fixtures
prune src/xblock_problem/capa/tests/test_files
prune src/xblock_problem/capa/tests/test_files
2 changes: 2 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ These are the XBlocks being moved here, and each of their statuses:
* ``discussion`` -- Ready to Use
* ``problem`` -- Ready to Use
* ``video`` -- Ready to Use
* ``openedx_flashcards`` -- In Development
* ``openedx_matching`` -- In Development

The possible XBlock statuses are:

Expand Down
6 changes: 6 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ dependencies = [
"oauthlib",
"openedx-calc",
"path",
"pydantic",
"pyparsing",
"pytz",
"random2",
Expand All @@ -68,6 +69,10 @@ _poll_question_extracted = "xblock_poll_question:PollBlock"
_problem_extracted = "xblock_problem:ProblemBlock"
_video_extracted = "xblock_video:VideoBlock"
_word_cloud_extracted = "xblock_word_cloud:WordCloudBlock"
# These are prefixed to avoid conflicting with existing community blocks.
openedx_flashcards = "xblock_flashcards:FlashcardsXBlock"
Comment thread
Kelketek marked this conversation as resolved.
Resolved
openedx_matching = "xblock_matching:MatchingXBlock"

# 'Done' XBlocks -- ones that are ready for general use today,
# and have been migrated fully from edx-platform or their original repository.
pdf = "xblock_pdf:PDFBlock"
Expand Down Expand Up @@ -117,6 +122,7 @@ dev = [
{ include-group = "quality" },
{ include-group = "ci" },
{ include-group = "doc" },
"edx-i18n-tools",
]

[tool.setuptools_scm]
Expand Down
3 changes: 3 additions & 0 deletions src/xblock_flashcards/__init__.py
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.
9 changes: 9 additions & 0 deletions src/xblock_flashcards/conf/locale/config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Configuration for i18n workflow.

locales:
Comment thread
farhan marked this conversation as resolved.
Resolved
- en # English - Source Language

# Directories we don't search for strings.
ignore_dirs:
- '*/css'
- 'public/js/translations'
104 changes: 104 additions & 0 deletions src/xblock_flashcards/flashcards.py
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)
Comment thread
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/>
""",
),
]
Loading