diff --git a/Scripts/Python/Library/gquiz.py b/Scripts/Python/Library/gquiz.py index b96822d..cde4489 100644 --- a/Scripts/Python/Library/gquiz.py +++ b/Scripts/Python/Library/gquiz.py @@ -23,6 +23,9 @@ class gquiz: self.form_service = None self.drive_service = None self.main_form = None + self.infoPrint = self._defaultPrint + self.progress = -1 + self.resultUri = "" self.submition["requests"].append({ "updateSettings": { @@ -35,6 +38,14 @@ class gquiz: } }) + def setProgress(self, i : int): + self.progress = i + def AttachProcessInfo(self, callback): + self.infoPrint = callback + + def _defaultPrint(self, text : str, progress: int): + print("{}% {}".format(progress, text)) + def generateService(self): ''' Start Tokenizing here is the way to get token @@ -52,19 +63,23 @@ class gquiz: # time. if os.path.exists('token.json'): creds = Credentials.from_authorized_user_file('token.json', SCOPES) + self.infoPrint("token already exist",self.progress) # If there are no (valid) credentials available, let the user log in. if not creds or not creds.valid: if creds and creds.expired and creds.refresh_token: creds.refresh(Request()) + self.infoPrint("refresh token",self.progress) else: flow = InstalledAppFlow.from_client_secrets_file( './secret/client_secrets.json', SCOPES) creds = flow.run_local_server(port=0) + self.infoPrint("generate token",self.progress) # Save the credentials for the next run with open('token.json', 'w') as token: token.write(creds.to_json()) try: + self.infoPrint("creating service",self.progress) service = build('docs', 'v1', credentials=creds) self.form_service, self.drive_service = \ build('forms', 'v1', credentials=creds, discoveryServiceUrl=DISCOVERY_DOC, static_discovery=False),\ @@ -81,6 +96,7 @@ class gquiz: name : name form """ try: + self.infoPrint("creating form",self.progress) self.main_form = self.form_service.forms().create(body={"info":{"title":name}}).execute() except HttpError as error: print('An error occurred: %s' % error) @@ -120,13 +136,15 @@ class gquiz: } }} ''' + self.infoPrint("creating option",self.progress) opt = {"value" : "{}".format(value)} if(image != None): - print("print with image, uploading... ") + self.infoPrint("uploading option image... ",self.progress) req = requests.post(self.image_temp_service_url,files={"file": open(image,'rb')}) if(req.json()['status'] == 'error'): raise Exception("upload failed : {}".format(req.json())) - print("success") + time.sleep(3) + self.infoPrint("uploading option image... ok",self.progress) u = urlparse(req.json()['data']['url']) opt.update({"image" : { "sourceUri": u._replace(path="/dl"+u.path).geturl(), @@ -168,12 +186,14 @@ class gquiz: } } } + self.infoPrint("create question...",self.progress) if (itemImage != None): - print("uploading image for quesiton : ") + self.infoPrint("uploading question image...",self.progress) req = requests.post(self.image_temp_service_url,files={"file": open(itemImage,'rb')}) if(req.json()['status'] == 'error'): raise Exception("upload failed : {}".format(req.json())) - print("succes") + time.sleep(3) + self.infoPrint("uploading question image... ok",self.progress) u = urlparse(req.json()['data']['url']) item['questionItem'].update(\ {"image": { @@ -183,7 +203,6 @@ class gquiz: } } }) - time.sleep(3) return item def submitQuestion(self, index, item): @@ -198,14 +217,18 @@ class gquiz: "item": item } }) + self.infoPrint("submit question",self.progress) print(self.submition) def update(self): + self.infoPrint("Updating Form...",self.progress) # Adds the question to the form question_setting = self.form_service.forms().batchUpdate(formId=self.main_form["formId"], body=self.submition).execute() print(question_setting) # Prints the result to show the question has been added get_result = self.form_service.forms().get(formId=self.main_form["formId"]).execute() + self.resultUri = get_result['responderUri'] print(get_result) + self.infoPrint("Updating Form... Done",self.progress) diff --git a/Scripts/Python/Library/moodleQuiz.py b/Scripts/Python/Library/moodleQuiz.py new file mode 100644 index 0000000..850821c --- /dev/null +++ b/Scripts/Python/Library/moodleQuiz.py @@ -0,0 +1,87 @@ +# coding: utf-8 +from __future__ import unicode_literals +import base64 +import xml.etree.ElementTree as ET + +class moodleQuiz: + def __init__(self): + self.infoPrint = self._defaultPrint + self.progress = -1 + self.resultUri = "" + self.data = ET.Element('quiz') + + def setProgress(self, i : int): + self.progress = i + self.infoPrint("generating...", self.progress) + + def AttachProcessInfo(self, callback): + self.infoPrint = callback + + def _defaultPrint(self, text : str, progress: int): + print("{}% {}".format(progress, text)) + + 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, value, image=None): + ans = ET.Element('answer') + ans.set('fraction', '0') + ans.set('format', 'html') + if(image == None): + ET.SubElement(ans, 'text').text = "
{}
".format(value) + else : + f = open(image,"rb") + ET.SubElement(ans, 'text').text = "" + \ + "
"\ + .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): + 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 = "{}
".format(description) + + if (itemImage != None): + f = open(itemImage,"rb") + descText.text = descText.text + "" + \ + "
"\ + .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 submitQuestion(self, index, item): + self.data.append(ET.fromstring(ET.tostring(item))) + + def copyFile(self,origin_file_id, copy_title): + print("noting to copy, it for moodle") + + def update(self): + ET.indent(self.data) + print(ET.tostring(self.data)) + ET.ElementTree(self.data).write("./moodleXMLMultichoiceQuestion.xml") + + diff --git a/Scripts/Python/quiz_generator.py b/Scripts/Python/quiz_generator.py index ecf51e8..99ae0d2 100644 --- a/Scripts/Python/quiz_generator.py +++ b/Scripts/Python/quiz_generator.py @@ -1,70 +1,102 @@ # coding: utf-8 from __future__ import unicode_literals import sys, os +import requests +import importlib + +# to run separatly from soffice +# $ soffice --calc --accept="socket,host=localhost,port=2002;urp;StarOffice.ServiceManager" # uncomemnt for run separatly from soffice -#sys.path.append("/lib64/libreoffice/program/") +# sys.path.append("/lib64/libreoffice/program/") + from scriptforge import ScriptForge, CreateScriptService from unohelper import fileUrlToSystemPath -# uncomemnt for run separatly from soffice -#ScriptForge(hostname='localhost', port=2021) -def generateTemplate(): - doc = CreateScriptService("Calc") - basic = CreateScriptService("Basic") - doc.SetArray(doc.CurrentSelection, \ - (("