-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNameList_2_Moodle_Activity.js
179 lines (171 loc) · 6.17 KB
/
NameList_2_Moodle_Activity.js
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
//Currently for pass/fail graded Moodle activities
//Choose "Näytä kaikki palautukset" or "View all submissions"
//Activate Javascript console and copy paste the following (remember to allow popup windows)
//Insert namelist to popup window to mark everyone accepted. You need to press "Tallenna kaikki nopean arvioinnin muutokset"/"Save all quick grading changes"-button finally.
/**
* Attempts to fix encoding mistakes when text comes from random sources and may contain botched characters
* @param {string} text - the text to be fixed
* @returns {string} - fixed text
*/
function fix_encoding(text) {
//Non-exhaustive list of replacements, generated with AI and manually added some
const replacements = {
"ä": "ä",
"Ã…": "Å",
"Ä": "Ä",
"Ö": "Ö",
"ü": "ü",
"Ãœ": "Ü",
"ö": "ö",
"ß": "ß",
"á": "á",
"é": "é",
"Ã": "í",
"ó": "ó",
"ú": "ú",
"ñ": "ñ",
"À": "À",
"Â": "Â",
"Ã": "Ã",
"Ç": "Ç",
"È": "È",
"É": "É",
"Ê": "Ê",
"Ë": "Ë",
"ÃŒ": "Ì",
"Ã": "Í",
"ÃŽ": "Î",
"Ã": "Ï",
"Ã": "Ð",
"Ñ": "Ñ",
"Ã’": "Ò",
"Ó": "Ó",
"Ô": "Ô",
"Õ": "Õ",
"Ø": "Ø",
"Ú": "Ú",
"Û": "Û",
"Ãœ": "Ü",
"Þ": "Þ",
"Þ": "þ",
"ß": "ß",
"Ã ": "à",
"á": "á",
"â": "â",
"ã": "ã",
"ä": "ä",
"Ã¥": "å",
"æ": "æ",
"ç": "ç",
"è": "è",
"é": "é",
"ê": "ê",
"ë": "ë",
"ì": "ì",
"Ã": "í",
"î": "î",
"ï": "ï",
"ð": "ð",
"ñ": "ñ",
"ò": "ò",
"ó": "ó",
"ô": "ô",
"õ": "õ",
"ö": "ö",
"ø": "ø",
"ù": "ù",
"ú": "ú",
"û": "û",
"ý": "ý",
"þ": "þ",
"ÿ": "ÿ",
"Ã\u0084": "Ä",
"Ã": "Ö",
"Ã": "Å",
};
//Replace and return
return text.replace(/Ã./g, match => replacements[match] || match);
}
/**
* Normalise text for matching purposes
* @param {string} text - the text to be normalised
* @returns {string} - normalised text
*/
function remove_special_characters(input) {return input.normalize('NFD').replace(/[\u0300-\u036f]/g, ''); }
/**
* Function for handling a name list of students and marking those students as accepted in the Moodle quick grading
* @param {string} namelist - the name list
*/
function approveStudents(namelist)
{
// Fix characters
namelist = fix_encoding(namelist);
// Remove whitespaces, commas and tabulators, split by newline and map to nimi1 nimi2 pairs for handling:
namelist.replace(/[ \t,;]+/g, ' ').trim().split('\n').map(line => { const [nimi1, nimi2] = line.split(' '); return { nimi1, nimi2 };}).forEach(({ nimi1, nimi2 }) => {
// We don't care if it firstname lastname or vice versa. Let's make comparison constants:
const nimi_1 = remove_special_characters(`${nimi1} ${nimi2}`.toLowerCase());
const nimi_2 = remove_special_characters(`${nimi2} ${nimi1}`.toLowerCase());
//Set up the display name:
var nimi = `${nimi1} ${nimi2}`;
// Find the right <a> tag:
var anchor = Array.from(document.querySelectorAll('a')).find(a => remove_special_characters(a.textContent.toLowerCase()).includes(nimi_1))
if (!anchor) {
//Try different name order
anchor = Array.from(document.querySelectorAll('a')).find(a => remove_special_characters(a.textContent.toLowerCase()).includes(nimi_2));
nimi = `${nimi2} ${nimi1}`;
}
if (anchor) {
// Find the parent TR
const emoTR = anchor.closest('tr');
if (emoTR) {
// Find the TD with class "c7"
const arviointiTD = emoTR.querySelector('td.c7');
if (arviointiTD) {
// Find the select
const select = arviointiTD.querySelector('select');
if (select) {
// Find the <option> that says "hyväksytty"
const hyvOption = Array.from(select.options).find(option => option.text === "Hyväksytty");
if (hyvOption) {
// If accepted already, no action
if (select.value == hyvOption.value) {console.log(`${nimi} on jo ok`);}
else {
//Otherwise approve
select.value = hyvOption.value;
console.log(`%cAsetettu ${nimi} hyväksytyksi!`, 'color: blue; font-size: 12px; font-weight: bold');
}
} else {
console.warn(`Oikeaa optiota ei löytynyt nimelle ${nimi}.`);
}
} else {
console.warn(`TD:n sisältä ei löytynyt <select>-tagia nimelle ${nimi}.`);
}
} else {
console.warn(`c7-luokalla varustettua <td>-tagia ei löytynyt nimelle ${nimi}.`);
}
} else {
console.warn(`Nimelle ${nimi} ei löytynyt TR-tagia ylempää DOM:sta?`);
}
} else {
console.warn(`Nimeä "${nimi}" ei löytynyt sivulta`);
}
});
}
// Create a window for user input
var nimilistaIkkuna = window.open('','_blank','width=600,height=500,top=100,left=100');
//New window HTML content
nimilistaIkkuna.document.write(`
<html>
<body>
<h3>Syötä opiskelijoiden nimet, yksi per rivi:</h3>
<textarea id="nimilista" rows="10" cols="40" style="width: 90%;"></textarea>
<br>
<button id="opiskelijatOk">Lähetä!</button>
<script>
document.getElementById('opiskelijatOk').onclick = function () {
window.opener.approveStudents(document.getElementById('nimilista').value);
};
</script>
</body>
</html>
`);