# -*- coding: utf-8 # -*- Mode: Python; py-indent-offset: 4 -*- # $Id$ # # Copyright (c) 2006, Vahur Rebas, HTK, TLU # # Permission to use, copy, modify, and distribute this software for any # purpose with or without fee is hereby granted, provided that the above # copyright notice and this permission notice appear in all copies. # # THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR # ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF # OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. """ Commons for questions and tests""" __version__ = "$Revision:33$"[11:-2] import Globals from Globals import Acquisition, Persistent from Acquisition import aq_base, aq_inner, aq_parent, Explicit from AccessControl import ClassSecurityInfo from OFS.Folder import Folder import zipfile from StringIO import StringIO import tempfile from Permissions import perm_view_question, perm_edit_question class commons: """ bridge between questions and tests """ security = ClassSecurityInfo() security.declareObjectPublic() def getQuestionById(self, id): """ get question by id """ return self.questions._getOb(id) def random_generator(self, prefix='question_'): import random return prefix+str(random.randint(1, 9999999999)) def random_nr(self): import random return random.randint(1, 999999999999999) def formatDateLong(self, date): """" """ import time date = str(date).split('.')[0] try: date = time.strptime(date, "%Y-%m-%d %H:%M:%S") date = time.strftime("%d-%m-%Y %H:%M", date) except ValueError: pass return date def _makeZip(self, remove): fd, filename = tempfile.mkstemp() import os os.close(fd) zip = zipfile.ZipFile(filename,"w",zipfile.ZIP_DEFLATED) f = StringIO() f.write(self.manifest_beginning()) from cStringIO import StringIO as cStringIO for ques in remove: x = getattr(self.questions, ques) #print x.xml2() xml2 = x.xml2(fullURLs=True) xml2 = xml2.encode('utf-8') zip.writestr(zipfile.ZipInfo(ques + ".xml"), xml2) #imageids = x.getImages() #for z in imageids: # picture = getattr(self.images, z) # # bindata = picture.data # streem = cStringIO() # if type(bindata) is type(''): # streem.write(bindata) # else: # while bindata is not None: # streem.write(bindata.data) # bindata = bindata.next # # imageContentType =picture.content_type.split('/') # imagetype = imageContentType[-1] # # streem.seek(0) # zip.writestr(zipfile.ZipInfo(z +"."+imagetype), streem.read()) quesType = x.getType() #if quesType == 'hotspot_type': # zip.writestr(zipfile.ZipInfo(x.getId() +".jpg"), x.drawPic().read()) if quesType == 'Choice_multiple': video_id = x.getVideoId() if video_id != '': video = getattr(self.videos, video_id) zip.writestr(zipfile.ZipInfo(video_id+".mov"), str(video.data)) f.write(x.package()) f.write(self.manifest_end()) f.seek(0) zip.writestr(zipfile.ZipInfo("imsmanifest.xml"), f.read()) zip.close() file = open(filename, 'rb') export_data=file.read() file.close() #import os #os.close(fd) try: os.remove(filename) except OSError: pass return export_data def manifest_beginning(self): """ """ pack='\n' pack += ' \n' pack=pack + '\t\n' pack=pack + '\t\tIMS Content\n' pack=pack + '\t\t1.1\n' pack=pack + '\t\t\n' pack=pack + '\t\t\t\n' pack=pack + '\t\t\t\t\n' pack=pack + '\t\t\t\t\tContentpackage with QTI v2.1 items\n' pack=pack + '\t\t\t\t\n' pack=pack + '\t\t\t\ten\n' pack=pack + '\t\t\t\t\n' pack=pack + '\t\t\t\t\tContentpackage\n' pack=pack + '\t\t\t\t\n' pack=pack + '\t\t\t\n' pack=pack + '\t\t\t\n' pack=pack + '\t\t\t\tLOMv1.0\n' pack=pack + '\t\t\t\tQTIv2.1\n' pack=pack + '\t\t\t\ten\n' pack=pack + '\t\t\t\n' pack=pack + '\t\t\t\n' pack=pack + '\t\t\t\t\n' pack=pack + '\t\t\t\t\t\n' pack=pack + '(c) 2004, IMS Global Learning Consortium; individual questions may have own copyright statement.' pack=pack + '\t\t\t\t\t\n' pack=pack + '\t\t\t\t\n' pack=pack + '\t\t\t\n' pack=pack + '\t\t\n' pack=pack + '\t\n' pack=pack + '\t\n' pack=pack + '\t\n' #REQUEST.RESPONSE.setHeader("Content-type", "text/xml") return pack def manifest_end(self): return "\t\n" from Products.Five.bridge import fromZ2Interface import zope.interface def classImplements(class_, *interfaces): # convert any Zope 2 interfaces to Zope 3 using fromZ2Interface interfaces = flatten(interfaces) normalized_interfaces = [] for i in interfaces: try: i = fromZ2Interface(i) except ValueError: # already a Zope 3 interface pass assert issubclass(i, zope.interface.Interface) normalized_interfaces.append(i) return zope.interface.classImplements(class_, *normalized_interfaces) def flatten(interfaces): flattened = [] _detuplize(interfaces, flattened.append) return tuple(flattened) def _detuplize(interfaces, append): if isinstance(interfaces, (tuple, list)): for sub in interfaces: _detuplize(sub, append) else: append(interfaces) # EOF