Commit e9819ffb by Piotr Mitros

customresponse basically works

parent 53155d22
...@@ -10,11 +10,11 @@ from content_parser import xpath_remove ...@@ -10,11 +10,11 @@ from content_parser import xpath_remove
from util import contextualize_text from util import contextualize_text
from inputtypes import textline, schematic from inputtypes import textline, schematic
from responsetypes import numericalresponse, formularesponse from responsetypes import numericalresponse, formularesponse, customresponse
response_types = {'numericalresponse':numericalresponse, response_types = {'numericalresponse':numericalresponse,
'formularesponse':formularesponse, 'formularesponse':formularesponse,
'customresponse':None} 'customresponse':customresponse}
entry_types = ['textline', 'schematic'] entry_types = ['textline', 'schematic']
response_properties = ["responseparam", "answer"] response_properties = ["responseparam", "answer"]
# How to convert from original XML to HTML # How to convert from original XML to HTML
...@@ -124,6 +124,12 @@ class LoncapaProblem(object): ...@@ -124,6 +124,12 @@ class LoncapaProblem(object):
responder = response_types[response.tag](response, self.context) responder = response_types[response.tag](response, self.context)
results = responder.get_answers() results = responder.get_answers()
answer_map.update(results) answer_map.update(results)
for entry in problems_simple.xpath("//"+"|//".join(response_properties+entry_types)):
answer = entry.get('correct_answer')
if answer != None:
answer_map[entry.get('id')] = contextualize_text(answer, self.context())
return answer_map return answer_map
# ======= Private ======== # ======= Private ========
......
import random, numpy, math, scipy
from util import contextualize_text from util import contextualize_text
from calc import evaluator from calc import evaluator
import random, math import random, math
from django.conf import settings
# TODO: Should be the same object as in capa_problem
global_context={'random':random,
'numpy':numpy,
'math':math,
'scipy':scipy}
class numericalresponse(object): class numericalresponse(object):
def __init__(self, xml, context): def __init__(self, xml, context):
...@@ -30,15 +38,31 @@ class numericalresponse(object): ...@@ -30,15 +38,31 @@ class numericalresponse(object):
class customresponse(object): class customresponse(object):
def __init__(self, xml, context): def __init__(self, xml, context):
self.xml = xml self.xml = xml
self.answer_id = xml.xpath('//*[@id=$id]//textline/@id', ## CRITICAL TODO: Should cover all entrytypes
id=xml.get('id'))[0] self.answer_ids = xml.xpath('//*[@id=$id]//textline/@id',
return {self.answer_id:'correct'} id=xml.get('id'))
self.context = context
answer = xml.xpath('//*[@id=$id]//answer',
id=xml.get('id'))[0]
answer_src = answer.get('src')
if answer_src != None:
self.code = open(settings.DATA_DIR+'src/'+answer_src).read()
else:
self.code = answer.text
def grade(self, student_answers): def grade(self, student_answers):
return {self.answer_id:'correct'} print "YY", self.answer_ids
print "XX", student_answers
submission = [student_answers[k] for k in sorted(self.answer_ids)]
self.context.update({'submission':submission})
print self.code
exec self.code in global_context, self.context
return zip(sorted(self.answer_ids), self.context['correct'])
def get_answers(self): def get_answers(self):
return {self.answer_id:'correct'} # Since this is explicitly specified in the problem, this will
# be handled by capa_problem
return {}
class formularesponse(object): class formularesponse(object):
......
...@@ -237,14 +237,15 @@ class LoncapaModule(XModule): ...@@ -237,14 +237,15 @@ class LoncapaModule(XModule):
for key in get: for key in get:
answers['_'.join(key.split('_')[1:])]=get[key] answers['_'.join(key.split('_')[1:])]=get[key]
try: #try:
if True:
old_state = self.lcp.get_state() old_state = self.lcp.get_state()
lcp_id = self.lcp.problem_id lcp_id = self.lcp.problem_id
filename = self.lcp.filename filename = self.lcp.filename
correct_map = self.lcp.grade_answers(answers) correct_map = self.lcp.grade_answers(answers)
except: #except:
self.lcp = LoncapaProblem(filename, id=lcp_id, state=old_state) # self.lcp = LoncapaProblem(filename, id=lcp_id, state=old_state)
return json.dumps({'success':'syntax'}) # return json.dumps({'success':'syntax'})
self.attempts = self.attempts + 1 self.attempts = self.attempts + 1
self.lcp.done=True self.lcp.done=True
......
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