From 5e8e14e49c8e5cd2d32d92eab2c4a25a71d0935b Mon Sep 17 00:00:00 2001 From: aadhavan Date: Thu, 26 Oct 2023 19:55:35 +0530 Subject: [PATCH] PREVIEW-ALGINMENT-CHANGE : AADHAVAN --- QuestionPaperPreview.js | 352 ++++++++++++++++++++-------------------- QuestionPreview.html | 89 ++++++---- 2 files changed, 233 insertions(+), 208 deletions(-) diff --git a/QuestionPaperPreview.js b/QuestionPaperPreview.js index 7c6952a..31cfc9e 100644 --- a/QuestionPaperPreview.js +++ b/QuestionPaperPreview.js @@ -1,200 +1,192 @@ - function QuestionPaperPreview(jsonData){ -// The provided JSON data -// console.log(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]); + 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); - + modifyHTMLProperties(jsonDat); + //end Remove

tag - - - -function convertToLatex(text, mathstatus) { - // Parse mathstatus into a boolean - const mathStatusBoolean = JSON.parse(mathstatus); - - // Check if the text contains

tags - if (!/

|<\/p>/i.test(text)) { - if (mathStatusBoolean) { - // Use MathJax to convert the text to LaTeX - const cov = MathJax.tex2chtml(text).outerHTML; - return cov; + + + function convertToLatex(text, mathstatus) { + // Parse mathstatus into a boolean + const mathStatusBoolean = JSON.parse(mathstatus); + // Check if the text contains

tags + if (!/

|<\/p>/i.test(text)) { + if (mathStatusBoolean) { + // Use MathJax to convert the text to LaTeX + const cov = MathJax.tex2chtml(text).outerHTML; + return cov; + } else { + // Return the text as-is + return text; + } } else { // Return the text as-is return text; } - } else { - // Return the text as-is - return text; - } -} -// Initialize the formatted question paper string -let questionPaper = ""; - - -function CheakQnType(question, questionIndex, depth,nested) { - const questionType = question.question_type; - if (questionType) { - processQuestion(question, questionIndex, depth,nested); - }else { - GroupQuestion(question, questionIndex, depth); } + // Initialize the formatted question paper string + let questionPaper = ""; + -} -var count =0 -var count1 =0 -var romanNumerals = ["", "i", "ii", "iii", "iv", "v", "vi", "vii", "viii", "ix", "x"]; -function processQuestion(question, questionIndex, depth,nested) { - // console.log('question Data') - // console.log(question); - - 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); - console.log(questionIndentation) - if (nested == 1) {// this adds the inside the questionGroup - count1++; - var romanNumeral=romanNumerals[count1]; - questionPaper += `

${questionIndentation}${romanNumeral}) ${convertToLatex(questionText, mathstatus)}${questionIndentation}

Marks :${questionMark}

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

Marks :${questionMark}

${convertToLatex(questionDescription, mathstatus)}
`; - } - - console.log(questionPaper); - - - // If it's an MCQ question, add choices - - // If it's an MCquestionText - - if (question.question_type === "group") { - const answers = question.answers; - const alphabetOptions = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'; // Define the alphabet options - - for (let i = 0; i < answers.length; i++) { // change the i +=2 aadhavan - const optionA = alphabetOptions[i]; - // 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)}` : ''; - 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) { - - 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); - 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; - + + // Function to recursively convert JSON to a formatted question paper + function convertJSONToQuestionPaper(data, depth = 0, sectionIndex , questionIndex = 1) { + const sections = data.sections; + const questionPaperTitle = data.question_paper_title; + const questionPaperDescription = data.question_paper_description; + + + // Wrap the heading elements in
elements with CSS for centering + questionPaper += `

${questionPaperTitle}

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


"; // Add a newline for separation + + // Process each section + sections.forEach((section, sectionIndex) => { + depth2=0 + description_align =28; + // console.log(sectionIndex); + - // Wrap the heading elements in
elements with CSS for centering - questionPaper += `

${questionPaperTitle}

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


"; // Add a newline for separation + const sectionTitle = section.section_title; + const modifiedSectionTitle = sectionTitle.replaceAll('

', '

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

Attempt Any : ${section.section_attempt_any}

`; + var sectionMark = `

Marks: ${section.section_marks}

`; + const sectionDescription = section.section_description; + // const questions = section.questions; + // Add appropriate indentation for nested sections + const indentation = " ".repeat(depth); + if (section.section_attempt_any === '') { + sectionAttempt = sectionAttempt.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; + if (section.section_marks === '') { + sectionMark = sectionMark.replace('

', '


${modifiedSectionTitle}${sectionMark}${sectionAttempt}${indentation}${sectionDescription}`; + displayQuestions(section) - // Add appropriate indentation for nested sections - const indentation = " ".repeat(depth); - - // questionPaper += `${indentation}${sectionTitle}${sectionAttempt}${sectionMark}${indentation}${sectionDescription}`; - questionPaper += `${indentation}
${modifiedSectionTitle}${sectionMark}${sectionAttempt}${indentation}${sectionDescription}`; - - - // Process each question in the section - questions.forEach((question, questionIndex) => { - CheakQnType(question, questionIndex,depth); - // if (CheakQnType){} - }); - }); + // Return the formatted question paper + // console.log(questionPaper); + return questionPaper; + } + var depth2 =0 + var description_align =25; + function displayQuestions(data, parentSno =''){ + var questionIndentation = " ".repeat(depth2); + var dataa =data.questions; + dataa.forEach((question, index) =>{ + var currentSno = parentSno + (index + 1) + '.'; - // Return the formatted question paper - // console.log(questionPaper); - return questionPaper; -} + var currentSno_length =currentSno.length-(currentSno.length/2) + if (currentSno_length == 1) { + description_align =25 + } else if (currentSno_length == 2) { + description_align =50 + }else if(currentSno_length == 3){ + description_align =65 + }else { + description_align =80 + } -// Call the function and display the formatted question paper -const formattedQuestionPaper = convertJSONToQuestionPaper(jsonData, 0, 1, 1); -const questionPaperElement = document.getElementById("questionPaper"); -console.log(questionPaperElement); -questionPaperElement.innerHTML = formattedQuestionPaper; // Use innerHTML here -} \ No newline at end of file + if (question.group_title) { + const questionGroupSetP = `${question.group_title}`; + const questionGroupmodify =questionGroupSetP.replaceAll('

', '

') + var questionGroupAttempt = `

Attempt Any : ${question.group_attempt_any}

` + var questionGroupMark = `

Marks : ${question.group_marks}

` + if (question.group_attempt_any === '') { + questionGroupAttempt = questionGroupAttempt.replace('

', '

', '

${currentSno}) ${questionGroupmodify}${questionGroupMark}${questionGroupAttempt}
` + + // questionPaper += `
`; + // questionPaper += `
`; + + questionPaper += `
${question.group_description}
`; + depth2++ + } else if (question.question) { + const questionTex = question.question; + const questionText = questionTex.replaceAll('

', '

'); + console.log(questionText); + const questionDescription = question.question_description; + const mathstatus = question.is_math_enabled; + var questionMark = `

Marks :${question.marks}

`; + // console.log(questionMark); + const multipleAnswer =question.question_sub_type; + if (question.marks == '') { + questionMark = questionMark.replace('

', '

${currentSno}) ${convertToLatex(questionText, mathstatus)}
${questionMark}
${convertToLatex(questionDescription, mathstatus)}
`; + + if (question.question_type === "group") { + const answers = question.answers; + const alphabetOptions = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'; // Define the alphabet options + + for (let i = 0; i < answers.length; i++) { // change the i +=2 aadhavan + const optionA = alphabetOptions[i]; + // 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)}` : ''; + if (multipleAnswer == 'false') { + questionPaper += `
${answerA.padEnd(10)}
`; + }else{ + questionPaper += `
${answerA.padEnd(10)}
`; + } + } + } + + } + + if (question.questions) { + displayQuestions(question, currentSno); + } + + }) + } + + // Call the function and display the formatted question paper + const formattedQuestionPaper = convertJSONToQuestionPaper(jsonData, 0, 1, 1); + const questionPaperElement = document.getElementById("questionPaper"); + questionPaperElement.innerHTML = formattedQuestionPaper; // Use innerHTML here + } \ No newline at end of file diff --git a/QuestionPreview.html b/QuestionPreview.html index 2ea7316..f63c408 100644 --- a/QuestionPreview.html +++ b/QuestionPreview.html @@ -49,6 +49,43 @@