Files
edx-platform/courseware/views.py
2011-12-20 19:43:53 -05:00

143 lines
4.8 KiB
Python

from django.http import HttpResponse
from django.template import Context, loader
from djangomako.shortcuts import render_to_response, render_to_string
from xml.dom.minidom import parse, parseString
import json, os, sys
from django.core.context_processors import csrf
from django.template import Context
from django.contrib.auth.models import User
from auth.models import UserProfile
from django.shortcuts import redirect
import StringIO
from django.http import Http404
import urllib
import capa_module
import video_module
from models import StudentModule
import urllib
from django.conf import settings
import content_parser
import uuid
from module_render import *
template_imports={'urllib':urllib}
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(content_parser.course_file(request.user))
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)
def render_accordion(request,course,chapter,section):
''' Draws navigation bar. Takes current position in accordion as
parameter. Returns (initialization_javascript, content)'''
def format_string(string):
return urllib.quote(string.replace(' ','_'))
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']:
active_chapter=i
context=dict([['active_chapter',active_chapter],
['toc',toc],
['course_name',course],
['format_string',format_string]]+ \
template_imports.items())
return {'init_js':render_to_string('accordion_init.js',context),
'content':render_to_string('accordion.html',context)}
def index(request, course="6.002 Spring 2012", chapter="Using the System", section="Hints"):
''' Displays courseware accordion, and any associated content.
'''
if not request.user.is_authenticated():
return redirect('/')
# Fixes URLs -- we don't get funny encoding characters from spaces
# so they remain readable
course=course.replace("_"," ")
chapter=chapter.replace("_"," ")
section=section.replace("_"," ")
# HACK: Force course to 6.002 for now
# Without this, URLs break
if course!="6.002 Spring 2012":
return redirect('/')
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)
if dom_section!=None:
module=[e for e in dom_section.childNodes if e.nodeType==1][0]
else:
module=None
accordion=render_accordion(request, course, chapter, section)
module=render_module(request, module)
if 'init_js' not in module:
module['init_js']=''
context={'init':accordion['init_js']+module['init_js'],
'accordion':accordion['content'],
'content':module['content']}
return render_to_response('courseware.html', context)