From: Vincent Vanwaelscappel Date: Fri, 22 Sep 2023 07:21:54 +0000 (+0200) Subject: wip #6186 @1 X-Git-Url: http://git.cubedesigners.com/?a=commitdiff_plain;h=db7022b7f65ba608941f068efab9259715f62ecd;p=fluidbook-toolbox.git wip #6186 @1 --- diff --git a/app/Console/Commands/ScormCloudTesting.php b/app/Console/Commands/ScormCloudTesting.php new file mode 100644 index 000000000..295dd7d50 --- /dev/null +++ b/app/Console/Commands/ScormCloudTesting.php @@ -0,0 +1,16 @@ +argument('arg')); + } +} diff --git a/app/Fluidbook/Packager/Download.php b/app/Fluidbook/Packager/Download.php index 917532055..ff91d6ccc 100644 --- a/app/Fluidbook/Packager/Download.php +++ b/app/Fluidbook/Packager/Download.php @@ -97,7 +97,7 @@ class Download extends DownloadBase if ($this->action === 'scormcloud') { try { - $scormURL = ScormCloud::send($url, 'toolbox_' . $this->type . '_' . $this->_id()); + $scormURL = ScormCloud::send($url, env('SCORM_CLOUD_PREFIX', 'toolbox_') . $this->type . '_' . $this->_id()); $actions[__('Tester sur SCORM Cloud')] = $scormURL; } catch (\Exception $e) { diff --git a/app/Services/ScormCloud.php b/app/Services/ScormCloud.php index 3a3327776..c5b4a7246 100644 --- a/app/Services/ScormCloud.php +++ b/app/Services/ScormCloud.php @@ -4,26 +4,42 @@ namespace App\Services; use RusticiSoftware\Cloud\V2; use InvalidArgumentException; +use RusticiSoftware\Cloud\V2\ApiException; class ScormCloud { - public static function send($url, $courseId, $delete = false) - { - $config = new V2\Configuration(); - $appId = env('SCORM_CLOUD_APP_ID'); - $config->setUsername($appId); - $config->setPassword(env('SCORM_CLOUD_SECRET_KEY')); - V2\Configuration::setDefaultConfiguration($config); + /** @var V2\Api\CourseApi */ + protected static $_api = null; - $courseAPI = new V2\Api\CourseApi(); - if ($delete) { - try { - $courseAPI->deleteCourse($courseId); - } catch (\Exception $e) { + protected static function getAppID() + { + return env('SCORM_CLOUD_APP_ID'); + } - } + /** + * @return V2\Api\CourseApi + */ + protected static function _getApi() + { + if (static::$_api === null) { + $config = new V2\Configuration(); + $appId = static::getAppID(); + $config->setUsername($appId); + $config->setPassword(env('SCORM_CLOUD_SECRET_KEY')); + V2\Configuration::setDefaultConfiguration($config); + + static::$_api = new V2\Api\CourseApi(); } + return static::$_api; + } + + /** + * @throws ApiException + */ + public static function send($url, $courseId, $delete = true) + { + $courseAPI = static::_getApi(); $request = new V2\Model\ImportFetchRequestSchema(['url' => $url, 'content_type' => 'application/zip']); $promise = $courseAPI->createFetchAndImportCourseJobAsync($courseId, $request, true); @@ -46,6 +62,36 @@ class ScormCloud throw new InvalidArgumentException('Course is not properly formatted: ' . $jobResult->getMessage()); } - return 'https://cloud.scorm.com/sc/user/Course?appId=' . $appId . '&courseId=' . $courseId; + + if ($delete) { + static::deleteOldVersions($courseId); + } + + return 'https://cloud.scorm.com/sc/user/Course?appId=' . self::getAppID() . '&courseId=' . $courseId; + } + + /** + * @throws ApiException + */ + public static function deleteOldVersions($courseId) + { + $api = static::_getApi(); + + $versions = $api->getCourseVersions($courseId); + if (count($versions->getCourses()) <= 1) { + return; + } + + $max = -1; + foreach ($versions->getCourses() as $version) { + $max = max($max, $version->getVersion()); + } + foreach ($versions->getCourses() as $version) { + $v = $version->getVersion(); + if ($v != $max) { + $api->deleteCourseVersion($courseId, $v); + } + } + } }