]> _ Git - fluidbook-toolbox.git/commitdiff
wip #6186 @1.5
authorVincent Vanwaelscappel <vincent@cubedesigners.com>
Thu, 21 Sep 2023 17:15:50 +0000 (19:15 +0200)
committerVincent Vanwaelscappel <vincent@cubedesigners.com>
Thu, 21 Sep 2023 17:15:50 +0000 (19:15 +0200)
app/Elearning/QuizCompiler.php
app/Http/Controllers/Admin/Operations/Quiz/DownloadOperation.php
app/Jobs/DownloadBase.php
app/Jobs/QuizDownload.php
app/Models/Quiz.php
resources/views/vendor/backpack/crud/buttons/quiz/download.blade.php

index 71ee7a9f3fb3c0411a3554dc9989e406b95c3658..65e5134d99c31015adc575a897889325dac5bb00 100644 (file)
@@ -52,7 +52,7 @@ class QuizCompiler extends Base
 
     public function __construct($quiz, $dest, $forceScorm = false)
     {
-        $this->quiz = Quiz::withoutGlobalScopes()->find($quiz);
+        $this->quiz = $quiz instanceof Quiz ? $quiz : Quiz::withoutGlobalScopes()->find($quiz);
         $this->theme = $this->quiz->getTheme();
         $this->compilePath = protected_path('quiz/compile/' . $this->quiz->id);
         $this->dest = $dest;
index 2ad05ae1eea09c95c0e9d7f98a0927d02bd316cf..f3744839804f6de9b896383d4282e6a96c6004eb 100644 (file)
@@ -2,8 +2,13 @@
 
 namespace App\Http\Controllers\Admin\Operations\Quiz;
 
+use App\Fields\SCORMVersion;
+use App\Http\Middleware\CheckIfAdmin;
 use App\Jobs\QuizDownload;
+use App\Models\FluidbookPublication;
 use App\Models\Quiz;
+use Cubist\Backpack\Http\Controllers\Base\XSendFileController;
+use Cubist\Scorm\Version;
 use Illuminate\Support\Facades\Route;
 use Prologue\Alerts\Facades\Alert;
 
@@ -13,7 +18,8 @@ trait DownloadOperation
 {
     protected function setupDownloadRoutes($segment, $routeName, $controller)
     {
-        Route::match(['get'], $segment . '/{id}/download/{action}', $controller . '@download');
+        Route::match(['get'], $segment . '/{id}/package/{action}/{version?}', $controller . '@package');
+        Route::match(['get'], $segment . '/{id}_{hash}/download/{file}', $controller . '@download')->withoutMiddleware([CheckIfAdmin::class]);;
     }
 
     protected function setupDownloadDefaults()
@@ -22,10 +28,28 @@ trait DownloadOperation
         $this->crud->addButtonFromView('line', 'download', 'quiz.download', 'end');
     }
 
-    protected function download($id, $action)
+    protected function package($id, $action, $version = null)
     {
-        QuizDownload::dispatch(Quiz::find($id), $action, backpack_user())->onQueue('download');;
+        if (!Quiz::hasPermission($id, 'read')) {
+            abort(401);
+        }
+        $quiz = Quiz::withoutGlobalScopes()->find($id);
+        $version = $version ?? $quiz->scorm_version;
+        QuizDownload::dispatch($quiz, $action, $version, backpack_user())->onQueue('download');;
         Alert::add('success', __('La compilation a été placée en file d\'attente. Vous recevrez un email lorsqu\'elle sera terminée.'))->flash();
         return redirect(backpack_url('quiz'));
     }
+
+    protected function download($id, $hash, $file)
+    {
+        $quiz = Quiz::withoutGlobalScopes()->where('id', $id)->where('hash', $hash)->first();
+        if (null === $quiz) {
+            abort(404, __('Ce fichier est indisponible'));
+        }
+        $path = protected_path('quiz/download/' . $id . '/' . $file);
+        if (!file_exists($path)) {
+            abort(404, __('Le fichier n\'existe pas'));
+        }
+        return XSendFileController::sendfile($path);
+    }
 }
index 28492f564f72f151dd49e98b7c7d32fb384adc2a..bdf2067606fe7e683217a1a53c46c70b68ad56bd 100644 (file)
@@ -46,7 +46,7 @@ class DownloadBase extends Base
      * @param $action string
      * @param $user User
      */
-    public function __construct($entry, $action, $user=null)
+    public function __construct($entry, $action, $user = null)
     {
         $this->entry = $entry;
         $this->setUser($user);
@@ -96,12 +96,12 @@ class DownloadBase extends Base
 
     protected function _dest($fname)
     {
-        return Files::mkdir(storage_path('app/public/' . $this->type . '/download/')) . $fname;
+        return Files::mkdir(protected_path($this->type . '/download/' . $this->entry->id . '/')) . $fname;
     }
 
     protected function _url($fname)
     {
-        return url('storage/' . $this->type . '/download/' . $fname);
+        return url('/' . $this->type . '/' . $this->entry->id . '_' . $this->entry->hash . '/download/' . $fname);
     }
 
     protected function _title()
@@ -124,13 +124,14 @@ class DownloadBase extends Base
             $text = __($this->_text, $translateVariables);
             $actions = [__('Télécharger') => $url];
             $showTextIfNotEmail = false;
-            try {
-                if ($this->action === 'scormcloud') {
-                    $scormURL = ScormCloud::send($url, env('SCORM_CLOUD_PREFIX','toolbox_') . $this->type . '_' . $this->_id());
+            if ($this->action === 'scormcloud') {
+                try {
+                    $scormURL = ScormCloud::send($url, env('SCORM_CLOUD_PREFIX', 'toolbox_') . $this->type . '_' . $this->_id());
                     $actions[__('Tester sur SCORM Cloud')] = $scormURL;
+                } catch (\Exception $e) {
+                    $text .= "\n\n" . __('Une erreur s\'est produite lors de l\'envoi sur SCORM Cloud (App ID :appid) : :error (:url)', ['error' => $e->getMessage(), 'appid' => env('SCORM_CLOUD_APP_ID'), 'url' => $url]);
+                    $showTextIfNotEmail = true;
                 }
-            } catch (\Exception $e) {
-                $text = __('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')]);
             }
         } catch (\Exception $e) {
             $subject = __('Erreur lors de la compilation du :type :nb', ['nb' => $this->_id(), 'type' => $this->type]);
@@ -152,7 +153,7 @@ class DownloadBase extends Base
 
         $compilepath = $this->_compile();
         $fname = $this->_fname();
-        $dest = Files::mkdir(storage_path('app/public/' . $this->type . '/download/')) . $fname;
+        $dest = Files::mkdir(protected_path($this->type . '/download/' . $this->entry->id . '/')) . $fname;
 
         Zip::archive($compilepath, $dest);
         if (!file_exists($dest)) {
index 9d2ce29610e9f6803f0e03d33a4c277df0ebb263..a67a0ed2c1fa277a18dae570c682ced2fd01f48e 100644 (file)
@@ -15,4 +15,20 @@ class QuizDownload extends DownloadBase
     // __('Le quiz ":title" est prêt au téléchargement')
     protected $_text = 'Le quiz ":title" est prêt au téléchargement';
 
+    protected $_scormVersion = null;
+
+    public function __construct($entry, $action, $scormVersion = null, $user = null)
+    {
+        $this->_scormVersion = $scormVersion;
+        parent::__construct($entry, $action, $user);
+    }
+
+    protected function _compile()
+    {
+        if (null !== $this->_scormVersion) {
+            $this->entry->scorm_version = $this->_scormVersion;
+        }
+        return parent::_compile();
+    }
+
 }
index dc675528d193489e4efcad17638429eeb5578d0a..cde87f9c53e0a0cd5810204d6b8df56903652652 100644 (file)
@@ -288,7 +288,7 @@ class Quiz extends ToolboxModel
      */
     public function compile($dest, $forceScorm = false, $user = null)
     {
-        $job = new QuizCompiler($this->id, $dest, $forceScorm);
+        $job = new QuizCompiler($this, $dest, $forceScorm);
         $job->handle();
     }
 
index 0e47108cef778be549e8e8629a081db804b5336d..05a75b9669bd8b493164af241cabe4c07812153f 100644 (file)
@@ -1,10 +1,31 @@
 {{-- __('!! e-Learning') --}}
+@php
+    $base=$crud->route.'/'.$entry->id.'/package/';
+    $actions['download']=[
+        'label'=>__('Télécharger'),
+        'url'=>$base.'download'
+    ];
+    if($entry->scorm){
+        $actions['sep_scorm']='---------';
+        foreach (\App\Fields\SCORMVersion::getSCORMVersions() as $k=>$v) {
+                $actions['download_scorm_'.$k]=[
+                'label'=>__('Version :version',['version'=>$v]),
+                'url'=>$base.'scorm/'.$k
+                ];
+        }
+        $actions['sep_scormcloud    ']='---------';
+        $actions['scormcloud']=['label'=>__('Tester sur SCORM Cloud'),
+            'url'=> $base.'scormcloud'
+        ];
+
+    }
+
+@endphp
+
 <a class="btn btn-sm btn-link exportquiz" href="#"
    data-toggle="tooltip"
    title="{{__('Exporter le quiz')}}"
-   data-context-actions="{{json_encode(['download'=>__('Télécharger'),'scormcloud'=>__('Tester sur Scorm Cloud')])}}"
-   data-context-route="{{$crud->route}}/$id/download/$action"
-   data-context-id="{{$entry->getKey()}}"
+   data-context-actions="{{json_encode($actions)}}"
 >
     <i class="la la-arrow-circle-down"></i> {{__('Exporter')}}
 </a>