Skip to content

Commit

Permalink
add usage of H5P.DragQuestion 1.14 and H5P.SingleChoiceSet 1.11
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Munz committed Jan 27, 2023
1 parent 651be2f commit 7ec810f
Showing 1 changed file with 61 additions and 6 deletions.
67 changes: 61 additions & 6 deletions h5p_python/h5ptranslate.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,22 +75,71 @@ def getDragTextText(self):
text +="</feedback>"
text += "</feedbacks>"
text += "<textField>" + self.data['action']['params']['textField'] + "</textField>"
text += "<taskDescription>" + self.data['action']['params']['taskDescription'] + "</taskDescription> "
return text


def setDragTextText(self, translated):
results = BeautifulSoup(translated, 'html.parser')

for cnt,a in enumerate(results.findAll("feedbacks")):
self.data['action']['params']['overallFeedback'][cnt]['feedback'] = a.find('feedback').getText()
self.data['action']['params']['textField'] = results.find('textfield').getText()
fb = a.find('feedback')
if fb is not None:
self.data['action']['params']['overallFeedback'][cnt]['feedback'] = fb.getText()
tf = results.find('textfield')
if tf is not None:
self.data['action']['params']['textField'] = tf.getText()
td = results.find('taskdescription')
if td is not None:
self.data['action']['params']['taskDescription'] = td.getText()


def getDragQuestionText(self):
return self.data['action']['metadata'].get('title', None)

def setDragQuestionText(self, translated):
self.data['action']['metadata']['title'] = translated

def getSingleChoiceSetText(self):
text = "<title>" + self.data['action']['metadata']['title'] + "</title>"
choices = self.data['action']['params']['choices']
text += "<choices>"
for c in choices:
text += "<question>"+c['question']+"</question>"
text += "<answers>"
answers = c['answers']
for a in answers:
text += a
text += "</answers>"
return text

def setSingleChoiceSetText(self, translated):
results = BeautifulSoup(translated, 'html.parser')

for q_cnt, c in enumerate(results.findAll("choices")):
q = c.find('question')
if q is not None:
self.data['action']['params']['choices'][q_cnt]['question'] = q.getText()
for a_cnt, a in enumerate(c.findAll("answers")):
a = a.find('answer')
if a is not None:
self.data['action']['params']['choices'][q_cnt]['answers'][a_cnt] = a.getText()

tf = results.find('title')
if tf is not None:
self.data['action']['metadata']['title'] = tf.getText()



def getText(self):
if self.isMultiChoiceElement():
text = self.getMultiChoiceText()
elif self.isDragTextElement():
text = self.getDragTextText()
elif self.isDragQuestionElement():
text = self.getDragQuestionText()
elif self.isSingleChoiceSet():
text = self.getSingleChoiceSetText()
else:
text = self.data['action']['params'].get('text', None)
if text is None:
Expand All @@ -104,20 +153,22 @@ def getText(self):

def setText(self, text):
if self.isMultiChoiceElement():
self.setMultiChoiceText(text)
self.setMultiChoiceText(text)
elif self.isDragTextElement():
text = self.setDragTextText(text)
self.setDragTextText(text)
elif self.isDragQuestionElement():
self.setDragQuestionText(text)
elif self.isSingleChoiceSet():
self.setSingleChoiceSetText(text)
else:
cur_text = self.data['action']['params'].get('text', None)
if cur_text is not None:
self.data['action']['params']['text'] = text
return

cur_text = self.data['action']['params'].get('question', None)
if cur_text is not None:
self.data['action']['params']['question'] = text
return

cur_text = self.data['action']['params'].get('textField', None)
if cur_text is not None:
self.data['action']['params']['textField'] = text
Expand Down Expand Up @@ -157,7 +208,11 @@ def isMultiChoiceElement(self):
def isDragTextElement(self):
return self.getLibrary() == "H5P.DragText 1.10"

def isDragQuestionElement(self):
return self.getLibrary() == "H5P.DragQuestion 1.14"

def isSingleChoiceSet(self):
return self.getLibrary() == "H5P.SingleChoiceSet 1.11"

def getMetaData(self, key):
metadatastr = self.data['action']['metadata'].get('authorComments', "{}")
Expand Down

0 comments on commit 7ec810f

Please sign in to comment.