+++ /dev/null
-<?php
-
-namespace App\Http\Controllers\Admin\Operations\Quiz;
-
-
-use App\Models\Quiz;
-use App\Models\QuizTranslation;
-use Cubist\Util\Files\Files;
-use Illuminate\Support\Facades\Route;
-use Prologue\Alerts\Facades\Alert;
-use ZipArchive;
-
-// __('!! e-Learning')
-
-trait ImportOperation
-{
- protected function setupImportRoutes($segment, $routeName, $controller)
- {
- Route::match(['post'], $segment . '/import', $controller . '@import');
- }
-
- protected function setupImportDefaults()
- {
- $this->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, '<quiz') === false) {
- Alert::warning($file . ' doesn\'t seem to be a valid quiz')->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('<b>' . $j . ' quiz(zes)</b> were imported')->flash();
- }
- return redirect($this->crud->route);
- }
-}
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;
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;