# 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, \ (("","", \ "", "", "", \ "", "", "", \ "", "", "", \ "", "", "", \ "", "", "", ),)) def _statusBarInfoUpdate(text : str, progress : int): ui.ShowProgressBar("loading...", "{}% {}".format(progress,text), progress) def _updateQuestion(q, isReverse = False): selctedCell = doc.CurrentSelection # cwd = curpath maxRow = doc.LastRow(selctedCell)+1-doc.FirstRow(selctedCell) rnge = reversed(range(0, maxRow)) if isReverse else range(0, maxRow) for nrow in rnge: q.setProgress(int(((nrow+1)/maxRow)*100)) item = doc.getValue(doc.Offset(doc.FirstCell(selctedCell),nrow,0,1,17)) # item[0] # item[1] # item[2] # item[3] # item[4] # item[5] # item[6] # item[7] # item[8] # item[9] # item[10] # item[11] # item[12] # item[13] # item[14] # item[15] # item[16] opt, theAnswer, c = [], -1, 1 for o in range(2, 17, 3): # mengambil kolim jawaban saja if (item[o] == 1) theAnswer = c opt.append(\ q.createOption(\ "{}. {}".format(chr(64+c),item[o+2]),\ cwd+item[o+1] if (item[o+1] != "") else None)) c = c+1 if (theAnswer == -1): raise Exception("Chose the correct answer") img = cwd+item[1] if ( item[1] != "") else None qq = q.createQuestion(\ title = "Soal No {}".format(nrow+1),\ description = item[0],\ indexAnswer = theAnswer, \ options = opt, itemImage=img) 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() q.createForm("Demo Soal") #TODO : put this mundane inside generateService() _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)) def GoogleDocs(): _load_module() from gdoc import gdoc q = gdoc() q.setSavePath(curpath) q.AttachProcessInfo(_statusBarInfoUpdate) q.setProgress(0) q.generateService() q.createDocs("Demo Soal") #TODO : put this mundane inside generateService() _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) ui.SetStatusbar("Done!") _statusBarInfoUpdate("Check *.xml file in curent folder!",100) g_exportedScripts = (MakeTemplate, GoogleQuiz, GoogleDocs, MoodleQuiz)