80 lines
3.2 KiB
Python
80 lines
3.2 KiB
Python
# coding: utf-8
|
|
from __future__ import unicode_literals
|
|
import base64
|
|
import xml.etree.ElementTree as ET
|
|
from bedc import bedc
|
|
|
|
class moodleQuiz(bedc):
|
|
def __init__(self):
|
|
super(moodleQuiz, self).__init__()
|
|
self.progress = -1
|
|
self.resultUri = ""
|
|
self.data = ET.Element('quiz')
|
|
|
|
def _subElementFile(self, parent, file):
|
|
Efile = ET.SubElement(parent, "file")
|
|
Efile.set('name',file.name.split("/").pop())
|
|
Efile.set('path','/')
|
|
Efile.set('encoding','base64')
|
|
Efile.text = base64.b64encode(file.read()).decode('utf-8')
|
|
|
|
def createOption(self, index, value, image=None):
|
|
super().createOption(index, value, image)
|
|
ans = ET.Element('answer')
|
|
ans.set('fraction', '0')
|
|
ans.set('format', 'html')
|
|
if(image == None):
|
|
ET.SubElement(ans, 'text').text = "<p dir=\"ltr\" style=\"text-align: left;\">{}. {}</p>".format(index, value)
|
|
else :
|
|
f = open(image,"rb")
|
|
ET.SubElement(ans, 'text').text = "<p dir=\"ltr\" style=\"text-align: left;\">" + \
|
|
"<img src=\"@@PLUGINFILE@@/{}\" alt=\"{}\" class=\"img-fluid atto_image_button_text-bottom\"></p>"\
|
|
.format(f.name.split("/").pop(), f.name.split("/").pop().split(".")[0])
|
|
self._subElementFile(ans, f)
|
|
ET.SubElement(ET.SubElement(ans,'feedback'), 'text').text = "Ooopss!"
|
|
return ans
|
|
|
|
|
|
def createQuestion(self, title, description, options, indexAnswer, itemImage=None):
|
|
super().createQuestion(title, description, options, indexAnswer, itemImage)
|
|
question = ET.Element('question')
|
|
question.set('type','multichoice')
|
|
|
|
ET.SubElement(ET.SubElement(question, 'name'), 'text')\
|
|
.text = title
|
|
|
|
descQuestion = ET.SubElement(question, 'questiontext')
|
|
descQuestion.set('format', 'html')
|
|
descText = ET.SubElement(descQuestion, 'text')
|
|
descText.text = "<p dir=\"ltr\" style=\"text-align: left;\">{}</p>".format(description)
|
|
|
|
if (itemImage != None):
|
|
f = open(itemImage,"rb")
|
|
descText.text = descText.text + "<p dir=\"ltr\" style=\"text-align: left;\">" + \
|
|
"<img src=\"@@PLUGINFILE@@/{}\" alt=\"{}\" class=\"img-fluid atto_image_button_text-bottom\"></p>"\
|
|
.format(f.name.split("/").pop(), f.name.split("/").pop().split(".")[0])
|
|
self._subElementFile(descQuestion, f)
|
|
|
|
for i in range(0, len(options)):
|
|
ans = options[i]
|
|
if i == (indexAnswer-1) :
|
|
ans.set('fraction', '100')
|
|
ans.find('feedback').find('text').text = "YES!"
|
|
question.append(ET.fromstring(ET.tostring(ans)))
|
|
|
|
ET.SubElement(question, 'shuffleanswers').text = '0'
|
|
ET.SubElement(question, 'answernumbering').text = 'abc'
|
|
return question
|
|
|
|
def submitItem(self, index, item):
|
|
super().submitItem()
|
|
self.data.append(ET.fromstring(ET.tostring(item)))
|
|
|
|
def update(self):
|
|
super().update()
|
|
ET.indent(self.data)
|
|
print(ET.tostring(self.data))
|
|
ET.ElementTree(self.data).write("{}/{}".format(self.savePath, "moodleXMLMultichoiceQuestion.xml"))
|
|
|
|
|