Skip to content

Commit

Permalink
refactor: add utility function to validate whether turnitin is enable…
Browse files Browse the repository at this point in the history
…d in course
  • Loading branch information
BryanttV committed May 9, 2024
1 parent 156888d commit 76af28f
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 40 deletions.
18 changes: 5 additions & 13 deletions platform_plugin_turnitin/extensions/filters.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
"""Filters for the Turnitin plugin."""

from django.conf import settings
from opaque_keys.edx.keys import UsageKey
from openedx_filters import PipelineStep

from platform_plugin_turnitin.edxapp_wrapper.modulestore import modulestore
from platform_plugin_turnitin.utils import enabled_in_course


class ORASubmissionViewTurnitinWarning(PipelineStep):
Expand All @@ -15,24 +14,17 @@ def run_filter(self, context: dict, template_name: str) -> dict: # pylint: disa
Execute filter that loads the submission template with a warning message that
notifies the user that the submission will be sent to Turnitin.
If the Turnitin feature is not enabled globally or in the course, the original
template is returned.
Args:
context (dict): The context dictionary.
template_name (str): ORA template name.
Returns:
dict: The context dictionary and the template name.
"""
if settings.ENABLE_TURNITIN_SUBMISSION:
return {
"context": context,
"template_name": "turnitin/oa_response.html",
}

course_key = UsageKey.from_string(context["xblock_id"]).course_key
course_block = modulestore().get_course(course_key)
enable_in_course = course_block.other_course_settings.get("ENABLE_TURNITIN_SUBMISSION", False)

if enable_in_course:
if settings.ENABLE_TURNITIN_SUBMISSION or enabled_in_course(context["xblock_id"]):
return {
"context": context,
"template_name": "turnitin/oa_response.html",
Expand Down
38 changes: 11 additions & 27 deletions platform_plugin_turnitin/handlers.py
Original file line number Diff line number Diff line change
@@ -1,42 +1,26 @@
"""Event handlers for the Turnitin plugin."""

from django.conf import settings
from opaque_keys.edx.keys import UsageKey

from platform_plugin_turnitin.edxapp_wrapper.modulestore import modulestore
from platform_plugin_turnitin.tasks import ora_submission_created_task
from platform_plugin_turnitin.utils import enabled_in_course


def ora_submission_created(submission, **kwargs):
"""
Handle the ORA_SUBMISSION_CREATED event.
Args:
submission (ORASubmissionData): The ORA submission data.
"""
if settings.ENABLE_TURNITIN_SUBMISSION:
call_ora_submission_created_task(submission)
return

course_key = UsageKey.from_string(submission.location).course_key
course_block = modulestore().get_course(course_key)
enable_in_course = course_block.other_course_settings.get("ENABLE_TURNITIN_SUBMISSION", False)

if enable_in_course:
call_ora_submission_created_task(submission)


def call_ora_submission_created_task(submission) -> None:
"""
Call the ORA submission created task.
If the Turnitin feature is enabled globally or in the course, create a new task to
send the ORA submission data to Turnitin.
Args:
submission (ORASubmissionData): The ORA submission data.
"""
ora_submission_created_task.delay(
submission.uuid,
submission.anonymous_user_id,
submission.answer.parts,
submission.answer.file_names,
submission.answer.file_urls,
)
if settings.ENABLE_TURNITIN_SUBMISSION or enabled_in_course(submission.location):
ora_submission_created_task.delay(
submission.uuid,
submission.anonymous_user_id,
submission.answer.parts,
submission.answer.file_names,
submission.answer.file_urls,
)
19 changes: 19 additions & 0 deletions platform_plugin_turnitin/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

from datetime import datetime

from opaque_keys.edx.keys import UsageKey

from platform_plugin_turnitin.edxapp_wrapper.modulestore import modulestore


def get_current_datetime() -> str:
"""
Expand All @@ -15,3 +19,18 @@ def get_current_datetime() -> str:
str: The current datetime in ISO 8601 format.
"""
return datetime.utcnow().strftime("%Y-%m-%dT%H:%M:%SZ")


def enabled_in_course(block_id: str) -> bool:
"""
Check if Turnitin feature is enabled in the course.
Args:
block_id (str): The block ID.
Returns:
bool: True if Turnitin feature is enabled in the course, False otherwise.
"""
course_key = UsageKey.from_string(block_id).course_key
course_block = modulestore().get_course(course_key)
return course_block.other_course_settings.get("ENABLE_TURNITIN_SUBMISSION", False)

0 comments on commit 76af28f

Please sign in to comment.