summaryrefslogtreecommitdiff
path: root/moodle-scraper.js
blob: ec5b5e845030f4c62dd7360fa614d4c922703db4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/*
 * This script scrapes the questions in a moodle questionary
 * and saves them into a local txt file
 */ 

// Downloadable file function
function download(filename, text)
{
	let element = document.createElement('a');
	element.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(text));
	element.setAttribute('download', filename);
	element.style.display = 'none';
	document.body.appendChild(element);
	element.click();
	document.body.removeChild(element);
}

// Declaring Vars
let questions, output = "", exposed = false, exposedAnswers = [];

// Checking for exposed answers and storing them in a array;
if ( document.querySelectorAll('[class=rightanswer]').length > 0 )
{
	exposed = true;
	exposedAnswers = document.querySelectorAll('[class=rightanswer]');
}

// Grabbing All the Questions
questions = document.querySelectorAll('[id^=question]');

// Iterating Questions
for (let i = 0; i < questions.length; i++)
{
	let answer = "Question " + (i + 1) + " : " + questions[i].getElementsByClassName("qtext")[0].innerText + "\n\n";
	//Grabing Question Type
	switch (questions[i].classList[1])
	{
		// True/False Type Questions
		case "truefalse":
			if ( exposed )
			{
				answer += exposedAnswers[i].innerText.replace(/(\r\n|\n|\r)/gm,"") + "\n";
			}
			else
			{
				answer += "Answer:\n"
				// If answer is correct grabs the correct one
				if ( questions[i].classList.contains("correct") )
				{
					answer += questions[i].getElementsByClassName("answer")[0].getElementsByClassName("correct")[0].getElementsByTagName("label")[0].textContent + "\n";
				}
				// Else grabs the opposite one
				else
				{
					if ( questions[i].querySelectorAll('[class=answer]')[0].innerHTML.includes("fa-check") )
					{
						if ( questions[i].querySelectorAll('[class=answer]')[0].childNodes[0].classList.contains("incorrect") )
						{
							answer += questions[i].querySelectorAll('[class=answer]')[0].childNodes[1].innerText + "\n";
						}
						else
						{
							answer += questions[i].querySelectorAll('[class=answer]')[0].childNodes[0].innerText + "\n";
						}
					}
					else
					{
						// In case the question ain't verified
						if ( questions[i].querySelectorAll('[class=answer]')[0].childNodes[0].childNodes[0].checked )
						{
							answer += questions[i].querySelectorAll('[class=answer]')[0].childNodes[0].innerText + " -> NOT CHECKED :( !!!\n";
						}
						else
						{
							answer += questions[i].querySelectorAll('[class=answer]')[0].childNodes[1].innerText + " -> NOT CHECKED :( !!!\n";
						}
					}
				}
			}
			break;

		// Selecting the correct options inside of every field
		case "match":
			if ( exposed )
			{
				answer += exposedAnswers[i].innerText.replace(/(\r\n|\n\n|\r)/gm,"\n") + "\n";
			}
			else
			{
				answer += "Answer:\n"
				for ( let j = 0; j < questions[i].getElementsByClassName("answer")[0].getElementsByTagName("tr").length; j++ )
				{

					answer += questions[i].getElementsByClassName("answer")[0].getElementsByTagName("tr")[j].getElementsByClassName("text")[0].innerText.replace(/(\r\n|\n|\r)/gm,"") + " -> "
						+ questions[i].getElementsByClassName("answer")[0].getElementsByTagName("tr")[j].getElementsByClassName("custom-select")[0].selectedOptions[0].innerText;

					if ( questions[i].getElementsByClassName("answer")[0].getElementsByTagName("tr")[j].getElementsByClassName("icon").length > 0 )
					{
						answer += " = " + questions[i].getElementsByClassName("answer")[0].getElementsByTagName("tr")[j].getElementsByClassName("icon")[0].title + "\n";
					}
					else
					{
						// In case the question ain't verified
						answer += " -> NOT CHECKED :( !!!\n"
					}
				}
				answer += "\n"
			}
			break;

		// Multi Choice Questions
		case "multichoice":
			if ( exposed )
			{
				answer += exposedAnswers[i].innerText.replace(/(\r\n|\n\n|\r)/gm,"\n") + "\n";
			}
			else
			{
				answer += "Answer:\n"
				if ( questions[i].getElementsByClassName("answer")[0].querySelectorAll('[class$=correct]').length > 0 )
				{
					for ( let j = 0; j < questions[i].getElementsByClassName("answer")[0].querySelectorAll('[class$=correct]').length; j++ )
					{
						answer += questions[i].getElementsByClassName("answer")[0].querySelectorAll('[class$=correct]')[j].getElementsByTagName('label')[0].innerText.replace(/(\r\n|\n|\r)/gm,"") + " -> "
							+ questions[i].getElementsByClassName("answer")[0].querySelector('[class$=correct]').getElementsByClassName("icon")[0].title + "\n"
					}
				}
				else
				{
					// In case the question ain't verified
					for ( let j = 0; j < questions[i].getElementsByClassName("answer")[0].childNodes.length; j += 2 )
					{
						if ( questions[i].getElementsByClassName("answer")[0].childNodes[j].innerHTML.includes("checked=") )
						{
							answer += questions[i].getElementsByClassName("answer")[0].childNodes[j].innerText.replace(/(\r\n|\n|\r)/gm,"") + " -> NOT CHECKED :( !!!\n"
						}
					}
				}
				answer += "\n"
			}
	}
	output += answer + "\n-----------------------------------------------\n\n";
}

download(document.title + ".txt", output);