Skip to content

Commit

Permalink
progress on edit: front end almost done
Browse files Browse the repository at this point in the history
  • Loading branch information
arielfayol37 committed Dec 4, 2023
1 parent 814fd72 commit 4e95e09
Show file tree
Hide file tree
Showing 7 changed files with 141 additions and 65 deletions.
2 changes: 1 addition & 1 deletion deimos/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
16 changes: 14 additions & 2 deletions phobos/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand All @@ -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
Expand Down Expand Up @@ -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):
"""
Expand Down Expand Up @@ -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

Expand Down
68 changes: 56 additions & 12 deletions phobos/static/phobos/js/create_question.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
Expand All @@ -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;
}
}



Expand Down Expand Up @@ -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}"]`);
Expand Down Expand Up @@ -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)=>{
Expand All @@ -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
Expand Down Expand Up @@ -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];

})

Expand All @@ -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];

});

Expand All @@ -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.
Expand All @@ -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];



Expand Down Expand Up @@ -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 = `<input type='hidden' value=${inputedMpAnswersDiv.dataset.mereCounter + 1} name="${inputedMpAnswersDiv.dataset.qnumber}_num_of_mps"/>`
nodeP.innerHTML = mpnum
Expand Down
14 changes: 7 additions & 7 deletions phobos/templates/phobos/create_question.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,6 @@
<li><a href="{% url 'phobos:search_question' %}">Search Question</a></li>
</ul>
</div>

<div class="settings-icon side-info-icon" title="Settings">
<ion-icon name="settings"></ion-icon>
</div>
<!-- TODO: Include buttons and invent listeners to create subquestions.-->

<div class="page-container" id="create-question">
Expand Down Expand Up @@ -75,9 +71,12 @@ <h3 class="original-size suggestion">Create a Question</h3>
<br/>
<input type="submit" class="btn btn-success create-question-btn" value="Create Question">
<br/>
<div class="question-settings hide">

<span id="close-x-settings">X</span><br/>
<div class="question-settings side">
<div class="settings-icon side-info-icon" title="Settings">
<ion-icon name="settings"></ion-icon>
</div>
<div class="hide side-info">
<span class="side-info-x">X</span><br/>
<h4 class="suggestion original-size">Settings</h4>
<hr/>
<select title="Part" name="settings-select" class="settings-select field-style">
Expand Down Expand Up @@ -141,6 +140,7 @@ <h4 class="suggestion original-size">Settings</h4>
</div>

</div>
</div>
</div>
<div class="question-variables side">
<div class="side-info-icon" title="Info">
Expand Down
6 changes: 3 additions & 3 deletions phobos/templates/phobos/edit_question.html
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
<div class="page-container" id="create-question">

<form class="edit-mode" method="post" enctype="multipart/form-data" id="question-form"
action="{% url 'phobos:create_question' assignment_id=question.assignment.id %}">
action="{% url 'phobos:edit_question' question_id=question.pk %}">
{% csrf_token %}

<div class="question">
Expand Down Expand Up @@ -460,8 +460,8 @@ <h3 class="suggestion original-size">Variables</h3>
{% endif %}

{% for interval in var.intervals.all %}
<input type="hidden" name="domain#lb#{{var.symbol}}{{forloop.counter0}}" value="{{interval.lower_bound}}" class="var-hidden-i"/>
<input type="hidden" name="domain#ub#{{var.symbol}}{{forloop.counter0}}" value="{{interval.upper_bound}}" class="var-hidden-i"/>
<input type="hidden" name="domain#lb#{{var.symbol}}#{{forloop.counter0}}" value="{{interval.lower_bound}}" class="var-hidden-i"/>
<input type="hidden" name="domain#ub#{{var.symbol}}#{{forloop.counter0}}" value="{{interval.upper_bound}}" class="var-hidden-i"/>
{% endfor %}
</div>
{% endfor %}
Expand Down
3 changes: 3 additions & 0 deletions phobos/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
path('edit_question/<int:question_id>',views.edit_question,
name='edit_question'
),
path('edit_question/<int:question_id>/<str:question_nums_types>',views.edit_question,
name='edit_question'
),
path('delete_question/<int:question_id>', views.delete_question, name='delete_question'),
path('question_bank', views.question_bank, name='question_bank'),
path('courses/<int:course_id>', views.course_management, name='course_management'),
Expand Down
Loading

0 comments on commit 4e95e09

Please sign in to comment.