boring_edu_doc/Scripts/python/quiz_generator.py

142 lines
5.0 KiB
Python
Raw Normal View History

# 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/")
from scriptforge import ScriptForge, CreateScriptService
from unohelper import fileUrlToSystemPath
ui = CreateScriptService("UI")
doc = CreateScriptService("Calc")
bas = CreateScriptService("Basic")
url = fileUrlToSystemPath(XSCRIPTCONTEXT.getDocument().URL )
curpath = '/'.join([url.split('/')[x] for x in range(0,len(url.split('/'))-1)])
if not ui.Activate(url) :
bas.MsgBox("Error no window active",bas.MB_OK)
def _load_module():
#[start] comment for run embeded module
if (not 'gquiz' in sys.modules) or (not 'moodleQuiz' in sys.modules):
doc = XSCRIPTCONTEXT.getDocument()
urlem = '{}/{}'.format(url,'/Scripts/python/Library')
sys.path.insert(0, urlem)
_statusBarInfoUpdate("Load embedded modul link form {}".format(url),0)
#[end]
#[start] uncomemnt for run separatly from soffice
#sys.path.insert(0, '{}/{}'.format(os.getcwd(),'Library'))
#ScriptForge(hostname='localhost', port=2002)
#[end]
def MakeTemplate():
doc.SetArray(doc.CurrentSelection, \
(("<Text question>","<IMG question image>", \
"<isAnswerA>", "<IMG option1>", "<Text Option1>", \
"<isAnswerB>", "<IMG option2>", "<Text Option2>", \
"<isAnswerC>", "<IMG option3>", "<Text Option3>", \
"<isAnswerD>", "<IMG option4>", "<Text Option4>", \
"<isAnswerE>", "<IMG option5>", "<Text Option5>", ),))
def _statusBarInfoUpdate(text : str, progress : int):
ui.ShowProgressBar("loading...", "{}% {}".format(progress,text), progress)
def _updateQuestion(q, isReverse = False, needPath = False):
selctedCell = doc.CurrentSelection #
cwd = curpath
maxRow = doc.LastRow(selctedCell)+1-doc.FirstRow(selctedCell)
2024-03-15 20:30:18 +07:00
rnge = reversed(range(0, maxRow)) if isReverse else range(0, maxRow)
for nrow in rnge:
xrow = (nrow - (maxRow-1)) if isReverse else nrow
q.setProgress(int(((xrow+1)/maxRow)*100))
item = doc.getValue(doc.Offset(doc.FirstCell(selctedCell),nrow,0,1,17))
# item[0] <Text question>
# item[1] <IMG question image>
# item[2] <isAnswerA>
# item[3] <IMG option1>
# item[4] <Text Option1>
# item[5] <isAnswerB>
# item[6] <IMG option2>
# item[7] <Text Option2>
# item[8] <isAnswerC>
# item[9] <IMG option3>
# item[10] <Text Option3>
# item[11] <isAnswerD>
# item[12] <IMG option4>
# item[13] <Text Option4>
# item[14] <isAnswerE>
# item[15] <IMG option5>
# item[16] <Text Option5>
opt, theAnswer, c = [], -1, 1
for o in range(2, 17, 3): # mengambil kolim jawaban saja
if (item[o] == 1):
2024-03-15 20:30:18 +07:00
theAnswer = c
if (item[o+1] != ""):
optImg = cwd+item[o+1] if needPath else item[o+1]
else:
optImg = None
opt.append(q.createOption(c, item[o+2], optImg))
c = c+1
2023-02-27 11:16:57 +07:00
if (theAnswer == -1):
raise Exception("Chose the correct answer")
if (item[1] != ""):
itmImg = cwd+item[1] if needPath else item[1]
else:
itmImg = None
qq = q.createQuestion(\
title = "Soal No {}".format(nrow+1),\
description = item[0],\
indexAnswer = theAnswer, \
options = opt,\
itemImage = itmImg)
q.submitItem(nrow,qq)
q.update()
def GoogleQuiz():
_load_module()
from gquiz import gquiz
q = gquiz()
q.setSavePath(curpath)
q.AttachProcessInfo(_statusBarInfoUpdate)
q.setProgress(0)
q.generateService("form generated using macro")
_updateQuestion(q)
ui.SetStatusbar("creating google form quiz done!")
_statusBarInfoUpdate("Finish!!",100)
bas.InputBox("Open link to edit your form:","Your Google Form Quiz, done!", "{}".format(q.resultUri))
2024-03-15 20:30:18 +07:00
def GoogleDocs():
_load_module()
from gdoc import gdoc
q = gdoc()
q.setSavePath(curpath)
q.AttachProcessInfo(_statusBarInfoUpdate)
q.setProgress(0)
q.generateService("docs generated using macro")
2024-03-15 20:30:18 +07:00
_updateQuestion(q, True)
ui.SetStatusbar("creating google form quiz done!")
_statusBarInfoUpdate("Finish!!",100)
bas.InputBox("Open link to edit your form:","Your Google Form Quiz, done!", "{}".format(q.resultUri))
def MoodleQuiz():
_load_module()
from moodleQuiz import moodleQuiz
q = moodleQuiz()
q.setSavePath(curpath)
q.AttachProcessInfo(_statusBarInfoUpdate)
q.setProgress(0)
_updateQuestion(q, needPath=True)
ui.SetStatusbar("Done!")
_statusBarInfoUpdate("Check *.xml file in curent folder!",100)
2024-03-15 20:30:18 +07:00
g_exportedScripts = (MakeTemplate, GoogleQuiz, GoogleDocs, MoodleQuiz)