Commit 1dad5cae by Piotr Mitros

Basic support for per-user courseware in place

parent 9ac4ac8a
...@@ -11,7 +11,7 @@ class UserProfile(models.Model): ...@@ -11,7 +11,7 @@ class UserProfile(models.Model):
language = models.TextField(blank=True) language = models.TextField(blank=True)
location = models.TextField(blank=True) location = models.TextField(blank=True)
meta = models.TextField(blank=True) # JSON dictionary for future expansion meta = models.TextField(blank=True) # JSON dictionary for future expansion
courseware = models.TextField(blank=True, default='courseware.xml') courseware = models.TextField(blank=True, default='course.xml')
class Registration(models.Model): class Registration(models.Model):
''' Allows us to wait for e-mail before user is registered. A ''' Allows us to wait for e-mail before user is registered. A
......
...@@ -9,7 +9,6 @@ from capa_problem import LoncapaProblem ...@@ -9,7 +9,6 @@ from capa_problem import LoncapaProblem
import dateutil import dateutil
import datetime import datetime
from xml.dom.minidom import parse, parseString from xml.dom.minidom import parse, parseString
## TODO: Abstract out from Django ## TODO: Abstract out from Django
......
from django.conf import settings from django.conf import settings
from xml.dom.minidom import parse, parseString from xml.dom.minidom import parse, parseString
import libxml2 import libxml2
from auth.models import UserProfile
''' This file will eventually form an abstraction layer between the ''' This file will eventually form an abstraction layer between the
course XML file and the rest of the system. course XML file and the rest of the system.
...@@ -8,10 +9,14 @@ course XML file and the rest of the system. ...@@ -8,10 +9,14 @@ 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 module_xml(module, id_tag, module_id): def course_file(user):
# TODO: Cache. Also, return the libxml2 object.
return settings.DATA_DIR+UserProfile.objects.get(user=user).courseware
def module_xml(coursefile, module, id_tag, module_id):
''' Get XML for a module based on module and module_id. Assumes ''' Get XML for a module based on module and module_id. Assumes
module occurs once in course.xml. ''' module occurs once in courseware XML file.. '''
doc = libxml2.parseFile(settings.DATA_DIR+'course.xml') doc = libxml2.parseFile(coursefile)
# Sanitize input # Sanitize input
if not module.isalnum(): if not module.isalnum():
...@@ -28,8 +33,8 @@ def module_xml(module, id_tag, module_id): ...@@ -28,8 +33,8 @@ def module_xml(module, id_tag, module_id):
return None return None
return result_set[0].serialize() return result_set[0].serialize()
def toc_from_xml(active_chapter,active_section): def toc_from_xml(coursefile, active_chapter, active_section):
dom=parse(settings.DATA_DIR+'course.xml') dom=parse(coursefile)
course = dom.getElementsByTagName('course')[0] course = dom.getElementsByTagName('course')[0]
name=course.getAttribute("name") name=course.getAttribute("name")
......
...@@ -142,7 +142,7 @@ def modx_dispatch(request, module=None, dispatch=None, id=None): ...@@ -142,7 +142,7 @@ def modx_dispatch(request, module=None, dispatch=None, id=None):
#print "X",s.xml, "Y",content_parser.module_xml(module, id_tag, id) #print "X",s.xml, "Y",content_parser.module_xml(module, id_tag, id)
print print
xml = content_parser.module_xml(module, id_tag, id) xml = content_parser.module_xml(content_parser.course_file(request.user), module, id_tag, id)
instance=modx_modules[module](xml, instance=modx_modules[module](xml,
s.module_id, s.module_id,
......
def profile(request):
''' User profile. Show username, location, etc, as well as grades .
We need to allow the user to change some of these settings .'''
if not request.user.is_authenticated():
return redirect('/')
dom=parse(settings.DATA_DIR+'course.xml')
hw=[]
course = dom.getElementsByTagName('course')[0]
chapters = course.getElementsByTagName('chapter')
responses=StudentModule.objects.filter(student=request.user)
for c in chapters:
for s in c.getElementsByTagName('section'):
problems=s.getElementsByTagName('problem')
scores=[]
if len(problems)>0:
for p in problems:
id = p.getAttribute('filename')
correct = 0
for response in responses:
if response.module_id == id:
if response.grade!=None:
correct=response.grade
else:
correct=0
total=capa_module.LoncapaModule(p.toxml(), "id").max_score() # TODO: Add state. Not useful now, but maybe someday problems will have randomized max scores?
scores.append((int(correct),total))
score={'course':course.getAttribute('name'),
'section':s.getAttribute("name"),
'chapter':c.getAttribute("name"),
'scores':scores,
}
hw.append(score)
user_info=UserProfile.objects.get(user=request.user)
context={'name':user_info.name,
'username':request.user.username,
'location':user_info.location,
'language':user_info.language,
'email':request.user.email,
'homeworks':hw,
'csrf':csrf(request)['csrf_token']
}
return render_to_response('profile.html', context)
...@@ -39,7 +39,7 @@ def profile(request): ...@@ -39,7 +39,7 @@ def profile(request):
if not request.user.is_authenticated(): if not request.user.is_authenticated():
return redirect('/') return redirect('/')
dom=parse(settings.DATA_DIR+'course.xml') dom=parse(content_parser.course_file(request.user))
hw=[] hw=[]
course = dom.getElementsByTagName('course')[0] course = dom.getElementsByTagName('course')[0]
chapters = course.getElementsByTagName('chapter') chapters = course.getElementsByTagName('chapter')
...@@ -87,7 +87,7 @@ def render_accordion(request,course,chapter,section): ...@@ -87,7 +87,7 @@ def render_accordion(request,course,chapter,section):
def format_string(string): def format_string(string):
return urllib.quote(string.replace(' ','_')) return urllib.quote(string.replace(' ','_'))
toc=content_parser.toc_from_xml(chapter, section) toc=content_parser.toc_from_xml(content_parser.course_file(request.user), chapter, section)
active_chapter=1 active_chapter=1
for i in range(len(toc)): for i in range(len(toc)):
if toc[i]['active']: if toc[i]['active']:
...@@ -117,7 +117,8 @@ def index(request, course="6.002 Spring 2012", chapter="Using the System", secti ...@@ -117,7 +117,8 @@ def index(request, course="6.002 Spring 2012", chapter="Using the System", secti
if course!="6.002 Spring 2012": if course!="6.002 Spring 2012":
return redirect('/') return redirect('/')
dom=parse(settings.DATA_DIR+'course.xml') cf = content_parser.course_file(request.user)
dom=parse(cf)
dom_course=content_parser.dom_select(dom, 'course', course) dom_course=content_parser.dom_select(dom, 'course', course)
dom_chapter=content_parser.dom_select(dom_course, 'chapter', chapter) dom_chapter=content_parser.dom_select(dom_course, 'chapter', chapter)
dom_section=content_parser.dom_select(dom_chapter, 'section', section) dom_section=content_parser.dom_select(dom_chapter, 'section', section)
......
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