function html2json() {
// console.clear();
$.fn.getTextQuestion = function(element_id) {
var questionTitle = document.getElementById('textQuestionName_' + element_id).querySelector('.ql-editor').innerHTML;
var marks = document.getElementById('Text_Question_Marks_Id_' + element_id).value;
var questionUUID = document.getElementById('Text_Question_UUID_ID_' + element_id).value;
var isMathEnabled = document.getElementById('math_' + element_id).checked;
var displayMarks = document.getElementById('Text_Question_Display_Marks_Id_' + element_id).checked;
var questionSubTitle = "";
if(isMathEnabled == true)
{
questionSubTitle = document.getElementById('textQuestionSubtitle_math_' + element_id).value;
}
else
{
questionSubTitle = document.getElementById('textQuestionSubtitle_' + element_id).querySelector('.ql-editor').innerHTML;
}
var questionType = document.getElementById('question_type_' + element_id).value;
// var questionSubType = document.getElementById('longAnswer_' + element_id).checked;
var questionSubType = 'false';
return {
"question": questionTitle,
"question_description": questionSubTitle,
"question_type": questionType,
"question_sub_type": questionSubType,
"is_math_enabled": isMathEnabled,
"display_marks": displayMarks,
"marks":marks,
"text_question_uuid":questionUUID,
}
}
$.fn.getChoiceQuestion = function(element_id) {
var questionTitle = document.getElementById('textQuestionName_' + element_id).querySelector('.ql-editor').innerHTML;
var marks = document.getElementById('Choice_Question_Marks_Id_' + element_id).value;
var questionUUID = document.getElementById('Choice_Question_UUID_ID_' + element_id).value;
var isMathEnabled = document.getElementById('math_' + element_id).checked;
var displayMarks = document.getElementById('Choice_Question_Display_Marks_Id_' + element_id).checked;
var questionSubTitle = "";
if(isMathEnabled == true)
{
questionSubTitle = document.getElementById('textQuestionSubtitle_math_' + element_id).value;
}
else
{
questionSubTitle = document.getElementById('textQuestionSubtitle_' + element_id).querySelector('.ql-editor').innerHTML;
}
var questionType = document.getElementById('question_type_' + element_id).value;
// console.log(questionType);
var questionSubType = document.getElementById('multipleAnswer_' + element_id).checked;
var answers = [];
//get choice group
//get default choice details
var default_choice_math_Enabled = document.getElementById('Choicemath_' + element_id).checked;
var choice_name = "";
if(default_choice_math_Enabled == true)
{
choice_name = document.getElementById('choiceTextField_math_' + element_id).value;
}
else
{
choice_name = document.getElementById('choiceTextField_' + element_id).querySelector('.ql-editor').innerHTML;
}
var choice_group_fixed_uuid = document.getElementById('Choice_Group_Fixed_UUID_ID_' + element_id).value;
answers.push({
"choice_name": choice_name,
"is_math_enabled": default_choice_math_Enabled,
"choice_uuid": choice_group_fixed_uuid,
"no": 'a'
});
// console.log('before group');
//get dynamically added choice detailscolor: #999;
const alphabets = ['b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'];
var $newlyAddedChoices = $('#addChoiceArea_' + element_id);
$newlyAddedChoices.children().each(function(index, element) {
var letter = alphabets[index]; //for choice field key[no] value
var groupChoiceId = ((element.id).split('_').slice(-1)[0]).split('.').slice(0)[0];
var individualChoiceId = (element.id).split('.').slice(-1)[0];
// console.log('newChoiceTextField_'+groupChoiceId+'.'+individualChoiceId);
var choice_math_Enabled = document.getElementById('choice_math_' + groupChoiceId + '.' + individualChoiceId).checked;
var choice_name = "";
if(choice_math_Enabled == true)
{
choice_name = document.getElementById('newchoiceTextField_math_' + groupChoiceId + '.' + individualChoiceId).value;
}
else
{
choice_name = document.getElementById('newChoiceTextField_' + groupChoiceId + '.' + individualChoiceId).querySelector('.ql-editor').innerHTML;
}
// console.log('choice_description id in the html to json script');
var id = document.getElementById('newChoiceCommentField_' + groupChoiceId + '.' + individualChoiceId);
var choice_group_dynamic_uuid = document.getElementById('Choice_Group_Dynamic_UUID_ID_' + groupChoiceId + '.' + individualChoiceId).value;
answers.push({
"choice_name": choice_name,
"is_math_enabled": choice_math_Enabled,
"choice_uuid": choice_group_dynamic_uuid,
"no":letter
});
});
return {
"question": questionTitle,
"question_description": questionSubTitle,
"question_type": questionType,
"question_sub_type": questionSubType,
"is_math_enabled": isMathEnabled,
"display_marks": displayMarks,
"marks":marks,
"choice_question_uuid":questionUUID,
"answers": answers
}
}
$.fn.getQuestionsData = function(element_id, type = 'question') // type for single question or group question
{
// console.log('getQuestionsData - '+element_id);
var $questionArea = (type == 'question' ? $('#question_area_' + element_id) : $('#group_question_area_' + element_id));
// console.log('getQuestionsData');
// console.log($questionArea);
var questionsArr = [];
$questionArea.children("div").each(function(index, element) {
var questionRowID = (element.id).split("_").slice(-1)[0];
// console.log('row question' + questionRowID);
var question_type = document.getElementById('question_type_' + questionRowID);
// check whether current element is single question or question group
if (typeof question_type !== null && question_type != undefined) {
if (document.getElementById('question_type_' + questionRowID).value == 'text') {
//get text type question data
var textQuestion = $.fn.getTextQuestion(questionRowID);
// console.log('textQuestion')
// console.log(textQuestion)
questionsArr.push(textQuestion);
} else {
// console.log('get choice question');
//get choice type question data
var getChoiceQuestion = $.fn.getChoiceQuestion(questionRowID);
// console.log('getChoiceQuestion');
// console.log(getChoiceQuestion);
questionsArr.push(getChoiceQuestion)
}
} else // handle question group
{
// console.log('QUESTION GROUP');
// console.log('row question' + questionRowID);
var questionGroupData = $.fn.getQuestionQroupData(element);
questionsArr.push(questionGroupData)
// console.log('questionGroupData');
// console.log(questionGroupData);
}
});
return questionsArr
}
$.fn.getQuestionQroupData = function(element) {
// console.log('getSectionData');
// console.log(element.id);
const rand_id = (element.id).split("_").slice(-1)[0];
// console.log(rand_id);
// console.log('section_title_text_'+rand_id);
var groupQuestion = {}
var group_title = document.getElementById('subSection_title_text_' + rand_id);
var group_description = document.getElementById('subSection_description_text_' + rand_id);
var group_marks = document.getElementById('Question_Group_Marks_Id_' + rand_id);
var group_uuid = document.getElementById('Question_Group_UUID_ID_' + rand_id);
var group_display_marks = document.getElementById('Question_Group_Display_Marks_Id_' + rand_id);
var group_attempt_any = document.getElementById('Question_Attempt_Any_Id_' + rand_id);
groupQuestion['group_title'] = ((typeof group_title !== null && group_title !== 'undefined') ? (group_title !== null ? group_title.querySelector('.ql-editor').innerHTML : null) : null);
groupQuestion['group_description'] = ((typeof group_description !== null && group_description !== 'undefined') ? (group_description !== null ? group_description.querySelector('.ql-editor').innerHTML : null) : null);
groupQuestion['group_marks'] = ((typeof group_marks !== null && group_marks !== 'undefined') ? (group_marks !== null ? group_marks.value : null) : null);
groupQuestion['group_uuid'] = ((typeof group_uuid !== null && group_uuid !== 'undefined') ? (group_uuid !== null ? group_uuid.value : null) : null);
groupQuestion['group_display_marks'] = ((typeof group_display_marks !== null && group_display_marks !== 'undefined') ? (group_display_marks !== null ? group_display_marks.checked : null) : null);
groupQuestion['group_attempt_any'] = ((typeof group_attempt_any !== null && group_attempt_any !== 'undefined') ? (group_attempt_any !== null ? group_attempt_any.value : null) : null);
if (group_title !== null) {
var questionsData = $.fn.getQuestionsData(rand_id, 'group');
// console.log('Section Data');
// console.log(questionsData);
groupQuestion['questions'] = questionsData;
return groupQuestion;
}
}
$.fn.getSectionData = function(element) {
// console.log('getSectionData');
// console.log(element.id);
const rand_id = (element.id).split("_").slice(-1)[0];
// console.log(rand_id);
// console.log('section_title_text_'+rand_id);
var section = {}
var section_title = document.getElementById('section_title_text_' + rand_id);
var section_description = document.getElementById('section_description_text_' + rand_id);
var section_marks = document.getElementById('Section_Marks_Id_' + rand_id);
var section_uuid = document.getElementById('Section_UUID_ID_' + rand_id);
var section_display_marks = document.getElementById('Section_Display_Marks_Id_' + rand_id);
var section_attempt_any = document.getElementById('Section_Attempt_Any_Id_' + rand_id);
section['section_title'] = ((typeof section_title !== null && section_title !== 'undefined') ? (section_title !== null ? section_title.querySelector('.ql-editor').innerHTML : null) : null);
section['section_description'] = ((typeof section_description !== null && section_description !== 'undefined') ? (section_description !== null ? section_description.querySelector('.ql-editor').innerHTML : null) : null);
section['section_marks'] = ((typeof section_marks !== null && section_marks !== 'undefined') ? (section_marks !== null ? section_marks.value : null) : null);
section['section_uuid'] = ((typeof section_uuid !== null && section_uuid !== 'undefined') ? (section_uuid !== null ? section_uuid.value : null) : null);
section['section_display_marks'] = ((typeof section_display_marks !== null && section_display_marks !== 'undefined') ? (section_display_marks !== null ? section_display_marks.checked : null) : null);
section['section_attempt_any'] = ((typeof section_attempt_any !== null && section_attempt_any !== 'undefined') ? (section_attempt_any !== null ? section_attempt_any.value : null) : null);
if (section_title !== null) {
var questionsData = $.fn.getQuestionsData(rand_id);
// console.log('Section Data');
// console.log(questionsData);
section['questions'] = questionsData;
return section;
}
}
var formData = {};
var question_paper_title = document.getElementById('Question_title').querySelector('.ql-editor').innerHTML;
var question_paper_description = document.getElementById('Question_description').querySelector('.ql-editor').innerHTML;
var max_marks = document.getElementById('Max_Marks').value;
var course_ID = document.getElementById('Course_ID').value;
var course_Code = document.getElementById('Course_Code').value;
var subject_ID = document.getElementById('Subject_ID').value;
var subject_Code = document.getElementById('Subject_Code').value;
var paper_No = document.getElementById('Paper_No').value;
var subject_Name = document.getElementById('Subject_Name').value;
var sub_Paper_Prefix = document.getElementById('Sub_Paper_Prefix').value;
var question_Paper_UUID = document.getElementById('QuestionPaper_UUID_ID').value;
formData['question_paper_title'] = question_paper_title;
formData['question_paper_description'] = question_paper_description;
formData['max_marks'] = max_marks;
formData['course_ID'] = course_ID;
formData['course_Code'] = course_Code;
formData['subject_ID'] = subject_ID;
formData['subject_Code'] = subject_Code;
formData['paper_No'] = paper_No;
formData['subject_Name'] = subject_Name;
formData['sub_Paper_Prefix'] = sub_Paper_Prefix;
formData['question_Paper_uuid'] = question_Paper_UUID;
formData['sections'] = [];
// console.log(formData);
// return False;
var $parentSectionElement = $("#addNewSection");
$parentSectionElement.find(".section").each(function(index, element) {
// console.log(element);
// if(index == 2){return false;}
var sectionData = $.fn.getSectionData(element);
// console.log(sectionData);
if (sectionData !== null && sectionData !== undefined) {
formData['sections'].push(sectionData);
}
});
// console.log((formData));
// console.log(JSON.stringify(formData, null, 1));
localStorage.setItem("json", JSON.stringify((formData)));
// console.log(JSON.stringify(formData));
// console.log(OBJtoXML(({'question_paper' : formData})));
}