From 566321eed8291c6bfecdc215fcb77ffd3bb983ee Mon Sep 17 00:00:00 2001 From: mrfry Date: Thu, 19 Nov 2020 18:12:17 +0100 Subject: [PATCH] Showing answer --- stable.user.js | 109 +++++++++++++++++++++++++++++-------------------- 1 file changed, 64 insertions(+), 45 deletions(-) diff --git a/stable.user.js b/stable.user.js index 9ff326c..66ea7b2 100755 --- a/stable.user.js +++ b/stable.user.js @@ -223,33 +223,57 @@ // ---------------------------------------------------------------------------------------------- function handleQuiz() { - // TODO: dropdown in question - // TODO: multiple selects, img in question - // TODO: - const promises = [] - const subjName = getCurrentSubjectName() - const questionNodes = Array.from( - document.getElementsByTagName('form')[0].childNodes[0].childNodes - ) + getQuizData().then(res => { + const promises = [] - let i = 0 - while ( - i < questionNodes.length && - questionNodes[i].tagName === 'DIV' && - questionNodes[i].className !== 'submitbtns' - ) { - promises.push(getQuestionPromiseForSingleQuestion(questionNodes[i])) - i++ - } + res.forEach(question => { + promises.push( + GetXHRQuestionAnswer({ + q: question.question, + data: question.data, + subj: getCurrentSubjectName(), + }) + ) + }) - Promise.all(promises) - .then(res => { - console.log(res) - }) - .catch(err => { - console.warn('Error in handleQuiz()') - console.warn(err) + Promise.all(promises).then(res => { + const answers = res.map(result => { + return PrepareAnswers(result) + }) + + // TODO: new lines in answers + ShowAnswers(answers, res[0].question) }) + }) + } + + function getQuizData() { + return new Promise(resolve => { + // TODO: dropdown in question + const promises = [] + const questionNodes = Array.from( + document.getElementsByTagName('form')[0].childNodes[0].childNodes + ) + + let i = 0 + while ( + i < questionNodes.length && + questionNodes[i].tagName === 'DIV' && + questionNodes[i].className !== 'submitbtns' + ) { + promises.push(getQuestionPromiseForSingleQuestion(questionNodes[i])) + i++ + } + + Promise.all(promises) + .then(res => { + resolve(res) + }) + .catch(err => { + console.warn('Error in handleQuiz()') + console.warn(err) + }) + }) } function getPossibleAnswersFromTest(node) { @@ -1158,34 +1182,29 @@ // : Answering stuffs {{{ // TODO - function PrepareAnswers(result, j) { + function PrepareAnswers(result) { assert(result) if (result.length > 0) { - var allMessages = [] // preparing all messages - for (var k = 0; k < result.length; k++) { - var msg = '' // the current message - if (getVal('showQuestions') === undefined || getVal('showQuestions')) { - msg += result[k].q.Q + '\n' // adding the question if yes - } - msg += result[k].q.A.replace(/, /g, '\n') // adding answer - if (result[k].q.data.type === 'image') { + return result.map(res => { + let msg = res.q.Q + '\n' + + // TODO: remove this maybe? + msg += res.q.A.replace(/, /g, '\n') // adding answer + + if (res.q.data.type === 'image') { msg += '\n\nKépek fenti válaszok sorrendjében: ' + - result[k].q.data.images.join(', ') // if it has image part, adding that too + res.q.data.images.join(', ') // if it has image part, adding that too } - if ( - result[k].detailedMatch && - result[k].detailedMatch.matchedSubjName - ) { - msg += '\n(Tárgy: ' + result[k].detailedMatch.matchedSubjName + ')' - } - allMessages.push({ + + msg += '\n(Tárgy: ' + res.detailedMatch.matchedSubjName + ')' + + return { m: msg, - p: result[k].match, - }) - } - return allMessages + p: res.match, + } + }) } }