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

Support for linebreaks in grading comments #5913

Merged
merged 6 commits into from
Dec 18, 2023
Merged

Conversation

marcospri
Copy link
Member

@marcospri marcospri commented Dec 12, 2023

Some LMS don't play nice with regular line breaks in grading comments (\n) but use HTML instead.

This PR uses a naive approach to maintain the new lines in our textarea. It doesn't however try to support the full range of HTML different LMSes support.

Testing

D2L

Moodle

Blackboard

@marcospri marcospri force-pushed the grading-comments-linebreaks branch from b882e36 to 3f034ec Compare December 12, 2023 13:23
@@ -12,3 +13,6 @@ def includeme(config): # pragma: nocover
config.register_service_factory(
BlackboardCourseCopyPlugin.factory, iface=BlackboardCourseCopyPlugin
)
config.register_service_factory(
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The blackboard misc plugin is back, we need to define different behavior for BB here.

class BlackboardMiscPlugin(MiscPlugin):
def format_grading_comment_for_lms(self, comment: str) -> str:
# Replace new lines by by html, otherwise format it lost when read back.
return comment.replace("\n", "<br/>")
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sad, but it's only for BB

@@ -63,21 +65,16 @@ def read_result(self, grading_id) -> GradingResult:
# We'll filter ourselves here instead.
results = [result for result in results if result["userId"] == grading_id]

# On top of this, blackboard adds some HTML to the comments it returns
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doing this with the plugin instead now.

from html.parser import HTMLParser


def strip_html_tags(html: str) -> str:
"""Get plain text from a string which may contain HTML tags."""
class WhiteSpaceHTMLParser(HTMLParser): # pylint:disable=abstract-method
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I initially thought I would have to substantially change this. I didn't. But I think this version is slightly more pythonic (whatever that means). I kept the refactor.

parser.feed(html)
parser.close()

# Strip leading/trailing whitespace and duplicate spaces
return " ".join("".join(chunks).split())
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was very specific to the use case in jstor, where it used in titles. This removes newlines.

Move it back to the jstor service.

@marcospri marcospri requested a review from acelaya December 12, 2023 14:05
Copy link
Contributor

@acelaya acelaya left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good and mostly works as expected.

I just noticed something we might want check on a later stage, which is that extra empty lines are discarded in D2L and Moodle, but they are preserved in Blackboard.

With "extra" I mean that it removes one empty line when saving, which means that, if you add more than one, it keeps one less every time, but if the same comment is submitted more times, eventually all empty lines are lost.

Grabacion.de.pantalla.desde.2023-12-13.09-58-19.webm

Base automatically changed from bb-grading-comments to main December 18, 2023 08:01
Keep the duplicate whitespace behaviour outside the service and move it
back to the JSTOR one, where it was originally written.
It turns out that LMS that support HTML also keep the new lines. We only
need to remove the HTML tags to display the text in our textarea.
@marcospri marcospri force-pushed the grading-comments-linebreaks branch from 57ca8b8 to ac3f738 Compare December 18, 2023 08:08
@marcospri marcospri merged commit fb96a1c into main Dec 18, 2023
@marcospri marcospri deleted the grading-comments-linebreaks branch December 18, 2023 08:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants