mirror of
https://gitlab.com/MrFry/moodle-test-userscript
synced 2025-04-01 20:22:48 +02:00
Refactoring element getting stuff into classes
This commit is contained in:
parent
da7eac9e56
commit
6ec179fdf9
1 changed files with 430 additions and 431 deletions
861
main.js
861
main.js
|
@ -224,6 +224,425 @@ class QuestionDB {
|
||||||
}
|
}
|
||||||
//: }}}
|
//: }}}
|
||||||
|
|
||||||
|
//: DOM getting stuff {{{
|
||||||
|
// all dom getting stuff are in this sections, so on
|
||||||
|
// moodle dom change, stuff breaks here
|
||||||
|
|
||||||
|
class QuestionsPageModell {
|
||||||
|
|
||||||
|
GetAllQuestionsDropdown() {
|
||||||
|
if (logElementGetting)
|
||||||
|
Log("getting dropdown question");
|
||||||
|
let items = document.getElementById("responseform").getElementsByTagName("p")[0].childNodes;
|
||||||
|
let r = "";
|
||||||
|
items.forEach((item) => {
|
||||||
|
if (item.tagName == undefined)
|
||||||
|
r += item.nodeValue;
|
||||||
|
|
||||||
|
});
|
||||||
|
return r;
|
||||||
|
}
|
||||||
|
|
||||||
|
GetAllQuestionsQtext() {
|
||||||
|
if (logElementGetting)
|
||||||
|
Log("getting all questions qtext");
|
||||||
|
return document.getElementById("responseform").getElementsByClassName("qtext"); // getting questions
|
||||||
|
}
|
||||||
|
|
||||||
|
GetAllQuestionsP() {
|
||||||
|
if (logElementGetting)
|
||||||
|
Log("getting all questions by tag p");
|
||||||
|
return document.getElementById("responseform").getElementsByTagName("p");
|
||||||
|
}
|
||||||
|
|
||||||
|
GetFormulationClearfix() {
|
||||||
|
if (logElementGetting)
|
||||||
|
Log("getting formulation clearfix lol");
|
||||||
|
return document.getElementsByClassName("formulation clearfix");
|
||||||
|
}
|
||||||
|
|
||||||
|
GetAnswerOptions() {
|
||||||
|
if (logElementGetting)
|
||||||
|
Log("getting all answer options");
|
||||||
|
return this.GetFormulationClearfix()[0].childNodes[3].innerText;
|
||||||
|
}
|
||||||
|
|
||||||
|
GetQuestionImages() {
|
||||||
|
if (logElementGetting)
|
||||||
|
Log("getting question images");
|
||||||
|
return this.GetFormulationClearfix()[0].getElementsByTagName("img");
|
||||||
|
}
|
||||||
|
|
||||||
|
// this function should return the question, posible answers, and image names
|
||||||
|
GetQuestionFromTest() {
|
||||||
|
var questions; // the important questions
|
||||||
|
var allQuestions; // all questions
|
||||||
|
try // trying to get tha current questions
|
||||||
|
{
|
||||||
|
allQuestions = this.GetAllQuestionsQtext(); // getting questions
|
||||||
|
if (allQuestions.length == 0) // if there are no found questions
|
||||||
|
{
|
||||||
|
var ddq = this.GetAllQuestionsDropdown();
|
||||||
|
if (EmptyOrWhiteSpace(ddq)) {
|
||||||
|
var questionData = "";
|
||||||
|
for (var j = 0; j < allQuestions.length; j++) {
|
||||||
|
// TODO: test dis
|
||||||
|
let subAllQuestions = allQuestions[j].childNodes;
|
||||||
|
for (var i = 0; i < subAllQuestions.length; i++) {
|
||||||
|
if (subAllQuestions[i].data != undefined && !EmptyOrWhiteSpace(subAllQuestions[i].data)) // if the current element has some text data to add
|
||||||
|
{
|
||||||
|
questionData += subAllQuestions[i].data + " "; // adding text to question data
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
questions = [questionData];
|
||||||
|
} else
|
||||||
|
questions = [ddq];
|
||||||
|
} else // if there is
|
||||||
|
{
|
||||||
|
questions = [];
|
||||||
|
for (var i = 0; i < allQuestions.length; i++) {
|
||||||
|
questions.push(allQuestions[i].innerText);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
Exception(e, "script error at getting question:");
|
||||||
|
}
|
||||||
|
var imgNodes = ""; // the image nodes for questions
|
||||||
|
try {
|
||||||
|
imgNodes = this.GetQuestionImages(); // getting question images, if there is any
|
||||||
|
AddImageNamesToImages(imgNodes); // adding image names to images, so its easier to search for, or even guessing
|
||||||
|
} catch (e) {
|
||||||
|
Log(e);
|
||||||
|
Log("Some error with images");
|
||||||
|
}
|
||||||
|
|
||||||
|
questions = questions.map((item, ind) => {
|
||||||
|
return ReplaceCharsWithSpace(item, "\n");
|
||||||
|
});
|
||||||
|
|
||||||
|
return {
|
||||||
|
imgnodes: imgNodes,
|
||||||
|
allQ: allQuestions,
|
||||||
|
q: questions
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class ResultsPageModell {
|
||||||
|
|
||||||
|
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() {
|
||||||
|
if (logElementGetting)
|
||||||
|
Log("getting selected answer");
|
||||||
|
var t = document.getElementsByTagName("select");
|
||||||
|
if (t.length > 0)
|
||||||
|
return t[0].options[document.getElementsByTagName("select")[
|
||||||
|
0].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;
|
||||||
|
}
|
||||||
|
|
||||||
|
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
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
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++) {
|
||||||
|
|
||||||
|
function 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;
|
||||||
|
}
|
||||||
|
|
||||||
|
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;
|
||||||
|
else
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
|
||||||
|
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) // if the qtext element is not 0 length
|
||||||
|
{
|
||||||
|
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) // if there is dropdown list in the current question
|
||||||
|
{
|
||||||
|
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 && !EmptyOrWhiteSpace(allQuestions[j].data)) {
|
||||||
|
currQuestion += allQuestions[j].data + " ";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
try {
|
||||||
|
currQuestion = this.GetCurrQuestion(i);
|
||||||
|
} catch (e) {
|
||||||
|
currQuestion = "REEEEEEEEEEEEEEEEEEEEE"; // this shouldnt really happen sry guys
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return currQuestion;
|
||||||
|
}
|
||||||
|
|
||||||
|
// tries to get right answer from result page
|
||||||
|
// i is the index of the question
|
||||||
|
GetRightAnswerFromResult(i) {
|
||||||
|
var fun = [];
|
||||||
|
|
||||||
|
// the basic type of getting answers
|
||||||
|
fun.push(function TryGet0(i) {
|
||||||
|
var temp = this.GetRightAnswerIfCorrectShown(i); // getting risht answer
|
||||||
|
if (temp.length > 0) // if the rightanswer element is not 0 length
|
||||||
|
return temp[0].innerText; // adding the answer to curr question as .a
|
||||||
|
});
|
||||||
|
|
||||||
|
// if there is dropdown list in the current question
|
||||||
|
fun.push(function TryGet1(i) {
|
||||||
|
if (this.GetDropboxes(i).length > 0)
|
||||||
|
return this.GetCurrentAnswer(i);
|
||||||
|
});
|
||||||
|
|
||||||
|
// if the correct answers are not shown, and the selected answer
|
||||||
|
// is correct
|
||||||
|
fun.push(function TryGet2(i) {
|
||||||
|
return this.GetRightAnswerIfCorrectNotShown(i);
|
||||||
|
});
|
||||||
|
|
||||||
|
// if there is dropbox in the question
|
||||||
|
fun.push(function TryGet3(i) {
|
||||||
|
return this.GetSelectAnswer();
|
||||||
|
});
|
||||||
|
|
||||||
|
// if the correct answers are not shown, and the selected answer
|
||||||
|
// is incorrect, and there are only 2 options
|
||||||
|
fun.push(function TryGet4(i) {
|
||||||
|
var possibleAnswers = this.GetPossibleAnswers(i);
|
||||||
|
if (possibleAnswers.length == 2) {
|
||||||
|
for (var k = 0; k < possibleAnswers.length; k++)
|
||||||
|
if (possibleAnswers[k].iscorrect == undefined)
|
||||||
|
return possibleAnswers[k].value;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
fun.push(function TryGetFinal(i) {
|
||||||
|
return undefined;
|
||||||
|
});
|
||||||
|
|
||||||
|
var j = 0;
|
||||||
|
var currAnswer;
|
||||||
|
while (j < fun.length && EmptyOrWhiteSpace(currAnswer)) {
|
||||||
|
currAnswer = fun[j](i);
|
||||||
|
j++;
|
||||||
|
}
|
||||||
|
|
||||||
|
return currAnswer;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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;
|
||||||
|
|
||||||
|
if (answerNodes.type == "checkbox")
|
||||||
|
return GetRightAnswerFromResult(i);
|
||||||
|
|
||||||
|
for (var j = 0; j < items.length; j++) {
|
||||||
|
var cn = items[j].className;
|
||||||
|
if (cn.includes("correct") && !cn.includes("incorrect"))
|
||||||
|
return items[j].innerText;
|
||||||
|
}
|
||||||
|
if (items.length == 2) {
|
||||||
|
for (var j = 0; j < items.length; j++) {
|
||||||
|
var cn = items[j].className;
|
||||||
|
if (!cn.includes("correct"))
|
||||||
|
return items[j].innerText;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
Log("error at new nodegetting, trying the oldschool way");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class MiscPageModell {
|
||||||
|
|
||||||
|
GetCurrentSubjectName() {
|
||||||
|
if (logElementGetting)
|
||||||
|
Log("getting current subjects name");
|
||||||
|
return document.getElementById("page-header").innerText.split("\n")[0];
|
||||||
|
}
|
||||||
|
|
||||||
|
GetVideo() {
|
||||||
|
if (logElementGetting)
|
||||||
|
Log("getting video stuff");
|
||||||
|
return document.getElementsByTagName("video")[0];
|
||||||
|
}
|
||||||
|
|
||||||
|
GetVideoElement() {
|
||||||
|
if (logElementGetting)
|
||||||
|
Log("getting video element");
|
||||||
|
return document.getElementById("videoElement").parentNode;
|
||||||
|
}
|
||||||
|
|
||||||
|
GetInputType(answers, i) {
|
||||||
|
if (logElementGetting)
|
||||||
|
Log("getting input type");
|
||||||
|
return answers[i].getElementsByTagName("input")[0].type;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//: }}}
|
||||||
|
|
||||||
|
var QPM = new QuestionsPageModell();
|
||||||
|
var RPM = new ResultsPageModell();
|
||||||
|
var MPM = new MiscPageModell();
|
||||||
|
|
||||||
//: Main function {{{
|
//: Main function {{{
|
||||||
function Main() {
|
function Main() {
|
||||||
'use strict';
|
'use strict';
|
||||||
|
@ -276,426 +695,6 @@ function Main() {
|
||||||
}
|
}
|
||||||
//: }}}
|
//: }}}
|
||||||
|
|
||||||
//: DOM getting stuff {{{
|
|
||||||
// all dom getting stuff are in this sections, so on
|
|
||||||
// moodle dom change, stuff breaks here
|
|
||||||
|
|
||||||
class QuestionsPageModell {
|
|
||||||
|
|
||||||
GetAllQuestionsDropdown() {
|
|
||||||
if (logElementGetting)
|
|
||||||
Log("getting dropdown question");
|
|
||||||
let items = document.getElementById("responseform").getElementsByTagName("p")[0].childNodes;
|
|
||||||
let r = "";
|
|
||||||
items.forEach((item) => {
|
|
||||||
if (item.tagName == undefined)
|
|
||||||
r += item.nodeValue;
|
|
||||||
|
|
||||||
});
|
|
||||||
return r;
|
|
||||||
}
|
|
||||||
|
|
||||||
GetAllQuestionsQtext() {
|
|
||||||
if (logElementGetting)
|
|
||||||
Log("getting all questions qtext");
|
|
||||||
return document.getElementById("responseform").getElementsByClassName("qtext"); // getting questions
|
|
||||||
}
|
|
||||||
|
|
||||||
GetAllQuestionsP() {
|
|
||||||
if (logElementGetting)
|
|
||||||
Log("getting all questions by tag p");
|
|
||||||
return document.getElementById("responseform").getElementsByTagName("p");
|
|
||||||
}
|
|
||||||
|
|
||||||
GetFormulationClearfix() {
|
|
||||||
if (logElementGetting)
|
|
||||||
Log("getting formulation clearfix lol");
|
|
||||||
return document.getElementsByClassName("formulation clearfix");
|
|
||||||
}
|
|
||||||
|
|
||||||
GetAnswerOptions() {
|
|
||||||
if (logElementGetting)
|
|
||||||
Log("getting all answer options");
|
|
||||||
return GetFormulationClearfix()[0].childNodes[3].innerText;
|
|
||||||
}
|
|
||||||
|
|
||||||
GetQuestionImages() {
|
|
||||||
if (logElementGetting)
|
|
||||||
Log("getting question images");
|
|
||||||
return GetFormulationClearfix()[0].getElementsByTagName("img");
|
|
||||||
}
|
|
||||||
|
|
||||||
// this function should return the question, posible answers, and image names
|
|
||||||
GetQuestionFromTest() {
|
|
||||||
var questions; // the important questions
|
|
||||||
var allQuestions; // all questions
|
|
||||||
try // trying to get tha current questions
|
|
||||||
{
|
|
||||||
allQuestions = this.GetAllQuestionsQtext(); // getting questions
|
|
||||||
if (allQuestions.length == 0) // if there are no found questions
|
|
||||||
{
|
|
||||||
var ddq = GetAllQuestionsDropdown();
|
|
||||||
if (EmptyOrWhiteSpace(ddq)) {
|
|
||||||
var questionData = "";
|
|
||||||
for (var j = 0; j < allQuestions.length; j++) {
|
|
||||||
allQuestions = this.GetAllQuestionsQtext()[j].childNodes;
|
|
||||||
for (var i = 0; i < allQuestions.length; i++) {
|
|
||||||
if (allQuestions[i].data != undefined && !EmptyOrWhiteSpace(allQuestions[i].data)) // if the current element has some text data to add
|
|
||||||
{
|
|
||||||
questionData += allQuestions[i].data + " "; // adding text to question data
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
questions = [questionData];
|
|
||||||
} else
|
|
||||||
questions = [ddq];
|
|
||||||
} else // if there is
|
|
||||||
{
|
|
||||||
questions = [];
|
|
||||||
for (var i = 0; i < allQuestions.length; i++) {
|
|
||||||
questions.push(allQuestions[i].innerText);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} catch (e) {
|
|
||||||
Exception(e, "script error at getting question:");
|
|
||||||
}
|
|
||||||
var imgNodes = ""; // the image nodes for questions
|
|
||||||
try {
|
|
||||||
imgNodes = GetQuestionImages(); // getting question images, if there is any
|
|
||||||
AddImageNamesToImages(imgNodes); // adding image names to images, so its easier to search for, or even guessing
|
|
||||||
} catch (e) {
|
|
||||||
Log(e);
|
|
||||||
Log("Some error with images");
|
|
||||||
}
|
|
||||||
|
|
||||||
questions = questions.map((item, ind) => {
|
|
||||||
return ReplaceCharsWithSpace(item, "\n");
|
|
||||||
});
|
|
||||||
|
|
||||||
return {
|
|
||||||
imgnodes: imgNodes,
|
|
||||||
allQ: allQuestions,
|
|
||||||
q: questions
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
class ResultsPageModell {
|
|
||||||
|
|
||||||
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 = GetFormResult(); // getting results element
|
|
||||||
var items = GetFormResult()[i].getElementsByTagName("label");
|
|
||||||
var r = [];
|
|
||||||
for (var j = 0; j < items.length; j++) {
|
|
||||||
|
|
||||||
function 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;
|
|
||||||
}
|
|
||||||
|
|
||||||
GetRightAnswerIfCorrectShown(i) {
|
|
||||||
if (logElementGetting)
|
|
||||||
Log("getting right answer if correct shown");
|
|
||||||
var results = GetFormResult(); // getting results element
|
|
||||||
return results[i].getElementsByClassName("rightanswer");
|
|
||||||
}
|
|
||||||
|
|
||||||
GetWrongAnswerIfCorrectNotShown(i) {
|
|
||||||
if (logElementGetting)
|
|
||||||
Log("getting wrong answer if correct not shown");
|
|
||||||
var results = 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 = 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;
|
|
||||||
else
|
|
||||||
return "";
|
|
||||||
}
|
|
||||||
|
|
||||||
GetFormCFOfResult(result) {
|
|
||||||
if (logElementGetting)
|
|
||||||
Log("getting formulation clearfix");
|
|
||||||
return result.getElementsByClassName("formulation clearfix")[0];
|
|
||||||
}
|
|
||||||
|
|
||||||
GetResultText(i) {
|
|
||||||
if (logElementGetting)
|
|
||||||
Log("getting result text");
|
|
||||||
var results = GetFormResult(); // getting results element
|
|
||||||
return GetFormCFOfResult(results[i]).getElementsByTagName("p");
|
|
||||||
}
|
|
||||||
|
|
||||||
GetResultImage(i) {
|
|
||||||
if (logElementGetting)
|
|
||||||
Log("getting result image");
|
|
||||||
var results = GetFormResult(); // getting results element
|
|
||||||
return GetFormCFOfResult(results[i]).getElementsByTagName("img");
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
// gets the question from the result page
|
|
||||||
// i is the index of the question
|
|
||||||
GetQuestionFromResult(i) {
|
|
||||||
var temp = GetQText(i);
|
|
||||||
var currQuestion = "";
|
|
||||||
if (temp.length > 0) // if the qtext element is not 0 length
|
|
||||||
{
|
|
||||||
currQuestion = temp[0].innerText; // adding the question to curr question as .q
|
|
||||||
} else {
|
|
||||||
// this is black magic fuckery a bit
|
|
||||||
if (GetDropboxes(i).length > 0) // if there is dropdown list in the current question
|
|
||||||
{
|
|
||||||
var allNodes = GetResultText(i);
|
|
||||||
currQuestion = "";
|
|
||||||
for (var k = 0; k < allNodes.length; k++) {
|
|
||||||
var allQuestions = GetResultText(i)[k].childNodes;
|
|
||||||
for (var j = 0; j < allQuestions.length; j++) {
|
|
||||||
if (allQuestions[j].data != undefined && !EmptyOrWhiteSpace(allQuestions[j].data)) {
|
|
||||||
currQuestion += allQuestions[j].data + " ";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
try {
|
|
||||||
currQuestion = GetCurrQuestion(i);
|
|
||||||
} catch (e) {
|
|
||||||
currQuestion = "REEEEEEEEEEEEEEEEEEEEE"; // this shouldnt really happen sry guys
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return currQuestion;
|
|
||||||
}
|
|
||||||
|
|
||||||
// tries to get right answer from result page
|
|
||||||
// i is the index of the question
|
|
||||||
GetRightAnswerFromResult(i) {
|
|
||||||
var fun = [];
|
|
||||||
|
|
||||||
// the basic type of getting answers
|
|
||||||
fun.push(function TryGet0(i) {
|
|
||||||
var temp = GetRightAnswerIfCorrectShown(i); // getting risht answer
|
|
||||||
if (temp.length > 0) // if the rightanswer element is not 0 length
|
|
||||||
return temp[0].innerText; // adding the answer to curr question as .a
|
|
||||||
});
|
|
||||||
|
|
||||||
// if there is dropdown list in the current question
|
|
||||||
fun.push(function TryGet1(i) {
|
|
||||||
if (GetDropboxes(i).length > 0)
|
|
||||||
return GetCurrentAnswer(i);
|
|
||||||
});
|
|
||||||
|
|
||||||
// if the correct answers are not shown, and the selected answer
|
|
||||||
// is correct
|
|
||||||
fun.push(function TryGet2(i) {
|
|
||||||
return GetRightAnswerIfCorrectNotShown(i);
|
|
||||||
});
|
|
||||||
|
|
||||||
// if there is dropbox in the question
|
|
||||||
fun.push(function TryGet3(i) {
|
|
||||||
return GetSelectAnswer();
|
|
||||||
});
|
|
||||||
|
|
||||||
// if the correct answers are not shown, and the selected answer
|
|
||||||
// is incorrect, and there are only 2 options
|
|
||||||
fun.push(function TryGet4(i) {
|
|
||||||
var possibleAnswers = GetPossibleAnswers(i);
|
|
||||||
if (possibleAnswers.length == 2) {
|
|
||||||
for (var k = 0; k < possibleAnswers.length; k++)
|
|
||||||
if (possibleAnswers[k].iscorrect == undefined)
|
|
||||||
return possibleAnswers[k].value;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
fun.push(function TryGetFinal(i) {
|
|
||||||
return undefined;
|
|
||||||
});
|
|
||||||
|
|
||||||
var j = 0;
|
|
||||||
var currAnswer;
|
|
||||||
while (j < fun.length && EmptyOrWhiteSpace(currAnswer)) {
|
|
||||||
currAnswer = fun[j](i);
|
|
||||||
j++;
|
|
||||||
}
|
|
||||||
|
|
||||||
return currAnswer;
|
|
||||||
}
|
|
||||||
|
|
||||||
// version 2 of getting right answer from result page
|
|
||||||
// i is the index of the question
|
|
||||||
GetRightAnswerFromResultv2(i) {
|
|
||||||
try {
|
|
||||||
var answerNodes = GetAnswerNode(i);
|
|
||||||
let items = answerNodes.nodes;
|
|
||||||
|
|
||||||
if (answerNodes.type == "checkbox")
|
|
||||||
return GetRightAnswerFromResult(i);
|
|
||||||
|
|
||||||
for (var j = 0; j < items.length; j++) {
|
|
||||||
var cn = items[j].className;
|
|
||||||
if (cn.includes("correct") && !cn.includes("incorrect"))
|
|
||||||
return items[j].innerText;
|
|
||||||
}
|
|
||||||
if (items.length == 2) {
|
|
||||||
for (var j = 0; j < items.length; j++) {
|
|
||||||
var cn = items[j].className;
|
|
||||||
if (!cn.includes("correct"))
|
|
||||||
return items[j].innerText;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} catch (e) {
|
|
||||||
Log("error at new nodegetting, trying the oldschool way");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
class MiscPageModell {
|
|
||||||
|
|
||||||
GetCurrentSubjectName() {
|
|
||||||
if (logElementGetting)
|
|
||||||
Log("getting current subjects name");
|
|
||||||
return document.getElementById("page-header").innerText.split("\n")[0];
|
|
||||||
}
|
|
||||||
|
|
||||||
GetVideo() {
|
|
||||||
if (logElementGetting)
|
|
||||||
Log("getting video stuff");
|
|
||||||
return document.getElementsByTagName("video")[0];
|
|
||||||
}
|
|
||||||
|
|
||||||
GetVideoElement() {
|
|
||||||
if (logElementGetting)
|
|
||||||
Log("getting video element");
|
|
||||||
return document.getElementById("videoElement").parentNode;
|
|
||||||
}
|
|
||||||
|
|
||||||
GetInputType(answers, i) {
|
|
||||||
if (logElementGetting)
|
|
||||||
Log("getting input type");
|
|
||||||
return answers[i].getElementsByTagName("input")[0].type;
|
|
||||||
}
|
|
||||||
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
GetQText(i) {
|
|
||||||
if (logElementGetting)
|
|
||||||
Log("getting qtext by index: " + i);
|
|
||||||
var results = GetFormResult(); // getting results element
|
|
||||||
return results[i].getElementsByClassName("qtext");
|
|
||||||
}
|
|
||||||
|
|
||||||
GetDropboxes(i) {
|
|
||||||
if (logElementGetting)
|
|
||||||
Log("getting dropboxes by index: " + i);
|
|
||||||
var results = GetFormResult(); // getting results element
|
|
||||||
return results[i].getElementsByTagName("select");
|
|
||||||
}
|
|
||||||
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
GetCurrentAnswer(i) {
|
|
||||||
if (logElementGetting)
|
|
||||||
Log("getting curr answer by index: " + i);
|
|
||||||
var results = 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];
|
|
||||||
}
|
|
||||||
|
|
||||||
GetSelectAnswer() {
|
|
||||||
if (logElementGetting)
|
|
||||||
Log("getting selected answer");
|
|
||||||
var t = document.getElementsByTagName("select");
|
|
||||||
if (t.length > 0)
|
|
||||||
return t[0].options[document.getElementsByTagName("select")[
|
|
||||||
0].selectedIndex].innerText;
|
|
||||||
}
|
|
||||||
|
|
||||||
GetAnswerNode(i) {
|
|
||||||
if (logElementGetting)
|
|
||||||
Log("getting answer node");
|
|
||||||
|
|
||||||
var results = 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 = DetermineQuestionType(ret);
|
|
||||||
|
|
||||||
return {
|
|
||||||
nodes: ret,
|
|
||||||
type: qtype
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
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;
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
//: }}}
|
|
||||||
|
|
||||||
var QPM = new QuestionsPageModell();
|
|
||||||
var RPM = new ResultsPageModell();
|
|
||||||
var MPM = new MiscPageModell();
|
|
||||||
|
|
||||||
//: Main logic stuff {{{
|
//: Main logic stuff {{{
|
||||||
|
|
||||||
//: Loading {{{
|
//: Loading {{{
|
||||||
|
@ -1279,7 +1278,7 @@ function SearchSameQuestion(questionData, quiz, i) {
|
||||||
function GetImageFormResult(i) {
|
function GetImageFormResult(i) {
|
||||||
var temp = null;
|
var temp = null;
|
||||||
try {
|
try {
|
||||||
var imgElements = GetResultImage(i); // trying to get image
|
var imgElements = RPM.GetResultImage(i); // trying to get image
|
||||||
var imgURL = []; // image urls
|
var imgURL = []; // image urls
|
||||||
for (var j = 0; j < imgElements.length; j++) {
|
for (var j = 0; j < imgElements.length; j++) {
|
||||||
if (!imgElements[j].src.includes("brokenfile")) // idk why brokenfile is in some urls, which are broken, so why tf are they there damn moodle
|
if (!imgElements[j].src.includes("brokenfile")) // idk why brokenfile is in some urls, which are broken, so why tf are they there damn moodle
|
||||||
|
@ -1331,7 +1330,7 @@ function SaveQuiz(quiz, questionData) {
|
||||||
var sentData = {};
|
var sentData = {};
|
||||||
try {
|
try {
|
||||||
try {
|
try {
|
||||||
sentData.subj = GetCurrentSubjectName();
|
sentData.subj = MPM.GetCurrentSubjectName();
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
sentData.subj = "NOSUBJ";
|
sentData.subj = "NOSUBJ";
|
||||||
Log("unable to get subject name :c");
|
Log("unable to get subject name :c");
|
||||||
|
@ -1358,19 +1357,19 @@ function SaveQuiz(quiz, questionData) {
|
||||||
function GetQuiz() {
|
function GetQuiz() {
|
||||||
try {
|
try {
|
||||||
var quiz = []; // final quiz stuff
|
var quiz = []; // final quiz stuff
|
||||||
var results = GetFormResult(); // getting results element
|
var results = RPM.GetFormResult(); // getting results element
|
||||||
for (var i = 0; i < results.length - 2; i++) // going through results, except last two, idk why, dont remember, go check it man
|
for (var i = 0; i < results.length - 2; i++) // going through results, except last two, idk why, dont remember, go check it man
|
||||||
{
|
{
|
||||||
var question = {}; // the current question
|
var question = {}; // the current question
|
||||||
// QUESTION --------------------------------------------------------------------------------------------------------------------
|
// QUESTION --------------------------------------------------------------------------------------------------------------------
|
||||||
var q = GetQuestionFromResult(i);
|
var q = RPM.GetQuestionFromResult(i);
|
||||||
if (q != undefined)
|
if (q != undefined)
|
||||||
question.q = SimplifyQuery(q);
|
question.q = SimplifyQuery(q);
|
||||||
|
|
||||||
// RIGHTANSWER ---------------------------------------------------------------------------------------------------------------------
|
// RIGHTANSWER ---------------------------------------------------------------------------------------------------------------------
|
||||||
var a = GetRightAnswerFromResultv2(i);
|
var a = RPM.GetRightAnswerFromResultv2(i);
|
||||||
if (a == undefined)
|
if (a == undefined)
|
||||||
a = GetRightAnswerFromResult(i);
|
a = RPM.GetRightAnswerFromResult(i);
|
||||||
if (a != undefined)
|
if (a != undefined)
|
||||||
question.a = SimplifyQuery(a);
|
question.a = SimplifyQuery(a);
|
||||||
// IMG ---------------------------------------------------------------------------------------------------------------------
|
// IMG ---------------------------------------------------------------------------------------------------------------------
|
||||||
|
@ -1437,7 +1436,7 @@ function AddVideoHotkeys(url) {
|
||||||
document.addEventListener("keydown", function(e) // keydown event listener
|
document.addEventListener("keydown", function(e) // keydown event listener
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
var video = GetVideo();
|
var video = MPM.GetVideo();
|
||||||
var keyCode = e.keyCode; // getting keycode
|
var keyCode = e.keyCode; // getting keycode
|
||||||
if (keyCode == 32) // if the keycode is 32 (space)
|
if (keyCode == 32) // if the keycode is 32 (space)
|
||||||
{
|
{
|
||||||
|
@ -1461,7 +1460,7 @@ function AddVideoHotkeys(url) {
|
||||||
Log(err.message);
|
Log(err.message);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
var toadd = GetVideoElement();
|
var toadd = MPM.GetVideoElement();
|
||||||
var node = CreateNodeWithText(toadd,
|
var node = CreateNodeWithText(toadd,
|
||||||
"Miután elindítottad: Play/pause: space. Seek: Bal/jobb nyíl.");
|
"Miután elindítottad: Play/pause: space. Seek: Bal/jobb nyíl.");
|
||||||
node.style.margin = "5px 5px 5px 5px"; // fancy margin
|
node.style.margin = "5px 5px 5px 5px"; // fancy margin
|
||||||
|
@ -1517,7 +1516,7 @@ function HighLightAnswer(results, currQuestionNumber) {
|
||||||
try {
|
try {
|
||||||
if (results.length > 0) // if there are items in the result
|
if (results.length > 0) // if there are items in the result
|
||||||
{
|
{
|
||||||
var answers = GetAllAnswer(currQuestionNumber); // getting all answers
|
var answers = RPM.GetAllAnswer(currQuestionNumber); // getting all answers
|
||||||
var toColor = []; // the numberth in the array will be colored, and .length items will be colored
|
var toColor = []; // the numberth in the array will be colored, and .length items will be colored
|
||||||
var type = ""; // type of the question. radio or ticbox or whatitscalled
|
var type = ""; // type of the question. radio or ticbox or whatitscalled
|
||||||
for (var i = 0; i < answers.length; i++) // going thtough answers
|
for (var i = 0; i < answers.length; i++) // going thtough answers
|
||||||
|
@ -1536,12 +1535,12 @@ function HighLightAnswer(results, currQuestionNumber) {
|
||||||
if (NormalizeSpaces(RemoveUnnecesarySpaces(correct)).includes(answer)) // if the correct answer includes the current answer
|
if (NormalizeSpaces(RemoveUnnecesarySpaces(correct)).includes(answer)) // if the correct answer includes the current answer
|
||||||
{
|
{
|
||||||
toColor.push(i); // adding the index
|
toColor.push(i); // adding the index
|
||||||
type = GetInputType(answers, i); // setting the type
|
type = MPM.GetInputType(answers, i); // setting the type
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (results[0].match == 100) // if the result is 100% correct
|
if (results[0].match == 100) // if the result is 100% correct
|
||||||
if (type !== "radio" || toColor.length == 1) // if the type is not radio or there is exactly one correct solution
|
if (type !== "radio" || toColor.length == 1) // TODO why not radio
|
||||||
for (var i = 0; i < toColor.length; i++) // going through "toColor"
|
for (var i = 0; i < toColor.length; i++) // going through "toColor"
|
||||||
answers[toColor[i]].style.backgroundColor = "#8cff66"; // and coloring the correct index
|
answers[toColor[i]].style.backgroundColor = "#8cff66"; // and coloring the correct index
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue