Basic support for per-user courseware in place

This commit is contained in:
Piotr Mitros
2011-12-20 19:43:53 -05:00
parent 9ac4ac8a12
commit 1dad5caef9
6 changed files with 16 additions and 59 deletions

View File

@@ -11,7 +11,7 @@ class UserProfile(models.Model):
language = models.TextField(blank=True)
location = models.TextField(blank=True)
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):
''' Allows us to wait for e-mail before user is registered. A

View File

@@ -9,7 +9,6 @@ from capa_problem import LoncapaProblem
import dateutil
import datetime
from xml.dom.minidom import parse, parseString
## TODO: Abstract out from Django

View File

@@ -1,6 +1,7 @@
from django.conf import settings
from xml.dom.minidom import parse, parseString
import libxml2
from auth.models import UserProfile
''' This file will eventually form an abstraction layer between the
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)
'''
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
module occurs once in course.xml. '''
doc = libxml2.parseFile(settings.DATA_DIR+'course.xml')
module occurs once in courseware XML file.. '''
doc = libxml2.parseFile(coursefile)
# Sanitize input
if not module.isalnum():
@@ -28,8 +33,8 @@ def module_xml(module, id_tag, module_id):
return None
return result_set[0].serialize()
def toc_from_xml(active_chapter,active_section):
dom=parse(settings.DATA_DIR+'course.xml')
def toc_from_xml(coursefile, active_chapter, active_section):
dom=parse(coursefile)
course = dom.getElementsByTagName('course')[0]
name=course.getAttribute("name")

View File

@@ -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
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,
s.module_id,

View File

@@ -1,48 +0,0 @@
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)

View File

@@ -39,7 +39,7 @@ def profile(request):
if not request.user.is_authenticated():
return redirect('/')
dom=parse(settings.DATA_DIR+'course.xml')
dom=parse(content_parser.course_file(request.user))
hw=[]
course = dom.getElementsByTagName('course')[0]
chapters = course.getElementsByTagName('chapter')
@@ -87,7 +87,7 @@ def render_accordion(request,course,chapter,section):
def format_string(string):
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
for i in range(len(toc)):
if toc[i]['active']:
@@ -117,7 +117,8 @@ def index(request, course="6.002 Spring 2012", chapter="Using the System", secti
if course!="6.002 Spring 2012":
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_chapter=content_parser.dom_select(dom_course, 'chapter', chapter)
dom_section=content_parser.dom_select(dom_chapter, 'section', section)