make ScormAPI a real adapter by changing its __init__()
git-svn-id: svn://svn.cy55.de/Zope3/src/cybertools/trunk@1865 fd906abe-77d9-0310-91a1-e0d9ade77398
This commit is contained in:
parent
821b922f6a
commit
bf41e3afd4
3 changed files with 22 additions and 9 deletions
|
@ -10,10 +10,14 @@ In order to work with the SCORM API we first need a tracking storage.
|
||||||
>>> tracks = TrackingStorage()
|
>>> tracks = TrackingStorage()
|
||||||
|
|
||||||
We can now create a ScormAPI adapter. Note that this adapter is stateless
|
We can now create a ScormAPI adapter. Note that this adapter is stateless
|
||||||
as usually a new instance is created upon each request.
|
as usually a new instance is created upon each request. In order to comply
|
||||||
|
with Zope's adapter protocol the constructor can only have one argument,
|
||||||
|
the context, i.e. the tracking storage. Therefore we have to set the
|
||||||
|
basic attributes of the adapter with a separate ``init()`` call.
|
||||||
|
|
||||||
>>> from cybertools.scorm.base import ScormAPI
|
>>> from cybertools.scorm.base import ScormAPI
|
||||||
>>> api = ScormAPI(tracks, 'a001', 0, 'user1')
|
>>> api = ScormAPI(tracks)
|
||||||
|
>>> api.init('a001', 0, 'user1')
|
||||||
|
|
||||||
The first step is always the initialize() call - though in our case it
|
The first step is always the initialize() call - though in our case it
|
||||||
does not do anything.
|
does not do anything.
|
||||||
|
|
|
@ -22,11 +22,13 @@ Base classes for providing a generic SCORM-compliant API.
|
||||||
$Id$
|
$Id$
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
from zope import component
|
||||||
|
from zope.component import adapts
|
||||||
from zope import interface
|
from zope import interface
|
||||||
from zope.interface import implements
|
from zope.interface import implements
|
||||||
|
|
||||||
from cybertools.scorm.interfaces import IScormAPI
|
from cybertools.scorm.interfaces import IScormAPI
|
||||||
from cybertools.tracking.btree import TrackingStorage
|
from cybertools.tracking.interfaces import ITrackingStorage
|
||||||
|
|
||||||
|
|
||||||
OK = '0'
|
OK = '0'
|
||||||
|
@ -51,12 +53,15 @@ class ScormAPI(object):
|
||||||
"""
|
"""
|
||||||
|
|
||||||
implements(IScormAPI)
|
implements(IScormAPI)
|
||||||
|
adapts(ITrackingStorage)
|
||||||
|
|
||||||
def __init__(self, storage, taskId, runId, userId):
|
def __init__(self, context):
|
||||||
|
self.context = context
|
||||||
|
|
||||||
|
def init(self, taskId, runId, userId):
|
||||||
self.taskId = taskId
|
self.taskId = taskId
|
||||||
self.runId = runId
|
self.runId = runId
|
||||||
self.userId = userId
|
self.userId = userId
|
||||||
self.storage = storage
|
|
||||||
|
|
||||||
def initialize(self, parameter=''):
|
def initialize(self, parameter=''):
|
||||||
# Note that the run has already been started upon SCO launch, the runId
|
# Note that the run has already been started upon SCO launch, the runId
|
||||||
|
@ -66,20 +71,20 @@ class ScormAPI(object):
|
||||||
def terminate(self, parameter=''):
|
def terminate(self, parameter=''):
|
||||||
rc = self.commit()
|
rc = self.commit()
|
||||||
if rc == OK:
|
if rc == OK:
|
||||||
self.storage.stopRun(self.taskId, self.runId)
|
self.context.stopRun(self.taskId, self.runId)
|
||||||
return rc
|
return rc
|
||||||
|
|
||||||
def commit(self, parameter=''):
|
def commit(self, parameter=''):
|
||||||
return OK
|
return OK
|
||||||
|
|
||||||
def setValue(self, element, value):
|
def setValue(self, element, value):
|
||||||
tracks = self.storage.getUserTracks(self.taskId, self.runId, self.userId)
|
tracks = self.context.getUserTracks(self.taskId, self.runId, self.userId)
|
||||||
prefix, key = self._splitKey(element)
|
prefix, key = self._splitKey(element)
|
||||||
data = self._getTrackData(tracks, prefix) or {}
|
data = self._getTrackData(tracks, prefix) or {}
|
||||||
update = bool(data)
|
update = bool(data)
|
||||||
data['key_prefix'] = prefix
|
data['key_prefix'] = prefix
|
||||||
data.update({key: value})
|
data.update({key: value})
|
||||||
self.storage.saveUserTrack(self.taskId, self.runId, self.userId, data,
|
self.context.saveUserTrack(self.taskId, self.runId, self.userId, data,
|
||||||
update=update)
|
update=update)
|
||||||
return OK
|
return OK
|
||||||
|
|
||||||
|
@ -93,7 +98,7 @@ class ScormAPI(object):
|
||||||
return OK
|
return OK
|
||||||
|
|
||||||
def getValue(self, element):
|
def getValue(self, element):
|
||||||
tracks = self.storage.getUserTracks(self.taskId, self.runId, self.userId)
|
tracks = self.context.getUserTracks(self.taskId, self.runId, self.userId)
|
||||||
if element.endswith('._count'):
|
if element.endswith('._count'):
|
||||||
base = element[:-len('._count')]
|
base = element[:-len('._count')]
|
||||||
if element.startswith('cmi.interactions.'):
|
if element.startswith('cmi.interactions.'):
|
||||||
|
|
|
@ -49,6 +49,10 @@ class IScormAPI(Interface):
|
||||||
runId = Attribute('Run ID (integer)')
|
runId = Attribute('Run ID (integer)')
|
||||||
userId = Attribute('User ID')
|
userId = Attribute('User ID')
|
||||||
|
|
||||||
|
def init(taskId, runId, userId):
|
||||||
|
""" Set the basic attributes with one call.
|
||||||
|
"""
|
||||||
|
|
||||||
def initialize(parameter):
|
def initialize(parameter):
|
||||||
""" Corresponds to API.Initialize('').
|
""" Corresponds to API.Initialize('').
|
||||||
Return CMIErrorCode.
|
Return CMIErrorCode.
|
||||||
|
|
Loading…
Add table
Reference in a new issue