From 86ef89c41da12caa5eec6e50d07271736c588bb0 Mon Sep 17 00:00:00 2001 From: Vincent Vanwaelscappel Date: Thu, 27 Jul 2023 17:37:10 +0200 Subject: [PATCH] wip #6180 @0.5 --- .../Admin/Operations/Quiz/ImportOperation.php | 166 ------------------ app/Models/Quiz.php | 3 +- .../crud/buttons/quiz/import.blade.php | 26 --- 3 files changed, 1 insertion(+), 194 deletions(-) delete mode 100644 app/Http/Controllers/Admin/Operations/Quiz/ImportOperation.php delete mode 100644 resources/views/vendor/backpack/crud/buttons/quiz/import.blade.php diff --git a/app/Http/Controllers/Admin/Operations/Quiz/ImportOperation.php b/app/Http/Controllers/Admin/Operations/Quiz/ImportOperation.php deleted file mode 100644 index 7c6168c1b..000000000 --- a/app/Http/Controllers/Admin/Operations/Quiz/ImportOperation.php +++ /dev/null @@ -1,166 +0,0 @@ -crud->addButtonFromView('top', 'import', 'quiz.import', 'end'); - } - - protected function import() - { - $files = $_FILES['file']['tmp_name']; - - if (!count($files)) { - Alert::warning('No file were imported')->flash(); - return; - } - - $default = ['title' => '', - 'translation' => '1', - 'scorm' => '1', - 'review' => 'always', - 'instantReview' => '1', - 'threshold' => '0', - 'owner' => auth()->user()->id]; - - foreach (Quiz::getColors() as $name => $color) { - $default[$name] = $color['default']; - } - - foreach (Quiz::getMessages() as $name => $message) { - $default[$name] = ''; - } - - - $j = 0; - foreach ($files as $file) { - $z = new ZipArchive(); - $ok = $z->open($file); - if ($ok !== true) { - Alert::warning('Unable to open ' . $file)->flash(); - continue; - } - - - $data = []; - $datacontent = $z->getFromName('data.xml'); - if (!$datacontent || stripos($datacontent, 'flash(); - continue; - } - $x = simplexml_load_string($datacontent, "SimpleXMLElement", LIBXML_NOERROR | LIBXML_ERR_NONE); - - // Discover translation - $validatetrans = $x->xpath('/quiz/translations/validateAnswer'); - if ($validatetrans) { - $validatetrans = (string)$validatetrans[0]; - $translation = QuizTranslation::where('validateAnswer', '=', $validatetrans)->first(); - if ($translation) { - $data['translation'] = $translation->id; - } - } - // Handle message from XML - foreach (Quiz::getMessages() as $name => $message) { - $e = $x->xpath('/quiz/' . $name); - if (!$e) { - continue; - } - $m = (string)$e[0]; - // We only define the message if different from the translation default - if (isset($translation) && $translation->$name !== $m) { - $data[$name] = $m; - } - } - - // Handle other attributes from XML - $attributes = ['title', 'review', 'thresehold']; - foreach (Quiz::getColors() as $name => $color) { - $attributes[] = $name; - } - foreach (Quiz::getActions() as $name => $action) { - $attributes[] = $name; - } - foreach ($attributes as $attribute => $xpath) { - if (is_int($attribute)) { - $attribute = $xpath; - $xpath = '/quiz/' . $xpath; - } - $e = $x->xpath($xpath); - if (!$e) { - continue; - } - $data[$attribute] = (string)$e[0]; - } - - // Handle Questions - $xquestions = $x->xpath('/quiz/questions/question'); - $questions = []; - foreach ($xquestions as $xquestion) { - $q = [ - 'type' => 'multiple', - 'count_for_score' => true, - 'report_label' => '', - 'placeholder' => '', - 'min_score' => 0, - 'question' => (string)$xquestion->label, - 'explaination' => (string)$xquestion->correction, - 'multiple' => isset($xquestion['multiple']) ? (bool)$xquestion['multiple'] : false, - 'answers' => [], - ]; - foreach ($xquestion->answers[0]->answer as $xanswer) { - $q['answers'][] = [ - 'answer' => (string)$xanswer, - 'correct' => isset($xanswer['correct']) ? (bool)$xanswer['correct'] : false, - ]; - } - $questions[] = $q; - - } - $data['questions'] = $questions; - $data = array_merge($default, $data); - - /** @var Quiz $q */ - $q = new Quiz(); - $q = $q->create($data); - - $temp = Files::tmpdir(); - $assets = ['logo' => 'logo.png', 'banner' => 'banner.jpg']; - foreach ($assets as $field => $asset) { - $f = $temp . '/' . $asset; - $c = $z->getFromName('assets/' . $asset); - if ($c) { - file_put_contents($f, $c); - $q->addMediaToField($field, $f); - } - } - $z->close(); - $j++; - } - - - if ($j === 0) { - Alert::warning('No quiz were imported')->flash(); - } else { - Alert::success('' . $j . ' quiz(zes) were imported')->flash(); - } - return redirect($this->crud->route); - } -} diff --git a/app/Models/Quiz.php b/app/Models/Quiz.php index ef5953e50..adb0707f8 100644 --- a/app/Models/Quiz.php +++ b/app/Models/Quiz.php @@ -6,7 +6,6 @@ use App\Elearning\QuizCompiler; use App\Fields\SCORMVersion; use App\Http\Controllers\Admin\Operations\ChangeownerOperation; use App\Http\Controllers\Admin\Operations\Quiz\DownloadOperation; -use App\Http\Controllers\Admin\Operations\Quiz\ImportOperation; use App\Http\Controllers\Admin\Operations\Quiz\LogOperation; use App\Http\Controllers\Admin\Operations\Quiz\PreviewOperation; use App\Http\Controllers\Admin\Operations\Quiz\ReportOperation; @@ -43,7 +42,7 @@ class Quiz extends ToolboxModel public $registerMediaConversionsUsingModelInstance = false; - protected $_operations = [PreviewOperation::class, DownloadOperation::class, LogOperation::class, ReportOperation::class, ImportOperation::class, ChangeownerOperation::class]; + protected $_operations = [PreviewOperation::class, DownloadOperation::class, LogOperation::class, ReportOperation::class, ChangeownerOperation::class]; use SCORMVersionTrait; diff --git a/resources/views/vendor/backpack/crud/buttons/quiz/import.blade.php b/resources/views/vendor/backpack/crud/buttons/quiz/import.blade.php deleted file mode 100644 index c3ec7e603..000000000 --- a/resources/views/vendor/backpack/crud/buttons/quiz/import.blade.php +++ /dev/null @@ -1,26 +0,0 @@ -{{-- __('!! e-Learning') --}} - - {{__('Importer')}} - - -@push('after_scripts') - -@endpush -- 2.39.5