Skip to content

Commit

Permalink
fix: [FC-0063] ORA file-related attributes setting is fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
myhailo-chernyshov-rg committed Jan 30, 2025
1 parent 4562d22 commit 28a6d8d
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 19 deletions.
53 changes: 41 additions & 12 deletions src/cc2olx/content_parsers/assignment.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import re
from typing import Dict, Optional, Set, Union
from typing import Dict, List, Optional, Set, Union

from cc2olx import filesystem
from cc2olx.content_parsers import AbstractContentParser
Expand Down Expand Up @@ -69,21 +69,17 @@ def _parse_response_data(self, resource_root: cc_xml.AssignmentElement) -> dict:
Parse response-related data.
"""
accepted_format_types = self._parse_accepted_format_types(resource_root)
is_file_submission_allowed = AssignmentSubmissionFormatType.FILE in accepted_format_types
is_textual_submission_allowed = bool(
AssignmentSubmissionFormatType.get_not_file_types().intersection(accepted_format_types)
)
text_response_editor = "tinymce" if AssignmentSubmissionFormatType.HTML in accepted_format_types else "text"
is_file_submission_allowed = self._is_file_submission_allowed(accepted_format_types)
is_textual_submission_allowed = self._is_textual_submission_allowed(accepted_format_types)
file_upload_response = self._get_file_upload_response(is_textual_submission_allowed, is_file_submission_allowed)

return {
"text_response": self._get_text_response(is_textual_submission_allowed, is_file_submission_allowed),
"text_response_editor": text_response_editor,
"file_upload_response": (
self._get_file_upload_response(is_textual_submission_allowed, is_file_submission_allowed)
),
"text_response_editor": self._get_text_response_editor(accepted_format_types),
"file_upload_response": file_upload_response,
"allow_multiple_files": True,
"file_upload_type": self.DEFAULT_FILE_UPLOAD_TYPE,
"white_listed_file_types": self.DEFAULT_WHITE_LISTED_FILE_TYPES,
"file_upload_type": self._get_file_upload_type(file_upload_response),
"white_listed_file_types": self._get_white_listed_file_types(file_upload_response),
}

def _parse_accepted_format_types(self, resource_root: cc_xml.AssignmentElement) -> Set[str]:
Expand All @@ -93,6 +89,20 @@ def _parse_accepted_format_types(self, resource_root: cc_xml.AssignmentElement)
accepted_format_types = {accepted_format.attrib["type"] for accepted_format in resource_root.accepted_formats}
return accepted_format_types or self.DEFAULT_ACCEPTED_FORMAT_TYPES

@staticmethod
def _is_file_submission_allowed(accepted_format_types: Set[str]) -> bool:
"""
Decide whether submitting a file as an answer to assignment is allowed.
"""
return AssignmentSubmissionFormatType.FILE in accepted_format_types

@staticmethod
def _is_textual_submission_allowed(accepted_format_types: Set[str]) -> bool:
"""
Decide whether submitting a textual answer to assignment is allowed.
"""
return bool(AssignmentSubmissionFormatType.get_not_file_types().intersection(accepted_format_types))

@staticmethod
def _get_text_response(is_textual_submission_allowed: bool, is_file_submission_allowed: bool) -> str:
"""
Expand All @@ -110,3 +120,22 @@ def _get_file_upload_response(is_textual_submission_allowed: bool, is_file_submi
if is_file_submission_allowed:
return "optional" if is_textual_submission_allowed else "required"
return ""

@staticmethod
def _get_text_response_editor(accepted_format_types: Set[str]) -> str:
"""
Provide text response editor type.
"""
return "tinymce" if AssignmentSubmissionFormatType.HTML in accepted_format_types else "text"

def _get_file_upload_type(self, file_upload_response: str) -> Optional[str]:
"""
Provide file upload type.
"""
return self.DEFAULT_FILE_UPLOAD_TYPE if file_upload_response else None

def _get_white_listed_file_types(self, file_upload_response: str) -> List[str]:
"""
Provide file types allowed to submit.
"""
return self.DEFAULT_WHITE_LISTED_FILE_TYPES if file_upload_response else []
15 changes: 9 additions & 6 deletions src/cc2olx/olx_generators/assignment.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,6 @@ class AssignmentOlxGenerator(AbstractOlxGenerator):
Generate OLX for assignments.
"""

FILE_UPLOAD_TYPE = "pdf-and-image"
WHITE_LISTED_FILE_TYPES = ["pdf", "gif", "jpg", "jpeg", "jfif", "pjpeg", "pjp", "png"]

def create_nodes(self, content: dict) -> List[xml.dom.minidom.Element]:
el = element_builder(self._doc)

Expand Down Expand Up @@ -46,12 +43,18 @@ def _generate_openassessment_attributes(content: dict) -> Dict[str, str]:
"""
Generate ORA root tag attributes.
"""
return {
attributes = {
"prompts_type": content["prompts_type"],
"text_response_editor": content["text_response_editor"],
"text_response": content["text_response"],
"file_upload_response": content["file_upload_response"],
"file_upload_type": content["file_upload_type"],
"white_listed_file_types": ",".join(content["white_listed_file_types"]),
"allow_multiple_files": str(content["allow_multiple_files"]),
}

if content["file_upload_type"] is not None:
attributes["file_upload_type"] = content["file_upload_type"]

if content["white_listed_file_types"]:
attributes["white_listed_file_types"] = ",".join(content["white_listed_file_types"])

return attributes
2 changes: 1 addition & 1 deletion tests/fixtures_data/studio_course_xml/course.xml
Original file line number Diff line number Diff line change
Expand Up @@ -407,7 +407,7 @@ allow="autoplay *">
</openassessment>
</vertical>
<vertical display_name="Assignment 2. Television role in education composition" url_name="xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx">
<openassessment allow_multiple_files="True" display_name="Assignment 2. Television role in education composition" file_upload_response="" file_upload_type="pdf-and-image" prompts_type="text" text_response="required" text_response_editor="text" url_name="resource_assignment_2" white_listed_file_types="pdf,gif,jpg,jpeg,jfif,pjpeg,pjp,png">
<openassessment allow_multiple_files="True" display_name="Assignment 2. Television role in education composition" file_upload_response="" prompts_type="text" text_response="required" text_response_editor="text" url_name="resource_assignment_2">
<title>Television role in education</title>
<assessments>
<assessment name="staff-assessment" required="True"/>
Expand Down

0 comments on commit 28a6d8d

Please sign in to comment.