<commit_before>const dbRefCourses = firebase.database().ref().child('Courses/');
const btnLogout = document.getElementById('btnLogout');
const courseHeader = document.getElementById('courseId');
const answerList = document.getElementById('answerList');
const questionP = document.getElementById('question');
const questionary = document.getElementById('questionary');
const info = document.getElementById('info');
const dbRefPoints = firebase.database().ref().child('Courses/');

var total;

//Log out the user
btnLogout.addEventListener('click', e => {
    firebase.auth().signOut();
    document.location.href = '../index.html?<?php echo time(); ?';

});


//Gets id from url, id is the course clicked
function getUrlVars() {
    var vars = {};
    var parts = window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi,
                function(m,key,value) {
        vars[key] = value;
    });
    return vars;
}


var value = getUrlVars()['id'];
var levelid = getUrlVars()['level'];
courseHeader.innerText = value;

//Create list of keys, in order to randomize questions
var list = new Array();


function fireQuestionary() {
    dbRefCourses.orderByKey().equalTo(value).on("child_added", snap => {
        for(var key in snap.val().questions){
            if(levelid != "random") {
                dbRefPoints.child(value + "/questions/" + key).once('value', snap => {
                    if (snap.hasChild("levelData")) {
                        if(snap.val().levelData.level == parseInt(levelid)) {
                            list.push(key);
                        }
                    } else {
                        if(parseInt(levelid) == 1) {
                            list.push(key);
                        }
                    }
                });
            } else {
                list.push(key);
            }
        }
        if(list.length > 10) {
            total = 10;
            questions();
        } else if(list.length == 0) {
            const h2 = document.createElement('h2');
            h2.innerText = "There are no questions on this level yet";
            h2.style.marginLeft = "10%";
            question.appendChild(h2);
        } else {
            total = list.length;
            questions();
        }
    });
}


//Clears Answers list of li elements/answers
function clearList() {
    if (answerList) {
        while (answerList.firstChild) {
            answerList.removeChild(answerList.firstChild);
        }
        info.removeChild(info.firstChild);
    }
}
var correct;
var currentKey;


//When next is clicked and document loads, it prints out questions and answers
function checkIfThereAreQuestions() {
    dbRefCourses.child(value).once('value', snap => {
        if(snap.hasChild('questions')) {
            fireQuestionary();

        } else {
            const h2 = document.createElement('h2');
            h2.innerText = "This course has no tests";
            h2.style.marginLeft = "25%";
            question.appendChild(h2);
        }
    });

}

function questions() {
    dbRefCourses.orderByKey().equalTo(value).on("child_added", snap => {
        clearList();
        var max = list.length;
        var random = Math.floor(Math.random()* max);
        var question = snap.val().questions[list[random]].question;
        var answers = snap.val().questions[list[random]].answers;
        correct = snap.val().questions[list[random]].correct;
        currentKey = list[random];
        var start ="";
        if(correct.constructor === Array) {
             start = "<input type='checkbox' name='checkAnswer' ";
        }
        else {
            start= "<input type='radio' name='groupAnswer' ";
        }
        $('#question').text(question);
        for (var key in answers) {
            $('#answerList').append("<li id='" + key + "'>" + start + "value=" + key + "> " +
                                                answers[key] + "</li>");
        }
        $('#info').append('<p> Number of answered questions: ' + answeredQuestions + '/' + total + ' </p>');
    });
}
//Checks whether a radio button is checked and returns the value;
function checkAnswerRadio() {
    return $('input[name=groupAnswer]:checked', '#answerList').val();
}

//Returns a list of the values of the checked checkboxes
function checkAnswerCheckbox() {
    var answeredValues = new Array();
    var checkboxes = document.getElementsByName("checkAnswer");
    for(var i = 0; i < checkboxes.length; i++) {
        if (checkboxes[i].checked) {
            answeredValues.push(checkboxes[i].value);
        }
    }
    return answeredValues;
}

//Checks whether anyy of the checkboxes are checked
function booleanChecked() {
    var checked = false;
    $('input[name="checkAnswer"]').each(function() {
        if ($(this).is(":checked")) {
            checked = true;
        }
    });
    return checked;
}
//Clears the div element after the test is finished.
function clearDiv() {
    if (questionary) {
        while (questionary.firstChild) {
            questionary.removeChild(questionary.firstChild);
        }
    }
}

//Writes to the questionray div the results from the test.
function returnResults() {
    const div = document.createElement('div');
    const h1 = document.createElement('h1');
    const pPoints = document.createElement('p');
    const pAnswered = document.createElement('p');
    const pPercentage = document.createElement('p');
    div.className = 'col-md-12';
    h1.innerText = 'Results';
    div.style.paddingTop = '3%';
    pPoints.innerText = 'Number of points: ' + points;
    pAnswered.innerText = 'Number of questions answered: ' + answeredQuestions;
    pPercentage.innerText = 'Percentage of right questions ' + round((points/answeredQuestions)*100, 0) + '%';

    questionary.appendChild(div);
    div.appendChild(h1);
    div.appendChild(pPoints);
    div.appendChild(pAnswered);
    div.appendChild(pPercentage);
}

var counter = 0;<commit_msg>Graphs on result site<commit_after>const dbRefCourses = firebase.database().ref().child('Courses/');
const btnLogout = document.getElementById('btnLogout');
const courseHeader = document.getElementById('courseId');
const answerList = document.getElementById('answerList');
const questionP = document.getElementById('question');
const questionary = document.getElementById('questionary');
const info = document.getElementById('info');
const dbRefPoints = firebase.database().ref().child('Courses/');

var total;

//Log out the user
btnLogout.addEventListener('click', e => {
    firebase.auth().signOut();
    document.location.href = '../index.html?<?php echo time(); ?';

});


//Gets id from url, id is the course clicked
function getUrlVars() {
    var vars = {};
    var parts = window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi,
                function(m,key,value) {
        vars[key] = value;
    });
    return vars;
}


var value = getUrlVars()['id'];
var levelid = getUrlVars()['level'];
courseHeader.innerText = value;

//Create list of keys, in order to randomize questions
var list = new Array();

//Checks wheter there are registered any questions at all and checks wheter there are any questions on a certain level.
function fireQuestionary() {
    dbRefCourses.orderByKey().equalTo(value).on("child_added", snap => {
        for(var key in snap.val().questions){
            if(levelid != "random") {
                dbRefPoints.child(value + "/questions/" + key).once('value', snap => {
                    if (snap.hasChild("levelData")) {
                        if(snap.val().levelData.level == parseInt(levelid)) {
                            list.push(key);
                        }
                    } else {
                        if(parseInt(levelid) == 1) {
                            list.push(key);
                        }
                    }
                });
            } else {
                list.push(key);
            }
        }
        if(list.length > 10) {
            total = 2;
            questions();
        } else if(list.length == 0) {
            const h2 = document.createElement('h2');
            h2.innerText = "There are no questions on this level yet";
            h2.style.marginLeft = "10%";
            question.appendChild(h2);
        } else {
            total = list.length;
            questions();
        }
    });
}


//Clears Answers list of li elements/answers
function clearList() {
    if (answerList) {
        while (answerList.firstChild) {
            answerList.removeChild(answerList.firstChild);
        }
        info.removeChild(info.firstChild);
    }
}

var correct;
var currentKey;


//When next is clicked and document loads, it prints out questions and answers
function checkIfThereAreQuestions() {
    dbRefCourses.child(value).once('value', snap => {
        if(snap.hasChild('questions')) {
            fireQuestionary();

        } else {
            const h2 = document.createElement('h2');
            h2.innerText = "This course has no tests";
            h2.style.marginLeft = "25%";
            question.appendChild(h2);
        }
    });

}

//Fetches questions, answers and correct values and writes the first two to the DOM.
function questions() {
    dbRefCourses.orderByKey().equalTo(value).on("child_added", snap => {
        clearList();
        var max = list.length;
        var random = Math.floor(Math.random()* max);
        var question = snap.val().questions[list[random]].question;
        var answers = snap.val().questions[list[random]].answers;
        correct = snap.val().questions[list[random]].correct;
        currentKey = list[random];
        var start ="";
        if(correct.constructor === Array) {
             start = "<input type='checkbox' name='checkAnswer' ";
        }
        else {
            start= "<input type='radio' name='groupAnswer' ";
        }
        $('#question').text(question);
        for (var key in answers) {
            $('#answerList').append("<li id='" + key + "'>" + start + "value=" + key + "> " +
                                                answers[key] + "</li>");
        }
        $('#info').append('<p> Number of answered questions: ' + answeredQuestions + '/' + total + ' </p>');
    });
}
//Checks whether a radio button is checked and returns the value;
function checkAnswerRadio() {
    return $('input[name=groupAnswer]:checked', '#answerList').val();
}

//Returns a list of the values of the checked checkboxes
function checkAnswerCheckbox() {
    var answeredValues = new Array();
    var checkboxes = document.getElementsByName("checkAnswer");
    for(var i = 0; i < checkboxes.length; i++) {
        if (checkboxes[i].checked) {
            answeredValues.push(checkboxes[i].value);
        }
    }
    return answeredValues;
}

//Checks whether anyy of the checkboxes are checked
function booleanChecked() {
    var checked = false;
    $('input[name="checkAnswer"]').each(function() {
        if ($(this).is(":checked")) {
            checked = true;
        }
    });
    return checked;
}
//Clears the div element after the test is finished.
function clearDiv() {
    if (questionary) {
        while (questionary.firstChild) {
            questionary.removeChild(questionary.firstChild);
        }
    }
}

//Writes to the questionray div the results from the test.
function returnResults() {
    const div = document.createElement('div');
    const divDiagram = document.createElement('div');
    const h1 = document.createElement('h1');
    const pPoints = document.createElement('p');
    const pAnswered = document.createElement('p');
    const pPercentage = document.createElement('p');
    const canvas = document.createElement('canvas');


    div.className = 'col-md-5';
    divDiagram.className = 'col-md-7';
    canvas.id = 'myChart';
    canvas.style.maxWidth = '300px';
    canvas.style.maxHeight = '300px';
    canvas.style.float = 'right';

    h1.innerText = 'Results';
    h1.style.height = '80px';
    div.style.paddingLeft = '7%';
    div.style.paddingTop = '3%';
    divDiagram.style.paddingTop = '5%';
    divDiagram.style.paddingRight = '8%';
    pPoints.style.height = '50px';
    pPoints.style.marginLeft = '6%';
    pPoints.innerText = 'Number of points: ' + points;
    pAnswered.style.height = '50px';
    pAnswered.style.marginLeft = '6%';
    pAnswered.innerText = 'Number of questions answered: ' + answeredQuestions;
    pPercentage.style.height = '50px';
    pPercentage.style.marginLeft = '6%';
    pPercentage.innerText = 'Percentage of right questions ' + round((points/answeredQuestions)*100, 0) + '%';


    questionary.appendChild(div);
    questionary.appendChild(divDiagram);
    div.appendChild(h1);
    div.appendChild(pPoints);
    div.appendChild(pAnswered);
    div.appendChild(pPercentage);
    divDiagram.appendChild(canvas);

    var myChart = new Chart(canvas, {
        type: 'pie',
        data: {
            labels: [
                "Right",
                "Wrong",
            ],
            datasets: [{
                data: [points, answeredQuestions-points],
                backgroundColor: [
                    "#00cc00",
                    "#cc0000"
                ],
                hoverBackgroundColor: [
                    "#00b300",
                    "#b30000"
                ]
            }]
        },
        options: {
            animation:{
                animateScale:true
            }
        }
    });
}




var counter = 0;
{'fraction_non_alphanumeric': 0.14235749367465397, 'fraction_numerical': 0.007739246911742819, 'mean_word_length': 2.272218164110056, 'pattern_counts': {'":': 2, '<': 19, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3067154', 'n_tokens_mistral': 3851, 'n_tokens_neox': 3711, 'n_words': 855}