From 063c872beb7768a7416dc87c806c59f1479b19e3 Mon Sep 17 00:00:00 2001 From: irfanuddinahmad Date: Fri, 4 Dec 2026 12:01:06 +0500 Subject: [PATCH] fix: close the LTI gaps from the built-in block (#359) Port the two platform changes that never reached xblock_lti, and fix five places where the extracted block behaves worse than the built-in one: - Docs link in the Studio help text: edx.readthedocs.io -> docs.openedx.org (openedx/openedx-platform#36461). - pytz -> zoneinfo in block.py and the LTI tests, and drop the direct pytz dependency (openedx/openedx-platform#37562). - lti.html: the grader feedback label used Mako syntax in a Django template, so learners saw the literal ${_("...")}. Use {% trans %}. - Score text: render "{points} / {total_points} points" and "{total_points} points possible" with the block's catalog, as the built-in block did, instead of "(1.0 total_points possible)". - The "View resource in a new window" default and the missing launch_url message used _() inside template variables, which reads Django's global catalog. Use {% trans %} so they come from the block's catalog. - lti.js: wrap the three confirm() prompts in gettext() again, using the block's JS catalog (window.XBlockLTII18N) when the runtime provides one. - lti.css: scope every rule to the block's .lti-wrapper root, so LTI styles no longer restyle other blocks' .problem-progress / .problem-header. Closes #359. Co-Authored-By: Claude Opus 5.5 --- pyproject.toml | 1 - src/xblock_lti/block.py | 34 +++- src/xblock_lti/static/css/lti.css | 24 +-- src/xblock_lti/static/js/src/lti.js | 15 +- src/xblock_lti/templates/lti.html | 20 +-- src/xblock_lti/tests/test_lti20_unit.py | 4 +- src/xblock_lti/tests/test_lti_student_view.py | 155 ++++++++++++++++++ src/xblock_lti/tests/test_lti_unit.py | 4 +- uv.lock | 2 - 9 files changed, 220 insertions(+), 39 deletions(-) create mode 100644 src/xblock_lti/tests/test_lti_student_view.py diff --git a/pyproject.toml b/pyproject.toml index 3e9ac359..26ade48a 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -45,7 +45,6 @@ dependencies = [ "openedx-calc", "path", "pyparsing", - "pytz", "random2", "requests", "shapely", diff --git a/src/xblock_lti/block.py b/src/xblock_lti/block.py index bc9950df..b2aa1a63 100644 --- a/src/xblock_lti/block.py +++ b/src/xblock_lti/block.py @@ -62,6 +62,7 @@ from unittest import mock from urllib import parse from xml.sax.saxutils import escape +from zoneinfo import ZoneInfo import markupsafe import nh3 @@ -71,7 +72,6 @@ from lxml import etree from oauthlib.oauth1.rfc5849 import signature from opaque_keys.edx.keys import CourseKey -from pytz import UTC from web_fragments.fragment import Fragment from webob import Response from xblock.core import List, Scope, String, XBlock @@ -94,7 +94,7 @@ DOCS_ANCHOR_TAG_OPEN = ( "" + "href='https://docs.openedx.org/en/latest/educators/navigation/components_activities.html#lti-component'>" ) BREAK_TAG = "
" @@ -389,6 +389,7 @@ class LTIBlock( # Indicates that this XBlock has been extracted from edx-platform. is_extracted = True + i18n_js_namespace = "XBlockLTII18N" def max_score(self): return self.weight if self.has_score else None @@ -499,6 +500,18 @@ def get_context(self): # This lets all plaintext through. sanitized_comment = nh3.clean(self.score_comment) + # The score text next to the title, translated with the block's catalog as in the built-in block. + gettext = self.runtime.service(self, "i18n").ugettext + if self.module_score is not None: + # Translators: "points" is the student's achieved score on this LTI unit, and "total_points" is the + # maximum number of points achievable. + score_text = gettext("{points} / {total_points} points").format( + points=self.module_score, total_points=self.weight + ) + else: + # Translators: "total_points" is the maximum number of points achievable on this LTI unit + score_text = gettext("{total_points} points possible").format(total_points=self.weight) + return { "input_fields": self.get_input_fields(), # These parameters do not participate in OAuth signing. @@ -512,6 +525,7 @@ def get_context(self): "has_score": self.has_score, "weight": self.weight, "module_score": self.module_score, + "score_text": score_text, "comment": sanitized_comment, "description": self.description, "ask_to_send_username": self.ask_to_send_username, @@ -520,6 +534,19 @@ def get_context(self): "accept_grades_past_due": self.accept_grades_past_due, } + def _add_i18n_js_catalog(self, fragment): + """ + Add this block's JavaScript translation catalog to the fragment, if the runtime provides one. + + The catalog defines ``window.XBlockLTII18N`` (see ``i18n_js_namespace``). + Add it before the block's own JS. + """ + i18n_service = self.runtime.service(self, "i18n") + url_getter = getattr(i18n_service, "get_javascript_i18n_catalog_url", None) + url = url_getter(self) if url_getter else None + if url: + fragment.add_javascript_url(url) + def student_view(self, _context=None): """ Create primary view of the LTIBlock, shown to students when viewing courses. @@ -532,6 +559,7 @@ def student_view(self, _context=None): ) frag.add_css(resource_loader.load_unicode("static/css/lti.css")) + self._add_i18n_js_catalog(frag) frag.add_javascript(resource_loader.load_unicode("static/js/src/lti.js")) frag.initialize_js("LTIBlock") return frag @@ -1006,7 +1034,7 @@ def is_past_due(self): close_date = due_date + self.graceperiod else: close_date = due_date - return close_date is not None and datetime.datetime.now(UTC) > close_date + return close_date is not None and datetime.datetime.now(ZoneInfo("UTC")) > close_date @classmethod def definition_from_xml(cls, xml_object, system): diff --git a/src/xblock_lti/static/css/lti.css b/src/xblock_lti/static/css/lti.css index 7e9b5761..7cab9d17 100644 --- a/src/xblock_lti/static/css/lti.css +++ b/src/xblock_lti/static/css/lti.css @@ -1,12 +1,14 @@ -/* CSS for LTIBlock */ +/* CSS for LTIBlock. Every rule is scoped to the block's root element (.lti-wrapper in + templates/lti.html) so it can't restyle other blocks on the page, as the built-in block's + .xmodule_display.xmodule_LTIBlock scope did. */ @import url("https://fonts.googleapis.com/css?family=Open+Sans:300,400,400i,600,700"); -h2.problem-header { +.lti-wrapper h2.problem-header { display: inline-block; } -div.problem-progress { +.lti-wrapper div.problem-progress { display: inline-block; padding-left: 5px; color: #666; @@ -14,44 +16,44 @@ div.problem-progress { font-size: 1em; } -div.lti { +.lti-wrapper div.lti { margin: 0 auto; } -div.lti .wrapper-lti-link { +.lti-wrapper div.lti .wrapper-lti-link { font-size: 14px; background-color: #f6f6f6; padding: 20px; } -div.lti .wrapper-lti-link .lti-link { +.lti-wrapper div.lti .wrapper-lti-link .lti-link { margin-bottom: 0; text-align: right; } -div.lti .wrapper-lti-link .lti-link .link_lti_new_window { +.lti-wrapper div.lti .wrapper-lti-link .lti-link .link_lti_new_window { font-size: 13px; line-height: 20.72px; } -div.lti form.ltiLaunchForm { +.lti-wrapper div.lti form.ltiLaunchForm { display: none; } -div.lti iframe.ltiLaunchFrame { +.lti-wrapper div.lti iframe.ltiLaunchFrame { width: 100%; height: 800px; display: block; border: 0px; } -div.lti h4.problem-feedback-label { +.lti-wrapper div.lti h4.problem-feedback-label { font-weight: 100; font-size: 1em; font-family: "Source Sans", "Open Sans", Verdana, Geneva, sans-serif, sans-serif; } -div.lti div.problem-feedback { +.lti-wrapper div.lti div.problem-feedback { margin-top: 5px; margin-bottom: 5px; } diff --git a/src/xblock_lti/static/js/src/lti.js b/src/xblock_lti/static/js/src/lti.js index a9ab1fd1..96fdf3a4 100644 --- a/src/xblock_lti/static/js/src/lti.js +++ b/src/xblock_lti/static/js/src/lti.js @@ -3,6 +3,15 @@ function LTIBlock(runtime, element) { 'use strict'; + /* + Use this block's translation catalog (window.XBlockLTII18N, loaded by the + runtime before this file) when it is available, else the page's global gettext. + */ + function gettext(msgid) { + var catalog = window.XBlockLTII18N || window; + return catalog.gettext ? catalog.gettext(msgid) : msgid; + } + const $lti = $(element).find('.lti'); const askToSendUsername = $lti.data('ask-to-send-username') === 'True'; const askToSendEmail = $lti.data('ask-to-send-email') === 'True'; @@ -11,11 +20,11 @@ function LTIBlock(runtime, element) { // accept or reject sending their information to a third party $(element).on('click', '.link_lti_new_window', function() { if (askToSendUsername && askToSendEmail) { - return confirm('Click OK to have your username and e-mail address sent to a 3rd party application.\n\nClick Cancel to return to this page without sending your information.'); + return confirm(gettext('Click OK to have your username and e-mail address sent to a 3rd party application.\n\nClick Cancel to return to this page without sending your information.')); } else if (askToSendUsername) { - return confirm('Click OK to have your username sent to a 3rd party application.\n\nClick Cancel to return to this page without sending your information.'); + return confirm(gettext('Click OK to have your username sent to a 3rd party application.\n\nClick Cancel to return to this page without sending your information.')); } else if (askToSendEmail) { - return confirm('Click OK to have your e-mail address sent to a 3rd party application.\n\nClick Cancel to return to this page without sending your information.'); + return confirm(gettext('Click OK to have your e-mail address sent to a 3rd party application.\n\nClick Cancel to return to this page without sending your information.')); } else { return true; } diff --git a/src/xblock_lti/templates/lti.html b/src/xblock_lti/templates/lti.html index da153fef..455f9900 100644 --- a/src/xblock_lti/templates/lti.html +++ b/src/xblock_lti/templates/lti.html @@ -7,18 +7,7 @@

{% if has_score and weight %} -
- {% if module_score is not None %} - {# Translators: "points" is the student's achieved score on this LTI unit, and "total_points" is the maximum number of points achievable. #} - {% trans "points" as points_trans %} - {% trans "{points} / {total_points} points" as score_trans %} - ({{ module_score }} / {{ weight }} {{ points_trans }}) - {% else %} - {# Translators: "total_points" is the maximum number of points achievable on this LTI unit #} - {% trans "total_points" as total_points_trans %} - ({{ weight }} {{ total_points_trans }} possible) - {% endif %} -
+
({{ score_text }})
{% endif %}
{% endif %} @@ -57,12 +47,12 @@

{% endif %} {% elif not hide_launch %}

- {{ _('Please provide launch_url. Click "Edit", and fill in the required fields.') }} + {% trans 'Please provide launch_url. Click "Edit", and fill in the required fields.' %}

{% endif %} {% if has_score and comment %} - +