Commit 4add868d by Piotr Mitros

Bug fixes based on Jerry's problems

parent 10e3d8b9
...@@ -69,7 +69,7 @@ def evaluator(variables, functions, string): ...@@ -69,7 +69,7 @@ def evaluator(variables, functions, string):
(dot,minus,plus,times,div,lpar,rpar,exp)=map(Literal,".-+*/()^") (dot,minus,plus,times,div,lpar,rpar,exp)=map(Literal,".-+*/()^")
number_part=Word(nums) number_part=Word(nums)
number=Optional("+-")+number_part+Optional("."+number_part)+ \ number=Optional(minus | plus)+number_part+Optional("."+number_part)+ \
Optional(CaselessLiteral("E")+Optional("-")+number_part)+ \ Optional(CaselessLiteral("E")+Optional("-")+number_part)+ \
Optional(number_suffix) Optional(number_suffix)
number=number.setParseAction( number_parse_action ) number=number.setParseAction( number_parse_action )
...@@ -114,3 +114,4 @@ if __name__=='__main__': ...@@ -114,3 +114,4 @@ if __name__=='__main__':
print evaluator({'R1': 2.0, 'R3':4.0}, {}, "13") print evaluator({'R1': 2.0, 'R3':4.0}, {}, "13")
# #
print evaluator({'a': 2.2997471478310274, 'k': 9, 'm': 8, 'x': 0.66009498411213041}, {}, "5") print evaluator({'a': 2.2997471478310274, 'k': 9, 'm': 8, 'x': 0.66009498411213041}, {}, "5")
print evaluator({},{}, "-1")
# For calculator:
# http://pyparsing.wikispaces.com/file/view/fourFn.py
import random, numpy, math, scipy, sys, StringIO, os, struct, json import random, numpy, math, scipy, sys, StringIO, os, struct, json
from x_module import XModule from x_module import XModule
...@@ -53,37 +50,41 @@ class LoncapaModule(XModule): ...@@ -53,37 +50,41 @@ class LoncapaModule(XModule):
html = self.lcp.get_html() html = self.lcp.get_html()
content={'name':self.name, content={'name':self.name,
'html':html} 'html':html}
closed = False
if self.lcp.done:
check_button="Reset"
else:
check_button="Check"
check_button = True
reset_button = True
save_button = True save_button = True
if self.max_attempts == None: # If we're after deadline, or user has exhuasted attempts,
pass # question is read-only.
elif self.max_attempts == self.attempts: if self.closed():
save_button = False
check_button = False check_button = False
else: reset_button = False
check_button = check_button + \
" ({a}/{m})".format(a=self.attempts, m=self.max_attempts)
if self.due_date != None and datetime.datetime.utcnow() > self.due_date:
save_button = False save_button = False
# User submitted a problem, and hasn't reset. We don't want
# more submissions.
if self.lcp.done:
check_button = False check_button = False
results = True save_button = False
# User hasn't submitted an answer yet -- we don't want resets
if not self.lcp.done:
reset_button = False
attempts_str = ""
if self.max_attempts != None:
attempts_str = " ({a}/{m})".format(a=self.attempts, m=self.max_attempts)
html=render_to_string('problem.html', html=render_to_string('problem.html',
{'problem':content, {'problem' : content,
'id':self.filename, 'id' : self.filename,
'check_button':check_button, 'check_button' : check_button,
'save_button':save_button, 'reset_button' : reset_button,
'answer_available':self.answer_available(), 'save_button' : save_button,
'ajax_url':self.ajax_url, 'answer_available' : self.answer_available(),
'ajax_url' : self.ajax_url,
}) })
if encapsulate: if encapsulate:
html = '<div id="main_{id}">'.format(id=self.item_id)+html+"</div>" html = '<div id="main_{id}">'.format(id=self.item_id)+html+"</div>"
...@@ -194,17 +195,24 @@ class LoncapaModule(XModule): ...@@ -194,17 +195,24 @@ class LoncapaModule(XModule):
# Figure out if we should move these to capa_problem? # Figure out if we should move these to capa_problem?
def get_problem(self, get): def get_problem(self, get):
''' Same as get_problem_html -- if we want to reconfirm we have the right ''' Same as get_problem_html -- if we want to reconfirm we
thing e.g. after several AJAX calls. ''' have the right thing e.g. after several AJAX calls.'''
return self.get_problem_html(encapsulate=False) return self.get_problem_html(encapsulate=False)
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
a map of correct/incorrect answers ''' returns a map of correct/incorrect answers'''
# Too late. Cannot submit
if self.closed(): if self.closed():
print "cp" print "cp"
raise Http404 raise Http404
# Problem submitted. Student should reset before checking
# again.
if self.lcp.done and self.resettable:
print "cpdr"
raise Http404
self.attempts = self.attempts + 1 self.attempts = self.attempts + 1
self.lcp.done=True self.lcp.done=True
answers=dict() answers=dict()
...@@ -217,9 +225,16 @@ class LoncapaModule(XModule): ...@@ -217,9 +225,16 @@ class LoncapaModule(XModule):
return js return js
def save_problem(self, get): def save_problem(self, get):
# Too late. Cannot submit
if self.closed(): if self.closed():
print "sp" print "sp"
raise Http404 return "Problem is closed"
# Problem submitted. Student should reset before saving
# again.
if self.lcp.done and self.resettable:
print "spdr"
return "Problem needs to be reset prior to save."
answers=dict() answers=dict()
for key in get: for key in get:
...@@ -232,19 +247,21 @@ class LoncapaModule(XModule): ...@@ -232,19 +247,21 @@ class LoncapaModule(XModule):
def reset_problem(self, get): def reset_problem(self, get):
''' Changes problem state to unfinished -- removes student answers, ''' Changes problem state to unfinished -- removes student answers,
and causes problem to rerender itself. ''' and causes problem to rerender itself. '''
if self.attempts == self.max_attempts: if self.closed():
return "Too many attempts. You shouldn't be here." return "Problem is closed"
if self.due_date != None and datetime.datetime.utcnow() > self.due_date: if not self.lcp.done:
return "Too late. problem was due." return "Refresh the page and make an attempt before resetting."
self.lcp.done=False self.lcp.done=False
self.lcp.answers=dict() self.lcp.answers=dict()
self.lcp.correct_map=dict()
if self.resettable:
self.lcp.context=dict() self.lcp.context=dict()
self.lcp.questions=dict() # Detailed info about questions in problem instance. TODO: Should be by id and not lid. self.lcp.questions=dict() # Detailed info about questions in problem instance. TODO: Should be by id and not lid.
self.lcp.answers=dict() # Student answers
self.lcp.correct_map=dict()
self.lcp.seed=None self.lcp.seed=None
filename=settings.DATA_DIR+self.filename+".xml" filename=settings.DATA_DIR+self.filename+".xml"
self.lcp=LoncapaProblem(filename, self.item_id, self.lcp.get_state()) self.lcp=LoncapaProblem(filename, self.item_id, self.lcp.get_state())
return json.dumps(self.get_problem_html(encapsulate=False)) return json.dumps(self.get_problem_html(encapsulate=False))
...@@ -75,8 +75,13 @@ class LoncapaProblem(): ...@@ -75,8 +75,13 @@ class LoncapaProblem():
self.answers=state['answers'] self.answers=state['answers']
if 'correct_map' in state: if 'correct_map' in state:
self.correct_map=state['correct_map'] self.correct_map=state['correct_map']
random.seed(self.seed) random.seed(self.seed)
dom=parse(filename).childNodes[0] dom=parse(filename)
for d in dom.childNodes:
if d.localName == 'problem':
dom = d
g={'random':random,'numpy':numpy,'math':math,'scipy':scipy} g={'random':random,'numpy':numpy,'math':math,'scipy':scipy}
...@@ -85,8 +90,11 @@ class LoncapaProblem(): ...@@ -85,8 +90,11 @@ class LoncapaProblem():
ot=False ## Are we in an outtext context? ot=False ## Are we in an outtext context?
print "Here", dom
# Loop through the nodes of the problem, and # Loop through the nodes of the problem, and
for e in dom.childNodes: for e in dom.childNodes:
#print e.localName
if e.localName=='script': if e.localName=='script':
exec e.childNodes[0].data in g,self.context exec e.childNodes[0].data in g,self.context
elif e.localName=='endouttext': elif e.localName=='endouttext':
......
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