boring_edu_doc/Scripts/python/Library/gquiz.py

124 lines
4.4 KiB
Python

# coding: utf-8
from __future__ import unicode_literals
import time
from gservice import gservice
class gquiz(gservice):
def __init__(self):
'''
Args :
templateId : get the id from link GoogleForm
'''
super(gquiz, self).__init__()
self.image_temp_service_url = "https://git.manakin-gentoo.ts.net/a2nr/boring_edu_doc/raw/branch/mapel_data_science"
# self.image_temp_service_url = "https://tmpfiles.org/api/v1/upload"
# self.image_temp_service_url = "https://uguu.se/upload.php"
self.submition = {"requests":[]}
self.main_form = None
self.progress = -1
self.resultUri = ""
self.submition["requests"].append({
"updateSettings": {
"updateMask": "*" ,
"settings": {
"quizSettings": {
"isQuiz": True
}
}
}
})
def generateService(self, name):
SCOPES = ["https://www.googleapis.com/auth/forms.body",]
DISCOVERY_DOC = "https://forms.googleapis.com/$discovery/rest?version=v1"
self.generateToken('form', SCOPES)
self.buildFormService(DISCOVERY_DOC)
self.main_form = self.service.forms().create(body={"info":{"title":name}}).execute()
def createOption(self, index, value, image=None):
'''
return {"value" : "A. option 1"}
return {"value": "Option",
"image": {
"sourceUri": "",
"properties": {
"alignment": "LEFT"
}
}}
'''
super().createOption(index, value, image)
opt = {"value" : "{}. {}".format(chr(64+index), value)}
if(image != None):
opt.update({"image" : {
"sourceUri": self.image_temp_service_url + image,
"properties": {"alignment": "LEFT"}
}})
return opt
def createQuestion(self, title, description, options, indexAnswer, itemImage=None):
'''
Args:
"title" : String
"desc" : String
"indexAnswer" : Integer, index for "options"
"options" :
[{"value" : "A. option 1"},
{"value" : "B. option 2"},
{"value" : "C. option 3"},
{"value" : "D. option 4"},
{"value" : "E. option 5"},]
'''
super().createQuestion(title, description, options, indexAnswer, itemImage)
item = {
"title" : title,
"description" : "{}".format(description),
"questionItem" : {
"question" : {
"grading" : {
"pointValue": 1,
"correctAnswers": {
"answers" : [ {"value": "{}".format(options[indexAnswer-1]["value"])} ]
}
},
"choiceQuestion" : {
"type" : "RADIO",
"options" : options
}
}
}
}
if (itemImage != None):
item['questionItem'].update(\
{"image": {
"sourceUri": self.image_temp_service_url + itemImage,
"properties": {
"alignment": "CENTER"
}
}
})
return item
def submitItem(self, index, item):
super().submitItem()
self.submition['requests'].append({
"createItem" : {
"location": {"index": index},
"item": item
}
})
print(self.submition)
def update(self):
super().update()
# Adds the question to the form
question_setting = self.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.service.forms().get(formId=self.main_form["formId"]).execute()
self.resultUri = get_result['responderUri']
print(get_result)
self.infoPrint("Updating Form... Done",self.progress)