diff --git a/deimos/views.py b/deimos/views.py index 881ee3d6..47b6b026 100644 --- a/deimos/views.py +++ b/deimos/views.py @@ -126,7 +126,7 @@ def gradebook(request, course_id): mcq_related_names = ['mcq_expression_answers', 'mcq_text_answers','mcq_float_answers', - 'mcq_variable_float_answers','mcq_image_answers''mcq_latex_answers'] + 'mcq_variable_float_answers','mcq_image_answers','mcq_latex_answers'] # TODO: Add the action link in answer_question.html # TODO: Implement question_view as well. diff --git a/phobos/models.py b/phobos/models.py index ae8bea16..c128ac6c 100644 --- a/phobos/models.py +++ b/phobos/models.py @@ -267,7 +267,7 @@ def get_mcq_pk_ac_list(self): # ac == answer_code output = [] # List of all answer types mcq_related_names = ['mcq_expression_answers', 'mcq_text_answers','mcq_float_answers', - 'mcq_variable_float_answers','mcq_image_answers''mcq_latex_answers'] + 'mcq_variable_float_answers','mcq_image_answers','mcq_latex_answers'] for mrn in mcq_related_names: for mcq in getattr(self, mrn).all(): output.append(mcq.get_pk_ac()) @@ -276,7 +276,7 @@ def get_mcq_pk_ac_list(self): # ac == answer_code def get_mcq_answers(self): output = [] mcq_related_names = ['mcq_expression_answers', 'mcq_text_answers','mcq_float_answers', - 'mcq_variable_float_answers','mcq_image_answers''mcq_latex_answers'] + 'mcq_variable_float_answers','mcq_image_answers','mcq_latex_answers'] for mrn in mcq_related_names: output.extend(getattr(self, mrn).all()) return output @@ -491,6 +491,12 @@ class Meta: def __str__(self): return f"MCQ Answer for {self.question}: {self.content}" + + def get_pk_ac(self): + """ + Returns 'pk_answercode', which is used for question editing and answer validation. + """ + return f'{self.pk}_{self.get_answer_code()}' class MCQFloatAnswer(MCQAnswerBase): """ @@ -604,6 +610,12 @@ class MatchingAnswer(models.Model): def __str__(self): return f"Matching pair answer for {self.question}" + def get_pk_ac(self): + """ + Returns 'pk_answercode', which is used for question editing and answer validation. + """ + return f'{self.pk}_{self.get_answer_code()}' + def get_answer_code(self): return 9 diff --git a/phobos/static/phobos/js/create_question.js b/phobos/static/phobos/js/create_question.js index 1c82c285..0e90a135 100644 --- a/phobos/static/phobos/js/create_question.js +++ b/phobos/static/phobos/js/create_question.js @@ -10,7 +10,7 @@ document.addEventListener('DOMContentLoaded', () => { const calculatorDiv = document.querySelector('.calculator'); calculatorDiv.style.display = 'none'; const createQuestionBtn = document.querySelector('.create-question-btn'); - var settingsPreviousValue = 0; + var settingsPreviousValue = -1; const settingsSelect = document.querySelector('.settings-select'); // Dispatching event on settings select so that the last changes are updated @@ -28,16 +28,40 @@ document.addEventListener('DOMContentLoaded', () => { var questionTypeDicts = { } + + const qtypeConfigs = { + 'm-answer':'3', + 'mp-answer':'8', + 'fr-answer':'4', + 'e-answer':'0', + 'f-answer':'1', + 'l-answer':'2' + } + + + + + + + + + + + + + if(form.classList.contains('create-mode')){ allQuestionBlocks.appendChild(addQuestionBlock()); }else{ - var blockCounter = 0; + allQuestionBlocks.querySelectorAll('.question-block').forEach((block)=>{ addEventListenersToQuestionBlock(block, set_initial_settings=false); - blockCounter += 1; + const hiddenQuestionType = block.querySelector('.hidden-q-type'); + questionTypeDicts[block.querySelector('.question-number-value').value] = qtypeConfigs[hiddenQuestionType.value]; + fillQuestionBlockCounters(block); }); - num_questions = blockCounter; - part_num_questions = blockCounter; + num_questions = allQuestionBlocks.length + 1; + part_num_questions = allQuestionBlocks.length + 1; // add the already created variables to the varSymbolsArray addedVarsDiv.querySelectorAll('.var-container').forEach((varContainer)=>{ varSymbolsArray.push(varContainer.querySelector('.var-symbol').value); @@ -55,6 +79,23 @@ document.addEventListener('DOMContentLoaded', () => { } + function fillQuestionBlockCounters(qBlock){ + const hiddenQuestionType = qBlock.querySelector('.hidden-q-type'); + if(hiddenQuestionType.value=='m-answer'){ + // count the number of mcq options and update dataset.counter + const inputedMcqAnswersDiv = qBlock.querySelector('.inputed-mcq-answers'); + const truemcqs = qBlock.querySelectorAll('.mcq-true').length; + const falsemcqs = qBlock.querySelectorAll('.mcq-false').length; + inputedMcqAnswersDiv.dataset.counter = truemcqs + falsemcqs; + inputedMcqAnswersDiv.dataset.trueCounter = truemcqs; + + }else if(hiddenQuestionType.value=='mp-answer'){ + // count the number of matching pair answers and make sure they exceed + // 2 but are less than or equal to 10 + const inputedMpAnswersDiv = qBlock.querySelector('.inputed-mp-answers'); + inputedMpAnswersDiv.dataset.mereCounter = qBlock.querySelectorAll('.inputed-mp-answer').length; + } + } @@ -667,7 +708,7 @@ function addEventListenersToQuestionBlock(questionBlock, set_initial_settings=tr // setting the initial hidden settings to the same as the default // for the assignment under which this question appears. if(set_initial_settings){ - var inputsWithSettingsClass = questionBlock.querySelectorAll('input.settings'); + var inputsWithSettingsClass = document.querySelectorAll('input.settings'); inputsWithSettingsClass.forEach((input)=>{ const question_num = parseInt(questionBlock.querySelector('.question-number-value').value) const settingsHiddenInput = questionBlock.querySelector(`input[name="${question_num}_${input.name}"]`); @@ -874,7 +915,7 @@ function addEventListenersToQuestionBlock(questionBlock, set_initial_settings=tr formattedAnswerDiv.style.display = 'none'; calculatorDiv.style.display = 'none'; mcqOptionBtnsDiv.style.display = 'block'; - questionTypeDicts[questionBlock.querySelector('.question-number-value').value] = '3'; + questionTypeDicts[questionBlock.querySelector('.question-number-value').value] = qtypeConfigs[hiddenQuestionType.value]; }); mpBtn.addEventListener('click', (event)=>{ @@ -891,10 +932,11 @@ function addEventListenersToQuestionBlock(questionBlock, set_initial_settings=tr mcqOptionBtnsDiv.style.display = 'none'; mpInputDiv.style.display = 'block'; mcqInputDiv.style.display = 'none'; - questionTypeDicts[questionBlock.querySelector('.question-number-value').value] = '8'; + questionTypeDicts[questionBlock.querySelector('.question-number-value').value] = qtypeConfigs[hiddenQuestionType.value]; }); + mcqOptionBtnsDiv.addEventListener('click', (event) => { event.preventDefault(); // This is to select the type of mcq the user wants to input @@ -1096,7 +1138,7 @@ function addEventListenersToQuestionBlock(questionBlock, set_initial_settings=tr answerFieldsDiv.innerHTML = ''; formattedAnswerDiv.scrollIntoView({behavior: 'smooth'}); - questionTypeDicts[questionBlock.querySelector('.question-number-value').value] = '4'; + questionTypeDicts[questionBlock.querySelector('.question-number-value').value] = qtypeConfigs[hiddenQuestionType.value]; }) @@ -1116,7 +1158,7 @@ function addEventListenersToQuestionBlock(questionBlock, set_initial_settings=tr screen.placeholder = 'Expression'; answerFieldsDiv.scrollIntoView({ behavior: 'smooth' }); - questionTypeDicts[questionBlock.querySelector('.question-number-value').value] = '0'; + questionTypeDicts[questionBlock.querySelector('.question-number-value').value] = qtypeConfigs[hiddenQuestionType.value]; }); @@ -1136,7 +1178,7 @@ function addEventListenersToQuestionBlock(questionBlock, set_initial_settings=tr screen.placeholder = 'Real number'; answerFieldsDiv.scrollIntoView({ behavior: 'smooth' }); - questionTypeDicts[questionBlock.querySelector('.question-number-value').value] = '1'; + questionTypeDicts[questionBlock.querySelector('.question-number-value').value] = qtypeConfigs[hiddenQuestionType.value]; }); // Latex button selected. Probably never used. @@ -1153,7 +1195,7 @@ function addEventListenersToQuestionBlock(questionBlock, set_initial_settings=tr answerFieldsDiv.innerHTML = latexAnswerDiv; answerFieldsDiv.scrollIntoView({ behavior: 'smooth' }); - questionTypeDicts[questionBlock.querySelector('.question-number-value').value] = '2' + questionTypeDicts[questionBlock.querySelector('.question-number-value').value] = qtypeConfigs[hiddenQuestionType.value]; @@ -1568,6 +1610,8 @@ function addEventListenersToQuestionBlock(questionBlock, set_initial_settings=tr alert('That is a little excessive. The number of matching pairs must not exceed 10'); return false }else { + // TODO: I don't think the following lines are useful now. + // they were probably used just for testing. const nodeP = document.createElement('p'); const mpnum = `` nodeP.innerHTML = mpnum diff --git a/phobos/templates/phobos/create_question.html b/phobos/templates/phobos/create_question.html index 851b8e12..d3c1f31a 100644 --- a/phobos/templates/phobos/create_question.html +++ b/phobos/templates/phobos/create_question.html @@ -13,10 +13,6 @@