projectb/QuestionPaperPreview.js
2023-10-13 15:25:50 +05:30

119 lines
4.4 KiB
JavaScript

function QuestionPaperPreview(jsonData){
// The provided JSON data
// console.log(jsonData)
function convertToLatex(text, mathstatus) {
// Parse mathstatus into a boolean
const mathStatusBoolean = JSON.parse(mathstatus);
// Check if the text contains <p> tags
if (!/<p>|<\/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;
}
}
// Initialize the formatted question paper string
let questionPaper = "";
function CheakQnType(question, questionIndex, depth) {
const questionType = question.question_type;
if (questionType) {
processQuestion(question, questionIndex, depth);
}else {
GroupQuestion(question, questionIndex, depth);
}
}
let count =0
function processQuestion(question, questionIndex, depth) {
const questionText = question.question;
const questionDescription = question.question_description;
const mathstatus = question.is_math_enabled;
// Add appropriate indentation for nested questions
const questionIndentation = " ".repeat(depth + 1);
count++
questionPaper += `<div class="text-qus-align">${questionIndentation}${count}) ${convertToLatex(questionText, mathstatus)}${questionIndentation}</div><div class="text-des">${convertToLatex(questionDescription, mathstatus)}</div>`;
// 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) {
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)}` : '';
questionPaper += `<div class="text-option-align">${questionIndentation}${answerA.padEnd(10)} ${answerB}</div>\n`;
}
}
}
function GroupQuestion(question, questionIndex, depth) {
questionPaper += `${question.group_title}`;
questionPaper += `${question.group_description}`;
const nestedQuestions = question.questions;
// Process nested questions in the same way
nestedQuestions.forEach((nestedQuestion) => {
// processQuestion(nestedQuestion, questionIndex, depth);
CheakQnType(nestedQuestion,questionIndex,depth);
});
}
// Function to recursively convert JSON to a formatted question paper
function convertJSONToQuestionPaper(data, depth = 0, sectionIndex = 1, questionIndex = 1) {
const sections = data.sections;
// Extract the heading information
const questionPaperTitle = data.question_paper_title;
const questionPaperDescription = data.question_paper_description;
// Wrap the heading elements in <div> elements with CSS for centering
questionPaper += `<div style="text-align: center; font-weight: bolder; "><h1>${questionPaperTitle}</h1></div>`;
questionPaper += `<div style="text-align: center;"><h5>${questionPaperDescription}</h5></div>`;
questionPaper += "<br><br><br>"; // Add a newline for separation
// Process each section
sections.forEach((section, sectionIndex) => {
const sectionTitle = section.section_title;
const sectionDescription = section.section_description;
const questions = section.questions;
// Add appropriate indentation for nested sections
const indentation = " ".repeat(depth);
questionPaper += `${indentation}${sectionTitle}${indentation}${sectionDescription}`;
// Process each question in the section
questions.forEach((question, questionIndex) => {
CheakQnType(question, questionIndex,depth);
// if (CheakQnType){}
});
});
// Return the formatted question paper
return questionPaper;
}
// 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
}