Skip to content

Commit

Permalink
feat: add xblock aside for in context analytics
Browse files Browse the repository at this point in the history
  • Loading branch information
Ian2012 committed Sep 20, 2024
1 parent b7ad177 commit fa25ea6
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 1 deletion.
3 changes: 3 additions & 0 deletions platform_plugin_aspects/apps.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,6 @@ def ready(self):
from platform_plugin_aspects.extensions import ( # pylint: disable=unused-import, import-outside-toplevel
filters,
)
from platform_plugin_aspects.xblock_aside import ( # pylint: disable=unused-import, import-outside-toplevel
AspectsAside
)
4 changes: 3 additions & 1 deletion platform_plugin_aspects/signals.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
)
from platform_plugin_aspects.utils import get_model

from opaque_keys import InvalidKeyError

try:
from openedx.core.djangoapps.user_api.accounts.signals import USER_RETIRE_LMS_MISC
except ImportError:
Expand Down Expand Up @@ -235,7 +237,7 @@ def on_object_tag_deleted( # pylint: disable=unused-argument # pragma: no cove
try:
CourseOverview.objects.get(id=instance.object_id)
dump_course_to_clickhouse.delay(instance.object_id)
except CourseOverview.DoesNotExist:
except (CourseOverview.DoesNotExist, InvalidKeyError):
pass


Expand Down
3 changes: 3 additions & 0 deletions platform_plugin_aspects/static/html/example.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<h1>
Hola desde aside {{xblock_id}}
</h1>
82 changes: 82 additions & 0 deletions platform_plugin_aspects/xblock_aside.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
# pyright: reportMissingImports=false

"""Xblock aside enabling OpenAI driven summaries."""

import logging

import pkg_resources
from django.template import Context, Template
from django.utils import translation
from web_fragments.fragment import Fragment
from common.djangoapps.edxmako.shortcuts import render_to_string
from xblock.core import XBlock, XBlockAside
from xblock.fields import Scope, String
from xblock.utils.resources import ResourceLoader

logger = logging.getLogger(__name__)
loader = ResourceLoader(__name__)

from platform_plugin_aspects.xblock import ResourceLoader

@XBlock.needs("user")
@XBlock.needs("i18n")
class AspectsAside(XBlockAside):
"""
XBlock aside that injects a superset dashboard for instructors.
"""

def _get_block(self):
"""
Get the block wrapped by this aside.
"""
from xmodule.modulestore.django import modulestore # pylint: disable=import-error, import-outside-toplevel

return modulestore().get_item(self.scope_ids.usage_id.usage_key)

@XBlockAside.aside_for("studio_view")
@XBlockAside.aside_for("author_view")
@XBlockAside.aside_for("student_view")
def student_view_aside(self, block, context): # pylint: disable=unused-argument
"""
Display the tag selector with specific categories and allowed values,
depending on the context.
"""
logger.info(f"Class name {block.__class__.__name__}")
if block.__class__.__name__.replace("WithMixins", "") in ['ProblemBlock']:
frag = Fragment()
context.update({
"xblock_id": self.scope_ids.usage_id.usage_key
})
frag.add_content(self.render_template("static/html/example.html", context))
# frag.add_javascript_url(self._get_studio_resource_url('/js/xblock_asides/structured_tags.js'))
# frag.initialize_js('StructuredTagsInit')
return frag
return Fragment()

@classmethod
def should_apply_to_block(cls, block):
"""
Override base XBlockAside implementation.
Indicates whether this aside should apply to a given block type, course, and user.
"""
# logger.info(block.__dict__)
return True


def render_template(self, template_path, context=None) -> str:
"""
Render a template with the given context.
The template is translatedaccording to the user's language.
args:
template_path: The path to the template
context: The context to render in the template
returns:
The rendered template
"""
return loader.render_django_template(
template_path, context, i18n_service=self.runtime.service(self, "i18n")
)
3 changes: 3 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,5 +185,8 @@ def is_requirement(line):
"xblock.v1": [
"superset = platform_plugin_aspects.xblock:SupersetXBlock",
],
"xblock_asides.v1": [
"aspects_aside = platform_plugin_aspects.xblock_aside:AspectsAside",
],
},
)

0 comments on commit fa25ea6

Please sign in to comment.