Commit 37896ac5 by Piotr Mitros

Gradual move to XPath/XQuery initiated

parent 20db8e6f
...@@ -7,7 +7,10 @@ from django.http import Http404 ...@@ -7,7 +7,10 @@ from django.http import Http404
import dateutil import dateutil
import datetime import datetime
from xml.dom.minidom import parse, parseString #from xml.dom.minidom import parse, parseString
import content_parser
import libxml2
## TODO: Abstract out from Django ## TODO: Abstract out from Django
from django.conf import settings from django.conf import settings
...@@ -65,7 +68,8 @@ class LoncapaModule(XModule): ...@@ -65,7 +68,8 @@ class LoncapaModule(XModule):
# User submitted a problem, and hasn't reset. We don't want # User submitted a problem, and hasn't reset. We don't want
# more submissions. # more submissions.
if self.lcp.done: if self.lcp.done and not self.rerandomize:
#print "!"
check_button = False check_button = False
save_button = False save_button = False
...@@ -85,6 +89,7 @@ class LoncapaModule(XModule): ...@@ -85,6 +89,7 @@ class LoncapaModule(XModule):
'save_button' : save_button, 'save_button' : save_button,
'answer_available' : self.answer_available(), 'answer_available' : self.answer_available(),
'ajax_url' : self.ajax_url, 'ajax_url' : self.ajax_url,
'attempts': attempts_str
}) })
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>"
...@@ -98,43 +103,53 @@ class LoncapaModule(XModule): ...@@ -98,43 +103,53 @@ class LoncapaModule(XModule):
self.max_attempts = None self.max_attempts = None
self.due_date = None self.due_date = None
dom=parseString(xml) #dom=parseString(xml)
node=dom.childNodes[0] dom2 = libxml2.parseMemory(xml, len(xml))
#node=dom.childNodes[0]
self.due_date=node.getAttribute("due") #self.due_date=node.getAttribute("due")
self.due_date=content_parser.item(dom2.xpathEval('/problem/@due'))
if len(self.due_date)>0: if len(self.due_date)>0:
self.due_date=dateutil.parser.parse(self.due_date) self.due_date=dateutil.parser.parse(self.due_date)
else: else:
self.due_date=None self.due_date=None
self.max_attempts=node.getAttribute("attempts") #self.max_attempts=node.getAttribute("attempts")
self.max_attempts=content_parser.item(dom2.xpathEval('/problem/@attempts'))
if len(self.max_attempts)>0: if len(self.max_attempts)>0:
self.max_attempts=int(self.max_attempts) self.max_attempts=int(self.max_attempts)
else: else:
self.max_attempts=None self.max_attempts=None
self.show_answer=node.getAttribute("showanswer") #self.show_answer=node.getAttribute("showanswer")
self.show_answer=content_parser.item(dom2.xpathEval('/problem/@showanswer'))
if self.show_answer=="": if self.show_answer=="":
self.show_answer="closed" self.show_answer="closed"
self.resettable=node.getAttribute("resettable") self.rerandomize=content_parser.item(dom2.xpathEval('/problem/@rerandomize'))
if self.resettable=="": #self.rerandomize=node.getAttribute("rerandomize")
self.resettable=True if self.rerandomize=="":
elif self.resettable=="false": self.rerandomize=True
self.resettable=False elif self.rerandomize=="false":
elif self.resettable=="true": self.rerandomize=False
self.resettable=True elif self.rerandomize=="true":
self.rerandomize=True
else: else:
raise Exception("Invalid resettable attribute "+self.resettable) raise Exception("Invalid rerandomize attribute "+self.rerandomize)
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:
self.attempts=state['attempts'] self.attempts=state['attempts']
self.filename=node.getAttribute("filename") self.filename=content_parser.item(dom2.xpathEval('/problem/@filename'))
#self.filename=node.getAttribute("filename")
#print self.filename
filename=settings.DATA_DIR+"problems/"+self.filename+".xml" filename=settings.DATA_DIR+"problems/"+self.filename+".xml"
self.name=node.getAttribute("name") #self.name=node.getAttribute("name")
self.name=content_parser.item(dom2.xpathEval('/problem/@name'))
self.lcp=LoncapaProblem(filename, self.item_id, state) self.lcp=LoncapaProblem(filename, self.item_id, state)
def handle_ajax(self, dispatch, get): def handle_ajax(self, dispatch, get):
...@@ -209,7 +224,7 @@ class LoncapaModule(XModule): ...@@ -209,7 +224,7 @@ class LoncapaModule(XModule):
# Problem submitted. Student should reset before checking # Problem submitted. Student should reset before checking
# again. # again.
if self.lcp.done and self.resettable: if self.lcp.done and self.rerandomize:
print "cpdr" print "cpdr"
raise Http404 raise Http404
...@@ -232,7 +247,7 @@ class LoncapaModule(XModule): ...@@ -232,7 +247,7 @@ class LoncapaModule(XModule):
# Problem submitted. Student should reset before saving # Problem submitted. Student should reset before saving
# again. # again.
if self.lcp.done and self.resettable: if self.lcp.done and self.rerandomize:
print "spdr" print "spdr"
return "Problem needs to be reset prior to save." return "Problem needs to be reset prior to save."
...@@ -257,7 +272,7 @@ class LoncapaModule(XModule): ...@@ -257,7 +272,7 @@ class LoncapaModule(XModule):
self.lcp.answers=dict() self.lcp.answers=dict()
self.lcp.correct_map=dict() self.lcp.correct_map=dict()
if self.resettable: if self.rerandomize:
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.seed=None self.lcp.seed=None
......
...@@ -90,7 +90,7 @@ class LoncapaProblem(): ...@@ -90,7 +90,7 @@ class LoncapaProblem():
ot=False ## Are we in an outtext context? ot=False ## Are we in an outtext context?
print "Here", dom #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:
......
...@@ -9,6 +9,15 @@ course XML file and the rest of the system. ...@@ -9,6 +9,15 @@ course XML file and the rest of the system.
TODO: Shift everything from xml.dom.minidom to XPath (or XQuery) TODO: Shift everything from xml.dom.minidom to XPath (or XQuery)
''' '''
def item(l, default="", process=lambda x:x):
if len(l)==0:
return default
elif len(l)==1:
return process(l[0].getContent())
else:
raise Exception('Malformed XML')
def course_file(user): def course_file(user):
# TODO: Cache. Also, return the libxml2 object. # TODO: Cache. Also, return the libxml2 object.
return settings.DATA_DIR+UserProfile.objects.get(user=user).courseware return settings.DATA_DIR+UserProfile.objects.get(user=user).courseware
......
...@@ -125,15 +125,26 @@ function videoDestroy() { ...@@ -125,15 +125,26 @@ function videoDestroy() {
ytplayer = false; ytplayer = false;
} }
function log_event(e) { function log_event(e, d) {
// CRITICAL TODO: Change to AJAX // CRITICAL TODO: Change to AJAX
//$("#eventlog").append("<br>"); //$("#eventlog").append("<br>");
//$("#eventlog").append(JSON.stringify(e)); //$("#eventlog").append(JSON.stringify(e));
window['console'].log(JSON.stringify(e));
// TODO: Figure out
// XMLHttpRequest cannot load http://localhost:7000/userlog. Origin http://localhost:8000 is not allowed by Access-Control-Allow-Origin.
/*window['console'].log(JSON.stringify(e));
$.get("http://localhost:7000/userlog",
{'user':'pmitros',
'key':'key',
'event_type':'unknown',
'data':'e'},
function(data) {
});*/
} }
function seek_slide(type,oe,value) { function seek_slide(type,oe,value) {
//log_event([type, value]); //log_event('video', [type, value]);
if(type=='slide') { if(type=='slide') {
// HACK/TODO: Youtube recommends this be false for slide and true for stop. // HACK/TODO: Youtube recommends this be false for slide and true for stop.
// Works better on my system with true/true. // Works better on my system with true/true.
...@@ -145,7 +156,7 @@ function seek_slide(type,oe,value) { ...@@ -145,7 +156,7 @@ function seek_slide(type,oe,value) {
ytplayer.seekTo(value, true); ytplayer.seekTo(value, true);
} else if (type=='stop') { } else if (type=='stop') {
ytplayer.seekTo(value, true); ytplayer.seekTo(value, true);
log_event([type, value]); log_event('video', [type, value]);
} }
update_captions(value); update_captions(value);
...@@ -166,7 +177,7 @@ function get_state() { ...@@ -166,7 +177,7 @@ function get_state() {
function onytplayerStateChange(newState) { function onytplayerStateChange(newState) {
setytplayerState(newState); setytplayerState(newState);
log_event(['State Change',newState, get_state()]); log_event('video', ['State Change',newState, get_state()]);
} }
function onPlayerError(errorCode) { function onPlayerError(errorCode) {
......
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