From 28ba37e907553aeac3841e221853683b9171f0db Mon Sep 17 00:00:00 2001 From: ankitjavalkar Date: Wed, 18 Mar 2015 12:16:20 +0530 Subject: Changes to Question model, Views, Add-Question UI - ref_code_path is now part of Question model - MCQ/MCC answers checked using solution field in question model - Formset should reload even after errors - add_question page chould display solution field only in MCQ/MCC --- testapp/exam/forms.py | 23 ++++++++++++----------- testapp/exam/models.py | 12 ++++++++---- testapp/exam/static/exam/js/add_question.js | 15 ++++++++++++--- testapp/exam/templates/exam/add_question.html | 9 +++++---- testapp/exam/templates/exam/edit_question.html | 24 ------------------------ testapp/exam/views.py | 14 ++++++++------ 6 files changed, 45 insertions(+), 52 deletions(-) diff --git a/testapp/exam/forms.py b/testapp/exam/forms.py index e3cef40..93584a6 100644 --- a/testapp/exam/forms.py +++ b/testapp/exam/forms.py @@ -187,8 +187,8 @@ class QuestionForm(forms.ModelForm): description = forms.CharField(widget=forms.Textarea\ (attrs={'cols': 40, 'rows': 1})) points = forms.FloatField() - # test = forms.CharField(widget=forms.Textarea\ - # (attrs={'cols': 40, 'rows': 1}), required=False) + solution = forms.CharField(widget=forms.Textarea\ + (attrs={'cols': 40, 'rows': 1}), required=False) options = forms.CharField(widget=forms.Textarea\ (attrs={'cols': 40, 'rows': 1}), required=False) language = forms.CharField(max_length=20, widget=forms.Select\ @@ -199,17 +199,18 @@ class QuestionForm(forms.ModelForm): tags = TagField(widget=TagAutocomplete(), required=False) snippet = forms.CharField(widget=forms.Textarea\ (attrs={'cols': 40, 'rows': 1}), required=False) + ref_code_path = forms.CharField(widget=forms.Textarea\ + (attrs={'cols': 40, 'rows': 1}), required=False) def save(self, commit=True): - summary = self.cleaned_data["summary"] - description = self.cleaned_data["description"] - points = self.cleaned_data['points'] - # test = self.cleaned_data["test"] - options = self.cleaned_data['options'] - language = self.cleaned_data['language'] - type = self.cleaned_data["type"] - active = self.cleaned_data["active"] - snippet = self.cleaned_data["snippet"] + summary = self.cleaned_data.get("summary") + description = self.cleaned_data.get("description") + points = self.cleaned_data.get("points") + options = self.cleaned_data.get("options") + language = self.cleaned_data.get("language") + type = self.cleaned_data.get("type") + active = self.cleaned_data.get("active") + snippet = self.cleaned_data.get("snippet") new_question = Question() new_question.summary = summary diff --git a/testapp/exam/models.py b/testapp/exam/models.py index dd0c806..5989be4 100644 --- a/testapp/exam/models.py +++ b/testapp/exam/models.py @@ -60,8 +60,11 @@ class Question(models.Model): # Number of points for the question. points = models.FloatField(default=1.0) - # # Test cases for the question in the form of code that is run. - # test = models.TextField(blank=True) + # Answer for MCQs. + solution = models.TextField(blank=True) + + # Test cases file paths + ref_code_path = models.TextField(blank=True) # Any multiple choice options. Place one option per line. options = models.TextField(blank=True) @@ -114,6 +117,7 @@ class Question(models.Model): info_parameter['id'] = self.id info_parameter['user_answer'] = user_answer info_parameter['test_parameter'] = test_case_parameter + info_parameter['ref_code_path'] = self.ref_code_path return json.dumps(info_parameter) @@ -449,5 +453,5 @@ class TestCase(models.Model): # Test case Expected answer in list form expected_answer = models.TextField(blank=True, null = True) - # Test case path to system test code applicable for CPP, C, Java and Scilab - ref_code_path = models.TextField(blank=True, null = True) + # # Test case path to system test code applicable for CPP, C, Java and Scilab + # ref_code_path = models.TextField(blank=True, null = True) diff --git a/testapp/exam/static/exam/js/add_question.js b/testapp/exam/static/exam/js/add_question.js index 267cdb2..5a94f4c 100644 --- a/testapp/exam/static/exam/js/add_question.js +++ b/testapp/exam/static/exam/js/add_question.js @@ -153,13 +153,17 @@ function textareaformat() if(value == 'mcq' || value == 'mcc') { document.getElementById('id_options').style.visibility='visible'; - document.getElementById('label_option').innerHTML="Options :" + document.getElementById('label_option').innerHTML="Options :"; + document.getElementById('id_solution').style.visibility='visible'; + document.getElementById('label_solution').innerHTML="Solutions :"; } else { document.getElementById('id_options').style.visibility='hidden'; document.getElementById('label_option').innerHTML = ""; + document.getElementById('id_solution').style.visibility='hidden'; + document.getElementById('label_solution').innerHTML="" } }); document.getElementById('my').innerHTML = document.getElementById('id_description').value ; @@ -168,12 +172,16 @@ function textareaformat() { document.getElementById('id_options').style.visibility='visible'; document.getElementById('label_option').innerHTML="Options :" + document.getElementById('id_solution').style.visibility='visible'; + document.getElementById('label_solution').innerHTML="Solutions :"; } else { document.getElementById('id_options').style.visibility='hidden'; document.getElementById('label_option').innerHTML = ""; + document.getElementById('id_solution').style.visibility='hidden'; + document.getElementById('label_solution').innerHTML="" } } @@ -189,8 +197,9 @@ function autosubmit() if(type.value == 'select') { type.style.border = 'solid red'; - return false; - } + return false; + } + if (type.value == 'mcq' || type.value == 'mcc') { diff --git a/testapp/exam/templates/exam/add_question.html b/testapp/exam/templates/exam/add_question.html index e5e2574..e117ef3 100644 --- a/testapp/exam/templates/exam/add_question.html +++ b/testapp/exam/templates/exam/add_question.html @@ -27,11 +27,12 @@
{{question.summary.value}} diff --git a/testapp/exam/views.py b/testapp/exam/views.py index 8aef5ab..840f30a 100644 --- a/testapp/exam/views.py +++ b/testapp/exam/views.py @@ -323,7 +323,7 @@ def add_question(request, question_id=None): def add_or_delete_test_form(post_request, instance): request_copy = post_request.copy() if 'add_test' in post_request: - request_copy['test-TOTAL_FORMS'] = int(request_copy['test-TOTAL_FORMS']) + 1 + request_copy['test-TOTAL_FORMS'] = int(request_copy['test-TOTAL_FORMS']) + 1 elif 'delete_test' in post_request: request_copy['test-TOTAL_FORMS'] = int(request_copy['test-TOTAL_FORMS']) - 1 test_case_formset = TestCaseFormSet(request_copy, prefix='test', instance=instance) @@ -402,8 +402,10 @@ def add_question(request, question_id=None): context_instance=ci) else: + test_case_formset = add_or_delete_test_form(request.POST, form.save(commit=False)) return my_render_to_response('exam/add_question.html', - {'form': form}, + {'form': form, + 'formset': test_case_formset}, context_instance=ci) else: form = QuestionForm() @@ -951,8 +953,8 @@ def check(request, q_id, attempt_num=None, questionpaper_id=None): # questions, we obtain the results via XML-RPC with the code executed # safely in a separate process (the code_server.py) running as nobody. if not question.type == 'upload': - if question.type == 'code': - info_parameter = question.consolidate_answer_data(test, user_answer) + info_parameter = question.consolidate_answer_data(test, user_answer) \ + if question.type == 'code' else None correct, result = validate_answer(user, user_answer, question, info_parameter) if correct: new_answer.correct = correct @@ -1013,11 +1015,11 @@ def validate_answer(user, user_answer, question, info_parameter=None): if user_answer is not None: if question.type == 'mcq': - if user_answer.strip() == question.test.strip(): ####add question.answer/question.solution instead of test + if user_answer.strip() == question.solution.strip(): correct = True message = 'Correct answer' elif question.type == 'mcc': - answers = set(question.test.splitlines()) ####add question.answer/question.solution instead of test + answers = set(question.solution.splitlines()) if set(user_answer) == answers: correct = True message = 'Correct answer' -- cgit |