diff --git a/QuestionPaperPreview.js b/QuestionPaperPreview.js index 99402a4..ed654f5 100644 --- a/QuestionPaperPreview.js +++ b/QuestionPaperPreview.js @@ -2,6 +2,44 @@ function QuestionPaperPreview(jsonData){ // The provided JSON data // console.log(jsonData) + +// start Remove

tag + +var jsonDat = jsonData; +// Remove

tags only with
tags +function removeEmptyPTags(htmlString) { + var tempdiv = document.createElement('div'); + tempdiv.innerHTML = htmlString; + var paragraphs = tempdiv.querySelectorAll('p'); + + paragraphs.forEach(function(paragraph) { + if (paragraph.innerHTML.trim() === '
') { + paragraph.remove(); + } + }); + + return tempdiv.innerHTML; +} +// Recursive function to modify HTML content in an object +function modifyHTMLProperties(obj) { + for (var key in obj) { + if (obj.hasOwnProperty(key)) { + if (typeof obj[key] === 'string') { + // If the property is a string, modify it + obj[key] = removeEmptyPTags(obj[key]); + } else if (typeof obj[key] === 'object') { + // If the property is an object, recursively modify its properties + modifyHTMLProperties(obj[key]); + } + } + } +} +modifyHTMLProperties(jsonDat); + +//end Remove

tag + + + function convertToLatex(text, mathstatus) { // Parse mathstatus into a boolean const mathStatusBoolean = JSON.parse(mathstatus); @@ -25,59 +63,82 @@ function convertToLatex(text, mathstatus) { let questionPaper = ""; -function CheakQnType(question, questionIndex, depth) { +function CheakQnType(question, questionIndex, depth,nested) { const questionType = question.question_type; - if (questionType) { - processQuestion(question, questionIndex, depth); + processQuestion(question, questionIndex, depth,nested); }else { GroupQuestion(question, questionIndex, depth); } } let count =0 -function processQuestion(question, questionIndex, depth) { - const questionText = question.question; +var count1 =0 +function processQuestion(question, questionIndex, depth,nested) { + const questionTex = question.question; + const questionText = questionTex.replace('

', '

'); const questionDescription = question.question_description; const mathstatus = question.is_math_enabled; - + const questionMark = question.marks; + const multipleAnswer =question.question_sub_type; // this gives true or false to chaeck multiple answers or not // Add appropriate indentation for nested questions const questionIndentation = " ".repeat(depth + 1); - count++ - questionPaper += `

${questionIndentation}${count}) ${convertToLatex(questionText, mathstatus)}${questionIndentation}
${convertToLatex(questionDescription, mathstatus)}
`; + if (nested == 1) {// this adds the inside the questionGroup + count1++ + questionPaper += `
${questionIndentation}${count}.${count1}) ${convertToLatex(questionText, mathstatus)}${questionIndentation}

Marks :${questionMark}

${convertToLatex(questionDescription, mathstatus)}
`; + } else { + count++ + questionPaper += `
${questionIndentation}${count}) ${convertToLatex(questionText, mathstatus)}${questionIndentation}

Marks :${questionMark}

${convertToLatex(questionDescription, mathstatus)}
`; + } // If it's an MCQ question, add choices if (question.question_type === "group") { const answers = question.answers; const alphabetOptions = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'; // Define the alphabet options - for (let i = 0; i < answers.length; i += 2) { + for (let i = 0; i < answers.length; i++) { // change the i +=2 aadhavan const optionA = alphabetOptions[i]; - const optionB = alphabetOptions[i + 1]; + // const optionB = alphabetOptions[i + 1]; const answerA = answers[i] ? `${optionA}. ${convertToLatex(answers[i].choice_name, mathstatus)}` : ''; - const answerB = answers[i + 1] ? `${optionB}. ${convertToLatex(answers[i + 1].choice_name, mathstatus)}` : ''; - - questionPaper += `
${questionIndentation}${answerA.padEnd(10)} ${answerB}
\n`; + // const answerB = answers[i + 1] ? `${optionB}. ${convertToLatex(answers[i + 1].choice_name, mathstatus)}` : ''; + if (multipleAnswer == 'false') { + questionPaper += `
${questionIndentation}${answerA.padEnd(10)}
`; + }else{ + questionPaper += `
${questionIndentation}${answerA.padEnd(10)}
`; + } + // questionPaper += `
${questionIndentation}${answerA.padEnd(10)}
`; + // questionPaper += `
${questionIndentation}${answerA.padEnd(10)} ${answerB}
`; } } } function GroupQuestion(question, questionIndex, depth) { - questionPaper += `${question.group_title}`; - questionPaper += `${question.group_description}`; + + const questionGroupSetP = `${question.group_title}`; + const questionGroupmodify =questionGroupSetP.replace('

', '

') + count++ + questionPaper += `

${count}) ${questionGroupmodify}
` + + questionPaper += `

Marks : ${question.group_marks}

`; + questionPaper += `

Attempt Any : ${question.group_attempt_any}

`; + + questionPaper += `
${question.group_description}
`; const nestedQuestions = question.questions; // Process nested questions in the same way + count1 =0 nestedQuestions.forEach((nestedQuestion) => { // processQuestion(nestedQuestion, questionIndex, depth); - CheakQnType(nestedQuestion,questionIndex,depth); + const nested =1; + + CheakQnType(nestedQuestion,questionIndex,depth,nested); }); } // Function to recursively convert JSON to a formatted question paper function convertJSONToQuestionPaper(data, depth = 0, sectionIndex = 1, questionIndex = 1) { const sections = data.sections; - + console.log(sections); // Extract the heading information const questionPaperTitle = data.question_paper_title; const questionPaperDescription = data.question_paper_description; @@ -87,17 +148,24 @@ function convertJSONToQuestionPaper(data, depth = 0, sectionIndex = 1, questionI // Wrap the heading elements in
elements with CSS for centering questionPaper += `

${questionPaperTitle}

`; questionPaper += `
${questionPaperDescription}
`; - questionPaper += "


"; // Add a newline for separation + // questionPaper += "


"; // Add a newline for separation // Process each section sections.forEach((section, sectionIndex) => { const sectionTitle = section.section_title; + const modifiedSectionTitle = sectionTitle.replace('

', '

');//add the id for Section Title + + const sectionAttempt = `

Attempt Any : ${section.section_attempt_any}

`; + const sectionMark = `

Marks: ${section.section_marks}

`; const sectionDescription = section.section_description; const questions = section.questions; // Add appropriate indentation for nested sections const indentation = " ".repeat(depth); - questionPaper += `${indentation}${sectionTitle}${indentation}${sectionDescription}`; + + // questionPaper += `${indentation}${sectionTitle}${sectionAttempt}${sectionMark}${indentation}${sectionDescription}`; + questionPaper += `${indentation}${modifiedSectionTitle}${sectionMark}${sectionAttempt}${indentation}${sectionDescription}`; + // Process each question in the section questions.forEach((question, questionIndex) => { @@ -108,6 +176,7 @@ function convertJSONToQuestionPaper(data, depth = 0, sectionIndex = 1, questionI }); // Return the formatted question paper + // console.log(questionPaper); return questionPaper; } diff --git a/QuestionPreview.html b/QuestionPreview.html index b47139c..bbbd0bf 100644 --- a/QuestionPreview.html +++ b/QuestionPreview.html @@ -49,8 +49,53 @@