mirror of
https://gitlab.com/MrFry/moodle-test-userscript
synced 2025-04-01 20:22:48 +02:00
Handling basic questions
This commit is contained in:
parent
245259699c
commit
8d38ea6b49
1 changed files with 61 additions and 494 deletions
555
stable.user.js
555
stable.user.js
|
@ -84,13 +84,13 @@
|
|||
|
||||
// forcing pages for testing. unless you test, do not set these to true!
|
||||
// only one of these should be true for testing
|
||||
setVal('ISDEVEL', false)
|
||||
setVal('ISDEVEL', true)
|
||||
const forceTestPage = false
|
||||
const forceResultPage = false
|
||||
const forceResultPage = true
|
||||
const forceDefaultPage = false
|
||||
const logElementGetting = false
|
||||
const log = false
|
||||
const showErrors = false
|
||||
const log = true
|
||||
const showErrors = true
|
||||
|
||||
const motdShowCount = 3 /* Ammount of times to show motd */
|
||||
let infoExpireTime = 60 // Every n seconds basic info should be loaded from server
|
||||
|
@ -154,6 +154,62 @@
|
|||
}
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------------------------------
|
||||
// Result page processing functions
|
||||
// ----------------------------------------------------------------------------------------------
|
||||
function getQuiz() {
|
||||
let result = []
|
||||
let i = 1
|
||||
let containerNode = document.getElementById(`q${i}`)
|
||||
do {
|
||||
const question = getQuestionFromNode(containerNode)
|
||||
const answer = getAnswerFromNode(containerNode)
|
||||
result = [
|
||||
...result,
|
||||
{
|
||||
Q: question,
|
||||
A: answer,
|
||||
},
|
||||
]
|
||||
console.log(question)
|
||||
console.log(answer)
|
||||
console.log('########################')
|
||||
|
||||
i++
|
||||
containerNode = document.getElementById(`q${i}`)
|
||||
} while (containerNode)
|
||||
|
||||
// [{
|
||||
// "Q": "Mekkora tényezővel kell számolnunk, ha 100.000 Ft jelenértékét keressük 24% kamatláb, havi tőkésítés és 2,5 éves futamidő mellett?\n\n\n",
|
||||
// "A": "c.\n\n0,552\n",
|
||||
// "data": {
|
||||
// "type": "simple"
|
||||
// }
|
||||
// }]
|
||||
return result
|
||||
}
|
||||
|
||||
function getAnswerFromNode(node) {
|
||||
let answer = node.getElementsByClassName('rightanswer')
|
||||
if (answer.length === 0) {
|
||||
console.warn('NO ANSWER FOR ANSWER')
|
||||
}
|
||||
answer = answer[0]
|
||||
|
||||
return SUtils.RemoveUnnecesarySpaces(answer.innerText)
|
||||
}
|
||||
|
||||
function getQuestionFromNode(node) {
|
||||
let question = node.getElementsByClassName('qtext')
|
||||
if (question.length === 0) {
|
||||
console.warn('NO QUESTION FOR QTEXT')
|
||||
}
|
||||
question = question[0].innerText
|
||||
return SUtils.RemoveUnnecesarySpaces(question)
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------------------------------
|
||||
|
||||
class StringUtils {
|
||||
RemoveStuff(value, removableStrings, toReplace) {
|
||||
removableStrings.forEach(x => {
|
||||
|
@ -485,465 +541,6 @@
|
|||
}
|
||||
}
|
||||
|
||||
class ResultsPageModell {
|
||||
GetFormulationClearfix() {
|
||||
if (logElementGetting) {
|
||||
Log('getting formulation clearfix lol')
|
||||
}
|
||||
return document.getElementsByClassName('formulation clearfix')
|
||||
}
|
||||
|
||||
GetGrade(i) {
|
||||
if (logElementGetting) {
|
||||
Log('getting grade')
|
||||
}
|
||||
const fcf = QPM.GetFormulationClearfix()[i]
|
||||
return fcf.parentNode.parentNode.childNodes[0].childNodes[2].innerText
|
||||
}
|
||||
|
||||
DetermineQuestionType(nodes) {
|
||||
let qtype = ''
|
||||
let i = 0
|
||||
|
||||
while (i < nodes.length && qtype === '') {
|
||||
let inps = nodes[i].getElementsByTagName('input')
|
||||
|
||||
if (inps.length > 0) {
|
||||
qtype = inps[0].type
|
||||
}
|
||||
|
||||
i++
|
||||
}
|
||||
|
||||
return qtype
|
||||
}
|
||||
|
||||
GetSelectAnswer(i) {
|
||||
if (logElementGetting) {
|
||||
Log('getting selected answer')
|
||||
}
|
||||
var t = document.getElementsByTagName('select')
|
||||
if (t.length > 0) {
|
||||
return t[i].options[t[i].selectedIndex].innerText
|
||||
}
|
||||
}
|
||||
|
||||
GetCurrQuestion(i) {
|
||||
if (logElementGetting) {
|
||||
Log('getting curr questions by index: ' + i)
|
||||
}
|
||||
return document.getElementsByTagName('form')[0].childNodes[0].childNodes[
|
||||
i
|
||||
].childNodes[1].childNodes[0].innerText
|
||||
}
|
||||
|
||||
GetFormResult() {
|
||||
if (logElementGetting) {
|
||||
Log('getting form result')
|
||||
}
|
||||
var t = document.getElementsByTagName('form')[0].childNodes[0].childNodes
|
||||
if (t.length > 0 && t[0].tagName === undefined) {
|
||||
// debreceni moodle
|
||||
return document.getElementsByTagName('form')[1].childNodes[0].childNodes
|
||||
} else {
|
||||
return t
|
||||
}
|
||||
}
|
||||
|
||||
getGeneralFeedback(i) {
|
||||
return document.getElementsByClassName('generalfeedback')[0]
|
||||
}
|
||||
|
||||
GetAnswerNode(i) {
|
||||
if (logElementGetting) {
|
||||
Log('getting answer node')
|
||||
}
|
||||
|
||||
var results = this.GetFormResult() // getting results element
|
||||
|
||||
var r = results[i].getElementsByClassName('answer')[0].childNodes
|
||||
var ret = []
|
||||
for (var j = 0; j < r.length; j++) {
|
||||
if (
|
||||
r[j].tagName !== undefined &&
|
||||
r[j].tagName.toLowerCase() === 'div'
|
||||
) {
|
||||
ret.push(r[j])
|
||||
}
|
||||
}
|
||||
|
||||
let qtype = this.DetermineQuestionType(ret)
|
||||
|
||||
return {
|
||||
nodes: ret,
|
||||
type: qtype,
|
||||
}
|
||||
}
|
||||
|
||||
getTextAreaAnswer() {
|
||||
const a = document.getElementsByClassName('generalfeedback')
|
||||
if (a.length > 0) {
|
||||
return a[0].innerText
|
||||
}
|
||||
}
|
||||
|
||||
GetCurrentAnswer(i) {
|
||||
if (logElementGetting) {
|
||||
Log('getting curr answer by index: ' + i)
|
||||
}
|
||||
var results = this.GetFormResult() // getting results element
|
||||
var t = results[i]
|
||||
.getElementsByClassName('formulation clearfix')[0]
|
||||
.getElementsByTagName('span')
|
||||
if (t.length > 2) {
|
||||
return t[1].innerHTML.split('<br>')[1]
|
||||
}
|
||||
}
|
||||
|
||||
GetQText(i) {
|
||||
if (logElementGetting) {
|
||||
Log('getting qtext by index: ' + i)
|
||||
}
|
||||
var results = this.GetFormResult() // getting results element
|
||||
return results[i].getElementsByClassName('qtext')
|
||||
}
|
||||
|
||||
GetDropboxes(i) {
|
||||
if (logElementGetting) {
|
||||
Log('getting dropboxes by index: ' + i)
|
||||
}
|
||||
var results = this.GetFormResult() // getting results element
|
||||
return results[i].getElementsByTagName('select')
|
||||
}
|
||||
|
||||
GetAllAnswer(index) {
|
||||
if (logElementGetting) {
|
||||
Log('getting all answers, ind: ' + index)
|
||||
}
|
||||
return document.getElementsByClassName('answer')[index].childNodes
|
||||
}
|
||||
|
||||
GetPossibleAnswers(i) {
|
||||
if (logElementGetting) {
|
||||
Log('getting possible answers')
|
||||
}
|
||||
var results = this.GetFormResult() // getting results element
|
||||
var items = results[i].getElementsByTagName('label')
|
||||
var r = []
|
||||
for (var j = 0; j < items.length; j++) {
|
||||
const TryGetCorrect = j => {
|
||||
var cn = items[j].parentNode.className
|
||||
if (cn.includes('correct')) {
|
||||
return cn.includes('correct') && !cn.includes('incorrect')
|
||||
}
|
||||
}
|
||||
r.push({
|
||||
value: items[j].innerText,
|
||||
iscorrect: TryGetCorrect(j),
|
||||
})
|
||||
}
|
||||
return r
|
||||
}
|
||||
|
||||
GetAnswersFromGrabBox(i) {
|
||||
try {
|
||||
if (logElementGetting) {
|
||||
Log('testing if question is grab-box')
|
||||
}
|
||||
let results = this.GetFormResult() // getting results element
|
||||
let t = results[i].getElementsByClassName('dragitems')[0].childNodes
|
||||
if (t.length !== 1) {
|
||||
Log('grab box drag items group length is not 1!')
|
||||
Log(results[i].getElementsByClassName('dragitems')[0])
|
||||
}
|
||||
let placedItems = t[0].getElementsByClassName('placed')
|
||||
let res = []
|
||||
for (let i = 0; i < placedItems.length; i++) {
|
||||
let item = placedItems[i]
|
||||
res.push({
|
||||
text: item.innerText,
|
||||
left: item.style.left,
|
||||
top: item.style.top,
|
||||
})
|
||||
}
|
||||
return res
|
||||
} catch (e) {
|
||||
if (showErrors) {
|
||||
console.info(e)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
GetRightAnswerIfCorrectShown(i) {
|
||||
if (logElementGetting) {
|
||||
Log('getting right answer if correct shown')
|
||||
}
|
||||
var results = this.GetFormResult() // getting results element
|
||||
return results[i].getElementsByClassName('rightanswer')
|
||||
}
|
||||
|
||||
GetWrongAnswerIfCorrectNotShown(i) {
|
||||
if (logElementGetting) {
|
||||
Log('getting wrong answer if correct not shown')
|
||||
}
|
||||
var results = this.GetFormResult() // getting results element
|
||||
var n = results[i].getElementsByTagName('i')[0].parentNode
|
||||
if (n.className.includes('incorrect')) {
|
||||
return results[i].getElementsByTagName('i')[0].parentNode.innerText
|
||||
} else {
|
||||
return ''
|
||||
}
|
||||
}
|
||||
|
||||
GetRightAnswerIfCorrectNotShown(i) {
|
||||
if (logElementGetting) {
|
||||
Log('Getting right answer if correct not shown')
|
||||
}
|
||||
var results = this.GetFormResult() // getting results element
|
||||
var n = results[i].getElementsByTagName('i')[0].parentNode
|
||||
if (
|
||||
n.className.includes('correct') &&
|
||||
!n.className.includes('incorrect')
|
||||
) {
|
||||
return results[i].getElementsByTagName('i')[0].parentNode.innerText
|
||||
}
|
||||
}
|
||||
|
||||
GetFormCFOfResult(result) {
|
||||
if (logElementGetting) {
|
||||
Log('getting formulation clearfix')
|
||||
}
|
||||
return result.getElementsByClassName('formulation clearfix')[0]
|
||||
}
|
||||
|
||||
GetResultText(i) {
|
||||
if (logElementGetting) {
|
||||
Log('getting result text')
|
||||
}
|
||||
var results = this.GetFormResult() // getting results element
|
||||
return this.GetFormCFOfResult(results[i]).getElementsByTagName('p')
|
||||
}
|
||||
|
||||
GetResultImage(i) {
|
||||
if (logElementGetting) {
|
||||
Log('getting result image')
|
||||
}
|
||||
var results = this.GetFormResult() // getting results element
|
||||
return this.GetFormCFOfResult(results[i]).getElementsByTagName('img')
|
||||
}
|
||||
|
||||
// gets the question from the result page
|
||||
// i is the index of the question
|
||||
GetQuestionFromResult(i) {
|
||||
var temp = this.GetQText(i)
|
||||
var currQuestion = ''
|
||||
if (temp.length > 0) {
|
||||
currQuestion = temp[0].innerText // adding the question to curr question as .q
|
||||
} else {
|
||||
// this is black magic fuckery a bit
|
||||
if (this.GetDropboxes(i).length > 0) {
|
||||
var allNodes = this.GetResultText(i)
|
||||
currQuestion = ''
|
||||
for (var k = 0; k < allNodes.length; k++) {
|
||||
var allQuestions = this.GetResultText(i)[k].childNodes
|
||||
for (var j = 0; j < allQuestions.length; j++) {
|
||||
if (
|
||||
allQuestions[j].data !== undefined &&
|
||||
!SUtils.EmptyOrWhiteSpace(allQuestions[j].data)
|
||||
) {
|
||||
currQuestion += allQuestions[j].data + ' '
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
try {
|
||||
currQuestion = this.GetCurrQuestion(i)
|
||||
} catch (e) {
|
||||
currQuestion = 'REEEEEEEEEEEEEEEEEEEEE' // this shouldnt really happen sry guys
|
||||
Log('Unable to get question in GetQuestionFromResult')
|
||||
}
|
||||
}
|
||||
}
|
||||
return currQuestion
|
||||
}
|
||||
|
||||
GetRightAnswerFromResult(i) {
|
||||
let res = this.GetRightAnswerFromResultv2(i)
|
||||
if (!res) {
|
||||
res = this.GetRightAnswerFromResultv1(i)
|
||||
}
|
||||
return res
|
||||
}
|
||||
|
||||
// tries to get right answer from result page
|
||||
// i is the index of the question
|
||||
GetRightAnswerFromResultv1(i) {
|
||||
var fun = []
|
||||
|
||||
// "húzza oda ..." skip
|
||||
fun.push(i => {
|
||||
let temp = RPM.GetAnswersFromGrabBox(i)
|
||||
return temp
|
||||
.map(x => {
|
||||
return x.text
|
||||
})
|
||||
.join(', ')
|
||||
})
|
||||
|
||||
// the basic type of getting answers
|
||||
fun.push(i => {
|
||||
var temp = RPM.GetRightAnswerIfCorrectShown(i) // getting risht answer
|
||||
if (temp.length > 0) {
|
||||
return temp[0].innerText
|
||||
} // adding the answer to curr question as .a
|
||||
})
|
||||
|
||||
// if there is dropdown list in the current question
|
||||
fun.push(i => {
|
||||
if (RPM.GetDropboxes(i).length > 0) {
|
||||
return RPM.GetCurrentAnswer(i)
|
||||
}
|
||||
})
|
||||
|
||||
// if the correct answers are not shown, and the selected answer
|
||||
// is correct
|
||||
fun.push(i => {
|
||||
return RPM.GetRightAnswerIfCorrectNotShown(i)
|
||||
})
|
||||
|
||||
// if there is dropbox in the question
|
||||
fun.push(i => {
|
||||
return RPM.GetSelectAnswer(i)
|
||||
})
|
||||
|
||||
// if the correct answers are not shown, and the selected answer
|
||||
// is incorrect, and there are only 2 options
|
||||
fun.push(i => {
|
||||
var possibleAnswers = RPM.GetPossibleAnswers(i)
|
||||
if (possibleAnswers.length === 2) {
|
||||
for (var k = 0; k < possibleAnswers.length; k++) {
|
||||
if (possibleAnswers[k].iscorrect === undefined) {
|
||||
return possibleAnswers[k].value
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
// if everything fails
|
||||
fun.push(i => {
|
||||
return undefined
|
||||
})
|
||||
|
||||
var j = 0
|
||||
var currAnswer
|
||||
while (j < fun.length && SUtils.EmptyOrWhiteSpace(currAnswer)) {
|
||||
try {
|
||||
currAnswer = fun[j](i)
|
||||
} catch (e) {
|
||||
if (showErrors) {
|
||||
console.info(e)
|
||||
}
|
||||
}
|
||||
j++
|
||||
}
|
||||
|
||||
return currAnswer
|
||||
}
|
||||
|
||||
GuessCorrectIn2LengthAnswersByIncorrect(items) {
|
||||
const first = items[0]
|
||||
const second = items[1]
|
||||
if (first.className.includes('incorrect')) {
|
||||
return second.innerText
|
||||
}
|
||||
if (second.className.includes('incorrect')) {
|
||||
return first.innerText
|
||||
}
|
||||
}
|
||||
|
||||
GuessCorrectIn2LengthAnswersByPoints(i, items) {
|
||||
const first = {
|
||||
elem: items[0],
|
||||
val: items[0].childNodes[0].checked,
|
||||
text: items[0].innerText,
|
||||
}
|
||||
const second = {
|
||||
elem: items[1],
|
||||
val: items[1].childNodes[0].checked,
|
||||
text: items[1].innerText,
|
||||
}
|
||||
|
||||
const grade = RPM.GetGrade(i) // 1,00 közül 1,00 leosztályozva
|
||||
const grades = grade.split(' ').reduce((acc, text) => {
|
||||
if (text.includes(',')) {
|
||||
// FIXME: fancy regexp
|
||||
acc.push(parseInt(text))
|
||||
} else if (text.includes('.')) {
|
||||
// FIXME: fancy regexp
|
||||
acc.push(parseInt(text))
|
||||
}
|
||||
return acc
|
||||
}, [])
|
||||
|
||||
if (grades[0] === 1) {
|
||||
if (first.val) {
|
||||
return first.text
|
||||
} else {
|
||||
return second.text
|
||||
}
|
||||
} else {
|
||||
if (!first.val) {
|
||||
return first.text
|
||||
} else {
|
||||
return second.text
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// version 2 of getting right answer from result page
|
||||
// i is the index of the question
|
||||
GetRightAnswerFromResultv2(i) {
|
||||
try {
|
||||
var answerNodes = this.GetAnswerNode(i)
|
||||
let items = answerNodes.nodes
|
||||
const generalfeedback = this.getGeneralFeedback(i)
|
||||
|
||||
if (generalfeedback) {
|
||||
return generalfeedback.innerText
|
||||
}
|
||||
|
||||
if (answerNodes.type === 'checkbox') {
|
||||
return RPM.GetRightAnswerFromResultv1(i)
|
||||
}
|
||||
|
||||
for (let j = 0; j < items.length; j++) {
|
||||
let cn = items[j].className
|
||||
if (cn.includes('correct') && !cn.includes('incorrect')) {
|
||||
return items[j].getElementsByTagName('label')[0].innerText
|
||||
}
|
||||
}
|
||||
if (items.length === 2) {
|
||||
const resByIncorrect = this.GuessCorrectIn2LengthAnswersByIncorrect(
|
||||
items
|
||||
)
|
||||
if (!resByIncorrect) {
|
||||
const resPoints = this.GuessCorrectIn2LengthAnswersByPoints(
|
||||
i,
|
||||
items
|
||||
)
|
||||
return resPoints
|
||||
}
|
||||
return resByIncorrect
|
||||
}
|
||||
} catch (e) {
|
||||
Log('error at new nodegetting, trying the oldschool way')
|
||||
if (showErrors) {
|
||||
console.info(e)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class MiscPageModell {
|
||||
GetCurrentSubjectName() {
|
||||
if (logElementGetting) {
|
||||
|
@ -977,7 +574,6 @@
|
|||
}
|
||||
|
||||
var QPM = new QuestionsPageModell()
|
||||
var RPM = new ResultsPageModell()
|
||||
var MPM = new MiscPageModell()
|
||||
|
||||
// : }}}
|
||||
|
@ -1439,7 +1035,7 @@
|
|||
// : Quiz saving {{{
|
||||
|
||||
function HandleResults(url) {
|
||||
SaveQuiz(GetQuiz(), ShowSaveQuizDialog) // saves the quiz questions and answers
|
||||
SaveQuiz(getQuiz(), ShowSaveQuizDialog) // saves the quiz questions and answers
|
||||
}
|
||||
|
||||
function ShowSaveQuizDialog(sendResult, sentData, newQuestions) {
|
||||
|
@ -1549,35 +1145,6 @@
|
|||
}
|
||||
}
|
||||
|
||||
// getting quiz from finish page
|
||||
function GetQuiz() {
|
||||
try {
|
||||
var quiz = [] // final quiz stuff
|
||||
var results = RPM.GetFormResult() // getting results element
|
||||
for (var i = 0; i < results.length - 2; i++) {
|
||||
var question = {} // the current question
|
||||
// QUESTION --------------------------------------------------------------------------------------------------------------------
|
||||
question.Q = RPM.GetQuestionFromResult(i)
|
||||
// RIGHTANSWER ---------------------------------------------------------------------------------------------------------------------
|
||||
question.A = RPM.GetRightAnswerFromResult(i)
|
||||
// DATA ---------------------------------------------------------------------------------------------------------------------
|
||||
question.data = GetDataFormResult(i)
|
||||
|
||||
if (question.A !== undefined) {
|
||||
quiz.push(question) // adding current question to quiz
|
||||
} else {
|
||||
Log(
|
||||
'error getting queston, no correct answer given, or its incorrect'
|
||||
)
|
||||
Log(question)
|
||||
}
|
||||
}
|
||||
return quiz
|
||||
} catch (e) {
|
||||
Exception(e, 'script error at quiz parsing:')
|
||||
}
|
||||
}
|
||||
|
||||
// : }}}
|
||||
|
||||
// : Helpers {{{
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue