From: Vincent Vanwaelscappel Date: Wed, 27 Jan 2021 21:09:31 +0000 (+0100) Subject: wait #4231 @3 X-Git-Url: http://git.cubedesigners.com/?a=commitdiff_plain;h=258b852ec4df5e8488e5f99479f0cb8204b203ed;p=fluidbook-toolbox.git wait #4231 @3 --- diff --git a/app/Models/Quiz.php b/app/Models/Quiz.php index eccd2af8f..d3e62f242 100644 --- a/app/Models/Quiz.php +++ b/app/Models/Quiz.php @@ -255,72 +255,7 @@ class Quiz extends CubistMagicAbstractModel } } - $data = $this->getPageData(); - - // Create data.xml - $xml = simplexml_load_string(''); - $xml->addChild('id', $data->get('id')); - $xml->addChild('title', $data->get('title')); - $xml->addChild('threshold', $data->get('threshold', '0') ?: '0'); - $xml->addChild('restart_button', $data->get('restart_button', '0') ? '1' : '0'); - $xml->addChild('instantReview', $data->get('instantReview', true) ? '1' : '0'); - $xml->addChild('review', $data->get('review', 'always')); - $xml->addChild('display_score', $data->get('display_score', true) ? '1' : '0'); - $xml->addChild('logattempts', $data->get('logattempts', false) ? '1' : '0'); - $xt = $xml->addChild('translations'); - /** @var QuizTranslation $translation */ - $tid = $data->get('translation', 1) ?? 1; - if ($tid === 'en') { - $tid = 1; - } - $translation = QuizTranslation::find($tid); - foreach (QuizTranslation::getTexts() as $text => $default) { - $xt->addChild($text, $translation->getAttribute($text)); - } - foreach (self::_getColors() as $color => $c) { - $xml->addChild($color, $this->getAttribute($color)); - } - foreach (self::_getMessages() as $message => $m) { - $xml->addChild($message, $this->getAttribute($message) ?: $translation->getAttribute($message)); - } - foreach (self::_getActions() as $action => $a) { - $xml->addChild($action, $this->getAttribute($action)); - } - $questions = $this->getAttribute('questions'); - $xqs = $xml->addChild('questions'); - foreach ($questions as $question) { - if (!isset($question['type'])) { - $question['type'] = 'multiple'; - } - if (!isset($question['required'])) { - $question['required'] = true; - } - if (!isset($question['count_for_score'])) { - $question['count_for_score'] = true; - } - $xq = $xqs->addChild('question'); - if ($question['multiple']) { - $xq->addAttribute('multiple', '1'); - } - $xq->addAttribute('required', $question['required'] ? '1' : '0'); - $xq->addAttribute('count', $question['count_for_score'] ? '1' : '0'); - $xq->addAttribute('type', $question['type']); - $xq->addAttribute('min_score', $question['min_score']); - $xq->addChild('label', htmlspecialchars($question['question'])); - if ($question['type'] === 'multiple') { - $xas = $xq->addChild('answers'); - foreach ($question['answers'] as $answer) { - $xa = $xas->addChild('answer', htmlspecialchars($answer['answer'])); - if ($answer['correct']) { - $xa->addAttribute('correct', '1'); - } - } - } else { - $xq->addAttribute('placeholder', htmlspecialchars($question['placeholder'])); - } - $xq->addChild('correction', htmlspecialchars($question['explaination'])); - } - file_put_contents($dest . '/data.xml', tidy_repair_string($xml->asXML(), ['input-xml' => 1, 'indent' => 1, 'wrap' => 0])); + file_put_contents($dest . '/data.js', 'var DATA=' . json_encode($this->getData()) . ';'); if ($this->getAttribute('scorm')) { $title = htmlspecialchars($this->getAttribute('title')); @@ -347,6 +282,39 @@ class Quiz extends CubistMagicAbstractModel } } + public function getData() + { + $data = $this->getPageData(); + $d = $data->getRawData(); + + // Create data.js + $d['translations'] = []; + $tid = $data->get('translation', 1) ?? 1; + if ($tid === 'en') { + $tid = 1; + } + $translation = QuizTranslation::find($tid); + foreach (QuizTranslation::getTexts() as $text => $default) { + $d['translations'][$text] = $translation->getAttribute($text); + } + + // Fix boolean + $booleans = ['multiple', 'required', 'count_for_score']; + foreach ($d['questions'] as $qn => $q) { + foreach ($booleans as $b) { + $d['questions'][$qn][$b] = !!($q[$b]); + } + } + + // Unnull + foreach ($d as $k => $v) { + if (is_null($v)) { + $d[$k] = ''; + } + } + return $d; + } + public static function getMessages() { return self::_getMessages(); diff --git a/resources/quiz/index.html b/resources/quiz/index.html index 3358ee128..00815481f 100644 --- a/resources/quiz/index.html +++ b/resources/quiz/index.html @@ -8,14 +8,6 @@ - - - - - - - -
@@ -28,5 +20,14 @@
+ + + + + + + + + diff --git a/resources/quiz/js/main.js b/resources/quiz/js/main.js index bda41f69c..b5f17f34e 100644 --- a/resources/quiz/js/main.js +++ b/resources/quiz/js/main.js @@ -3,19 +3,11 @@ var data; var score; var questionStatus = {}; - $.ajax( - { - url: 'data.xml', - dataType: 'xml', - success: xmlLoaded, - error: function () { - - } - } - ); $(window).on('resize', resize); + initContents(); + function resize() { // Window size that reserve enough space for all questions and avoid scroll jumps var maxHeight = 0; @@ -49,13 +41,8 @@ } } - function xmlLoaded(d) { - data = $(d); - initContents(); - } - function getTranslation(key) { - return $(data).find('translations ' + key).text(); + return DATA.translations[key]; } function resizeContainer() { @@ -72,38 +59,27 @@ } function initContents() { - - if ($(data).find('title').length > 0) { - var title = $(data).find('title:eq(0)').text(); - $("header .headerholder .titleHolder").append('

' + title + '

'); - $("header .headerholder .titleHolder h1").css('width', '99%'); - $("title").text(title); - } - - var mainColor = '#e7007a'; - if ($(data).find('mainColor').length > 0) { - mainColor = $(data).find('mainColor').text(); - } - document.documentElement.style.setProperty('--main-color', mainColor); - document.documentElement.style.setProperty('--header-overlay', $(data).find('overlay').text()); - document.documentElement.style.setProperty('--ok-color', $(data).find('okColor').text()); - document.documentElement.style.setProperty('--nok-color', $(data).find('nokColor').text()); - document.documentElement.style.setProperty('--review-background', $(data).find('reviewBackground').text()); - $('head').append(''); + $("header .headerholder .titleHolder").append('

' + DATA.title + '

'); + $("header .headerholder .titleHolder h1").css('width', '99%'); + $("title").text(DATA.title); + + document.documentElement.style.setProperty('--main-color', DATA.mainColor); + document.documentElement.style.setProperty('--header-overlay', DATA.overlay); + document.documentElement.style.setProperty('--ok-color', DATA.okColor); + document.documentElement.style.setProperty('--nok-color', DATA.nokColor); + document.documentElement.style.setProperty('--review-background', DATA.reviewBackground); + $('head').append(''); cssVars({}); - var rev = $(data).find('review'); - showReview = rev.text(); + showReview = DATA.review; threshold = 0; - instantReview = $(data).find('instantReview').text() === '1'; - logAttempts = $(data).find('logattempts').text() === '1'; - displayScore = $(data).find('display_score').text() === '1'; - - if ($(data).find('threshold').length > 0 && !isNaN(parseFloat($(data).find('threshold').text()))) { - threshold = parseFloat($(data).find('threshold').text()); - if (threshold > 1) { - threshold /= 100; - } + instantReview = DATA.instantReview; + logAttempts = DATA.logattempts; + displayScore = DATA.display_score; + + threshold = DATA.threshold; + if (threshold > 1) { + threshold /= 100; } // Create quiz container @@ -115,49 +91,51 @@ var qn = 1; countQuestions = 0; // Create questions - $(data).find('questions question').each(function () { - if ($(this).attr('count') == '1') { + $.each(DATA.questions, function (k, question) { + console.log(question); + if (question.count) { countQuestions++; } - var type = $(this).attr('type'); - var multiple = $(this).attr('multiple') == '1'; - var multipleclass = multiple ? ' multiple' : ''; - var question = '
'; - question += '

' + sprintf(getTranslation('question'), qn) + '

' + $(this).find('label').text() + '
'; - question += '
'; - question += '
'; - if (multiple) { - question += '' + getTranslation('validateAnswer') + ''; - } else if (type !== 'multiple') { - question += '' + getTranslation('validateAnswerSingular') + ''; + + var multipleclass = question.multiple ? ' multiple' : ''; + var questionHtml = '
'; + questionHtml += '

' + sprintf(getTranslation('question'), qn) + '

' + question.question + '
'; + questionHtml += '
'; + questionHtml += '
'; + if (question.multiple) { + questionHtml += '' + getTranslation('validateAnswer') + ''; + } else if (question.type !== 'multiple') { + questionHtml += '' + getTranslation('validateAnswerSingular') + ''; } - question += '
'; + questionHtml += '
'; - var questionCorrection = $(this).find('correction').text(); + var questionCorrection = question.correction; if (questionCorrection === '') { var c = []; - $(this).find('answer[correct=1]').each(function () { - c.push($(this).text()); - }) + $.each(function (kk, answer) { + if (answer.correct) { + c.push(answer.answer); + } + }); questionCorrection = c.join('
'); } - var c = '

' + sprintf(getTranslation('question'), qn) + ' ' + $(this).find('label').text() + '

' + questionCorrection + '

'; + var c = '

' + sprintf(getTranslation('question'), qn) + ' ' + question.question + '

' + questionCorrection + '

'; $(correction).append(c); - var q = $(question); - var required = $(q).attr('required') === '1' ? 'required' : ''; + var q = $(questionHtml); + var required = question.required === 1 ? 'required' : ''; // Append answers var holder = $('
'); - if (type === 'multiple') { + if (question.type === 'multiple') { var an = 0; var correctCount = 0; - $(this).find('answer').each(function () { - var a = $('
' + $(this).text() + '
'); + $.each(question.answers, function (aa, answer) { + var a = $('
' + answer.answer + '
'); // Set correct tag - var correct = $(this).attr('correct') == '1' ? 1 : 0; + var correct = answer.correct ? 1 : 0; correctCount += correct; $(a).find('.answer').attr('data-correct', correct); $(holder).append($(a)); @@ -167,10 +145,13 @@ if (correctCount > 1 || $(this).attr('multiple') !== undefined) { q.addClass('multiple'); } - } else if (type === 'text' || type === "email") { - $(holder).append('') - } else if (type === 'textarea') { - $(holder).append('') + } else if (question.type === 'text' || question.type === "email") { + $(holder).append('') + } else if (question.type === 'textarea') { + $(holder).append('') + } else if (question.type === 'country') { + var select = ''; + $(holder).append(select); } $(q).find('.answers').append($(holder)); @@ -180,12 +161,12 @@ qn++; }); - var defaultMessage = $(data).find('defaultMessage').length > 0 && $(data).find('defaultMessage').text() !== '' ? $(data).find('defaultMessage').text() : 'You have finished the quiz!'; - var passedMessage = $(data).find('passedMessage').length > 0 && $(data).find('passedMessage').text() !== '' ? $(data).find('passedMessage').text() : defaultMessage; - var failedMessage = $(data).find('failedMessage').length > 0 && $(data).find('failedMessage').text() !== '' ? $(data).find('failedMessage').text() : defaultMessage; + var defaultMessage = DATA.defaultMessage !== '' ? DATA.defaultMessage : 'You have finished the quiz!'; + var passedMessage = DATA.passedMessage !== '' ? DATA.passedMessage : defaultMessage; + var failedMessage = DATA.failedMessage !== '' ? DATA.failedMessage : defaultMessage; - passedAction = $(data).find('passedAction').length > 0 && $(data).find('passedAction').text() !== '' ? $(data).find('passedAction').text() : null; - failedAction = $(data).find('failedAction').length > 0 && $(data).find('failedAction').text() !== '' ? $(data).find('failedAction').text() : null; + passedAction = DATA.passedAction !== '' ? DATA.passedAction : null; + failedAction = DATA.failedAction !== '' ? DATA.failedAction : null; var results = '
'; results += '
';