/** * Register a custom interaction from a zip package * * @param string $archive * @param boolean $replace * @return array * @throws \common_Exception * @throws ExtractException */ public function add($archive, $replace = false) { $returnValue = null; $qtiPackageParser = new CreatorPackageParser($archive); $qtiPackageParser->validate(); if ($qtiPackageParser->isValid()) { //obtain the id from manifest file $manifest = $qtiPackageParser->getManifest(true); $typeIdentifier = $manifest['typeIdentifier']; $label = $manifest['label']; //check if such PCI creator already exists $existingInteraction = $this->getResource($typeIdentifier); if ($existingInteraction) { if ($replace) { $this->remove($typeIdentifier); } else { throw new \common_Exception('The Creator Package already exists'); } } //extract the package $folder = $qtiPackageParser->extract(); if (!is_dir($folder)) { throw new ExtractException(); } $directory = $this->storage->spawnDirectory(true); $directoryId = $directory->getId(); //copy content in the directory: $this->storage->import($directoryId, $folder); $this->registryClass->createInstanceWithProperties(array($this->propTypeIdentifier->getUri() => $typeIdentifier, $this->propDirectory->getUri() => $directoryId, RDFS_LABEL => $label)); $returnValue = $this->get($typeIdentifier); } else { throw new \common_Exception('invalid PCI creator package format'); } return $returnValue; }
public function testGetManifest() { $packageValid = dirname(__FILE__) . '/samples/valid.zip'; $parser = new CreatorPackageParser($packageValid); $manifest = $parser->getManifest(); $this->assertTrue(!!count($manifest)); $this->assertEquals($manifest['typeIdentifier'], 'likertScaleInteraction'); }
/** * Service to check if the uploaded file archive is a valid and non-existing one * * JSON structure: * { * "valid" : true/false (if is a valid package) * "exists" : true/false (if the package is valid, check if the typeIdentifier is already used in the registry) * } */ public function verify() { $result = array('valid' => false, 'exists' => false); $file = tao_helpers_Http::getUploadedFile('content'); $creatorPackageParser = new CreatorPackageParser($file['tmp_name']); $creatorPackageParser->validate(); if ($creatorPackageParser->isValid()) { $result['valid'] = true; $manifest = $creatorPackageParser->getManifest(); $result['typeIdentifier'] = $manifest['typeIdentifier']; $result['label'] = $manifest['label']; $interaction = $this->registry->get($manifest['typeIdentifier']); if (!is_null($interaction)) { $result['exists'] = true; } } else { $result['package'] = $creatorPackageParser->getErrors(); } $this->returnJson($result); }