2023-02-26 20:29:34 +07:00
|
|
|
# coding: utf-8
|
|
|
|
from __future__ import unicode_literals
|
|
|
|
|
2024-03-16 17:33:23 +07:00
|
|
|
import time
|
|
|
|
from gservice import gservice
|
2023-02-26 20:29:34 +07:00
|
|
|
|
2024-03-16 17:33:23 +07:00
|
|
|
class gquiz(gservice):
|
2023-02-26 20:29:34 +07:00
|
|
|
def __init__(self):
|
|
|
|
'''
|
|
|
|
Args :
|
|
|
|
templateId : get the id from link GoogleForm
|
|
|
|
'''
|
2024-03-16 17:33:23 +07:00
|
|
|
super(gquiz, self).__init__()
|
2024-03-16 18:46:56 +07:00
|
|
|
self.image_temp_service_url = "https://git.manakin-gentoo.ts.net/a2nr/boring_edu_doc/raw/branch/mapel_data_science"
|
2024-03-16 17:33:23 +07:00
|
|
|
# self.image_temp_service_url = "https://tmpfiles.org/api/v1/upload"
|
2023-02-27 11:16:57 +07:00
|
|
|
# self.image_temp_service_url = "https://uguu.se/upload.php"
|
2023-02-26 20:29:34 +07:00
|
|
|
self.submition = {"requests":[]}
|
|
|
|
self.main_form = None
|
2023-03-10 23:27:23 +07:00
|
|
|
self.progress = -1
|
|
|
|
self.resultUri = ""
|
2023-02-26 20:29:34 +07:00
|
|
|
|
|
|
|
self.submition["requests"].append({
|
|
|
|
"updateSettings": {
|
|
|
|
"updateMask": "*" ,
|
|
|
|
"settings": {
|
|
|
|
"quizSettings": {
|
|
|
|
"isQuiz": True
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2024-03-16 17:33:23 +07:00
|
|
|
def generateService(self, name):
|
2024-03-14 23:07:12 +07:00
|
|
|
SCOPES = ["https://www.googleapis.com/auth/forms.body",]
|
2023-02-26 20:29:34 +07:00
|
|
|
DISCOVERY_DOC = "https://forms.googleapis.com/$discovery/rest?version=v1"
|
2024-03-16 17:33:23 +07:00
|
|
|
self.generateToken('form', SCOPES)
|
|
|
|
self.buildFormService(DISCOVERY_DOC)
|
|
|
|
self.main_form = self.service.forms().create(body={"info":{"title":name}}).execute()
|
2023-02-26 20:29:34 +07:00
|
|
|
|
2024-03-16 17:33:23 +07:00
|
|
|
def createOption(self, index, value, image=None):
|
2023-02-26 20:29:34 +07:00
|
|
|
'''
|
|
|
|
return {"value" : "A. option 1"}
|
|
|
|
return {"value": "Option",
|
|
|
|
"image": {
|
|
|
|
"sourceUri": "",
|
|
|
|
"properties": {
|
|
|
|
"alignment": "LEFT"
|
|
|
|
}
|
|
|
|
}}
|
|
|
|
'''
|
2024-03-16 17:33:23 +07:00
|
|
|
super().createOption(index, value, image)
|
|
|
|
opt = {"value" : "{}. {}".format(chr(64+index), value)}
|
2023-02-26 20:29:34 +07:00
|
|
|
if(image != None):
|
|
|
|
opt.update({"image" : {
|
2024-03-16 17:33:23 +07:00
|
|
|
"sourceUri": self.image_temp_service_url + image,
|
|
|
|
"properties": {"alignment": "LEFT"}
|
2023-02-26 20:29:34 +07:00
|
|
|
}})
|
|
|
|
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"},]
|
|
|
|
'''
|
2024-03-16 17:33:23 +07:00
|
|
|
super().createQuestion(title, description, options, indexAnswer, itemImage)
|
2023-02-26 20:29:34 +07:00
|
|
|
item = {
|
|
|
|
"title" : title,
|
2023-02-28 22:00:33 +07:00
|
|
|
"description" : "{}".format(description),
|
2023-02-26 20:29:34 +07:00
|
|
|
"questionItem" : {
|
|
|
|
"question" : {
|
|
|
|
"grading" : {
|
|
|
|
"pointValue": 1,
|
|
|
|
"correctAnswers": {
|
2023-02-28 22:00:33 +07:00
|
|
|
"answers" : [ {"value": "{}".format(options[indexAnswer-1]["value"])} ]
|
2023-02-26 20:29:34 +07:00
|
|
|
}
|
|
|
|
},
|
|
|
|
"choiceQuestion" : {
|
|
|
|
"type" : "RADIO",
|
|
|
|
"options" : options
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (itemImage != None):
|
|
|
|
item['questionItem'].update(\
|
|
|
|
{"image": {
|
2024-03-16 17:33:23 +07:00
|
|
|
"sourceUri": self.image_temp_service_url + itemImage,
|
2023-02-26 20:29:34 +07:00
|
|
|
"properties": {
|
|
|
|
"alignment": "CENTER"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
return item
|
|
|
|
|
2024-03-12 22:15:06 +07:00
|
|
|
def submitItem(self, index, item):
|
2024-03-16 17:33:23 +07:00
|
|
|
super().submitItem()
|
2023-02-26 20:29:34 +07:00
|
|
|
self.submition['requests'].append({
|
|
|
|
"createItem" : {
|
|
|
|
"location": {"index": index},
|
|
|
|
"item": item
|
|
|
|
}
|
|
|
|
})
|
2023-02-28 22:00:33 +07:00
|
|
|
print(self.submition)
|
2023-02-26 20:29:34 +07:00
|
|
|
|
|
|
|
def update(self):
|
2024-03-16 17:33:23 +07:00
|
|
|
super().update()
|
2023-02-26 20:29:34 +07:00
|
|
|
# Adds the question to the form
|
2024-03-16 18:12:39 +07:00
|
|
|
question_setting = self.service.forms().batchUpdate(formId=self.main_form["formId"], body=self.submition).execute()
|
2023-02-26 20:29:34 +07:00
|
|
|
print(question_setting)
|
|
|
|
|
|
|
|
# Prints the result to show the question has been added
|
2024-03-16 17:33:23 +07:00
|
|
|
get_result = self.service.forms().get(formId=self.main_form["formId"]).execute()
|
2023-03-10 23:27:23 +07:00
|
|
|
self.resultUri = get_result['responderUri']
|
2023-02-26 20:29:34 +07:00
|
|
|
print(get_result)
|
2023-03-10 23:27:23 +07:00
|
|
|
self.infoPrint("Updating Form... Done",self.progress)
|