| 1 | from zope.interface import implements |
|---|
| 2 | from collective.imstransport.utilities.interfaces import IIMSManifestWriter |
|---|
| 3 | from collective.imstransport.utilities.packagingio import ZipfileWriter |
|---|
| 4 | from collective.imstransport.utilities.moodle.moodlewriter import MoodleWriter |
|---|
| 5 | from collective.imstransport.utilities.imscp.cpresourcewriter import CPResourceWriter |
|---|
| 6 | from collective.imstransport.utilities.imsinterchange import IMSWriter |
|---|
| 7 | from collective.imstransport import IMSTransportMessageFactory as _ |
|---|
| 8 | |
|---|
| 9 | class IMSMoodleWriter(IMSWriter): |
|---|
| 10 | """ Write an IMS content package manifest file. """ |
|---|
| 11 | |
|---|
| 12 | def createPackage(self, filename, context): |
|---|
| 13 | """ Creates an IMS Package """ |
|---|
| 14 | |
|---|
| 15 | destination = ZipfileWriter(context, file) |
|---|
| 16 | mw = MoodleWriter() |
|---|
| 17 | |
|---|
| 18 | # Create the Manifest |
|---|
| 19 | manId = self._createPathId(context.virtual_url_path(), 'MAN') |
|---|
| 20 | manVer = context.ModificationDate() |
|---|
| 21 | doc = mw.createManifest(manId, manVer) |
|---|
| 22 | |
|---|
| 23 | # Add top level metadata |
|---|
| 24 | lang = context.Language() |
|---|
| 25 | if not lang: |
|---|
| 26 | lang = context.portal_properties.site_properties.getProperty('default_language') |
|---|
| 27 | mw.writeTopLevelMetadata(doc, |
|---|
| 28 | context.getId(), |
|---|
| 29 | context.Title(), |
|---|
| 30 | context.portal_url(), |
|---|
| 31 | lang, |
|---|
| 32 | context.Description(), |
|---|
| 33 | context.Subject()) |
|---|
| 34 | |
|---|
| 35 | # Write Organizations and Resources |
|---|
| 36 | orgId = self._createPathId(context.virtual_url_path(), 'ORG') |
|---|
| 37 | orgs = mw.createOrganizations(doc, orgId) |
|---|
| 38 | res = mw.createResources(doc) |
|---|
| 39 | |
|---|
| 40 | if orgs and res: |
|---|
| 41 | objs = self._getAllObjects(context) |
|---|
| 42 | for obj in objs: |
|---|
| 43 | # Need to consider excluding folders |
|---|
| 44 | # Get resource info |
|---|
| 45 | path = self._getObjectPath(obj, context) |
|---|
| 46 | vpath = obj.virtual_url_path() |
|---|
| 47 | refid = self._createPathId(vpath, 'RES') |
|---|
| 48 | # Check if we need to add to the organizations section |
|---|
| 49 | if not obj.getExcludeFromNav(): |
|---|
| 50 | itemId = self._createPathId(vpath, 'ITM') |
|---|
| 51 | mw.writeItem(orgs, itemId, refid, obj.title) |
|---|
| 52 | rn = mw.writeResource(res, refid, obj.Type(), path) |
|---|
| 53 | |
|---|
| 54 | id = obj.getId() |
|---|
| 55 | title = obj.Title() |
|---|
| 56 | urlbase = obj.portal_url() |
|---|
| 57 | lang = obj.Language() |
|---|
| 58 | if not lang: |
|---|
| 59 | lang = obj.portal_properties.site_properties.getProperty('default_language') |
|---|
| 60 | desc = obj.Description() |
|---|
| 61 | kw = obj.Subject() |
|---|
| 62 | creators = obj.Creators() |
|---|
| 63 | contrib = obj.Contributors() |
|---|
| 64 | mod = obj.ModificationDate() |
|---|
| 65 | email = obj.portal_url.getPortalObject().getProperty('email_from_address') |
|---|
| 66 | format = obj.Format() |
|---|
| 67 | size = self.getObjSize(obj) |
|---|
| 68 | location = obj.renderBase() |
|---|
| 69 | value = 'yes' |
|---|
| 70 | rights_holder = obj.portal_properties.site_properties.getProperty('rights_holder') |
|---|
| 71 | rights_holder_email = obj.portal_properties.site_properties.getProperty('rights_holder_email') |
|---|
| 72 | copyright = self._getCopyrightString(obj.Rights(), rights_holder, rights_holder_email) |
|---|
| 73 | |
|---|
| 74 | md = mw.createResourceMetadata(rn) |
|---|
| 75 | mw.writeGeneralNode(md, id, title, 'en', desc, kw) |
|---|
| 76 | mw.writeLifeCycleNode(md, creators, contrib, mod, None) |
|---|
| 77 | mw.writeMetaMetadataNode(md, id, urlbase, email, mod, None, contrib) |
|---|
| 78 | mw.writeTechnicalNode(md, format, size, location) |
|---|
| 79 | mw.writeRightsNode(md, value, copyright, None) |
|---|
| 80 | |
|---|
| 81 | if obj.Type() == 'Link': |
|---|
| 82 | link = mw.getLinkXml(title, obj.getRemoteUrl()) |
|---|
| 83 | rpath = '%s' %path |
|---|
| 84 | self._writeObjectData(link, rpath, destination) |
|---|
| 85 | mw.writeResourceFile(rn, rpath) |
|---|
| 86 | else: |
|---|
| 87 | self._writeObjectData(obj, path, destination) |
|---|
| 88 | mw.writeResourceFile(rn, path) |
|---|
| 89 | |
|---|
| 90 | self._writeObjectData(mw.getManifest(doc), 'imsmanifest.xml', destination) |
|---|
| 91 | |
|---|
| 92 | if destination: |
|---|
| 93 | return destination.getOutput() |
|---|
| 94 | else: |
|---|
| 95 | return None, None |
|---|
| 96 | |
|---|
| 97 | |
|---|