mirror of
https://gitlab.com/MrFry/moodle-test-userscript
synced 2025-04-01 20:22:48 +02:00
added custom data handling to question class
This commit is contained in:
parent
fd5813ae28
commit
339d67d5cc
2 changed files with 123 additions and 68 deletions
|
@ -1 +1 @@
|
|||
Subproject commit 82c5b21c7fd374b9f32887808b6a3d90f545b93e
|
||||
Subproject commit 01b971b4c875a4be8b7a8c6dcdd58b2d83761f9d
|
157
stable.user.js
157
stable.user.js
|
@ -42,8 +42,9 @@
|
|||
//
|
||||
// TODO:
|
||||
// test select boxes on result page
|
||||
// dragboxes test on quiz page
|
||||
// image in questions sorted !
|
||||
// grabboxes test on quiz page
|
||||
// image in questions sorted ! deleted ?
|
||||
// image in question in general
|
||||
|
||||
(function() { // eslint-disable-line
|
||||
// GM functions, only to disable ESLINT errors
|
||||
|
@ -58,7 +59,7 @@
|
|||
|
||||
var data // all data, which is in the resource txt
|
||||
var addEventListener // add event listener function
|
||||
const lastChangeLog = 'Kérdés parsolás bugfixek, old school fálj beolvasás kiszedése, részletesebb hibajelentés és egyéb fixek'
|
||||
const lastChangeLog = '' // TODO
|
||||
// const serverAdress = 'https://qmining.frylabs.net/'
|
||||
const serverAdress = 'http://localhost:8080/' // TODO
|
||||
|
||||
|
@ -91,6 +92,7 @@
|
|||
const specialChars = [ '&', '\\+' ]
|
||||
const lengthDiffMultiplier = 10 /* Percent minus for length difference */
|
||||
const minMatchAmmount = 60 /* Minimum ammount to consider that two questions match during answering */
|
||||
const notSameDataTypePenalty = 30 // substracted from match percent if 2 questions are not same type
|
||||
|
||||
const assert = (val) => {
|
||||
if (!val) { throw new Error('Assertion failed') }
|
||||
|
@ -251,22 +253,17 @@
|
|||
const SUtils = new StringUtils()
|
||||
|
||||
class Question {
|
||||
constructor (q, a, i) {
|
||||
constructor (q, a, data) {
|
||||
this.Q = SUtils.SimplifyQuestion(q)
|
||||
this.A = SUtils.SimplifyAnswer(a)
|
||||
// TODO: sorting breaks image order
|
||||
try {
|
||||
this.I = JSON.parse(i).sort((a, b) => {
|
||||
return a.localeCompare(b)
|
||||
})
|
||||
} catch (e) {
|
||||
this.I = i
|
||||
}
|
||||
this.data = { ...data }
|
||||
}
|
||||
|
||||
toString () {
|
||||
var r = '?' + this.Q + '\n!' + this.A
|
||||
if (this.I) { r += '\n>' + this.I }
|
||||
if (this.data.type === 'image') {
|
||||
r += '\n>' + this.data.images.join(', ')
|
||||
}
|
||||
return r
|
||||
}
|
||||
|
||||
|
@ -279,39 +276,58 @@
|
|||
}
|
||||
|
||||
HasImage () {
|
||||
return this.I !== undefined && (typeof this.I === 'string' || Array.isArray(this.I))
|
||||
return this.data.type === 'image'
|
||||
}
|
||||
|
||||
IsComplete () {
|
||||
return this.HasQuestion() && this.HasAnswer()
|
||||
}
|
||||
|
||||
Compare (q2, i) {
|
||||
assert(q2)
|
||||
|
||||
if (typeof q2 === 'string') {
|
||||
var qmatchpercent = SUtils.CompareString(this.Q, q2)
|
||||
|
||||
if (i === undefined || i.length === 0) { return qmatchpercent } else {
|
||||
if (this.HasImage()) {
|
||||
const iString = typeof this.I === 'string' ? this.I : this.I.join(' ')
|
||||
const imatchpercent = this.HasImage() ? SUtils.CompareString(iString, i.join(' ')) : 0
|
||||
return (qmatchpercent + imatchpercent) / 2
|
||||
} else {
|
||||
qmatchpercent -= 30
|
||||
if (qmatchpercent < 0) { return 0 } else { return qmatchpercent }
|
||||
}
|
||||
}
|
||||
} else {
|
||||
CompareToQuestionObj (q2) {
|
||||
const qmatchpercent = SUtils.CompareString(this.Q, q2.Q)
|
||||
const amatchpercent = SUtils.CompareString(this.A, q2.A)
|
||||
if (this.I !== undefined) {
|
||||
const imatchpercent = q2.I === undefined ? 0 : SUtils.CompareString(this.I.join(' '), q2.I.join(' '))
|
||||
if (this.data.images !== undefined) {
|
||||
const imatchpercent = this.data.images === undefined ? SUtils.CompareString(this.data.images.join(' '), q2.data.images.join(
|
||||
' ')) : 0
|
||||
return (qmatchpercent + amatchpercent + imatchpercent) / 3
|
||||
} else {
|
||||
return (qmatchpercent + amatchpercent) / 2
|
||||
}
|
||||
}
|
||||
|
||||
CompareImage (data2) {
|
||||
return SUtils.CompareString(this.data.images.join(' '), data2.images.join(' '))
|
||||
}
|
||||
|
||||
CompareToQuestionString (q2, data) {
|
||||
assert(data)
|
||||
|
||||
let qmatchpercent = SUtils.CompareString(this.Q, q2)
|
||||
let dataMatchPercent = 0
|
||||
|
||||
if (data.type === this.data.type) { // both questins are same type
|
||||
if (data.type === 'simple') {
|
||||
return qmatchpercent
|
||||
} else if (data.type === 'image') {
|
||||
dataMatchPercent = this.CompareImage(data)
|
||||
}
|
||||
|
||||
return (qmatchpercent + dataMatchPercent) / 2
|
||||
} else { // question types are not the same
|
||||
dataMatchPercent = 0
|
||||
qmatchpercent -= notSameDataTypePenalty
|
||||
if (qmatchpercent < 0) { return 0 } else { return qmatchpercent }
|
||||
}
|
||||
}
|
||||
|
||||
Compare (q2, data) {
|
||||
assert(q2)
|
||||
|
||||
if (typeof q2 === 'string') {
|
||||
return this.CompareToQuestionString(q2, data)
|
||||
} else {
|
||||
return this.CompareToQuestionObj(q2)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -368,12 +384,12 @@
|
|||
}
|
||||
}
|
||||
|
||||
Search (q, img) {
|
||||
Search (q, data) {
|
||||
assert(q)
|
||||
|
||||
var r = []
|
||||
for (let i = 0; i < this.length; i++) {
|
||||
let percent = this.Questions[i].Compare(q, img)
|
||||
let percent = this.Questions[i].Compare(q, data)
|
||||
if (percent > minMatchAmmount) {
|
||||
r.push({
|
||||
q: this.Questions[i],
|
||||
|
@ -443,12 +459,12 @@
|
|||
}
|
||||
}
|
||||
|
||||
Search (q, img) {
|
||||
Search (q, data) {
|
||||
assert(q)
|
||||
|
||||
var r = []
|
||||
for (let i = 0; i < this.length; i++) {
|
||||
if (this.GetIfActive(i)) { r = r.concat(this.Subjects[i].Search(q, img)) }
|
||||
if (this.GetIfActive(i)) { r = r.concat(this.Subjects[i].Search(q, data)) }
|
||||
}
|
||||
|
||||
for (let i = 0; i < r.length; i++) {
|
||||
|
@ -682,10 +698,11 @@
|
|||
}
|
||||
|
||||
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!'); console.log(results[i].getElementsByClassName('dragitems')[0]) }
|
||||
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++) {
|
||||
|
@ -697,6 +714,9 @@
|
|||
})
|
||||
}
|
||||
return res
|
||||
} catch (e) {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
GetRightAnswerIfCorrectShown (i) {
|
||||
|
@ -1114,7 +1134,7 @@
|
|||
var j = 0
|
||||
for (j = 0; j < d.Subjects[i].Questions.length; j++) {
|
||||
var currQ = d.Subjects[i].Questions[j]
|
||||
s.AddQuestion(new Question(currQ.Q, currQ.A, currQ.I))
|
||||
s.AddQuestion(new Question(currQ.Q, currQ.A, currQ.data))
|
||||
}
|
||||
rt.push({
|
||||
name: d.Subjects[i].Name,
|
||||
|
@ -1244,7 +1264,8 @@
|
|||
var answers = []
|
||||
questions.forEach((x, j) => {
|
||||
let question = SUtils.EmptyOrWhiteSpace(x) ? '' : SUtils.RemoveUnnecesarySpaces(x) // simplifying question
|
||||
var result = data.Search(question, SimplifyImages(imgNodes))
|
||||
// TODO: getdatafromquestion, implement grabboxes answering
|
||||
var result = data.Search(question, GetImageDataFromImgNodes(imgNodes))
|
||||
var r = PrepareAnswers(result, j)
|
||||
if (r !== undefined) { answers.push(r) }
|
||||
HighLightAnswer(result, j) // highlights the answer for the current result
|
||||
|
@ -1264,7 +1285,7 @@
|
|||
}
|
||||
msg += result[k].q.A.replace(/, /g, '\n') // adding answer
|
||||
if (result[k].q.HasImage()) {
|
||||
msg += '\n' + result[k].q.I // if it has image part, adding that too
|
||||
msg += '\n\nKépek fenti válaszok sorrendjében: ' + result[k].q.data.images.join(', ') // if it has image part, adding that too
|
||||
}
|
||||
allMessages.push({
|
||||
m: msg,
|
||||
|
@ -1346,8 +1367,8 @@
|
|||
|
||||
// this should get the image url from a result page
|
||||
// i is the index of the question
|
||||
// FIXME: move this to RPM class ??? and refactor this
|
||||
function GetImageFormResult (i) {
|
||||
var temp = null
|
||||
try {
|
||||
var imgElements = RPM.GetResultImage(i) // trying to get image
|
||||
var imgURL = [] // image urls
|
||||
|
@ -1359,14 +1380,40 @@
|
|||
}
|
||||
}
|
||||
if (imgURL.length > 0) {
|
||||
temp = JSON.stringify(imgURL)
|
||||
return temp
|
||||
return imgURL
|
||||
}
|
||||
} catch (e) {
|
||||
Log("Couldn't get images from result")
|
||||
}
|
||||
}
|
||||
|
||||
// TODO
|
||||
// function GetGrabBoxPositionsFromQuestion (i) {
|
||||
// return asd
|
||||
// }
|
||||
|
||||
function GetDataFormResult (i) {
|
||||
let data = { type: 'simple' }
|
||||
|
||||
let img = GetImageFormResult(i)
|
||||
let grabbox = RPM.GetAnswersFromGrabBox(i)
|
||||
if (img) {
|
||||
data = {
|
||||
type: 'image',
|
||||
images: img
|
||||
}
|
||||
}
|
||||
if (grabbox) {
|
||||
data = {
|
||||
type: 'grabbox',
|
||||
images: img,
|
||||
grabbox: grabbox
|
||||
}
|
||||
}
|
||||
|
||||
return data
|
||||
}
|
||||
|
||||
// saves the current quiz. questionData contains the active subjects questions
|
||||
function SaveQuiz (quiz, questionData) {
|
||||
try {
|
||||
|
@ -1384,7 +1431,7 @@
|
|||
toAdd += '?' + SUtils.RemoveUnnecesarySpaces(quiz[i].Q) + '\n' // adding quiz question
|
||||
toAdd += '!' + SUtils.RemoveUnnecesarySpaces(quiz[i].A) + '\n' // adding quiz answer
|
||||
if (quiz[i].HasImage()) {
|
||||
let imgString = typeof quiz[i].I === 'string' ? quiz[i].I : JSON.stringify(quiz[i].I)
|
||||
let imgString = quiz[i].data.images.join(', ')
|
||||
toAdd += '>' + imgString + '\n' // adding quiz image if there is any
|
||||
}
|
||||
if (SearchSameQuestion(questionData, quiz, i) === -1) {
|
||||
|
@ -1443,12 +1490,11 @@
|
|||
var a = RPM.GetRightAnswerFromResultv2(i)
|
||||
if (a === undefined) { a = RPM.GetRightAnswerFromResult(i) }
|
||||
if (a !== undefined) { question.a = SUtils.SimplifyAnswer(a) }
|
||||
// IMG ---------------------------------------------------------------------------------------------------------------------
|
||||
var img = GetImageFormResult(i)
|
||||
question.i = img
|
||||
// DATA ---------------------------------------------------------------------------------------------------------------------
|
||||
question.data = GetDataFormResult(i)
|
||||
|
||||
if (question.a !== undefined) {
|
||||
quiz.push(new Question(question.q, question.a, question.i)) // adding current question to quiz
|
||||
quiz.push(new Question(question.q, question.a, question.data)) // adding current question to quiz
|
||||
} else {
|
||||
Log('error getting queston, no correct answer given, or its incorrect')
|
||||
Log(question)
|
||||
|
@ -1464,7 +1510,7 @@
|
|||
|
||||
// : Helpers {{{
|
||||
|
||||
function SimplifyImages (imgs) {
|
||||
function GetImageDataFromImgNodes (imgs) {
|
||||
var questionImages = [] // the array for the image names in question
|
||||
for (var i = 0; i < imgs.length; i++) {
|
||||
if (!imgs[i].src.includes('brokenfile')) {
|
||||
|
@ -1473,7 +1519,16 @@
|
|||
questionImages.push(decodeURI(SUtils.RemoveUnnecesarySpaces(SUtils.ShortenString(filePart, 30)))) // decodes uri codes, and removes exess spaces, and shortening it
|
||||
}
|
||||
}
|
||||
return questionImages
|
||||
if (questionImages.length > 0) {
|
||||
return {
|
||||
type: 'image',
|
||||
images: questionImages
|
||||
}
|
||||
} else {
|
||||
return {
|
||||
type: 'simple'
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// adds image names to image nodes
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue