from interfaces import ISCORMTool from zope.interface import implements from Products.Five import BrowserView from zope.component import getMultiAdapter, getAdapter from zope.component import queryMultiAdapter, queryAdapter from zope.component import getUtility from AccessControl import getSecurityManager from Products.iva.interfaces import IIVAPlugin, IStatistics from Products.iva.common import translate from SCORMTool import SCORMTool from urllib2 import HTTPError def _getMyTool(self): return getattr(self.context, SCORMTool.id, None) class ScormUserView(BrowserView): """ scorm view. upload, view etc """ def init_session(self): """ view """ req = self.request u = str(getSecurityManager().getUser()) getUtility(IStatistics)._viewedObject(req, self.context.getRelativeURL(self.context)) p = {'package_id': self.context.getPackageId(), 'access_key': self.context._public_key, 'uname': u } try: sk = self.context._call_remote('/startSession', p)[0] req.SESSION.set('scorm_session_key', sk) req.SESSION.set('userLocation', 'Scorm') return req.RESPONSE.redirect(self.context.absolute_url()+'/@@scorm_init?session_key='+sk) except HTTPError: i18msg = translate(self.context, 'Invalid request. SCORM package cannot be shown.', domain='scorm') return self.context.restrictedTraverse('message_dialog.html')(action=self.context.absolute_url()+'/../', message=i18msg) def saver(self): sc = _getMyTool(self) req = self.request msg = "" if req.get('add_existing'): msg = sc.addExisting(self.context, req) elif req.get('update_existing'): if not req.get('existing_package'): msg = 'Please choose which package you wish to update' else: msg = sc.updateExisting(self.context, req) elif req.get('delete_existing'): msg = sc.deleteExisting(self.context, req) elif req.get('cancel'): return res.RESPONSE.redirect(self.context.absolute_url()) elif req.get('new_key_button'): # add key pk = req.get('package_key') if pk: pk = pk.strip() if not pk: msg = 'Invalid key' else: msg = sc.probeAccess(self.context, req, pk) else: msg = sc.uploadPackage(self.context, req) i18msg = translate(self.context, msg, domain='scorm') return self.context.restrictedTraverse('message_dialog.html')(action=self.context.absolute_url(), message=i18msg) def getPlayerLocation(self): return _getMyTool(self).getPlayerLocation() def registerVisit(self): #print "ScormUserView, register visit" pass class ScormManagerView(BrowserView): """ scorm management view """ def saver(self): vis = self.request.get('is_visible', 0) loc = self.request.get('scorm_player_location', '') ide = self.request.get('scorm_player_identification', '') sma = getAdapter(self.context, ISCORMTool, '') sma.setVisible(vis) sma.setLocation(loc) sma.setIdentification(ide) return self.request.RESPONSE.redirect('@@manage-scorm') def isVisible(self): return getAdapter(self.context, ISCORMTool, '').isVisible() def getPlayerLocation(self): return _getMyTool(self).getPlayerLocation() def getPlayerIdentification(self): return _getMyTool(self)._getPlayerIdentification() def registerVisit(self): #print "ScormManagerView, register visit" pass class ScormManagerAdapter(object): implements(ISCORMTool, IIVAPlugin) def __init__(self, context): self.context = context self.tool = _getMyTool(self) def setVisible(self, vis): self.tool.setVisible(vis) def setLocation(self, loc): self.tool.setPlayerLocation(loc) def setIdentification(self, ide): self.tool.setPlayerIdentification(ide) # IIVAPlugin def isInstalled(self): """ returns true if module is installed """ return _getMyTool(self) # IIVAPlugin def _install(self): """ install/enable plugin """ scorm = SCORMTool() if not hasattr(self.context, scorm.id): self.context._setObject(scorm.id, scorm) # IIVAPlugin def _uninstall(self): """ uninstall plugin """ self.context._delObject(SCORMTool.id) # IIVAPlugin def isVisible(self): return self.tool.isVisible()