Unused code removal, and some function moving

This commit is contained in:
YourFriendlyNeighborhoodDealer 2018-12-13 16:12:45 +01:00
parent 8956cbade9
commit 702a5d90fa

208
main.js
View file

@ -610,27 +610,6 @@ function GetRightAnswerFromResultv2(i) {
}
}
// this should get the image url from a result page
// i is the index of the question
function GetImageFormResult(i) {
var temp = null;
try {
var imgElements = GetResultImage(i); // trying to get image
var imgURL = []; // image urls
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
{
var filePart = imgElements[j].src.split("/"); // splits the link by "/"
filePart = filePart[filePart.length - 1]; // the last one is the image name
imgURL.push(decodeURI(ShortenString(filePart, 30)));
}
}
if (imgURL.length > 0) {
temp = JSON.stringify(imgURL);
return temp;
}
} catch (e) {}
}
//-----------------------------------------------------------------------------------------------------
// Interface stuff
@ -698,19 +677,6 @@ function Init(cwith) {
return count;
}
function SearchInActiveSubjs(question, imgNodes, data) {
var result = []; // the results will be here
for (var i = 0; i < data.length; i++) // going through data
{
if (data.GetIfActive(i)) // if the current subject is marked as active
{
result = result.concat(Search(question, imgNodes, data.Subjects[i]));
}
}
return result;
}
function PrepareAnswers(result, j) {
if (result.length > 0) // if there are more than zero results
{
@ -976,27 +942,6 @@ function NLoad(resource, cwith) {
cwith(count);
}
function SearchSameQuestion(questionData, quiz, i) {
var j = 0;
for (var j = 0; j < questionData.length; j++) {
var q1 = SimplifyStringForComparison(questionData[j].q); // existing data-s question
var q2 = SimplifyStringForComparison(quiz[i].q); // new datas question
var a1 = SimplifyStringForComparison(questionData[j].a); // existing data-s answer
var a2 = SimplifyStringForComparison(quiz[i].a); // new datas answer
if (q1.includes(q2) && a2.includes(a2)) { // if the questions and the answers match
if (quiz[i].i && questionData[j].i) { // if it has image questionData
if (questionData[j].i.includes(quiz[i].i)) // if they are the same
return j;
} else {
return j;
}
}
}
return -1;
}
function ShowSaveQuizDialog(addedQ, allQ, allOutput, output, sendSuccess, sentData) {
var msg = "";
if (addedQ > 0) // if there are more than 0 new question
@ -1025,6 +970,47 @@ function ShowSaveQuizDialog(addedQ, allQ, allOutput, output, sendSuccess, sentDa
});
}
function SearchSameQuestion(questionData, quiz, i) {
var j = 0;
for (var j = 0; j < questionData.length; j++) {
var q1 = SimplifyStringForComparison(questionData[j].q); // existing data-s question
var q2 = SimplifyStringForComparison(quiz[i].q); // new datas question
var a1 = SimplifyStringForComparison(questionData[j].a); // existing data-s answer
var a2 = SimplifyStringForComparison(quiz[i].a); // new datas answer
if (q1.includes(q2) && a2.includes(a2)) { // if the questions and the answers match
if (quiz[i].i && questionData[j].i) { // if it has image questionData
if (questionData[j].i.includes(quiz[i].i)) // if they are the same
return j;
} else {
return j;
}
}
}
return -1;
}
// this should get the image url from a result page
// i is the index of the question
function GetImageFormResult(i) {
var temp = null;
try {
var imgElements = GetResultImage(i); // trying to get image
var imgURL = []; // image urls
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
{
var filePart = imgElements[j].src.split("/"); // splits the link by "/"
filePart = filePart[filePart.length - 1]; // the last one is the image name
imgURL.push(decodeURI(ShortenString(filePart, 30)));
}
}
if (imgURL.length > 0) {
temp = JSON.stringify(imgURL);
return temp;
}
} catch (e) {}
}
// saves the current quiz. questionData contains the active subjects questions
function SaveQuiz(quiz, questionData) {
try {
@ -1149,118 +1135,6 @@ function SimplifyImages(imgs) {
return questionImages;
}
function GetImageMatchCount(dataImages, questionImages) {
var includedImages = 0;
for (var j = 0; j < dataImages.length; j++) // going through the read data from the .txt
{
// maybe this loop is not needed, if we use dataImages.includes(questionImages[k]), but JSON data is not parsed, so its still string
for (var k = 0; k < questionImages.length; k++) // going through the read data from the .txt
{
if (dataImages[j].includes(questionImages[k])) {
includedImages++; // match count
}
}
}
return includedImages;
}
function AddImageResults(dataImages, results, currData, percent) {
if (dataImages.length > 1) // if there are multiple images, it adds the image names, so its easier to figure out the order. Moodle mixes questions, and without this, the help is useless.
{
var questionImagesAsText = "Képek sorban:\n" + dataImages.join(", ") + ".";
results.push({
"q": currData.q,
"a": currData.a.replace(/, /g, "\n").replace(/: /g, ":\n"),
"p": percent,
"i": questionImagesAsText
});
} else // if there is only one image, it wont add the image name, couse its trivial
{
results.push({
"q": currData.q,
"a": currData.a.replace(/, /g, "\n").replace(/: /g, ":\n"),
"p": percent
});
}
}
function ImageSearch(question, imgs, questionData, questionParts) {
var results = []; // the final results
var currData;
var questionImages = SimplifyImages(imgs); // the array for the image names in question
// searching for the image name in the current questionData.
for (var i = 0; i < questionData.length; i++) // going throug questionData
{
if (questionData[i].i) // if the questionData[i]-s image part is not null, else it does not matter, since our question is with image
{
currData = questionData[i]; // gets the current questionData from all data
var dataImages = ""; // this will be an array with string in it. the strings are image file names
try // try cus json
{
dataImages = JSON.parse(currData.i);
} catch (e) {
dataImages = [currData.i]; // if json couldnt parse the string, than it may be from a previous versions export. Also, if its corrupted, mayhaps this works.
}
var includedImages = GetImageMatchCount(dataImages, questionImages);
if (includedImages > 0) // if there is at least one match
{
var percent = GetMatchPercent(currData, questionParts); // getting question match
percent -= Math.abs(dataImages.length - includedImages) * 10; // substarcting some percent based on how many images does not match
if (percent > 10) // only adding if its more than 10%
AddImageResults(dataImages, results, currData, percent);
}
} // end if questionData[i].i
} // end for questionData.length
return results;
}
function TextSearch(question, imgs, questionData, questionParts) {
var results = [];
for (var j = 0; j < questionData.length; j++) // goin thru the questionData
{
var currData = questionData[j]; // the current question parts greatly simplified and split by " ", and i-th item
var percent = GetMatchPercent(currData, questionParts);
if (percent > 10) // only adding if its more than 10%
{
results.push({
"q": currData.q,
"a": currData.a.replace(/, /g, "\n").replace(/: /g, ":\n"),
"p": percent
}); // adds it to results
}
}
// simple sorting by results percent
return SortByPercent(results);
}
// searches for a "question", or the "imgs" if given or not null in questiondata
function Search(question, imgs, questionData) {
if (EmptyOrWhiteSpace(question)) // if the question is empty
{
throw {
message: "question is empty!"
};
}
try {
var questionParts = RemoveMultipleItems(SimplifyQuery(question).split(" ")); // the current question parts greatly simplified and split
if (imgs && imgs.length > 0) // if there is img link
return ImageSearch(question, imgs, questionData, questionParts);
else // if the question doesnt have any pictures
return TextSearch(question, imgs, questionData, questionParts);
} catch (e) {
console.log("------------------------------------------");
console.log("script error at searching:");
console.log(e.message);
console.log("------------------------------------------");
console.log(e.stack);
console.log("------------------------------------------");
}
}
//-----------------------------------------------------------------------------------------------------
// Minor logic stuff
//-----------------------------------------------------------------------------------------------------