mirror of
https://gitlab.com/MrFry/moodle-test-userscript
synced 2025-04-01 20:22:48 +02:00
Basic question answering with new data structure
This commit is contained in:
parent
b3389c2b46
commit
38185a3741
1 changed files with 95 additions and 38 deletions
121
main.js
121
main.js
|
@ -26,7 +26,7 @@ var serverAdress = "http://questionmining.tk/";
|
|||
|
||||
// forcing pages for testing. unless you test, do not set these to true!
|
||||
// only one of these should be true for testing
|
||||
const forceTestPage = false;
|
||||
const forceTestPage = true; // TODO
|
||||
const forceResultPage = false;
|
||||
const forceDefaultPage = false;
|
||||
const logElementGetting = false;
|
||||
|
@ -34,6 +34,7 @@ const logElementGetting = false;
|
|||
var motdShowCount = 3;
|
||||
var motd = "";
|
||||
|
||||
var minMatchAmmount = 50; // TODO
|
||||
// ---------------------------------------------------------------------------------------------
|
||||
// Class descriptions
|
||||
// ---------------------------------------------------------------------------------------------
|
||||
|
@ -63,6 +64,9 @@ class Question {
|
|||
return this.HasQuestion() && this.HasAnswer();
|
||||
}
|
||||
Compare(q2) {
|
||||
if (typeof q2 == 'string') {
|
||||
return Question.CompareString(this.Q, q2);
|
||||
} else {
|
||||
const qmatchpercent = Question.CompareString(this.Q, q2.Q);
|
||||
const amatchpercent = Question.CompareString(this.A, q2.A);
|
||||
if (this.I != undefined) {
|
||||
|
@ -72,6 +76,7 @@ class Question {
|
|||
return (qmatchpercent + amatchpercent) / 2;
|
||||
}
|
||||
}
|
||||
}
|
||||
static CompareString(s1, s2) {
|
||||
//if (s1 == undefined || s2 == undefined)
|
||||
// return 0;
|
||||
|
@ -101,6 +106,27 @@ class Subject {
|
|||
AddQuestion(q) {
|
||||
this.Questions.push(q);
|
||||
}
|
||||
Search(q) {
|
||||
var r = [];
|
||||
for (var i = 0; i < this.length; i++) {
|
||||
let percent = this.Questions[i].Compare(q);
|
||||
if (percent > minMatchAmmount)
|
||||
r.push({
|
||||
q: this.Questions[i],
|
||||
match: percent
|
||||
});
|
||||
}
|
||||
|
||||
for (var i = 0; i < r.length; i++)
|
||||
for (var j = i; j < r.length; j++)
|
||||
if (r[i].match < r[j].match) {
|
||||
var tmp = r[i];
|
||||
r[i] = r[j];
|
||||
r[j] = tmp;
|
||||
}
|
||||
|
||||
return r;
|
||||
}
|
||||
toString() {
|
||||
var r = [];
|
||||
for (var i = 0; i < this.Questions.length; i++)
|
||||
|
@ -116,6 +142,21 @@ class QuestionDB {
|
|||
get length() {
|
||||
return this.Subjects.length;
|
||||
}
|
||||
get activeIndexes() {
|
||||
var r = [];
|
||||
for (var i = 0; i < this.length; i++) {
|
||||
if (GM_getValue("Is" + i + "Active")) {
|
||||
r.push(i);
|
||||
}
|
||||
}
|
||||
return r;
|
||||
}
|
||||
GetIfActive(ind) {
|
||||
return GM_getValue("Is" + ind + "Active");
|
||||
}
|
||||
ChangeActive(i, value) {
|
||||
GM_setValue("Is" + i + "Active", !!value);
|
||||
}
|
||||
AddQuestion(subj, q) {
|
||||
var i = 0;
|
||||
while (i < this.Subjects.length && this.Subjects[i].Name != subj)
|
||||
|
@ -128,6 +169,22 @@ class QuestionDB {
|
|||
this.Subjects.push(n);
|
||||
}
|
||||
}
|
||||
Search(q) {
|
||||
// TODO: image
|
||||
var r = [];
|
||||
for (var i = 0; i < this.length; i++)
|
||||
r = r.concat(this.Subjects[i].Search(q));
|
||||
|
||||
for (var i = 0; i < r.length; i++)
|
||||
for (var j = i; j < r.length; j++)
|
||||
if (r[i].match < r[j].match) {
|
||||
var tmp = r[i];
|
||||
r[i] = r[j];
|
||||
r[j] = tmp;
|
||||
}
|
||||
|
||||
return r;
|
||||
}
|
||||
AddSubject(subj) {
|
||||
var i = 0;
|
||||
while (i < this.length && subj.Name != this.Subjects[i].Name)
|
||||
|
@ -152,6 +209,7 @@ class QuestionDB {
|
|||
// ---------------------------------------------------------------------------------------------
|
||||
function Main() {
|
||||
'use strict';
|
||||
console.clear();
|
||||
Init(function(count) {
|
||||
var url = location.href;
|
||||
try {
|
||||
|
@ -628,9 +686,9 @@ function SearchInActiveSubjs(question, imgNodes, data) {
|
|||
var result = []; // the results will be here
|
||||
for (var i = 0; i < data.length; i++) // going through data
|
||||
{
|
||||
if (GM_getValue("Is" + i + "Active")) // if the current subject is marked as active
|
||||
if (data.GetIfActive(i)) // if the current subject is marked as active
|
||||
{
|
||||
result = result.concat(Search(question, imgNodes, data[i].questions));
|
||||
result = result.concat(Search(question, imgNodes, data.Subjects[i]));
|
||||
}
|
||||
}
|
||||
return result;
|
||||
|
@ -683,9 +741,11 @@ function HandleQuiz(url, count) {
|
|||
var answers = [];
|
||||
for (var j = 0; j < questions.length; j++) // going thru all answers
|
||||
{
|
||||
// TODO: images, and other stuffs
|
||||
var question = RemoveUnnecesarySpaces(questions[j]); // simplifying question
|
||||
var result = SearchInActiveSubjs(question, imgNodes, data);
|
||||
result = SortByPercent(result); // sorting result by percent
|
||||
console.log(question);
|
||||
console.log(SimplifyImages(imgNodes));
|
||||
var result = data.Search(question);
|
||||
PrepareAnswers(result, answers, j);
|
||||
}
|
||||
ShowAnswers(answers);
|
||||
|
@ -713,25 +773,21 @@ function HandleUI(url, count) {
|
|||
{
|
||||
greetMsg = "Moodle/Elearning/KMOOC segéd v. " + GM_info.script.version + ". " + count +
|
||||
" kérdés és " + data.length + " tárgy betöltve. (click for help).";
|
||||
if (data.length > 0 && data[0].name !== "NONAME") // if there is data, and its not named NONAME
|
||||
console.log(data);
|
||||
if (data.length > 0)
|
||||
{
|
||||
var toAdd = []; // the subjects that will be displayed
|
||||
for (var i = 0; i < data.length; i++) // going thru data
|
||||
{
|
||||
if (GM_getValue("Is" + i + "Active")) // if the i. item is active
|
||||
{
|
||||
toAdd.push(data[i].name + " (" + data[i].count + ")"); // adds subject name and count to displayed items
|
||||
var toAdd = [];
|
||||
for (var i = 0; i < data.length; i++) {
|
||||
if (data.GetIfActive(i)) {
|
||||
toAdd.push(data.Subjects[i].Name + " (" + data.Subjects[i].length + ")");
|
||||
}
|
||||
}
|
||||
if (toAdd.length != 0) // if there are active items
|
||||
{
|
||||
if (toAdd.length != 0) {
|
||||
greetMsg += "\nAktív tárgyak: " + toAdd.join(", ") + ".";
|
||||
} else // if there are no active items selected
|
||||
{
|
||||
} else {
|
||||
greetMsg += "\nNincs aktív tárgyad. Menüből válassz ki eggyet!";
|
||||
}
|
||||
} else // if there are no subje loaded or it has no name
|
||||
{
|
||||
} else {
|
||||
greetMsg += " Az adatfájlban nem adtál meg nevet. Vagy nem elérhető a szerver. Katt a helpért!";
|
||||
// TODO if online mode print another error!
|
||||
}
|
||||
|
@ -752,6 +808,7 @@ function HandleUI(url, count) {
|
|||
"-re. Changelog:\n" + lastChangeLog;
|
||||
GM_setValue("lastVerson", GM_info.script.version); // setting lastVersion
|
||||
}
|
||||
console.log("MOTD " + motd);
|
||||
if (!EmptyOrWhiteSpace(motd)) {
|
||||
|
||||
var prevmotd = GM_getValue("motd");
|
||||
|
@ -784,9 +841,10 @@ function HandleUI(url, count) {
|
|||
|
||||
function HandleResults(url) {
|
||||
var allResults = [];
|
||||
// TODO fix this
|
||||
if (data)
|
||||
for (var i = 0; i < data.length; i++) // going trough all subjects
|
||||
if (GM_getValue("Is" + i + "Active")) // if the subject is active
|
||||
if (data.GetIfActive(i)) // if the subject is active
|
||||
allResults = allResults.concat(data[i].questions); // searching for the answer, with the given question and image (if there is)
|
||||
SaveQuiz(GetQuiz(), allResults); // saves the quiz questions and answers
|
||||
}
|
||||
|
@ -828,7 +886,6 @@ function ReadNetDB(cwith, useNetDB) {
|
|||
synchronous: true,
|
||||
url: url,
|
||||
onload: function(response) {
|
||||
// console.log(response.responseText);
|
||||
NLoad(response.responseText, cwith);
|
||||
},
|
||||
onerror: function() {
|
||||
|
@ -859,7 +916,7 @@ function Load(cwith) {
|
|||
|
||||
function LoadMOTD(resource) {
|
||||
try {
|
||||
motd = resource.substr;
|
||||
motd = resource.motd;
|
||||
} catch (e) {
|
||||
console.log("Error loading motd :c");
|
||||
console.log(e);
|
||||
|
@ -871,21 +928,21 @@ function LoadMOTD(resource) {
|
|||
function NLoad(resource, cwith) {
|
||||
var count = -1;
|
||||
try {
|
||||
var data = JSON.parse(resource);
|
||||
var d = JSON.parse(resource);
|
||||
var r = new QuestionDB();
|
||||
var rt = [];
|
||||
var allCount = -1;
|
||||
LoadMOTD(resource);
|
||||
LoadMOTD(d);
|
||||
|
||||
for (var i = 0; i < data.Subjects.length; i++) {
|
||||
let s = new Subject(data.Subjects[i].Name);
|
||||
for (var i = 0; i < d.Subjects.length; i++) {
|
||||
let s = new Subject(d.Subjects[i].Name);
|
||||
var j = 0;
|
||||
for (j = 0; j < data.Subjects[i].Questions.length; j++) {
|
||||
var currQ = data.Subjects[i].Questions[j];
|
||||
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));
|
||||
}
|
||||
rt.push({
|
||||
name: data.Subjects[i].Name,
|
||||
name: d.Subjects[i].Name,
|
||||
count: j
|
||||
});
|
||||
allCount += j;
|
||||
|
@ -893,7 +950,7 @@ function NLoad(resource, cwith) {
|
|||
}
|
||||
console.log(r);
|
||||
console.log(rt);
|
||||
|
||||
data = r;
|
||||
count = allCount + 1; // couse starting with -1 to show errors
|
||||
} catch (e) {
|
||||
console.log("------------------------------------------");
|
||||
|
@ -1649,7 +1706,7 @@ function ShowMenuList() {
|
|||
var subjRow = tbl.insertRow();
|
||||
|
||||
var td = subjRow.insertCell();
|
||||
var textBox = CreateNodeWithText(td, data[i].name + " (" + data[i].count + ")");
|
||||
var textBox = CreateNodeWithText(td, data.Subjects[i].Name + " (" + data.Subjects[i].length + ")");
|
||||
textBox.style.margin = fiveMargin; // fancy margin
|
||||
|
||||
td = subjRow.insertCell();
|
||||
|
@ -1659,14 +1716,14 @@ function ShowMenuList() {
|
|||
checkbox.style.margin = "5px 5px 5px 5px"; // fancy margin
|
||||
td.appendChild(checkbox); // adding text box to main td
|
||||
|
||||
var active = GM_getValue("Is" + i + "Active");
|
||||
var active = data.GetIfActive(i);
|
||||
checkbox.checked = active;
|
||||
|
||||
checkbox.setAttribute("id", "HelperTextNode" + i);
|
||||
|
||||
checkbox.addEventListener("click", function() {
|
||||
var checked = document.getElementById("HelperTextNode" + i).checked;
|
||||
GM_setValue("Is" + i + "Active", checked);
|
||||
data.ChangeActive(i, checked);
|
||||
}); // adding click
|
||||
}
|
||||
} else // if no data
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue