From f793bcc7ee61c918ee31550dc4f5fa7e49b4f6ef Mon Sep 17 00:00:00 2001 From: Vincent Vanwaelscappel Date: Thu, 21 Sep 2023 19:15:50 +0200 Subject: [PATCH] wip #6186 @1.5 --- app/Elearning/QuizCompiler.php | 2 +- .../Operations/Quiz/DownloadOperation.php | 30 +++++++++++++++++-- app/Jobs/DownloadBase.php | 19 ++++++------ app/Jobs/QuizDownload.php | 16 ++++++++++ app/Models/Quiz.php | 2 +- .../crud/buttons/quiz/download.blade.php | 27 +++++++++++++++-- 6 files changed, 79 insertions(+), 17 deletions(-) diff --git a/app/Elearning/QuizCompiler.php b/app/Elearning/QuizCompiler.php index 71ee7a9f3..65e5134d9 100644 --- a/app/Elearning/QuizCompiler.php +++ b/app/Elearning/QuizCompiler.php @@ -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; diff --git a/app/Http/Controllers/Admin/Operations/Quiz/DownloadOperation.php b/app/Http/Controllers/Admin/Operations/Quiz/DownloadOperation.php index 2ad05ae1e..f37448398 100644 --- a/app/Http/Controllers/Admin/Operations/Quiz/DownloadOperation.php +++ b/app/Http/Controllers/Admin/Operations/Quiz/DownloadOperation.php @@ -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); + } } diff --git a/app/Jobs/DownloadBase.php b/app/Jobs/DownloadBase.php index 28492f564..bdf206760 100644 --- a/app/Jobs/DownloadBase.php +++ b/app/Jobs/DownloadBase.php @@ -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)) { diff --git a/app/Jobs/QuizDownload.php b/app/Jobs/QuizDownload.php index 9d2ce2961..a67a0ed2c 100644 --- a/app/Jobs/QuizDownload.php +++ b/app/Jobs/QuizDownload.php @@ -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(); + } + } diff --git a/app/Models/Quiz.php b/app/Models/Quiz.php index dc675528d..cde87f9c5 100644 --- a/app/Models/Quiz.php +++ b/app/Models/Quiz.php @@ -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(); } diff --git a/resources/views/vendor/backpack/crud/buttons/quiz/download.blade.php b/resources/views/vendor/backpack/crud/buttons/quiz/download.blade.php index 0e47108ce..05a75b966 100644 --- a/resources/views/vendor/backpack/crud/buttons/quiz/download.blade.php +++ b/resources/views/vendor/backpack/crud/buttons/quiz/download.blade.php @@ -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 + {{__('Exporter')}} -- 2.39.5