support multi soal sekaligus
							parent
							
								
									8c4fb14bcf
								
							
						
					
					
						commit
						2dfadd4ae9
					
				| 
						 | 
				
			
			@ -1,7 +1,7 @@
 | 
			
		|||
# coding: utf-8
 | 
			
		||||
from __future__ import unicode_literals
 | 
			
		||||
 | 
			
		||||
import os.path
 | 
			
		||||
import os.path, time
 | 
			
		||||
from google.auth.transport.requests import Request
 | 
			
		||||
from google.oauth2.credentials import Credentials
 | 
			
		||||
from google_auth_oauthlib.flow import InstalledAppFlow
 | 
			
		||||
| 
						 | 
				
			
			@ -18,6 +18,7 @@ class gquiz:
 | 
			
		|||
            templateId : get the id from link GoogleForm
 | 
			
		||||
        '''
 | 
			
		||||
        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.form_service = None
 | 
			
		||||
        self.drive_service = None 
 | 
			
		||||
| 
						 | 
				
			
			@ -74,6 +75,16 @@ class gquiz:
 | 
			
		|||
        ''' End Tokenizing
 | 
			
		||||
        '''
 | 
			
		||||
 | 
			
		||||
    def createForm(self, name):
 | 
			
		||||
        """ Create Form
 | 
			
		||||
            Args: 
 | 
			
		||||
            name : name form
 | 
			
		||||
        """
 | 
			
		||||
        try: 
 | 
			
		||||
            self.main_form = self.form_service.forms().create(body={"info":{"title":name}}).execute()
 | 
			
		||||
        except HttpError as error:
 | 
			
		||||
            print('An error occurred: %s' % error)			
 | 
			
		||||
 | 
			
		||||
    def copyFile(self,origin_file_id, copy_title):
 | 
			
		||||
        """Copy an existing file.
 | 
			
		||||
        Args:
 | 
			
		||||
| 
						 | 
				
			
			@ -97,7 +108,6 @@ class gquiz:
 | 
			
		|||
 | 
			
		||||
        except HttpError as error:
 | 
			
		||||
            print('An error occurred: %s' % error)
 | 
			
		||||
            return None
 | 
			
		||||
 | 
			
		||||
    def createOption(self, value, image=None):
 | 
			
		||||
        '''
 | 
			
		||||
| 
						 | 
				
			
			@ -124,6 +134,7 @@ class gquiz:
 | 
			
		|||
                            "alignment": "LEFT"
 | 
			
		||||
                            }
 | 
			
		||||
                        }})
 | 
			
		||||
        time.sleep(3)
 | 
			
		||||
        return opt
 | 
			
		||||
 | 
			
		||||
    def createQuestion(self, title, description, options, indexAnswer, itemImage=None):
 | 
			
		||||
| 
						 | 
				
			
			@ -172,6 +183,7 @@ class gquiz:
 | 
			
		|||
                            }
 | 
			
		||||
                        }
 | 
			
		||||
                     })
 | 
			
		||||
        time.sleep(3)
 | 
			
		||||
        return item
 | 
			
		||||
 | 
			
		||||
    def submitQuestion(self, index, item):
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -0,0 +1,23 @@
 | 
			
		|||
import requests
 | 
			
		||||
 | 
			
		||||
class ImageProvides:
 | 
			
		||||
    def __init__(self):
 | 
			
		||||
        self.service_url = [ \
 | 
			
		||||
                "https://tmpfiles.org/api/v1/upload" ]
 | 
			
		||||
        self.service_use = 0
 | 
			
		||||
        self.successStatus = False
 | 
			
		||||
        self.urlImage = ""
 | 
			
		||||
 | 
			
		||||
    def upload(self, imagePath):
 | 
			
		||||
        try: 
 | 
			
		||||
            urlService = self.service_url[self.service_use]
 | 
			
		||||
            req = requests.post(urlService,files={"file": open(image,'rb')})
 | 
			
		||||
            if(req.json()['status'] == 'error'):
 | 
			
		||||
                self.successStatus = False
 | 
			
		||||
            self.successStatus = True
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    def isSuccess(self):
 | 
			
		||||
        return self.successStatus
 | 
			
		||||
 | 
			
		||||
    def getUrl(self):
 | 
			
		||||
| 
						 | 
				
			
			@ -7,50 +7,59 @@ def generateTemplate():
 | 
			
		|||
	doc = CreateScriptService("Calc")
 | 
			
		||||
	basic = CreateScriptService("Basic")
 | 
			
		||||
	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>", ),))
 | 
			
		||||
	  (("<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 updateQuestion():
 | 
			
		||||
	import sys, os
 | 
			
		||||
	from unohelper import fileUrlToSystemPath
 | 
			
		||||
	if not 'gquiz' in sys.modules:
 | 
			
		||||
		doc = XSCRIPTCONTEXT.getDocument()
 | 
			
		||||
		url = fileUrlToSystemPath('{}/{}'.format(doc.URL,'Scripts/python/Library'))
 | 
			
		||||
		sys.path.insert(0, url)
 | 
			
		||||
	from gquiz import gquiz
 | 
			
		||||
	import requests
 | 
			
		||||
		
 | 
			
		||||
  import sys, os
 | 
			
		||||
  from unohelper import fileUrlToSystemPath
 | 
			
		||||
  if not 'gquiz' in sys.modules:
 | 
			
		||||
    doc = XSCRIPTCONTEXT.getDocument()
 | 
			
		||||
    url = fileUrlToSystemPath('{}/{}'.format(doc.URL,'Scripts/python/Library'))
 | 
			
		||||
    sys.path.insert(0, url)
 | 
			
		||||
    
 | 
			
		||||
  from gquiz import gquiz
 | 
			
		||||
  import requests
 | 
			
		||||
	
 | 
			
		||||
	doc = CreateScriptService("Calc")
 | 
			
		||||
	item = doc.getValue(doc.Offset(doc.CurrentSelection,0,0,1,17))
 | 
			
		||||
	
 | 
			
		||||
	q = gquiz()
 | 
			
		||||
  doc = CreateScriptService("Calc")
 | 
			
		||||
  selctedCell = doc.CurrentSelection
 | 
			
		||||
  
 | 
			
		||||
  q = gquiz()
 | 
			
		||||
  q.generateService()
 | 
			
		||||
  q.createForm("Demo Soal")
 | 
			
		||||
	cwd = os.getcwd()
 | 
			
		||||
  for nrow in range(0, doc.LastRow(selctedCell)+1-doc.FirstRow(selctedCell)):
 | 
			
		||||
    item = doc.getValue(doc.Offset(doc.FirstCell(selctedCell),nrow,0,1,17))
 | 
			
		||||
    opt = []
 | 
			
		||||
    theAnswer = -1
 | 
			
		||||
    c = 1
 | 
			
		||||
    
 | 
			
		||||
    for o in range(2,17,3):
 | 
			
		||||
      if (item[o] == 1):
 | 
			
		||||
        theAnswer = c
 | 
			
		||||
      c = c+1
 | 
			
		||||
      if (item[o+1] != ""): 
 | 
			
		||||
        opt.append(q.createOption(item[o+2],cwd+item[o+1]))
 | 
			
		||||
      else:
 | 
			
		||||
        opt.append(q.createOption(item[o+2]))
 | 
			
		||||
    
 | 
			
		||||
    if (theAnswer == -1):
 | 
			
		||||
      raise Exception("Chose the correct answer")	
 | 
			
		||||
 | 
			
		||||
	q.generateService()
 | 
			
		||||
 | 
			
		||||
	q.copyFile("16-rh3W-NwYzdKVBZJmi574sTWe_rMIdE-FQSw_33qXI", "Demo Soal")
 | 
			
		||||
	
 | 
			
		||||
	opt = []
 | 
			
		||||
			
 | 
			
		||||
	theAnswer = -1
 | 
			
		||||
	c = 1
 | 
			
		||||
	for o in range(2,17,3):
 | 
			
		||||
		if (item[o] == 1):
 | 
			
		||||
			theAnswer = c
 | 
			
		||||
		c = c+1
 | 
			
		||||
		opt.append(q.createOption(item[o+2],os.getcwd()+item[o+1]))
 | 
			
		||||
	
 | 
			
		||||
	if (theAnswer == -1):
 | 
			
		||||
		raise Exception("Chose the correct answer")	
 | 
			
		||||
 | 
			
		||||
	qq = q.createQuestion(title = "Soal No 1",\
 | 
			
		||||
	 description = item[0],\
 | 
			
		||||
	 indexAnswer = theAnswer, options = opt, itemImage=os.getcwd()+item[1])
 | 
			
		||||
	q.submitQuestion(0,qq)
 | 
			
		||||
	q.update()	
 | 
			
		||||
    if( item[1] != ""):
 | 
			
		||||
      img = cwd+item[1]
 | 
			
		||||
    else:
 | 
			
		||||
      img = None
 | 
			
		||||
      
 | 
			
		||||
    qq = q.createQuestion(title = "Soal No 1",\
 | 
			
		||||
      description = item[0],\
 | 
			
		||||
      indexAnswer = theAnswer, \
 | 
			
		||||
      options = opt, itemImage=img)
 | 
			
		||||
    q.submitQuestion(nrow,qq)
 | 
			
		||||
  q.update()	
 | 
			
		||||
	
 | 
			
		||||
g_exportedScripts = (generateTemplate, updateQuestion, )
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
		Reference in New Issue