Skip to content
Snippets Groups Projects
Select Git revision
  • main default protected
1 result

alquiz-fantasy-bg-ver3.js

Blame
  • alquiz-fantasy-bg-ver3.js 247.62 KiB
    console.log("here starts alquiz fantasy ver 3");
    
    class GamifiedQuiz {
        constructor(quizObject) {
            this.currentQuestionId;
            this.currentPage;
            this.Parser = new DOMParser();
            this.QuestionGroups = {};
            this.solvedVariants = [];
            //this.QuestionGroupsById = {};
            if (quizObject != undefined) {
                if (quizObject.groups != undefined) {
                    for (let questionGroupId in quizObject.groups) {
                        this.QuestionGroups[questionGroupId] = new QuestionGroup(questionGroupId, quizObject.groups[questionGroupId]);
                    }
                }
                let pageCount = 0;
                if (quizObject.questions != undefined) {
                    for (let questionId in quizObject.questions) {
                        let groupToAddId;
                        if (this.QuestionGroups[quizObject.questions[questionId].group] != undefined) {
                            groupToAddId = quizObject.questions[questionId].group;
                        }
                        else {
                            //try to identify group by token, elsewise add to group "unsorted"
                            if (questionId.indexOf("_") != -1) {
                                let expectedGroupNameMatch = questionId.match(/^(.*)_/);
                                if (expectedGroupNameMatch[1] != undefined && expectedGroupNameMatch[1] != "") {
                                    if (this.QuestionGroups[expectedGroupNameMatch[1]] != undefined) {
                                        groupToAddId = expectedGroupNameMatch[1];
                                    }
                                }
                            }
                            if (groupToAddId == undefined) {
                                if (this.QuestionGroups.unsorted == undefined) {
                                    this.QuestionGroups.unsorted = new QuestionGroup("unsorted", "Unsorted Questions");
                                }
                                groupToAddId = "unsorted";
                            }
                            this.QuestionGroups[groupToAddId].addQuestion(new Question(questionId, quizObject.questions[questionId].name));
                        }
                        let ElementToAdd;
                        if (quizObject.questions[questionId].type == "instruction") {
                            ElementToAdd = new Instruction(questionId, quizObject.questions[questionId].name, pageCount, quizObject.questions[questionId].onsuccess, quizObject.questions[questionId].onfailure, quizObject.questions[questionId].BubbleInfo, quizObject.questions[questionId].onpage);
                        }
                        else {
                            ElementToAdd = new Question(questionId, quizObject.questions[questionId].name, pageCount, quizObject.questions[questionId].needs, quizObject.questions[questionId].BubbleInfo, quizObject.questions[questionId].onsuccess, quizObject.questions[questionId].onfailure, quizObject.questions[questionId].askBeforeSkip, quizObject.questions[questionId].onpage, quizObject.questions[questionId].variants, quizObject.questions[questionId].color, quizObject.questions[questionId].filter);
                        }
                        this.QuestionGroups[groupToAddId].addQuestion(ElementToAdd);
                        pageCount = pageCount + 1 + (ElementToAdd.variants > 1 ? ElementToAdd.variants-1 : 0) + (ElementToAdd.questionsOnPage > 1 ? ElementToAdd.questionsOnPage : 0); 
                    }
                }
            }
    
            //auto assign onsuccess and onfailure for undefined
            let groupNames = Object.keys(this.QuestionGroups);
            let grouplength = groupNames.length;
            for (let i = 0; i < grouplength; i++) {
                let questionNames = Object.keys(this.QuestionGroups[groupNames[i]].Questions);
                let questionlength = questionNames.length;
                for (let j = 0; j < questionlength; j++) {
                    if (!this.QuestionGroups[groupNames[i]].Questions[questionNames[j]].onsuccess || !this.QuestionGroups[groupNames[i]].Questions[questionNames[j]].onfailure) {
                        //console.log("something for "+questionNames[j]+" is undefined");
                        //probably next question or next group
                        if (j < questionlength - 1) {
                            //next question
                            if(this.QuestionGroups[groupNames[i]].Questions[questionNames[j]].onsuccess == undefined) {
                                this.QuestionGroups[groupNames[i]].Questions[questionNames[j]].onsuccess = questionNames[j + 1];
                            }
                            if(this.QuestionGroups[groupNames[i]].Questions[questionNames[j]].onfailure == undefined) {