Commit 10e3d8b9 by Piotr Mitros

Show answer appears to work

parent b973f358
...@@ -5,6 +5,7 @@ import random, numpy, math, scipy, sys, StringIO, os, struct, json ...@@ -5,6 +5,7 @@ import random, numpy, math, scipy, sys, StringIO, os, struct, json
from x_module import XModule from x_module import XModule
from capa_problem import LoncapaProblem from capa_problem import LoncapaProblem
from django.http import Http404
import dateutil import dateutil
import datetime import datetime
...@@ -81,6 +82,7 @@ class LoncapaModule(XModule): ...@@ -81,6 +82,7 @@ class LoncapaModule(XModule):
'id':self.filename, 'id':self.filename,
'check_button':check_button, 'check_button':check_button,
'save_button':save_button, 'save_button':save_button,
'answer_available':self.answer_available(),
'ajax_url':self.ajax_url, 'ajax_url':self.ajax_url,
}) })
if encapsulate: if encapsulate:
...@@ -114,6 +116,16 @@ class LoncapaModule(XModule): ...@@ -114,6 +116,16 @@ class LoncapaModule(XModule):
if self.show_answer=="": if self.show_answer=="":
self.show_answer="closed" self.show_answer="closed"
self.resettable=node.getAttribute("resettable")
if self.resettable=="":
self.resettable=True
elif self.resettable=="false":
self.resettable=False
elif self.resettable=="true":
self.resettable=True
else:
raise Exception("Invalid resettable attribute "+self.resettable)
if state!=None: if state!=None:
state=json.loads(state) state=json.loads(state)
if state!=None and 'attempts' in state: if state!=None and 'attempts' in state:
...@@ -135,12 +147,22 @@ class LoncapaModule(XModule): ...@@ -135,12 +147,22 @@ class LoncapaModule(XModule):
response = self.reset_problem(get) response = self.reset_problem(get)
elif dispatch=='problem_save': elif dispatch=='problem_save':
response = self.save_problem(get) response = self.save_problem(get)
elif dispatch=='get_answer': elif dispatch=='problem_show':
response = self.get_answer(get) response = self.get_answer(get)
else: else:
return "Error" return "Error"
return response return response
def closed(self):
''' Is the student still allowed to submit answers? '''
if self.attempts == self.max_attempts:
return True
if self.due_date != None and datetime.datetime.utcnow() > self.due_date:
return True
return False
def answer_available(self): def answer_available(self):
''' Is the user allowed to see an answer? ''' Is the user allowed to see an answer?
''' '''
...@@ -160,13 +182,14 @@ class LoncapaModule(XModule): ...@@ -160,13 +182,14 @@ class LoncapaModule(XModule):
return True return True
if self.show_answer == 'closed' and not self.closed(): if self.show_answer == 'closed' and not self.closed():
return False return False
print "aa", self.show_answer
raise Http404 raise Http404
def get_answer(self, get): def get_answer(self, get):
if not self.answer_available(): if not self.answer_available():
raise Http404 raise Http404
else: else:
raise Http404 return json.dumps(self.lcp.get_question_answers())
# Figure out if we should move these to capa_problem? # Figure out if we should move these to capa_problem?
...@@ -178,11 +201,9 @@ class LoncapaModule(XModule): ...@@ -178,11 +201,9 @@ class LoncapaModule(XModule):
def check_problem(self, get): def check_problem(self, get):
''' Checks whether answers to a problem are correct, and returns ''' Checks whether answers to a problem are correct, and returns
a map of correct/incorrect answers ''' a map of correct/incorrect answers '''
if self.attempts == self.max_attempts: if self.closed():
return "Too many attempts. You shouldn't be here." print "cp"
raise Http404
if self.due_date != None and datetime.datetime.utcnow() > self.due_date:
return "Too late. problem was due."
self.attempts = self.attempts + 1 self.attempts = self.attempts + 1
self.lcp.done=True self.lcp.done=True
...@@ -196,11 +217,9 @@ class LoncapaModule(XModule): ...@@ -196,11 +217,9 @@ class LoncapaModule(XModule):
return js return js
def save_problem(self, get): def save_problem(self, get):
if self.attempts == self.max_attempts: if self.closed():
return "Too many attempts. You shouldn't be here." print "sp"
raise Http404
if self.due_date != None and datetime.datetime.utcnow() > self.due_date:
return "Too late. problem was due."
answers=dict() answers=dict()
for key in get: for key in get:
......
...@@ -121,6 +121,12 @@ class LoncapaProblem(): ...@@ -121,6 +121,12 @@ class LoncapaProblem():
def set_answers(self, answers): def set_answers(self, answers):
self.answers=answers self.answers=answers
def get_question_answers(self):
rv = dict()
for key in self.questions:
rv[key]=str(self.questions[key]['answer'])
return rv
def grade_answers(self, answers): def grade_answers(self, answers):
''' Takes a map of IDs to answers. Return which ones are correct ''' ''' Takes a map of IDs to answers. Return which ones are correct '''
self.answers=answers self.answers=answers
...@@ -189,7 +195,7 @@ class LoncapaProblem(): ...@@ -189,7 +195,7 @@ class LoncapaProblem():
if id in self.correct_map and self.correct_map[id]=='incorrect': if id in self.correct_map and self.correct_map[id]=='incorrect':
icon='close' icon='close'
html='<input type="text" name="input_{id}" id="input_{id}" value="{value}"><span class="ui-icon ui-icon-{icon}" style="display:inline-block;" id="status_{id}"></span> '.format(id=id,value=value,icon=icon) html='<input type="text" name="input_{id}" id="input_{id}" value="{value}"><span id="answer_{id}"></span><span class="ui-icon ui-icon-{icon}" style="display:inline-block;" id="status_{id}"></span> '.format(id=id,value=value,icon=icon)
return html return html
def grade_fr(self, question, answer): def grade_fr(self, question, answer):
...@@ -245,7 +251,7 @@ class LoncapaProblem(): ...@@ -245,7 +251,7 @@ class LoncapaProblem():
if id in self.correct_map and self.correct_map[id]=='incorrect': if id in self.correct_map and self.correct_map[id]=='incorrect':
icon='close' icon='close'
html='<input type="text" name="input_{id}" id="input_{id}" value="{value}"><span class="ui-icon ui-icon-{icon}" style="display:inline-block;" id="status_{id}"></span> '.format(id=id,value=value,icon=icon) html='<input type="text" name="input_{id}" id="input_{id}" value="{value}"><span id="answer_{id}"></span><span class="ui-icon ui-icon-{icon}" style="display:inline-block;" id="status_{id}"></span> '.format(id=id,value=value,icon=icon)
return html return html
graders={'numericalresponse':grade_nr, graders={'numericalresponse':grade_nr,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment