]> _ Git - fluidbook-toolbox.git/commitdiff
wait #4951 @3
authorVincent Vanwaelscappel <vincent@cubedesigners.com>
Thu, 9 Dec 2021 12:52:28 +0000 (13:52 +0100)
committerVincent Vanwaelscappel <vincent@cubedesigners.com>
Thu, 9 Dec 2021 12:52:28 +0000 (13:52 +0100)
app/Jobs/FluidbookCollectionDownload.php

index 0dc3483733efd1fd065e858e4b1fa3d466a084f1..753bceef9164c8fb9f603c8fe66b7b4b03e0205c 100644 (file)
@@ -9,6 +9,7 @@ use Cubist\Util\Files\Files;
 use Cubist\Util\PHP;
 use Cubist\Util\Str;
 use Cubist\Util\Zip;
+use GuzzleHttp\Exception\RequestException;
 use Illuminate\Bus\Queueable;
 use Illuminate\Contracts\Queue\ShouldQueue;
 use Illuminate\Foundation\Bus\Dispatchable;
@@ -16,6 +17,7 @@ use Illuminate\Queue\InteractsWithQueue;
 use Illuminate\Queue\SerializesModels;
 use Illuminate\Support\Facades\Mail;
 use InvalidArgumentException;
+use Psr\Http\Message\ResponseInterface;
 use RusticiSoftware\Cloud\V2 as ScormCloud;
 
 
@@ -77,13 +79,13 @@ class FluidbookCollectionDownload implements ShouldQueue
 
             try {
                 if ($this->action === 'scormcloud') {
-                    $scormURL = $this->sendToSCORMCloud($dest);
+                    $scormURL = $this->sendToSCORMCloud($url);
                     $body .= "<br><br>";
                     $body .= __('La collection peut être testée sur SCORM Cloud : <a href=":url">:url</a>', ['url' => $scormURL]);
                 }
             } catch (\Exception $e) {
                 $body .= "<br><br>";
-                $body .= __('Une erreur s\'est produite lors de l\'envoi sur SCORM Cloud : :error', ['error' => $e->getMessage()]);
+                $body .= __('Une erreur s\'est produite lors de l\'envoi sur SCORM Cloud (App ID :appid) : :error', ['error' => $e->getMessage(), 'appid' => env('SCORM_CLOUD_APP_ID')]);
             }
 
 
@@ -156,57 +158,60 @@ class FluidbookCollectionDownload implements ShouldQueue
         }
 
         $options = $this->getCollectionGlobalSettings();
+        $options['scorm_enable'] = true;
 
-        if ($data->locale_switch) {
-            $multilang = [];
-            foreach ($langs as $fbid => $lang) {
-                if ($countries[$fbid]) {
-                    $flag = $countries[$fbid];
-                } else {
-                    if (strlen($lang) === 5) {
-                        $flag = substr($lang, 3, 2);
-                    } else if (isset($defaultFlags[$lang])) {
-                        $flag = $defaultFlags[$lang];
+        if (count($data->publications) >= 2) {
+
+            if ($data->locale_switch) {
+                $multilang = [];
+                foreach ($langs as $fbid => $lang) {
+                    if ($countries[$fbid]) {
+                        $flag = $countries[$fbid];
                     } else {
-                        $flag = $lang;
+                        if (strlen($lang) === 5) {
+                            $flag = substr($lang, 3, 2);
+                        } else if (isset($defaultFlags[$lang])) {
+                            $flag = $defaultFlags[$lang];
+                        } else {
+                            $flag = $lang;
+                        }
                     }
+                    $multilang[] = $lang . ',' . strtolower($flag) . ',../' . $lang . '/index.html';
                 }
-                $multilang[] = $lang . ',' . strtolower($flag) . ',../' . $lang . '/index.html';
+                $options['multilang'] = implode("\n", $multilang);
+                $options['multilangDisplay'] = 'lang';
+            } else {
+                $options['multilang'] = '';
             }
-            $options['multilang'] = implode("\n", $multilang);
-            $options['multilangDisplay'] = 'lang';
-        } else {
-            $options['multilang'] = '';
-        }
 
-        foreach ($data->publications as $publication) {
-            $fbid = $publication['fluidbook'];
-            $lang = $langs[$fbid];
-            $dir = $path . '/' . $lang;
-            $ws->installBook($fbid, $dir, $options, 'scorm', 3);
-        }
-
-        if (in_array('en', $langs)) {
-            $default = 'en';
-        } else {
-            $default = $langs[0];
-        }
+            foreach ($data->publications as $publication) {
+                $fbid = $publication['fluidbook'];
+                $lang = $langs[$fbid];
+                $dir = $path . '/' . $lang;
+                $ws->installBook($fbid, $dir, $options, 'scorm', 3);
+            }
 
-        $manifestFile = $path . '/imsmanifest.xml';
-        foreach ($langs as $lang) {
-            $manifest = $path . '/' . $lang . '/imsmanifest.xml';
-            if ($lang === $default) {
-                rename($manifest, $manifestFile);
+            if (in_array('en', $langs)) {
+                $default = 'en';
             } else {
-                unlink($manifest);
+                $default = $langs[0];
             }
-        }
 
-        $manifestContent = file_get_contents($manifestFile);
-        $manifestContent = preg_replace('/\<title\>(.*)\<\/title\>/U', '<title>' . htmlspecialchars($this->collection->title) . '</title>', $manifestContent);
-        file_put_contents($manifestFile, $manifestContent);
+            $manifestFile = $path . '/imsmanifest.xml';
+            foreach ($langs as $lang) {
+                $manifest = $path . '/' . $lang . '/imsmanifest.xml';
+                if ($lang === $default) {
+                    rename($manifest, $manifestFile);
+                } else {
+                    unlink($manifest);
+                }
+            }
+
+            $manifestContent = file_get_contents($manifestFile);
+            $manifestContent = preg_replace('/\<title\>(.*)\<\/title\>/U', '<title>' . htmlspecialchars($this->collection->title) . '</title>', $manifestContent);
+            file_put_contents($manifestFile, $manifestContent);
 
-        $redirectionScript = "<html>
+            $redirectionScript = "<html>
 <head></head>
 <body>
 <script type=\"text/javascript\">
@@ -253,7 +258,15 @@ window.location='./' + locale + '/index.html';
 </body>
 </html>";
 
-        file_put_contents($path . '/index.html', $redirectionScript);
+            file_put_contents($path . '/index.html', $redirectionScript);
+        } else {
+            foreach ($data->publications as $publication) {
+                $fbid = $publication['fluidbook'];
+                $dir = $path;
+                $ws->installBook($fbid, $dir, $options, 'scorm', 3);
+                break;
+            }
+        }
     }
 
     protected function compileExport($data, $path)
@@ -284,7 +297,7 @@ window.location='./' + locale + '/index.html';
         }
     }
 
-    public function sendToSCORMCloud($file)
+    public function sendToSCORMCloud($url)
     {
         $config = new ScormCloud\Configuration();
         $appId = env('SCORM_CLOUD_APP_ID');
@@ -301,17 +314,26 @@ window.location='./' + locale + '/index.html';
 
         }
 
-        $jobId = $courseAPI->createUploadAndImportCourseJob($courseId, true, null, 'application/zip', null, new \SplFileObject($file));
+        $request = new ScormCloud\Model\ImportFetchRequestSchema(['url' => $url, 'content_type' => 'application/zip']);
+        $promise = $courseAPI->createFetchAndImportCourseJobAsync($courseId, $request, true);
+        $jobId = $promise->wait();
 
-
-        $jobResult = $courseAPI->getImportJobStatus($jobId->getResult());
+        for ($i = 0; $i <= 20; $i++) {
+            try {
+                $jobResult = $courseAPI->getImportJobStatus($jobId->getResult());
+                break;
+            } catch (\Exception $e) {
+                sleep(10);
+            }
+        }
         while ($jobResult->getStatus() == ScormCloud\Model\ImportJobResultSchema::STATUS_RUNNING) {
             sleep(1);
             $jobResult = $courseAPI->getImportJobStatus($jobId->getResult());
         }
 
-        if ($jobResult->getStatus() == ScormCloud\Model\ImportJobResultSchema::STATUS_ERROR)
+        if ($jobResult->getStatus() == ScormCloud\Model\ImportJobResultSchema::STATUS_ERROR) {
             throw new InvalidArgumentException('Course is not properly formatted: ' . $jobResult->getMessage());
+        }
 
         return 'https://cloud.scorm.com/sc/user/Course?appId=' . $appId . '&courseId=' . $courseId;
     }