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);
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);
+ }
+ }
+
}
}