/** * Store a file referenced by $qtiResource into the final $testContent folder. If the path provided * by $qtiResource contains sub-directories, they will be created before copying the file (even * if $copy = false). * * @param Directory $testContent The pointer to the TAO Test Content folder. * @param oat\taoQtiItem\model\qti\Resource|string $qtiResource The QTI resource to be copied into $testContent. If given as a string, it must be the relative (to the IMS QTI Package) path to the resource file. * @param string $origin The path to the directory (root folder of extracted IMS QTI package) containing the QTI resource to be copied. * @param boolean $copy If set to false, the file will not be actually copied. * @param string $rename A new filename e.g. 'file.css' to be used at storage time. * @return string The path were the file was copied/has to be copied (depending on the $copy argument). * @throws InvalidArgumentException If one of the above arguments is invalid. * @throws common_Exception If the copy fails. */ public static function storeQtiResource(Directory $testContent, $qtiResource, $origin, $copy = true, $rename = '') { $fss = ServiceManager::getServiceManager()->get(FileSystemService::SERVICE_ID); $fs = $fss->getFileSystem($testContent->getFileSystem()->getId()); $contentPath = $testContent->getPrefix(); $ds = DIRECTORY_SEPARATOR; $contentPath = rtrim($contentPath, $ds); if ($qtiResource instanceof Resource) { $filePath = $qtiResource->getFile(); } else { if (is_string($qtiResource) === true) { $filePath = $qtiResource; } else { throw new InvalidArgumentException("The 'qtiResource' argument must be a string or a taoQTI_models_classes_QTI_Resource object."); } } $resourcePathinfo = pathinfo($filePath); if (empty($resourcePathinfo['dirname']) === false && $resourcePathinfo['dirname'] !== '.') { // The resource file is not at the root of the archive but in a sub-folder. // Let's copy it in the same way into the Test Content folder. $breadCrumb = $contentPath . $ds . str_replace('/', $ds, $resourcePathinfo['dirname']); $breadCrumb = rtrim($breadCrumb, $ds); $finalName = empty($rename) === true ? $resourcePathinfo['filename'] . '.' . $resourcePathinfo['extension'] : $rename; $finalPath = $breadCrumb . $ds . $finalName; } else { // The resource file is at the root of the archive. // Overwrite template test.xml (created by self::createContent() method above) file with the new one. $finalName = empty($rename) === true ? $resourcePathinfo['filename'] . '.' . $resourcePathinfo['extension'] : $rename; $finalPath = $contentPath . $ds . $finalName; } if ($copy === true) { $origin = str_replace('/', $ds, $origin); $origin = rtrim($origin, $ds); $sourcePath = $origin . $ds . str_replace('/', $ds, $filePath); if (is_readable($sourcePath) === false) { throw new common_Exception("An error occured while copying the QTI resource from '{$sourcePath}' to '{$finalPath}'."); } $fh = fopen($sourcePath, 'r'); $success = $fs->writeStream($finalPath, $fh); fclose($fh); if (!$success) { throw new common_Exception("An error occured while copying the QTI resource from '{$sourcePath}' to '{$finalPath}'."); } } return $finalPath; }