Utente:MFH/monobook.js: differenze tra le versioni

Vai alla navigazione Vai alla ricerca
Contenuto aggiunto Contenuto cancellato
Nessun oggetto della modifica
Nessun oggetto della modifica
Riga 4: Riga 4:


import_script((wgAction=='edit' || wgAction == 'submit'), "Nonciclopedia:Script/Searchbox.js");
import_script((wgAction=='edit' || wgAction == 'submit'), "Nonciclopedia:Script/Searchbox.js");

var testGroups = new Array(); /* Associative array, every element is associated with the name of the group of the test
syntax: testGroups[group_name][question][answer]
group_name: name given with the attribute group in the test tag
question: number that identifies the question, only for internal use
answer: part of the text written between the test tags, separated with |
not properly an answer, it contains the score of every answer too*/
var testResults = new Array(); /* Associative array, contains the results of a test
syntax: testResults[group_name][number]
group_name: same as before
number: contains the profiles and the maximum score for each profile*/

function displayTest() {
var testList = new Array();
testList = document.getElementsByTagName('body')[0].innerHTML.match(/\<test(.|\r|\n)*?\<\/test\>/gm); /* Array with every test tag */
for (var i = 0; i < testList.length; i++) { /* Divides tests in groups using the parameter group */
if (testList[i].match(/group=\"(\w)*?\"/)) { /* Searches for the group */
var testGroup = testList[i].match(/group=\"(\w)*?\"/);
testGroup = testGroup.slice('"')[0];
testGroup = testGroup.substring(7,testGroup.length - 1);
}
else {
var testGroup = 'test';
}
if (testList[i].match(/type=\"(\w)*?\"/)) { /* Searches for the type */
var testType = testList[i].match(/type=\"(\w)*?\"/);
testType = testType.slice('"')[0];
testType = testType.substring(6,testType.length - 1);
}
else {
var testType = 'mct';
}
if (testGroups[testGroup]) { /* Divides the text between the test tags in parts */
testGroups[testGroup].push(testList[i].split('|'));
}
else {
testGroups[testGroup] = new Array(testList[i].split('|'));
}
var testPos = testGroups[testGroup].length - 1; /* Cleans the text, test tags are useless now */
testGroups[testGroup][testPos][0] = testGroups[testGroup][testPos][0].slice(testGroups[testGroup][testPos][0].indexOf('>') + 1);
testGroups[testGroup][testPos][testGroups[testGroup][testPos].length - 1] = testGroups[testGroup][testPos][testGroups[testGroup][testPos].length - 1].slice(0,testGroups[testGroup][testPos][testGroups[testGroup][testPos].length - 1].indexOf('</test>'));
var testStructure = '<div style="font-weight: 700;">' + testGroups[testGroup][testPos][0] + '</div><form>'; /* Creates output */
if (testType == 'sct' || testType == 'single') {
for (var j = 0; j < (testGroups[testGroup][testPos].length - 1) / 2; j++) {
testStructure = testStructure + '<input type="radio" name="' + testGroup + '" value="' + testGroups[testGroup][testPos][2 * j + 2] + '" /> ' + testGroups[testGroup][testPos][2 * j + 1] + '<br />';
}
}
else { /* For testType == 'mct' or testType == 'multi' */
for (var j = 0; j < (testGroups[testGroup][testPos].length - 1) / 2; j++) {
testStructure = testStructure + '<input type="checkbox" name="' + testGroup + '" value="' + testGroups[testGroup][testPos][2 * j + 2] + '" /> ' + testGroups[testGroup][testPos][2 * j + 1] + '<br />';
}
}
testStructure = testStructure + '</form>';
document.getElementsByTagName('body')[0].innerHTML = document.getElementsByTagName('body')[0].innerHTML.replace(document.getElementsByTagName('body')[0].innerHTML.match(/\<test(.|\r|\n)*?\<\/test\>/m)[0],testStructure);
}
}
addOnloadHook(displayTest());

function displayResult(){
var results = document.getElementsByTagName('body')[0].innerHTML.match(/\<result(.|\r|\n)*?\<\/result\>/gm);
for (var i = 0; i < results.length; i++) {
if (results[i].match(/group=\"(\w)*?\"/)) { /* Searches for the group of the button */
var resultGroup = results[i].match(/group=\"(\w)*?\"/);
resultGroup = resultGroup.slice('"')[0];
resultGroup = resultGroup.substring(7,resultGroup.length - 1);
}
else {
var resultGroup = 'test';
}
if (results[i].split('|').length > 1) { /* Divides the text between the result tags, if exists */
testResults[resultGroup] = results[i].split('|');
var resultPos = testResults[resultGroup].length - 1; /* Cleans the text, result tags are useless now */
testResults[resultGroup][0] = testResults[resultGroup][0].slice(testResults[resultGroup][0].indexOf('>') + 1);
testResults[resultGroup][resultPos] = testResults[resultGroup][resultPos].slice(0,testResults[resultGroup][resultPos].indexOf('</result>'));
}
var resultStructure = '<input type="submit" name="' + resultGroup + '" value="Risultato" onclick="javascript:calculateResult(' + "'" + resultGroup + "'" + ');" /><br /><div id="' + resultGroup + '"></div>'; /* Creates structure */
document.getElementsByTagName('body')[0].innerHTML = document.getElementsByTagName('body')[0].innerHTML.replace(document.getElementsByTagName('body')[0].innerHTML.match(/\<result(.|\r|\n)*?\<\/result\>/m)[0],resultStructure);
}

}
addOnloadHook(displayResult());

function calculateResult(group) {
if (testGroups[group]) {
var inputButtons = document.getElementsByTagName('input');
var resultScore = 0;
for (var i = 0; i < inputButtons.length; i++) {
if ((inputButtons[i].type == 'radio' || inputButtons[i].type == 'checkbox') && inputButtons[i].name == group && inputButtons[i].checked) {
resultScore = resultScore + parseInt(inputButtons[i].value);
}
}
if (testResults[group]) {
var resultProfile = '';
if (resultScore <= parseInt(testResults[group][2])) {
resultProfile = testResults[group][1];
}
else if (testResults[group].length >= 6) {
for(var j = 1; j < testResults[group].length / 2 - 1; j++) {
if (resultScore > parseInt(testResults[group][2 * j]) && resultScore <= parseInt(testResults[group][2 * j + 2])) {
resultProfile = testResults[group][2 * j + 1];
}
}
if (!resultProfile) {
resultProfile = testResults[group][testResults[group].length - 1];
}
}
var resultResponse = testResults[group][0] + '<br />Hai ottenuto ' + resultScore + ' punti.<br />' + resultProfile;
}
else {
var resultResponse = 'Hai ottenuto ' + resultScore + ' punti.';
}
document.getElementById(group).innerHTML = resultResponse;
}
else {
document.getElementById(group).innerHTML = 'Errore! Gruppo non riconosciuto. Ricontrollare il codice per favore.';
}
}